@@ -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 }
0 commit comments