Skip to content

Commit

Permalink
hash: added dogecoin_hashwriter_free
Browse files Browse the repository at this point in the history
headersdb_file: updated dogecoin_headers_db_free to include freeing of chaintips and chainbottom
spv: updated dogecoin_spv_client_free to call disconnect_tip and check for headers_db_ctx
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 already handles unspent and spends vector/rbtrees
wallet: updated dogecoin_wallet_new frees and dogecoin_wallet_free to call dogecoin_hdnode_free and dogecoin_btree_tdestroy with NULL/free
  • Loading branch information
edtubbs committed Nov 28, 2023
1 parent 93d8f3b commit 519a020
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 130 deletions.
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
18 changes: 18 additions & 0 deletions src/headersdb_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ void dogecoin_headers_db_free(dogecoin_headers_db* db) {
db->tree_root = NULL;
}

if (db->chaintip) {
dogecoin_blockindex *scan_tip = db->chaintip;
while (scan_tip->prev) {
dogecoin_blockindex *prev = scan_tip->prev;
dogecoin_free(scan_tip);
scan_tip = prev;
}
}

if (db->chainbottom) {
dogecoin_blockindex *scan_tip = db->chainbottom;
while (scan_tip->prev) {
dogecoin_blockindex *prev = scan_tip->prev;
dogecoin_free(scan_tip);
scan_tip = prev;
}
}

dogecoin_free(db);
}

Expand Down
64 changes: 34 additions & 30 deletions src/spv.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void dogecoin_net_spv_node_handshake_done(dogecoin_node *node);
* the nodegroup's handshake_done_cb to dogecoin_net_spv_node_handshake_done,
* the nodegroup's node_connection_state_changed_cb to NULL, and the
* nodegroup's periodic_timer_cb to dogecoin_net_spv_node_timer_callback
*
*
* @param nodegroup The nodegroup to set the callbacks for.
*/
void dogecoin_net_set_spv(dogecoin_node_group *nodegroup)
Expand All @@ -81,11 +81,11 @@ void dogecoin_net_set_spv(dogecoin_node_group *nodegroup)

