Skip to content

Commit e6d33b0

Browse files
committed
Fixes to the balance db building process.
modified: src/ctrl_mods/btk_balance.c modified: src/mods/txoa.c modified: src/mods/txoa.h
1 parent fe2b359 commit e6d33b0

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

src/ctrl_mods/btk_balance.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,12 @@ int btk_balance_process(thread_args args)
434434
{
435435
uint32_t j;
436436
char address[BUFSIZ];
437+
uint64_t amount;
437438
uint64_t prev_balance;
438439

439440
for (j = 0; j < block->transactions[i]->input_count; j++)
440441
{
442+
amount = 0;
441443
memset(address, 0, BUFSIZ);
442444

443445
// Skip coinbase inputs. No deduction for them.
@@ -446,22 +448,41 @@ int btk_balance_process(thread_args args)
446448
continue;
447449
}
448450

449-
r = txoa_get(address, block->transactions[i]->inputs[j]->tx_hash, block->transactions[i]->inputs[j]->index);
451+
r = txoa_get(address, &amount, block->transactions[i]->inputs[j]->tx_hash, block->transactions[i]->inputs[j]->index);
450452
ERROR_CHECK_NEG(r, "Could not get address from txoa database.");
451453

452454
if (*address)
453455
{
454-
// Every input is spent 100% (recouped via change address).
455-
// Set all inputs to zero balance.
456+
prev_balance = 0;
456457

457-
r = balance_batch_delete(address);
458-
ERROR_CHECK_NEG(r, "Could not update address balance.");
458+
// Every input is spent 100% (recouped via change address).
459459

460460
r = txoa_batch_delete(block->transactions[i]->inputs[j]->tx_hash, block->transactions[i]->inputs[j]->index);
461461
ERROR_CHECK_NEG(r, "Could not delete txao entry after spending.");
462+
463+
// Get previous balance (if any)
464+
r = balance_get(&prev_balance, address);
465+
ERROR_CHECK_NEG(r, "Could not query balance database.");
466+
467+
if (prev_balance > amount)
468+
{
469+
r = balance_batch_put(address, prev_balance - amount);
470+
ERROR_CHECK_NEG(r, "Could not add entry to balance database.");
471+
}
472+
else
473+
{
474+
r = balance_batch_delete(address);
475+
ERROR_CHECK_NEG(r, "Could not update address balance.");
476+
}
462477
}
463478
}
464479

480+
r = txoa_batch_write();
481+
ERROR_CHECK_NEG(r, "Could not batch write txao records.");
482+
483+
r = balance_batch_write();
484+
ERROR_CHECK_NEG(r, "Could not batch write balance records.");
485+
465486
for (j = 0; j < block->transactions[i]->output_count; j++)
466487
{
467488
memset(address, 0, BUFSIZ);
@@ -472,20 +493,22 @@ int btk_balance_process(thread_args args)
472493
block->transactions[i]->version);
473494
ERROR_CHECK_NEG(r, "Could not get address from output script.");
474495

475-
if (*address)
496+
amount = block->transactions[i]->outputs[j]->amount;
497+
498+
if (*address && amount > 0)
476499
{
477500
prev_balance = 0;
478501

502+
// TXOA Database
503+
r = txoa_batch_put(block->transactions[i]->txid, j, address, amount);
504+
ERROR_CHECK_NEG(r, "Could not put entry in the txoa database.");
505+
479506
// Get previous balance (if any)
480507
r = balance_get(&prev_balance, address);
481508
ERROR_CHECK_NEG(r, "Could not query balance database.");
482509

483-
// TXOA Database
484-
r = txoa_batch_put(block->transactions[i]->txid, j, address);
485-
ERROR_CHECK_NEG(r, "Could not put entry in the txoa database.");
486-
487510
// Balance Database
488-
r = balance_batch_put(address, block->transactions[i]->outputs[j]->amount + prev_balance);
511+
r = balance_batch_put(address, amount + prev_balance);
489512
ERROR_CHECK_NEG(r, "Could not add entry to balance database.");
490513
}
491514
}

src/mods/txoa.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void txoa_close(void)
6363
dbref = NULL;
6464
}
6565

66-
int txoa_get(char *address, unsigned char *tx_hash, uint32_t index)
66+
int txoa_get(char *address, uint64_t *amount, unsigned char *tx_hash, uint32_t index)
6767
{
6868
int r;
6969
size_t len;
@@ -79,7 +79,11 @@ int txoa_get(char *address, unsigned char *tx_hash, uint32_t index)
7979
r = database_get(&tmp, &len, dbref, key, TXOA_KEY_LEN);
8080
ERROR_CHECK_NEG(r, "Could not get address from txoa database.");
8181

82-
memcpy(address, tmp, len);
82+
if (len > sizeof(uint64_t))
83+
{
84+
memcpy(address, tmp, len - sizeof(uint64_t));
85+
deserialize_uint64(amount, tmp + (len - sizeof(uint64_t)), SERIALIZE_ENDIAN_LIT);
86+
}
8387

8488
free(tmp);
8589

@@ -159,20 +163,25 @@ int txoa_get_last_block(int *block_num)
159163
return 1;
160164
}
161165

162-
int txoa_batch_put(unsigned char *tx_hash, uint32_t index, char *address)
166+
int txoa_batch_put(unsigned char *tx_hash, uint32_t index, char *address, uint64_t amount)
163167
{
164168
int r;
165169
unsigned char key[TXOA_KEY_LEN];
170+
unsigned char value[BUFSIZ];
166171

167172
assert(tx_hash);
168173
assert(address);
169174

170175
memset(key, 0, TXOA_KEY_LEN);
176+
memset(value, 0, BUFSIZ);
171177

172178
serialize_uchar(key, tx_hash, TRANSACTION_ID_LEN);
173179
serialize_uint32(key + TRANSACTION_ID_LEN, index, SERIALIZE_ENDIAN_LIT);
174180

175-
r = database_batch_put(dbref, key, TXOA_KEY_LEN, (unsigned char *)address, strlen(address));
181+
memcpy(value, address, strlen(address));
182+
serialize_uint64(value + strlen(address), amount, SERIALIZE_ENDIAN_LIT);
183+
184+
r = database_batch_put(dbref, key, TXOA_KEY_LEN, value, strlen(address) + sizeof(uint64_t));
176185
ERROR_CHECK_NEG(r, "Could not add entry to txoa database.");
177186

178187
return 1;

src/mods/txoa.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
int txoa_open(char *, bool);
1515
void txoa_close(void);
16-
int txoa_get(char *, unsigned char *, uint32_t);
16+
int txoa_get(char *, uint64_t *, unsigned char *, uint32_t);
1717
int txoa_put(unsigned char *, uint32_t, char *);
1818
int txoa_delete(unsigned char *, uint32_t);
1919
int txoa_set_last_block(int);
2020
int txoa_get_last_block(int *);
2121
int txoa_get_record_count(size_t *);
2222

23-
int txoa_batch_put(unsigned char *, uint32_t, char *);
23+
int txoa_batch_put(unsigned char *, uint32_t, char *, uint64_t);
2424
int txoa_batch_delete(unsigned char *, uint32_t);
2525
int txoa_batch_write(void);
2626

0 commit comments

Comments
 (0)