Skip to content

Commit

Permalink
bkpr: add 'msat' suffix to all msat denominated fields
Browse files Browse the repository at this point in the history
Makes our json schema parsing work as expected.
  • Loading branch information
niftynei authored and rustyrussell committed Jul 28, 2022
1 parent d885407 commit e2ef44c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 45 deletions.
14 changes: 7 additions & 7 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ static struct command_result *json_inspect(struct command *cmd,

fee_sum = find_sum_for_txid(fee_sums, set->txid);
if (fee_sum)
json_add_amount_msat_only(res, "fees_paid",
json_add_amount_msat_only(res, "fees_paid_msat",
fee_sum->fees_paid);
else
json_add_amount_msat_only(res, "fees_paid",
json_add_amount_msat_only(res, "fees_paid_msat",
AMOUNT_MSAT(0));

json_array_start(res, "outputs");
Expand Down Expand Up @@ -311,9 +311,9 @@ static struct command_result *json_inspect(struct command *cmd,
json_add_num(res, "outnum",
ev->outpoint.n);
json_add_string(res, "output_tag", ev->tag);
json_add_amount_msat_only(res, "output_value",
json_add_amount_msat_only(res, "output_value_msat",
ev->output_value);
json_add_amount_msat_only(res, "credit",
json_add_amount_msat_only(res, "credit_msat",
ev->credit);
json_add_string(res, "currency", ev->currency);
if (ev->origin_acct)
Expand All @@ -328,15 +328,15 @@ static struct command_result *json_inspect(struct command *cmd,
ev->acct_name);
json_add_num(res, "outnum",
ev->outpoint.n);
json_add_amount_msat_only(res, "output_value",
json_add_amount_msat_only(res, "output_value_msat",
ev->output_value);
json_add_string(res, "currency",
ev->currency);
}
json_add_string(res, "spend_tag", ev->tag);
json_add_txid(res, "spending_txid",
ev->spending_txid);
json_add_amount_msat_only(res, "debit", ev->debit);
json_add_amount_msat_only(res, "debit_msat", ev->debit);
if (ev->payment_id)
json_add_sha256(res, "payment_id",
ev->payment_id);
Expand Down Expand Up @@ -499,7 +499,7 @@ static struct command_result *json_list_balances(struct command *cmd,
json_array_start(res, "balances");
for (size_t j = 0; j < tal_count(balances); j++) {
json_object_start(res, NULL);
json_add_amount_msat_only(res, "balance",
json_add_amount_msat_only(res, "balance_msat",
balances[j]->balance);
json_add_string(res, "coin_type",
balances[j]->currency);
Expand Down
4 changes: 2 additions & 2 deletions plugins/bkpr/chain_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ void json_add_chain_event(struct json_stream *out, struct chain_event *ev)
json_add_string(out, "origin", ev->origin_acct);
json_add_string(out, "type", "chain");
json_add_string(out, "tag", ev->tag);
json_add_amount_msat_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_amount_msat_only(out, "credit_msat", ev->credit);
json_add_amount_msat_only(out, "debit_msat", ev->debit);
json_add_string(out, "currency", ev->currency);
json_add_outpoint(out, "outpoint", &ev->outpoint);

Expand Down
6 changes: 3 additions & 3 deletions plugins/bkpr/channel_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ void json_add_channel_event(struct json_stream *out,
json_add_string(out, "account", ev->acct_name);
json_add_string(out, "type", "channel");
json_add_string(out, "tag", ev->tag);
json_add_amount_msat_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_amount_msat_only(out, "credit_msat", ev->credit);
json_add_amount_msat_only(out, "debit_msat", ev->debit);
if (!amount_msat_zero(ev->fees))
json_add_amount_msat_only(out, "fees", ev->fees);
json_add_amount_msat_only(out, "fees_msat", ev->fees);
json_add_string(out, "currency", ev->currency);
if (ev->payment_id)
json_add_sha256(out, "payment_id", ev->payment_id);
Expand Down
22 changes: 11 additions & 11 deletions plugins/bkpr/channelsapy.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,21 @@ void json_add_channel_apy(struct json_stream *res,

json_add_string(res, "account", apy->acct_name);

json_add_amount_msat_only(res, "routed_out", apy->routed_out);
json_add_amount_msat_only(res, "routed_in", apy->routed_in);
json_add_amount_msat_only(res, "lease_fee_paid", apy->lease_out);
json_add_amount_msat_only(res, "lease_fee_earned", apy->lease_in);
json_add_amount_msat_only(res, "pushed_out", apy->push_out);
json_add_amount_msat_only(res, "pushed_in", apy->push_in);

json_add_amount_msat_only(res, "our_start_balance", apy->our_start_bal);
json_add_amount_msat_only(res, "channel_start_balance",
json_add_amount_msat_only(res, "routed_out_msat", apy->routed_out);
json_add_amount_msat_only(res, "routed_in_msat", apy->routed_in);
json_add_amount_msat_only(res, "lease_fee_paid_msat", apy->lease_out);
json_add_amount_msat_only(res, "lease_fee_earned_msat", apy->lease_in);
json_add_amount_msat_only(res, "pushed_out_msat", apy->push_out);
json_add_amount_msat_only(res, "pushed_in_msat", apy->push_in);

json_add_amount_msat_only(res, "our_start_balance_msat", apy->our_start_bal);
json_add_amount_msat_only(res, "channel_start_balance_msat",
apy->total_start_bal);

ok = amount_msat_add(&total_fees, apy->fees_in, apy->fees_out);
assert(ok);
json_add_amount_msat_only(res, "fees_out", apy->fees_out);
json_add_amount_msat_only(res, "fees_in", apy->fees_in);
json_add_amount_msat_only(res, "fees_out_msat", apy->fees_out);
json_add_amount_msat_only(res, "fees_in_msat", apy->fees_in);

/* utilization (out): routed_out/total_balance */
assert(!amount_msat_zero(apy->total_start_bal));
Expand Down
4 changes: 2 additions & 2 deletions plugins/bkpr/incomestmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ void json_add_income_event(struct json_stream *out, struct income_event *ev)
json_object_start(out, NULL);
json_add_string(out, "account", ev->acct_name);
json_add_string(out, "tag", ev->tag);
json_add_amount_msat_only(out, "credit", ev->credit);
json_add_amount_msat_only(out, "debit", ev->debit);
json_add_amount_msat_only(out, "credit_msat", ev->credit);
json_add_amount_msat_only(out, "debit_msat", ev->debit);
json_add_string(out, "currency", ev->currency);
json_add_u64(out, "timestamp", ev->timestamp);

Expand Down
4 changes: 2 additions & 2 deletions plugins/bkpr/onchain_fee.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ void json_add_onchain_fee(struct json_stream *out,
json_add_string(out, "account", fee->acct_name);
json_add_string(out, "type", "onchain_fee");
json_add_string(out, "tag", "onchain_fee");
json_add_amount_msat_only(out, "credit", fee->credit);
json_add_amount_msat_only(out, "debit", fee->debit);
json_add_amount_msat_only(out, "credit_msat", fee->credit);
json_add_amount_msat_only(out, "debit_msat", fee->debit);
json_add_string(out, "currency", fee->currency);
json_add_u64(out, "timestamp", fee->timestamp);
json_add_txid(out, "txid", &fee->txid);
Expand Down
32 changes: 16 additions & 16 deletions tests/test_bookkeeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_bookkeeping_closing_trimmed_htlcs(node_factory, bitcoind, executor):
fees = find_tags(evs, 'onchain_fee')
close_fee = [e for e in fees if e['txid'] == close['txid']]
assert len(close_fee) == 1
assert Millisatoshi(close_fee[0]['credit']) + Millisatoshi(delayed_to['credit']) == Millisatoshi(close['debit'])
assert close_fee[0]['credit_msat'] + delayed_to['credit_msat'] == close['debit_msat']

# l2's fees should equal the trimmed htlc out
evs = l2.rpc.listaccountevents()['events']
Expand All @@ -66,8 +66,8 @@ def test_bookkeeping_closing_trimmed_htlcs(node_factory, bitcoind, executor):
close_fee = [e for e in fees if e['txid'] == close['txid']]
assert len(close_fee) == 1
# sent htlc was too small, we lose it, rounded up to nearest sat
assert close_fee[0]['credit'] == '101000msat'
assert Millisatoshi(close_fee[0]['credit']) + Millisatoshi(deposit['credit']) == Millisatoshi(close['debit'])
assert close_fee[0]['credit_msat'] == Millisatoshi('101000msat')
assert close_fee[0]['credit_msat'] + deposit['credit_msat'] == close['debit_msat']


@unittest.skipIf(TEST_NETWORK != 'regtest', "fixme: broadcast fails, dusty")
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_bookkeeping_closing_subsat_htlcs(node_factory, bitcoind, chainparams):
fees = find_tags(evs, 'onchain_fee')
close_fee = [e for e in fees if e['txid'] == close['txid']]
assert len(close_fee) == 1
assert Millisatoshi(close_fee[0]['credit']) + Millisatoshi(delayed_to['credit']) == Millisatoshi(close['debit'])
assert close_fee[0]['credit_msat'] + delayed_to['credit_msat'] == close['debit_msat']

evs = l2.rpc.listaccountevents()['events']
close = find_first_tag(evs, 'channel_close')
Expand All @@ -105,8 +105,8 @@ def test_bookkeeping_closing_subsat_htlcs(node_factory, bitcoind, chainparams):
close_fee = [e for e in fees if e['txid'] == close['txid']]
assert len(close_fee) == 1
# too small to fit, we lose them as miner fees
assert close_fee[0]['credit'] == '333msat'
assert Millisatoshi(close_fee[0]['credit']) + Millisatoshi(deposit['credit']) == Millisatoshi(close['debit'])
assert close_fee[0]['credit_msat'] == Millisatoshi('333msat')
assert close_fee[0]['credit_msat'] + deposit['credit_msat'] == close['debit_msat']


@unittest.skipIf(TEST_NETWORK != 'regtest', "External wallet support doesn't work with elements yet.")
Expand Down Expand Up @@ -141,15 +141,15 @@ def test_bookkeeping_external_withdraws(node_factory, bitcoind):
for inc in incomes:
assert inc['account'] == 'wallet'
assert inc['tag'] == 'deposit'
assert Millisatoshi(inc['credit']) == amount_msat
assert inc['credit_msat'] == amount_msat
# The event should show up in the 'listaccountevents' however
events = l1.rpc.listaccountevents()['events']
assert len(events) == 3
external = [e for e in events if e['account'] == 'external'][0]
assert Millisatoshi(external['credit']) == Millisatoshi(amount // 2 * 1000)
assert external['credit_msat'] == Millisatoshi(amount // 2 * 1000)

btc_balance = only_one(only_one(l1.rpc.listbalances()['accounts'])['balances'])
assert Millisatoshi(btc_balance['balance']) == amount_msat * 2
assert btc_balance['balance_msat'] == amount_msat * 2

# Restart the node, issues a balance snapshot
# If we were counting these incorrectly,
Expand All @@ -166,7 +166,7 @@ def test_bookkeeping_external_withdraws(node_factory, bitcoind):

# the wallet balance should be unchanged
btc_balance = only_one(only_one(l1.rpc.listbalances()['accounts'])['balances'])
assert Millisatoshi(btc_balance['balance']) == amount_msat * 2
assert btc_balance['balance_msat'] == amount_msat * 2

# ok now we mine a block
bitcoind.generate_block(1)
Expand All @@ -177,16 +177,16 @@ def test_bookkeeping_external_withdraws(node_factory, bitcoind):
incomes = l1.rpc.listincome()['income_events']
# 2 wallet deposits, 1 wallet withdrawal, 1 onchain_fee
assert len(incomes) == 4
withdraw_amt = Millisatoshi(find_tags(incomes, 'withdrawal')[0]['debit'])
withdraw_amt = find_tags(incomes, 'withdrawal')[0]['debit_msat']
assert withdraw_amt == Millisatoshi(amount // 2 * 1000)

fee_events = find_tags(incomes, 'onchain_fee')
assert len(fee_events) == 1
fees = Millisatoshi(fee_events[0]['debit'])
fees = fee_events[0]['debit_msat']

# wallet balance is decremented now
btc_balance = only_one(only_one(l1.rpc.listbalances()['accounts'])['balances'])
assert Millisatoshi(btc_balance['balance']) == amount_msat * 2 - withdraw_amt - fees
assert btc_balance['balance_msat'] == amount_msat * 2 - withdraw_amt - fees


@unittest.skipIf(TEST_NETWORK != 'regtest', "External wallet support doesn't work with elements yet.")
Expand Down Expand Up @@ -233,7 +233,7 @@ def test_bookkeeping_external_withdraw_missing(node_factory, bitcoind):

# the wallet balance should be unchanged
btc_balance = only_one(only_one(l1.rpc.listbalances()['accounts'])['balances'])
assert Millisatoshi(btc_balance['balance']) == amount_msat * 2
assert btc_balance['balance_msat'] == amount_msat * 2

# ok now we mine a block
bitcoind.generate_block(1)
Expand All @@ -248,12 +248,12 @@ def test_bookkeeping_external_withdraw_missing(node_factory, bitcoind):

fee_events = find_tags(incomes, 'onchain_fee')
assert len(fee_events) == 1
fees = Millisatoshi(fee_events[0]['debit'])
fees = fee_events[0]['debit_msat']
assert fees > Millisatoshi(amount // 2 * 1000)

# wallet balance is decremented now
bal = only_one(only_one(l1.rpc.listbalances()['accounts'])['balances'])
assert Millisatoshi(bal['balance']) == amount_msat * 2 - fees
assert bal['balance_msat'] == amount_msat * 2 - fees


@unittest.skipIf(TEST_NETWORK != 'regtest', "External wallet support doesn't work with elements yet.")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,9 +1314,9 @@ def _income_tagset(node, tagset):
# the balance on l3 should equal the invoice
bal = only_one(only_one(l3.rpc.listbalances()['accounts'])['balances'])['balance']
assert incomes[0]['tag'] == 'invoice'
assert bal == incomes[0]['debit']
assert Millisatoshi(bal) == incomes[0]['debit_msat']
inve = only_one([e for e in l1.rpc.listaccountevents()['events'] if e['tag'] == 'invoice'])
assert Millisatoshi(inve['debit']) == Millisatoshi(incomes[0]['debit']) + Millisatoshi(incomes[1]['debit'])
assert inve['debit_msat'] == incomes[0]['debit_msat'] + incomes[1]['debit_msat']


@pytest.mark.developer("needs DEVELOPER=1 for dev_ignore_htlcs")
Expand Down

0 comments on commit e2ef44c

Please sign in to comment.