Skip to content

Commit

Permalink
hash: added dogecoin_hashwriter_free
Browse files Browse the repository at this point in the history
auxpow: updated check_merkle_branch iterator to size_t and hash parameters
block: updated dogecoin_block_header_new to initialize header
block: updated dogecoin_block_header_free to clear version and removed debug print
block: updated dogecoin_block_header_copy to copy auxpow
block: updated header and dogecoin_block_header_serialize/deserialize to signed version
headersdb_file: updated dogecoin_headers_db_free to free chaintip to chainbottom
headersdb_file: updated dogecoin_headers_db_connect_hdr to free blockindex and todo
headersdb_file: updated dogecoin_headers_db_load to print progress
spv: updated dogecoin_spv_client_free to check for headers_db_ctx
spv: updated dogecoin_net_spv_post_cmd to recover after invalid headers and blocks
spv: updated dogecoin_net_spv_periodic_statecheck to request blocks in full block sync
spv: updated dogecoin_net_spv_request_headers to requests blocks from node with best height
transaction: updated sign_transaction_w_privkey to free script_pubkey and txtmp
tx: updated dogecoin_tx_out_pubkey_hash_to_p2pkh_address to free copy of txout
wallet: removed dogecoin_wallet_add_to_spent as dogecoin_wallet_scrape_utxos handles spends
wallet: updated dogecoin_wallet_new frees and to call dogecoin_hdnode_free
wallet: updated dogecoin_wallet_free to call dogecoin_btree_tdestroy with NULL/free
wallet: updated dogecoin_wallet_create to check for open wallet file
wallet: updated dogecoin_wallet_init with temp_address_copy and free address_copy
wallet: updated dogecoin_wallet_init with dogecoin_wallet_addr_free if address found
wallet: updated dogecoin_wallet_load_address and to free buf
wallet: updated dogecoin_wallet_load_transaction to free buf
wallet: updated dogecoin_wallet_next_addr to free key
wallet: updated dogecoin_wallet_next_bip44_addr to free keys and waddr
wallet: updated dogecoin_register_watch_address_with_node to free address_copy and wallet
wallet: updated dogecoin_register_watch_address_with_node to free waddr
wallet: updated dogecoin_unregister_watch_address_with_node to free address_copy and record
wallet: updated dogecoin_unregister_watch_address_with_node to free waddr_check and waddr
wallet: updated dogecoin_get_utxo functions and dogecoin_get_balance to free wallet/utxos
wallet: updated dogecoin_wallet_utxo_new to initialize elements
wallet: updated dogecoin_wallet_scrape_utxos to check pointers before memcmp
address_test: updated generateHDMasterPubKeypair testnet parameter to true
bip44_test: updated test_seed pointer and assignment
block_test: updated to signed version
hash_test: added dogecoin_hashwriter_free
wallet_test: added vector_add of waddr and dogecoin_free of tx_data to test_wallet
wallet_test: added dogecoin_wallet_flush and dogecoin_wallet_free to test_wallet
wallet_test: added vector_free of addrs to test_wallet_basics
  • Loading branch information
edtubbs committed Dec 12, 2023
1 parent 93d8f3b commit 4ec69f6
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 197 deletions.
2 changes: 1 addition & 1 deletion include/dogecoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct _auxpow {
} auxpow;

