Skip to content

Commit

Permalink
Remove update_count from find_account_onchain_fees
Browse files Browse the repository at this point in the history
update_count is just the count of the records for a tx.  To calculate onchain fees
for an account we must sum all credits vs debits.  We don't need to GROUP BY update_count
nor ORDER BY update_count since it is just a running index of updates to this tx.
  • Loading branch information
evansmj authored and cdecker committed Feb 2, 2024
1 parent 78ba413 commit c90ca10
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 41 deletions.
2 changes: 1 addition & 1 deletion plugins/bkpr/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ struct fee_sum **find_account_onchain_fees(const tal_t *ctx,
" FROM onchain_fees"
" WHERE account_id = ?"
" GROUP BY txid"
" ORDER BY txid, update_count"));
" ORDER BY txid"));

db_bind_u64(stmt, acct->db_id);
db_query_prepared(stmt);
Expand Down
42 changes: 2 additions & 40 deletions tests/test_bookkeeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,26 +520,8 @@ def test_bookkeeping_inspect_multifundchannel(node_factory, bitcoind):
wait_for(lambda: l1.channel_state(l3) == 'CHANNELD_NORMAL')
wait_for(lambda: l1.channel_state(l4) == 'CHANNELD_NORMAL')

# calculate the tx fee from bitcoin-cli by subtracting the multifundchannel tx inputs and outputs
# now use getblock to get the tx fee from bitcoin-cli's perspective
multifundchannel_rawtx = l1.bitcoin.rpc.getrawtransaction(multifundchannel_txid, True)

inputs_total_btc = 0
vins = multifundchannel_rawtx['vin']
for vin in vins:
temp_txid = vin['txid']
temp_vout = vin['vout']
temp_rawtx = l1.bitcoin.rpc.getrawtransaction(temp_txid, True)
output_amount_btc = temp_rawtx['vout'][temp_vout]['value']
inputs_total_btc += output_amount_btc

outputs_total_btc = 0
vouts = multifundchannel_rawtx['vout']
for vout in vouts:
outputs_total_btc += vout['value']

calculated_total_fees_btc = inputs_total_btc - outputs_total_btc

# now use getblock to get the tx fee also from bitcoin-cli's perspective
blockhash = multifundchannel_rawtx['blockhash']
getblock_tx = l1.bitcoin.rpc.getblock(blockhash, 2)['tx']
getblock_fee_btc = 0
Expand All @@ -556,7 +538,6 @@ def test_bookkeeping_inspect_multifundchannel(node_factory, bitcoind):
+ channel_13_multifundchannel_fee_msat
+ channel_14_multifundchannel_fee_msat).to_btc()

assert bkpr_total_fee_btc == calculated_total_fees_btc
assert bkpr_total_fee_btc == getblock_fee_btc


Expand Down Expand Up @@ -604,26 +585,8 @@ def test_bookkeeping_inspect_mfc_dual_funded(node_factory, bitcoind):
wait_for(lambda: l1.channel_state(l3) == 'CHANNELD_NORMAL')
wait_for(lambda: l1.channel_state(l4) == 'CHANNELD_NORMAL')

# calculate the tx fee from bitcoin-cli by subtracting the multifundchannel tx inputs and outputs
# now use getblock to get the tx fee from bitcoin-cli's perspective
multifundchannel_rawtx = l1.bitcoin.rpc.getrawtransaction(multifundchannel_txid, True)

inputs_total_btc = 0
vins = multifundchannel_rawtx['vin']
for vin in vins:
temp_txid = vin['txid']
temp_vout = vin['vout']
temp_rawtx = l1.bitcoin.rpc.getrawtransaction(temp_txid, True)
output_amount_btc = temp_rawtx['vout'][temp_vout]['value']
inputs_total_btc += output_amount_btc

outputs_total_btc = 0
vouts = multifundchannel_rawtx['vout']
for vout in vouts:
outputs_total_btc += vout['value']

calculated_total_fees_btc = inputs_total_btc - outputs_total_btc

# now use getblock to get the tx fee also from bitcoin-cli's perspective
blockhash = multifundchannel_rawtx['blockhash']
getblock_tx = l1.bitcoin.rpc.getblock(blockhash, 2)['tx']
getblock_fee_btc = 0
Expand All @@ -646,7 +609,6 @@ def test_bookkeeping_inspect_mfc_dual_funded(node_factory, bitcoind):
+ channel_14_multifundchannel_fee_msat
+ channel_41_multifundchannel_fee_msat).to_btc()

assert bkpr_total_fee_btc == calculated_total_fees_btc
assert bkpr_total_fee_btc == getblock_fee_btc


Expand Down

0 comments on commit c90ca10

Please sign in to comment.