/**
* The function creates a new dogecoin_spv_client object and initializes it
*
*
* @param params The chainparams struct that we created earlier.
* @param debug If true, the node will print out debug messages to stdout.
* @param headers_memonly If true, the headers database will not be loaded from disk.
*
*
* @return A pointer to a dogecoin_spv_client object.
*/
dogecoin_spv_client* dogecoin_spv_client_new(const dogecoin_chainparams *params, dogecoin_bool debug, dogecoin_bool headers_memonly, dogecoin_bool use_checkpoints, dogecoin_bool full_sync)
Expand Down Expand Up @@ -128,7 +128,7 @@ dogecoin_spv_client* dogecoin_spv_client_new(const dogecoin_chainparams *params,

/**
* It adds peers to the nodegroup.
*
*
* @param client the dogecoin_spv_client object
* @param ips A comma-separated list of IPs or seeds to connect to.
*/
Expand All @@ -140,7 +140,7 @@ void dogecoin_spv_client_discover_peers(dogecoin_spv_client* client, const char
/**
* The function loops through all the nodes in the node group and connects to the next nodes in the
* node group
*
*
* @param client The dogecoin_spv_client object.
*/
void dogecoin_spv_client_runloop(dogecoin_spv_client* client)
Expand All @@ -151,9 +151,9 @@ void dogecoin_spv_client_runloop(dogecoin_spv_client* client)

/**
* It frees the memory allocated for the client
*
*
* @param client The client object to be freed.
*
*
* @return Nothing.
*/
void dogecoin_spv_client_free(dogecoin_spv_client *client)
Expand All @@ -163,7 +163,11 @@ void dogecoin_spv_client_free(dogecoin_spv_client *client)

if (client->headers_db)
{
client->headers_db->free(client->headers_db_ctx);
// client->headers_db->disconnect_tip(client->headers_db_ctx);
if (client->headers_db_ctx)
{
client->headers_db->free(client->headers_db_ctx);
}
client->headers_db_ctx = NULL;
client->headers_db = NULL;
}
Expand All @@ -178,10 +182,10 @@ void dogecoin_spv_client_free(dogecoin_spv_client *client)

/**
* Loads the headers database from a file
*
*
* @param client the client object
* @param file_path The path to the headers database file.
*
*
* @return A boolean value.
*/
dogecoin_bool dogecoin_spv_client_load(dogecoin_spv_client *client, const char *file_path)
Expand All @@ -198,7 +202,7 @@ dogecoin_bool dogecoin_spv_client_load(dogecoin_spv_client *client, const char *

/**
* If we are in the header sync state, we request headers from a random node
*
*
* @param node the node that we are checking
* @param now The current time in seconds.
*/
Expand Down Expand Up @@ -246,17 +250,17 @@ void dogecoin_net_spv_periodic_statecheck(dogecoin_node *node, uint64_t *now)
}

/**
* This function is called by the dogecoin_node_timer_callback function.
*
* It checks if the last_statecheck_time is greater than the minimum time delta for state checks.
*
* If it is, it calls the dogecoin_net_spv_periodic_statecheck function.
*
* The dogecoin_net_spv_periodic_statecheck function checks if the node is connected to the network.
*
* This function is called by the dogecoin_node_timer_callback function.
*
* It checks if the last_statecheck_time is greater than the minimum time delta for state checks.
*
* If it is, it calls the dogecoin_net_spv_periodic_statecheck function.
*
* The dogecoin_net_spv_periodic_statecheck function checks if the node is connected to the network.
*
* @param node The node that the timer is being called on.
* @param now the current time in seconds since the epoch
*
*
* @return A boolean value.
*/
static dogecoin_bool dogecoin_net_spv_node_timer_callback(dogecoin_node *node, uint64_t *now)
Expand All @@ -273,10 +277,10 @@ static dogecoin_bool dogecoin_net_spv_node_timer_callback(dogecoin_node *node, u

/**
* Fill up the blocklocators vector with the blocklocators from the headers database
*
*
* @param client the spv client
* @param blocklocators a vector of block hashes that we want to scan from
*
*
* @return The blocklocators are being returned.
*/
void dogecoin_net_spv_fill_block_locator(dogecoin_spv_client *client, vector *blocklocators) {
Expand All @@ -298,7 +302,7 @@ void dogecoin_net_spv_fill_block_locator(dogecoin_spv_client *client, vector *bl
}
}
}
if (blocklocators->len > 0) return; // return if we could fill up the blocklocator with checkpoints
if (blocklocators->len > 0) return; // return if we could fill up the blocklocator with checkpoints
}
uint256 *hash = dogecoin_calloc(1, sizeof(uint256));
memcpy_safe(hash, &client->chainparams->genesisblockhash, sizeof(uint256));
Expand All @@ -312,7 +316,7 @@ void dogecoin_net_spv_fill_block_locator(dogecoin_spv_client *client, vector *bl
/**
* This function is called when a node is in headers sync state. It will request the next block headers
* from the node
*
*
* @param node The node that is requesting headers or blocks.
* @param blocks boolean, true if we want to request blocks, false if we want to request headers
*/
Expand Down Expand Up @@ -345,9 +349,9 @@ void dogecoin_net_spv_node_request_headers_or_blocks(dogecoin_node *node, dogeco
/**
* If we have not yet reached the height of the blockchain tip, we request headers from a peer. If we
* have reached the height of the blockchain tip, we request blocks from a peer
*
*
* @param client the spv client
*
*
* @return dogecoin_bool
*/
dogecoin_bool dogecoin_net_spv_request_headers(dogecoin_spv_client *client)
Expand Down Expand Up @@ -406,7 +410,7 @@ dogecoin_bool dogecoin_net_spv_request_headers(dogecoin_spv_client *client)

/**
* When the handshake is done, we request the headers
*
*
* @param node The node that just completed the handshake.
*/
void dogecoin_net_spv_node_handshake_done(dogecoin_node *node)
Expand All @@ -416,11 +420,11 @@ void dogecoin_net_spv_node_handshake_done(dogecoin_node *node)

/**
* The function is called when a new message is received from a peer
*
*
* @param node
* @param hdr
* @param buf
*
*
* @return Nothing.
*/
void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, struct const_buffer *buf)
Expand Down Expand Up @@ -540,7 +544,7 @@ void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, s
client->nodegroup->log_write_cb("Header deserialization (tx count skip) failed (node %d)\n", node->nodeid);
return;
}

if (!connected)
{
client->nodegroup->log_write_cb("Got invalid headers (not in sequence) from node %d\n", node->nodeid);
Expand Down
Loading

0 comments on commit 519a020

Please sign in to comment.