Skip to content

Commit

Permalink
bkpr: add payment_id parameter to listaccountevents to filter events.
Browse files Browse the repository at this point in the history
When you have *lots* of events in your bkpr database looking up a
specific event via calling bkpr-listaccountevents and using jq or
grep to filter gets very slow (and wasteful of CPU and disk resources).
This commit adds the paremeter payment_id to the call to filter for a
specific payment id via a where clause in the request to the database of bkpr.

Changelog-Added: Plugins: Add payment_id parameter to bkpr-listaccountevents to filter events.
  • Loading branch information
michael1011 committed Aug 7, 2024
1 parent 87defec commit 843b72d
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 129 deletions.
7 changes: 6 additions & 1 deletion .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@
"Bkpr-ListAccountEvents.events[].type": 2
},
"Bkpr-listaccounteventsRequest": {
"Bkpr-ListAccountEvents.account": 1
"Bkpr-ListAccountEvents.account": 1,
"Bkpr-ListAccountEvents.payment_id": 2
},
"Bkpr-listaccounteventsResponse": {
"Bkpr-ListAccountEvents.events[]": 1
Expand Down Expand Up @@ -3872,6 +3873,10 @@
"added": "pre-v0.10.1",
"deprecated": null
},
"Bkpr-ListAccountEvents.payment_id": {
"added": "v24.08",
"deprecated": null
},
"Bkpr-ListBalances": {
"added": "pre-v0.10.1",
"deprecated": null
Expand Down
12 changes: 0 additions & 12 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,6 @@ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
return NULL;
}

/* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
static void reverse_bytes(u8 *arr, size_t len)
{
unsigned int i;

for (i = 0; i < len / 2; i++) {
unsigned char tmp = arr[i];
arr[i] = arr[len - 1 - i];
arr[len - 1 - i] = tmp;
}
}

bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_txid *txid)
{
Expand Down
12 changes: 12 additions & 0 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ struct bitcoin_tx *clone_bitcoin_tx(const tal_t *ctx,
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
size_t hexlen);

/* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
static inline void reverse_bytes(u8 *arr, size_t len)
{
unsigned int i;

for (i = 0; i < len / 2; i++) {
unsigned char tmp = arr[i];
arr[i] = arr[len - 1 - i];
arr[len - 1 - i] = tmp;
}
}

/* Parse hex string to get txid (reversed, a-la bitcoind). */
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_txid *txid);
Expand Down
1 change: 1 addition & 0 deletions cln-grpc/proto/node.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cln-grpc/src/convert.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cln-rpc/src/model.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions contrib/msggen/msggen/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,10 @@
"",
"If the optional parameter **account** is set, we only emit events for the specified account, if exists.",
"",
"If the optional parameter **payment_id** is set, we only emit events which have that value as payment hash or as transaction id.",
"",
"The parameters **account** and **payment_id** are mutually exclusive.",
"",
"Note that the type **onchain_fees** that are emitted are of opposite credit/debit than as they appear in **listincome**, as **listincome** shows all events from the perspective of the node, whereas **listaccountevents** just dumps the event data as we've got it. Onchain fees are updated/recorded as we get more information about input and output spends -- the total onchain fees that were recorded for a transaction for an account can be found by summing all onchain fee events and taking the difference between the **credit_msat** and **debit_msat** for these events. We do this so that successive calls to **listaccountevents** always produce the same list of events -- no previously emitted event will be subsequently updated, rather we add a new event to the list."
],
"request": {
Expand All @@ -1589,6 +1593,13 @@
"description": [
"Receive events for the specified account."
]
},
"payment_id": {
"type": "string",
"added": "v24.08",
"description": [
"Receive events for the specified payment id."
]
}
}
},
Expand Down
148 changes: 74 additions & 74 deletions contrib/pyln-grpc-proto/pyln/grpc/node_pb2.py

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions doc/schemas/lightning-bkpr-listaccountevents.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"",
"If the optional parameter **account** is set, we only emit events for the specified account, if exists.",
"",
"If the optional parameter **payment_id** is set, we only emit events which have that value as payment hash or as transaction id.",
"",
"The parameters **account** and **payment_id** are mutually exclusive.",
"",
"Note that the type **onchain_fees** that are emitted are of opposite credit/debit than as they appear in **listincome**, as **listincome** shows all events from the perspective of the node, whereas **listaccountevents** just dumps the event data as we've got it. Onchain fees are updated/recorded as we get more information about input and output spends -- the total onchain fees that were recorded for a transaction for an account can be found by summing all onchain fee events and taking the difference between the **credit_msat** and **debit_msat** for these events. We do this so that successive calls to **listaccountevents** always produce the same list of events -- no previously emitted event will be subsequently updated, rather we add a new event to the list."
],
"request": {
Expand All @@ -19,6 +23,13 @@
"description": [
"Receive events for the specified account."
]
},
"payment_id": {
"type": "string",
"added": "v24.08",
"description": [
"Receive events for the specified payment id."
]
}
}
},
Expand Down
19 changes: 19 additions & 0 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,25 @@ static struct command_result *json_list_account_events(struct command *cmd,
{
struct json_stream *res;
struct account *acct;
struct sha256 *payment_id;
struct bitcoin_txid *tx_id;
const char *acct_name;
struct channel_event **channel_events;
struct chain_event **chain_events;
struct onchain_fee **onchain_fees;

if (!param(cmd, buf, params,
p_opt("account", param_string, &acct_name),
p_opt("payment_id", param_sha256, &payment_id),
NULL))
return command_param_failed();

if (acct_name && payment_id != NULL) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Can only specify one of "
"{account} or {payment_id}");
}

if (acct_name) {
db_begin_transaction(db);
acct = find_account(cmd, db, acct_name);
Expand All @@ -386,6 +395,16 @@ static struct command_result *json_list_account_events(struct command *cmd,
channel_events = account_get_channel_events(cmd, db, acct);
chain_events = account_get_chain_events(cmd, db, acct);
onchain_fees = account_get_chain_fees(cmd, db, acct);
} else if (payment_id != NULL) {
channel_events = get_channel_events_by_id(cmd, db, payment_id);

tx_id = tal(cmd, struct bitcoin_txid);
tx_id->shad.sha = *payment_id;
/* Transaction ids are stored as big-endian in the database */
reverse_bytes(tx_id->shad.sha.u.u8, sizeof(tx_id->shad.sha.u.u8));

chain_events = find_chain_events_bytxid(cmd, db, tx_id);
onchain_fees = get_chain_fees_by_txid(cmd, db, tx_id);
} else {
channel_events = list_channel_events(cmd, db);
chain_events = list_chain_events(cmd, db);
Expand Down
Loading

0 comments on commit 843b72d

Please sign in to comment.