Skip to content

Commit

Permalink
wallet_pool wip
Browse files Browse the repository at this point in the history
  • Loading branch information
teadetime committed Apr 18, 2022
1 parent 0aec59c commit 4045871
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/core/globals/blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ int blockchain_init_leveldb(){
memset(genesis_block->header.prev_header_hash, 0, BLOCK_HASH_LEN);

int ret = blockchain_add_leveldb(genesis_block);
if(ret != 0){
free(genesis_block);
return 3;
}
return 0;
}

Expand Down
28 changes: 11 additions & 17 deletions src/core/globals/utxo_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
#include "init_db.h"
#include "ser_tx.h"

int make_utxo_pool_key_with_hash(unsigned char **dest, size_t *len, unsigned char *hash, unsigned int vout){
int make_utxo_pool_key_with_hash(unsigned char *dest, size_t *len, unsigned char *hash, unsigned int vout){
*len = TX_HASH_LEN+sizeof(vout);
*dest = malloc(*len);
memcpy(*dest, hash, TX_HASH_LEN);
memcpy(*dest+TX_HASH_LEN, &vout, sizeof(vout));
memcpy(dest, hash, TX_HASH_LEN);
memcpy(dest+TX_HASH_LEN, &vout, sizeof(vout));
return 0;
}

int make_utxo_pool_key(unsigned char **dest, size_t *len, Transaction *tx, unsigned int vout){
int make_utxo_pool_key(unsigned char *dest, size_t *len, Transaction *tx, unsigned int vout){
if(vout > tx->num_outputs-1){
return 1;
}
Expand All @@ -29,12 +28,12 @@ int utxo_pool_add_leveldb(Transaction *tx, unsigned int vout){
if(check_if_db_loaded(&utxo_pool_db, utxo_pool_path) != 0){
return 5;
}
// NOTE WE SHOULD CHECK TO SEE IF VOUT IS LARGER THAN NUM OUTPUTS!
unsigned char *db_key;

unsigned char db_key[UTXO_POOL_KEY_LEN];
size_t key_len;
char *err = NULL;
//Make Key
if(make_utxo_pool_key(&db_key, &key_len, tx, vout) != 0){
if(make_utxo_pool_key(db_key, &key_len, tx, vout) != 0){
return 2;
}
// Make Value (Build new UTXO)
Expand All @@ -46,13 +45,11 @@ int utxo_pool_add_leveldb(Transaction *tx, unsigned int vout){
unsigned char *serialized_utxo = ser_utxo_alloc(&utxo_size, utxo);
free(utxo);
if(!serialized_utxo){
free(db_key);
return 3;
}
leveldb_writeoptions_t *woptions = leveldb_writeoptions_create();
leveldb_put(utxo_pool_db, woptions, db_key, key_len, serialized_utxo, utxo_size, &err);
leveldb_writeoptions_destroy(woptions);
free(db_key);
free(serialized_utxo);
dump_buf("", "KEY: ", db_key, key_len);

Expand All @@ -61,7 +58,6 @@ int utxo_pool_add_leveldb(Transaction *tx, unsigned int vout){
leveldb_free(err);
return(1);
}
leveldb_free(err);

return 0;
}
Expand All @@ -71,20 +67,19 @@ int utxo_pool_find_leveldb(UTXO **found_utxo, unsigned char *tx_hash, unsigned i
return 5;
}

unsigned char *db_key;
unsigned char db_key[UTXO_POOL_KEY_LEN];
size_t key_len;
char *err = NULL;
size_t read_len;
char *read = NULL;
//Make Key
if(make_utxo_pool_key_with_hash(&db_key, &key_len, tx_hash, vout) != 0){
if(make_utxo_pool_key_with_hash(db_key, &key_len, tx_hash, vout) != 0){
return 2;
}

leveldb_readoptions_t *roptions = leveldb_readoptions_create();
read = leveldb_get(utxo_pool_db, roptions, db_key, key_len, &read_len, &err);
leveldb_readoptions_destroy(roptions);
free(db_key);

if (err != NULL) {
fprintf(stderr, "Read fail: %s\n", err);
Expand All @@ -109,17 +104,16 @@ int utxo_pool_remove_leveldb(unsigned char *tx_hash, unsigned int vout){
if(check_if_db_loaded(&utxo_pool_db, utxo_pool_path) != 0){
return 5;
}
unsigned char *db_key;
unsigned char db_key[UTXO_POOL_KEY_LEN];
size_t key_len;
char *err = NULL;
//Make Key
if(make_utxo_pool_key_with_hash(&db_key, &key_len, tx_hash, vout) != 0){
if(make_utxo_pool_key_with_hash(db_key, &key_len, tx_hash, vout) != 0){
return 2;
}
leveldb_writeoptions_t *woptions = leveldb_writeoptions_create();
leveldb_delete(utxo_pool_db, woptions, db_key, key_len, &err);
leveldb_writeoptions_destroy(woptions);
free(db_key);
if (err != NULL) {
fprintf(stderr, "Delete fail: %s\n", err);
leveldb_free(err);
Expand Down
3 changes: 2 additions & 1 deletion src/core/txs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ add_library(wallet_pool STATIC wallet_pool.c)
target_include_directories(wallet_pool PUBLIC ${INCLUDE_ALL})
target_link_libraries(wallet_pool
core_tx
leveldb)
init_db
utxopool)

add_library(wallet STATIC wallet.c)
target_include_directories(wallet PUBLIC ${INCLUDE_ALL})
Expand Down
58 changes: 58 additions & 0 deletions src/core/txs/wallet_pool.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
#include <stdio.h>
#include "wallet_pool.h"
#include "crypto.h"
#include "utxo_pool.h"

void wallet_init() {
wallet_pool = NULL;
key_pool = NULL;
}

int wallet_init_leveldb(){
int init_wallet_pool = init_db(&wallet_pool_db, &wallet_pool_path, "/wallet_pool");
int init_key_pool = init_db(&key_pool_db, &key_pool_path, "/key_pool");
if(init_wallet_pool!= 0){
return 5;
}
if(init_key_pool!= 0){
return 6;
}
}

int wallet_pool_add_leveldb(Transaction *tx, unsigned int vout, mbedtls_ecdsa_context *key_pair){
//WalletPool *new_entry, *found_entry;


unsigned char db_key[UTXO_POOL_KEY_LEN];
size_t key_len;
char *err = NULL;
//Make Key
if(make_utxo_pool_key(db_key, &key_len, tx, vout) != 0){
return 2;
}
//new_entry = malloc(sizeof(WalletPool));
// // Build key
// memset(&(new_entry->id), 0, sizeof(UTXOPoolKey));
// hash_tx(new_entry->id.tx_hash, tx);
// new_entry->id.vout = vout;


// Build content
WalletEntry *content = malloc(sizeof(WalletEntry));
content->amt = tx->outputs[vout].amt;
content->key_pair = key_pair;
content->spent = 0;


// size_t wallet_entry_size;
// unsigned char *serialized_wallet_entry = ser_utxo_alloc(&wallet_entry_size, content);
// free(content);
// if(!serialized_wallet_entry ){
// return 3;
// }
// leveldb_writeoptions_t *woptions = leveldb_writeoptions_create();
// leveldb_put(utxo_pool_db, woptions, db_key, key_len, serialized_wallet_entry, wallet_entry_size, &err);
// leveldb_writeoptions_destroy(woptions);
// free(serialized_wallet_entry);
// dump_buf("", "KEY: ", db_key, key_len);

// if (err != NULL) {
// fprintf(stderr, "Write fail: %s\n", err);
// leveldb_free(err);
// return(1);
// }
leveldb_free(err);

}

/* WALLET POOL FUNCS */

WalletEntry *wallet_pool_add(
Expand Down
5 changes: 3 additions & 2 deletions src/includes/globals/utxo_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "uthash.h"
#include "base_tx.h"
#include "leveldb/c.h"
#define UTXO_POOL_KEY_LEN TX_HASH_LEN+sizeof(((Transaction*)0)->inputs->prev_utxo_output)

typedef struct UTXOPoolKey {
unsigned char tx_hash[TX_HASH_LEN];
Expand Down Expand Up @@ -35,7 +36,7 @@ int utxo_pool_init_leveldb();
* @param vout output to use
* @return int 0 if success not zero if failure
*/
int make_utxo_pool_key_with_hash(unsigned char **dest, size_t *len, unsigned char *hash, unsigned int vout);
int make_utxo_pool_key_with_hash(unsigned char *dest, size_t *len, unsigned char *hash, unsigned int vout);

/**
* @brief Make Utxo pool from a transaction and vout
Expand All @@ -46,7 +47,7 @@ int make_utxo_pool_key_with_hash(unsigned char **dest, size_t *len, unsigned cha
* @param vout output to use as utxo
* @return int 0 if success not zero if failure
*/
int make_utxo_pool_key(unsigned char **dest, size_t *len, Transaction *tx, unsigned int vout);
int make_utxo_pool_key(unsigned char *dest, size_t *len, Transaction *tx, unsigned int vout);

/**
* @brief Add a transaction to the utxo Pool
Expand Down
7 changes: 7 additions & 0 deletions src/includes/txs/wallet_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "constants.h"
#include "uthash.h"
#include "utxo_pool.h"
#include "init_db.h"

typedef struct {
mbedtls_ecdsa_context *key_pair;
Expand All @@ -22,6 +23,12 @@ typedef struct {
UT_hash_handle hh;
} KeyPool;

char *key_pool_path;
leveldb_t *key_pool_db; // Level DB Database
char *wallet_pool_path;
leveldb_t *wallet_pool_db; // Level DB Database


/* The wallet pool tracks all outputs the wallet controls
*
* Entries should be added whenever the wallet receives a new block and finds an
Expand Down

0 comments on commit 4045871

Please sign in to comment.