diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index 45258abb6..e7e1409bf 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -1723,6 +1723,12 @@ static char *parse_tags(const tal_t *ctx, return NULL; } +static bool json_to_tok(const char *buffer, const jsmntok_t *tok, const jsmntok_t **ret) +{ + *ret = tok; + return true; +} + static struct command_result *json_utxo_deposit(struct command *cmd, const char *buf, const jsmntok_t *params) { const char *move_tag ="utxo_deposit"; @@ -1730,18 +1736,20 @@ static struct command_result *json_utxo_deposit(struct command *cmd, const char struct account *acct; const char *err; struct bkpr *bkpr = bkpr_of(cmd->plugin); + const jsmntok_t *transfer_from; + transfer_from = NULL; err = json_scan(tmpctx, buf, params, "{utxo_deposit:{" "account:%" - ",transfer_from:%" + ",transfer_from?:%" ",outpoint:%" ",amount_msat:%" ",timestamp:%" ",blockheight:%" "}}", - JSON_SCAN_TAL(tmpctx, json_strdup, &ev->acct_name), - JSON_SCAN_TAL(tmpctx, json_strdup, &ev->origin_acct), + JSON_SCAN_TAL(ev, json_strdup, &ev->acct_name), + JSON_SCAN(json_to_tok, &transfer_from), JSON_SCAN(json_to_outpoint, &ev->outpoint), JSON_SCAN(json_to_msat, &ev->credit), JSON_SCAN(json_to_u64, &ev->timestamp), @@ -1753,6 +1761,11 @@ static struct command_result *json_utxo_deposit(struct command *cmd, const char move_tag, err, json_tok_full_len(params), json_tok_full(buf, params)); + if (!transfer_from || json_tok_is_null(buf, transfer_from)) + ev->origin_acct = NULL; + else + ev->origin_acct = json_strdup(ev, buf, transfer_from); + /* Log the thing */ db_begin_transaction(bkpr->db); acct = find_or_create_account(cmd, bkpr, ev->acct_name); diff --git a/tests/test_bookkeeper.py b/tests/test_bookkeeper.py index f590c0e87..27a339fef 100644 --- a/tests/test_bookkeeper.py +++ b/tests/test_bookkeeper.py @@ -891,7 +891,7 @@ def test_rebalance_tracking(node_factory, bitcoind): assert outbound_ev['payment_id'] == pay_hash -def test_bookkeeper_custom_notifs(node_factory): +def test_bookkeeper_custom_notifs(node_factory, chainparams): # FIXME: what happens if we send internal funds to 'external' wallet? plugin = os.path.join( os.path.dirname(__file__), "plugins", "bookkeeper_custom_coins.py" @@ -939,6 +939,50 @@ def test_bookkeeper_custom_notifs(node_factory): assert len(onchain_fees) == 2 assert onchain_fees[0]['credit_msat'] == onchain_fee_one assert onchain_fees[1]['debit_msat'] == withdraw_amt + assert events == [{'account': "nifty's secret stash", + 'blockheight': 111, + 'credit_msat': 180000000, + 'currency': chainparams['bip173_prefix'], + 'debit_msat': 0, + 'outpoint': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:0', + 'tag': 'deposit', + 'timestamp': 1679955976, + 'type': 'chain'}, + {'account': "nifty's secret stash", + 'blockheight': 111, + 'credit_msat': 0, + 'currency': chainparams['bip173_prefix'], + 'debit_msat': 180000000, + 'outpoint': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:0', + 'tag': 'withdrawal', + 'timestamp': 1679955976, + 'txid': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'type': 'chain'}, + {'account': "nifty's secret stash", + 'blockheight': 111, + 'credit_msat': 124443000, + 'currency': chainparams['bip173_prefix'], + 'debit_msat': 0, + 'outpoint': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:0', + 'tag': 'deposit', + 'timestamp': 1679955976, + 'type': 'chain'}, + {'account': "nifty's secret stash", + 'credit_msat': 55557000, + 'currency': chainparams['bip173_prefix'], + 'debit_msat': 0, + 'tag': 'onchain_fee', + 'timestamp': 1679955976, + 'txid': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'type': 'onchain_fee'}, + {'account': "nifty's secret stash", + 'credit_msat': 0, + 'currency': chainparams['bip173_prefix'], + 'debit_msat': 55555000, + 'tag': 'onchain_fee', + 'timestamp': 1679955976, + 'txid': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'type': 'onchain_fee'}] # This should not blow up incomes = l1.rpc.bkpr_listincome()['income_events']