Skip to content

Commit

Permalink
spvnode/wallet: support multiple addresses on wallet cmd test
Browse files Browse the repository at this point in the history
-wallet cmd test now iterates through multiple addresses and deletes everything from the wallet
-free waddr and buf and call dogecoin_wallet_next_addr on edge case where we delete our last address
  • Loading branch information
xanimo committed Dec 23, 2023
1 parent b4c6d71 commit ebb0443
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
47 changes: 28 additions & 19 deletions src/cli/spvnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,28 +473,37 @@ int main(int argc, char* argv[]) {
#if WITH_WALLET
dogecoin_ecc_start();
if (address != NULL) {
int res = dogecoin_register_watch_address_with_node(address);
printf("registered: %d %s\n", res, address);
uint64_t amount = dogecoin_get_balance(address);
if (amount > 0) {
char* amount_str = dogecoin_get_balance_str(address);
printf("amount: %s\n", amount_str);
unsigned int utxo_count = dogecoin_get_utxos_length(address);
if (utxo_count) {
printf("utxo count: %d\n", utxo_count);
unsigned int i = 1;
for (; i <= utxo_count; i++) {
printf("txid: %s\n", dogecoin_get_utxo_txid_str(address, i));
printf("vout: %d\n", dogecoin_get_utxo_vout(address, i));
char* utxo_amount_str = dogecoin_get_utxo_amount(address, i);
printf("amount: %s\n", utxo_amount_str);
dogecoin_free(utxo_amount_str);
char delim[] = " ";
// copy address into a new string, strtok modifies the string
char* address_copy = strdup(address);
char *ptr;
char* temp_address_copy = address_copy;

while((ptr = strtok_r(temp_address_copy, delim, &temp_address_copy))) {
int res = dogecoin_register_watch_address_with_node(ptr);
printf("registered: %d %s\n", res, ptr);
uint64_t amount = dogecoin_get_balance(ptr);
if (amount > 0) {
char* amount_str = dogecoin_get_balance_str(ptr);
printf("amount: %s\n", amount_str);
unsigned int utxo_count = dogecoin_get_utxos_length(ptr);
if (utxo_count) {
printf("utxo count: %d\n", utxo_count);
unsigned int i = 1;
for (; i <= utxo_count; i++) {
printf("txid: %s\n", dogecoin_get_utxo_txid_str(ptr, i));
printf("vout: %d\n", dogecoin_get_utxo_vout(ptr, i));
char* utxo_amount_str = dogecoin_get_utxo_amount(ptr, i);
printf("amount: %s\n", utxo_amount_str);
dogecoin_free(utxo_amount_str);
}
}
dogecoin_free(amount_str);
}
dogecoin_free(amount_str);
res = dogecoin_unregister_watch_address_with_node(ptr);
printf("unregistered: %s\n", res ? "true" : "false");
}
res = dogecoin_unregister_watch_address_with_node(address);
printf("unregistered: %s\n", res ? "true" : "false");
dogecoin_free(address_copy);
}
dogecoin_ecc_stop();
#endif
Expand Down
7 changes: 6 additions & 1 deletion src/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,7 @@ int dogecoin_unregister_watch_address_with_node(char* address) {
dogecoin_p2pkh_addr_from_hash160(waddr->pubkeyhash, wallet->chain, p2pkh_check, P2PKHLEN);
if (memcmp(record->str, buf, record->len)==0) {
found = 1;
dogecoin_wallet_addr_free(waddr);
} else {
const char* addr_match = find_needle(ptr, strlen(ptr), p2pkh_check, P2PKHLEN);
if (!addr_match) {
Expand All @@ -1590,7 +1591,6 @@ int dogecoin_unregister_watch_address_with_node(char* address) {
dogecoin_wallet_addr_free(waddr);
}
}
if (found) dogecoin_wallet_addr_free(waddr);
free(buf);
} else if (rectype == WALLET_DB_REC_TYPE_TX) {
unsigned char* buf = dogecoin_uchar_vla(reclen);
Expand All @@ -1613,6 +1613,7 @@ int dogecoin_unregister_watch_address_with_node(char* address) {
goto copy;
}
}
dogecoin_free(buf);
copy:
dogecoin_wallet_scrape_utxos(wallet_new, wtx);
dogecoin_wallet_add_wtx_move(wallet_new, wtx); // hands memory management over to the binary tree
Expand All @@ -1621,6 +1622,10 @@ int dogecoin_unregister_watch_address_with_node(char* address) {
}
}

if (!wallet_new->waddr_vector->len) {
dogecoin_wallet_next_addr(wallet_new);
}

cstr_free(record, true);
dogecoin_wallet_flush(wallet);
dogecoin_wallet_flush(wallet_new);
Expand Down

0 comments on commit ebb0443

Please sign in to comment.