Skip to content

Commit

Permalink
wallet: prevent duplicate utxos from being added to spends vector
Browse files Browse the repository at this point in the history
  • Loading branch information
xanimo committed Jun 20, 2023
1 parent 43dcaff commit 229e8e3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pattern_match* init_pattern_match(size_t length) {
dogecoin_bool check(void *ctx, uint256* hash, uint32_t chainid, dogecoin_chainparams* params) {
dogecoin_auxpow_block* block = (dogecoin_auxpow_block*)ctx;

if ((block->parent_merkle_index || block->aux_merkle_index) != 0) {
if (block->parent_merkle_index != 0) {
printf("Auxpow is not a generate\n");
return false;
}
Expand Down Expand Up @@ -302,7 +302,7 @@ int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct cons
dogecoin_block_header_copy(header, block->header);
if ((block->header->version & BLOCK_VERSION_AUXPOW_BIT) != 0) {
if (!deserialize_dogecoin_auxpow_block(block, buf, params)) {
printf("%d:%s\n", __LINE__, __func__);
// printf("%d:%s\n", __LINE__, __func__);
}
}
dogecoin_auxpow_block_free(block);
Expand Down
2 changes: 1 addition & 1 deletion src/validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dogecoin_bool check_auxpow(dogecoin_auxpow_block block, dogecoin_chainparams* pa
the chain ID is correct. Legacy blocks are not allowed since
the merge-mining start, which is checked in AcceptBlockHeader
where the height is known. */
if (!is_legacy(block.header->version) && params->strict_id && get_chainid(block.header->version) == params->auxpow_id) {
if (!is_legacy(block.header->version) && params->strict_id && get_chainid(block.header->version) != params->auxpow_id) {
printf("%s:%d:%s : block does not have our chain ID"
" (got %d, expected %d, full nVersion %d) : %s\n",
__FILE__, __LINE__, __func__, get_chainid(block.header->version),
Expand Down
71 changes: 45 additions & 26 deletions src/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,22 @@ void dogecoin_wallet_scrape_utxos(dogecoin_wallet* wallet, dogecoin_wtx* wtx) {
uint8_t* prevout_hash_bytes = utils_hex_to_uint8(prevout_hash);
// compare wtx->tx->vin->prevout.hash and prevout.n with utxo->txid and utxo->vout:
if (memcmp(prevout_hash_bytes, utxo->txid, 32)==0 && (int)tx_in->prevout.n == utxo->vout) {
// prevent spending/solving:
utxo->spendable = 0;
utxo->solvable = 0;
// add to spends vector:
vector_add(wallet->spends, utxo);
// remove index from unspent vector:
vector_remove_idx(wallet->unspent, l);
size_t m = 0, n = 0;
for (; m < wallet->spends->len; m++) {
dogecoin_utxo* spent_utxo = vector_idx(wallet->spends, m);
if (memcmp(spent_utxo->txid, utxo->txid, 32) == 0 && spent_utxo->vout == utxo->vout) {
n++;
}
}
if (n == 0) {
// prevent spending/solving:
utxo->spendable = 0;
utxo->solvable = 0;
// add to spends vector:
vector_add(wallet->spends, utxo);
// remove index from unspent vector:
vector_remove_idx(wallet->unspent, l);
}
}
}
}
Expand All @@ -422,15 +431,14 @@ void dogecoin_wallet_scrape_utxos(dogecoin_wallet* wallet, dogecoin_wtx* wtx) {
vector* addrs = vector_new(1, free);
// grab all addresses in vector:
dogecoin_wallet_get_addresses(wallet, addrs);
unsigned int i;
unsigned int i, h = 0, g;
// loop through addresses:
for (i = 0; i < addrs->len; i++) {
char* addr = vector_idx(addrs, i);
// compare wtx->tx->vout with address from wallet->waddr_vector:
if (strncmp(p2pkh_from_script_pubkey, addr, P2PKH_ADDR_STRINGLEN - 1)==0) {
// match so we populate utxo struct:
dogecoin_utxo* utxo = dogecoin_wallet_utxo_new();

// make the txid:
dogecoin_tx_hash(wtx->tx, (uint8_t*)utxo->txid);
// convert from uint8_t to char*
Expand All @@ -439,22 +447,33 @@ void dogecoin_wallet_scrape_utxos(dogecoin_wallet* wallet, dogecoin_wtx* wtx) {
utils_reverse_hex(hexbuf, DOGECOIN_HASH_LENGTH*2);
// copy back to utxo->txid as uint256 (uint8_t* or uint8_t[32]):
memcpy_safe(utxo->txid, utils_hex_to_uint8(hexbuf), 32);

// copy matching script_pubkey:
memcpy_safe(utxo->script_pubkey, utils_uint8_to_hex((const uint8_t*)tx_out->script_pubkey->str, tx_out->script_pubkey->len), SCRIPT_PUBKEY_STRINGLEN);
// set tx->tx_in->prevout.n (utxo->vout):
utxo->vout = j;

// set utxo p2pkh address:
memcpy_safe(utxo->address, p2pkh_from_script_pubkey, P2PKH_ADDR_STRINGLEN);

// set amount of utxo:
koinu_to_coins_str(tx_out->value, utxo->amount);

// finally add utxo to rbtree:
dogecoin_btree_tfind(utxo, &wallet->unspent_rbtree, dogecoin_utxo_compare);
// and vector:
vector_add(wallet->unspent, utxo);
g = 0;
for (; h < wallet->unspent->len; h++) {
dogecoin_utxo* unspent_utxo = vector_idx(wallet->unspent, h);
if (memcmp(unspent_utxo->txid, utxo->txid, 32)==0 && (size_t)unspent_utxo->vout == j) {
g++;
}
}
for (h = 0; h < wallet->spends->len; h++) {
dogecoin_utxo* spent_utxo = vector_idx(wallet->spends, h);
if (memcmp(spent_utxo->txid, utxo->txid, 32)==0 && (size_t)spent_utxo->vout == j) {
g++;
}
}
if (g == 0) {
// copy matching script_pubkey:
memcpy_safe(utxo->script_pubkey, utils_uint8_to_hex((const uint8_t*)tx_out->script_pubkey->str, tx_out->script_pubkey->len), SCRIPT_PUBKEY_STRINGLEN);
// set tx->tx_in->prevout.n (utxo->vout):
utxo->vout = j;
// set utxo p2pkh address:
memcpy_safe(utxo->address, p2pkh_from_script_pubkey, P2PKH_ADDR_STRINGLEN);
// set amount of utxo:
koinu_to_coins_str(tx_out->value, utxo->amount);
// finally add utxo to rbtree:
dogecoin_btree_tfind(utxo, &wallet->unspent_rbtree, dogecoin_utxo_compare);
// and vector:
vector_add(wallet->unspent, utxo);
}
}
}
vector_free(addrs, true);
Expand Down Expand Up @@ -1067,7 +1086,7 @@ dogecoin_bool dogecoin_wallet_get_unspent(dogecoin_wallet* wallet, vector* unspe
void dogecoin_wallet_check_transaction(void *ctx, dogecoin_tx *tx, unsigned int pos, dogecoin_blockindex *pindex) {
(void)(pos);
dogecoin_wallet *wallet = (dogecoin_wallet *)ctx;
if (dogecoin_wallet_is_mine(wallet, tx) || dogecoin_wallet_is_from_me(wallet, tx)) {
if (dogecoin_wallet_is_mine(wallet, tx)) {
printf("\nFound relevant transaction!\n");
dogecoin_wtx* wtx = dogecoin_wallet_wtx_new();
uint256 blockhash;
Expand Down

0 comments on commit 229e8e3

Please sign in to comment.