typedef struct dogecoin_block_header_ {
uint32_t version;
int32_t version;
uint256 prev_block;
uint256 merkle_root;
uint32_t timestamp;
Expand Down
6 changes: 6 additions & 0 deletions include/dogecoin/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ static hashwriter* init_hashwriter(int n_type, int n_version) {
return hw;
}

static void dogecoin_hashwriter_free(hashwriter* hw) {
free(hw->ctx->sha);
free(hw->ctx);
dogecoin_free(hw->hash);
dogecoin_free(hw);
}

/** SipHash 2-4 */
typedef struct siphasher {
Expand Down
1 change: 0 additions & 1 deletion include/dogecoin/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ LIBDOGECOIN_API int64_t dogecoin_wallet_wtx_get_available_credit(dogecoin_wallet
LIBDOGECOIN_API dogecoin_bool dogecoin_wallet_txout_is_mine(dogecoin_wallet* wallet, dogecoin_tx_out* tx_out);

/** checks if a transaction outpoint is owned by the wallet */
LIBDOGECOIN_API void dogecoin_wallet_add_to_spent(dogecoin_wallet* wallet, const dogecoin_wtx* wtx);
LIBDOGECOIN_API dogecoin_bool dogecoin_wallet_is_spent(dogecoin_wallet* wallet, uint256 hash, uint32_t n);
LIBDOGECOIN_API dogecoin_bool dogecoin_wallet_get_unspents(dogecoin_wallet* wallet, vector* unspents);
LIBDOGECOIN_API dogecoin_bool dogecoin_wallet_get_unspent(dogecoin_wallet* wallet, vector* unspent);
Expand Down
6 changes: 3 additions & 3 deletions src/auxpow.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ uint256* check_merkle_branch(uint256* hash, const vector* merkle_branch, int ind
uint256* current_hash = dogecoin_uint256_vla(1);
memcpy(current_hash, hash, sizeof(uint256)); // Copy the initial hash

for (int i = 0; i < merkle_branch->len; ++i) {
for (size_t i = 0; i < merkle_branch->len; ++i) {
uint256* next_branch_hash = (uint256*)vector_idx(merkle_branch, i);
uint256* new_hash;

if (index & 1) {
new_hash = Hash(next_branch_hash, current_hash);
new_hash = Hash((const uint256*) next_branch_hash, (const uint256*) current_hash);
} else {
new_hash = Hash(current_hash, next_branch_hash);
new_hash = Hash((const uint256*) current_hash, (const uint256*) next_branch_hash);
}

memcpy(current_hash, new_hash, sizeof(uint256)); // Update the current hash
Expand Down
24 changes: 17 additions & 7 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ dogecoin_bool check(void *ctx, uint256* hash, uint32_t chainid, dogecoin_chainpa
dogecoin_block_header* dogecoin_block_header_new() {
dogecoin_block_header* header;
header = dogecoin_calloc(1, sizeof(*header));
header->version = 0;
dogecoin_mem_zero(&header->prev_block, DOGECOIN_HASH_LENGTH);
dogecoin_mem_zero(&header->merkle_root, DOGECOIN_HASH_LENGTH);
header->bits = 0;
header->timestamp = 0;
header->nonce = 0;
header->auxpow->check = check;
header->auxpow->ctx = header;
return header;
}

Expand All @@ -173,10 +181,10 @@ dogecoin_auxpow_block* dogecoin_auxpow_block_new() {
block->parent_coinbase = dogecoin_tx_new();
dogecoin_mem_zero(&block->parent_hash, DOGECOIN_HASH_LENGTH);
block->parent_merkle_count = 0;
block->parent_coinbase_merkle;
block->parent_coinbase_merkle = NULL;
block->parent_merkle_index = 0;
block->aux_merkle_count = 0;
block->aux_merkle_branch;
block->aux_merkle_branch = NULL;
block->aux_merkle_index = 0;
block->parent_header = dogecoin_block_header_new();
block->header->auxpow->check = check;
Expand All @@ -194,7 +202,7 @@ dogecoin_auxpow_block* dogecoin_auxpow_block_new() {
*/
void dogecoin_block_header_free(dogecoin_block_header* header) {
if (!header) return;
header->version = 1;
header->version = 0;
dogecoin_mem_zero(&header->prev_block, DOGECOIN_HASH_LENGTH);
dogecoin_mem_zero(&header->merkle_root, DOGECOIN_HASH_LENGTH);
header->bits = 0;
Expand Down Expand Up @@ -300,12 +308,13 @@ void print_block(dogecoin_auxpow_block* block) {
*
* @param header The header object to be constructed.
* @param buf The buffer to deserialize from.
* @param params The chain parameters.
*
* @return 1 if deserialization was successful, 0 otherwise.
*/
int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct const_buffer* buf, const dogecoin_chainparams *params) {
dogecoin_auxpow_block* block = dogecoin_auxpow_block_new();
if (!deser_u32(&block->header->version, buf))
if (!deser_s32(&block->header->version, buf))
return false;
if (!deser_u256(block->header->prev_block, buf))
return false;
Expand Down Expand Up @@ -392,7 +401,7 @@ int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const
printf("%s:%d:%s:%s\n", __FILE__, __LINE__, __func__, strerror(errno));
return false;
}
if (!deser_u32(&block->parent_header->version, buffer)) {
if (!deser_s32(&block->parent_header->version, buffer)) {
printf("%s:%d:%s:%s\n", __FILE__, __LINE__, __func__, strerror(errno));
return false;
}
Expand All @@ -419,7 +428,6 @@ int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const

if (!check_auxpow(block, (dogecoin_chainparams*)params)) {
printf("check_auxpow failed!\n");
print_block(block);
return false;
}

Expand All @@ -436,7 +444,7 @@ int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const
* @return Nothing.
*/
void dogecoin_block_header_serialize(cstring* s, const dogecoin_block_header* header) {
ser_u32(s, header->version);
ser_s32(s, header->version);
ser_u256(s, header->prev_block);
ser_u256(s, header->merkle_root);
ser_u32(s, header->timestamp);
Expand All @@ -460,6 +468,8 @@ void dogecoin_block_header_copy(dogecoin_block_header* dest, const dogecoin_bloc
dest->timestamp = src->timestamp;
dest->bits = src->bits;
dest->nonce = src->nonce;
dest->auxpow->check = src->auxpow->check;
dest->auxpow->ctx = src->auxpow->ctx;
}

/**
Expand Down
57 changes: 45 additions & 12 deletions src/headersdb_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,30 @@ void dogecoin_headers_db_free(dogecoin_headers_db* db) {
}

if (db->tree_root) {
dogecoin_btree_tdestroy(db->tree_root, dogecoin_free);
dogecoin_btree_tdestroy(db->tree_root, NULL);
db->tree_root = NULL;
}

// Free all blockindex structures starting from chaintip to chainbottom
if (db->chaintip) {
dogecoin_blockindex *scan_tip = db->chaintip;
while (scan_tip && scan_tip != db->chainbottom) {
dogecoin_blockindex *prev = scan_tip->prev;
dogecoin_free(scan_tip);
scan_tip = prev;
}
#ifndef __APPLE__
// If scan_tip is chainbottom, free it
if (scan_tip == db->chainbottom) {
dogecoin_free(scan_tip);
db->chainbottom = NULL;
}
#endif
}

db->chaintip = NULL;
db->chainbottom = NULL;

dogecoin_free(db);
}

Expand Down Expand Up @@ -181,8 +201,17 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
size_t connected_headers_count = 0;
if (db->headers_tree_file && !create)
{
printf("Loading headers from disk, this may take several minutes...\n");

while (!feof(db->headers_tree_file))
{
// print progress
if (connected_headers_count % 1000 == 0)
{
printf("\r%ld headers loaded", connected_headers_count);
fflush(stdout);
}

uint8_t buf_all[32+4+80];
if (fread(buf_all, sizeof(buf_all), 1, db->headers_tree_file) == 1) {
struct const_buffer cbuf_all = {buf_all, sizeof(buf_all)};
Expand All @@ -201,7 +230,7 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
if (!dogecoin_block_header_deserialize(&chainheader->header, &cbuf_all, db->params)) {
dogecoin_block_header_free(&chainheader->header);
dogecoin_free(chainheader);
fprintf(stderr, "Error: Invalid data found.\n");
fprintf(stderr, "\nError: Invalid data found.\n");
return -1;
}
dogecoin_block_header_hash(&chainheader->header, (uint8_t *)&chainheader->hash);
Expand All @@ -212,7 +241,7 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
dogecoin_headers_db_connect_hdr(db, &cbuf_all, true, &connected);
if (!connected)
{
printf("Connecting header failed (at height: %d)\n", db->chaintip->height);
printf("\nConnecting header failed (at height: %d)\n", db->chaintip->height);
}
else {
connected_headers_count++;
Expand All @@ -221,7 +250,7 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
}
}
}
printf("Connected %ld headers, now at height: %d\n", connected_headers_count, db->chaintip->height);
printf("\nConnected %ld headers, now at height: %d\n", connected_headers_count, db->chaintip->height);
return (db->headers_tree_file != NULL);
}

Expand Down Expand Up @@ -261,7 +290,12 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
*connected = false;

dogecoin_blockindex *blockindex = dogecoin_calloc(1, sizeof(dogecoin_blockindex));
if (!dogecoin_block_header_deserialize(&blockindex->header, buf, db->params)) return NULL;
if (!dogecoin_block_header_deserialize(&blockindex->header, buf, db->params))
{
dogecoin_free(blockindex);
fprintf(stderr, "Error deserializing block header\n");
return NULL;
}

dogecoin_block_header_hash(&blockindex->header, (uint8_t *)&blockindex->hash);

Expand Down Expand Up @@ -313,6 +347,7 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
}
}
if (db->use_binary_tree) {
/* TODO: update when fork handling is implemented */
dogecoin_btree_tfind(blockindex, &db->tree_root, dogecoin_header_compare);
}

Expand Down Expand Up @@ -340,14 +375,12 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
}
}
*connected = true;
return blockindex;
} else {
// Connection not established, free allocated memory
dogecoin_free(blockindex);
return NULL;
}
else {
//TODO, add to orphans
char hex[65] = {0};
utils_bin_to_hex(blockindex->hash, DOGECOIN_HASH_LENGTH, hex);
}

return blockindex;
}

/**
Expand Down
Loading

0 comments on commit 4ec69f6

Please sign in to comment.