bkpr: add new json RPC "bkpr-editdescriptionbypaymentid"

This takes an {payment_id} and {description}.
It looks for all chain + channel events that match
that {payment_id} and updates the description for those events.

We return all the updated events. If no events are updated, an empty
list is returned.

Changelog-Added: PLUGINS: bookkeeper has a new RPC `bkpr-editdescriptionbypaymentid` which will update the description for any event with matching payment_id
This commit is contained in:
niftynei 2024-08-22 17:16:48 -05:00 committed by Rusty Russell
parent 4a6e014213
commit a61b7ef347
7 changed files with 614 additions and 19 deletions

View file

@ -2605,6 +2605,268 @@
}
]
},
"lightning-bkpr-editdescriptionbypaymentid.json": {
"$schema": "../rpc-schema-draft.json",
"type": "object",
"additionalProperties": false,
"rpc": "bkpr-editdescriptionbypaymentid",
"title": "Command to change the description for events with {payment_id} after they're made",
"description": [
"The **bkpr-editdescriptionbypaymentid** RPC command updates all chain and channel events that match the {payment_id} with the provided {description}"
],
"request": {
"required": [
"payment_id",
"description"
],
"properties": {
"payment_id": {
"type": "string",
"description": [
"The payment hash (payment_id) to update the description for."
]
},
"description": {
"type": "string",
"description": [
"The description to update to"
]
}
}
},
"response": {
"required": [
"updated"
],
"properties": {
"updated": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"required": [
"account",
"type",
"tag",
"credit_msat",
"debit_msat",
"currency",
"timestamp",
"description"
],
"properties": {
"account": {
"type": "string",
"description": [
"The account name. If the account is a channel, the channel_id."
]
},
"type": {
"type": "string",
"enum": [
"chain",
"channel"
],
"description": [
"Coin movement type."
]
},
"tag": {
"type": "string",
"description": [
"Description of movement."
]
},
"credit_msat": {
"type": "msat",
"description": [
"Amount credited."
]
},
"debit_msat": {
"type": "msat",
"description": [
"Amount debited."
]
},
"currency": {
"type": "string",
"description": [
"Human-readable bech32 part for this coin type."
]
},
"timestamp": {
"type": "u32",
"description": [
"Timestamp this event was recorded by the node. For consolidated events such as onchain_fees, the most recent timestamp."
]
},
"description": {
"type": "string",
"description": [
"The description of this event"
]
}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"chain"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"outpoint": {
"type": "string",
"description": [
"The txid:outnum for this event."
]
},
"blockheight": {
"type": "u32",
"description": [
"For chain events, blockheight this occured at."
]
},
"origin": {
"type": "string",
"description": [
"The account this movement originated from."
]
},
"payment_id": {
"type": "hex",
"description": [
"Lightning payment identifier. For an htlc, this will be the preimage."
]
},
"txid": {
"type": "txid",
"description": [
"The txid of the transaction that created this event."
]
}
},
"required": [
"outpoint",
"blockheight"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"onchain_fee"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {},
"txid": {
"type": "txid",
"description": [
"The txid of the transaction that created this event."
]
}
},
"required": [
"txid"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"channel"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {},
"fees_msat": {
"type": "msat",
"description": [
"Amount paid in fees."
]
},
"is_rebalance": {
"type": "boolean",
"description": [
"Is this payment part of a rebalance."
]
},
"payment_id": {
"type": "hex",
"description": [
"Lightning payment identifier. For an htlc, this will be the preimage."
]
},
"part_id": {
"type": "u32",
"description": [
"Counter for multi-part payments."
]
}
},
"additionalProperties": false
}
}
]
}
}
}
},
"author": [
"Lisa Neigut <<niftynei@gmail.com>> is mainly responsible."
],
"see_also": [
"lightning-bkpr-listaccountevents(7)",
"lightning-bkpr-listincome(7)"
],
"resources": [
"Main web site: <https://github.com/ElementsProject/lightning>"
],
"examples": []
},
"lightning-bkpr-inspect.json": {
"$schema": "../rpc-schema-draft.json",
"type": "object",
@ -2999,6 +3261,12 @@
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {
"type": "string",
"description": [
"The description of this event."
]
},
"outpoint": {
"type": "string",
"description": [
@ -3028,12 +3296,6 @@
"description": [
"The txid of the transaction that created this event."
]
},
"description": {
"type": "string",
"description": [
"The description of this event."
]
}
},
"required": [

View file

@ -0,0 +1,263 @@
{
"$schema": "../rpc-schema-draft.json",
"type": "object",
"additionalProperties": false,
"rpc": "bkpr-editdescriptionbypaymentid",
"title": "Command to change the description for events with {payment_id} after they're made",
"description": [
"The **bkpr-editdescriptionbypaymentid** RPC command updates all chain and channel events that match the {payment_id} with the provided {description}"
],
"request": {
"required": [
"payment_id",
"description"
],
"properties": {
"payment_id": {
"type": "string",
"description": [
"The payment hash (payment_id) to update the description for."
]
},
"description": {
"type": "string",
"description": [
"The description to update to"
]
}
}
},
"response": {
"required": [
"updated"
],
"properties": {
"updated": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"required": [
"account",
"type",
"tag",
"credit_msat",
"debit_msat",
"currency",
"timestamp",
"description"
],
"properties": {
"account": {
"type": "string",
"description": [
"The account name. If the account is a channel, the channel_id."
]
},
"type": {
"type": "string",
"enum": [
"chain",
"channel"
],
"description": [
"Coin movement type."
]
},
"tag": {
"type": "string",
"description": [
"Description of movement."
]
},
"credit_msat": {
"type": "msat",
"description": [
"Amount credited."
]
},
"debit_msat": {
"type": "msat",
"description": [
"Amount debited."
]
},
"currency": {
"type": "string",
"description": [
"Human-readable bech32 part for this coin type."
]
},
"timestamp": {
"type": "u32",
"description": [
"Timestamp this event was recorded by the node. For consolidated events such as onchain_fees, the most recent timestamp."
]
},
"description": {
"type": "string",
"description": [
"The description of this event"
]
}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"chain"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"outpoint": {
"type": "string",
"description": [
"The txid:outnum for this event."
]
},
"blockheight": {
"type": "u32",
"description": [
"For chain events, blockheight this occured at."
]
},
"origin": {
"type": "string",
"description": [
"The account this movement originated from."
]
},
"payment_id": {
"type": "hex",
"description": [
"Lightning payment identifier. For an htlc, this will be the preimage."
]
},
"txid": {
"type": "txid",
"description": [
"The txid of the transaction that created this event."
]
}
},
"required": [
"outpoint",
"blockheight"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"onchain_fee"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {},
"txid": {
"type": "txid",
"description": [
"The txid of the transaction that created this event."
]
}
},
"required": [
"txid"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"type": {
"type": "string",
"enum": [
"channel"
]
}
}
},
"then": {
"properties": {
"account": {},
"type": {},
"tag": {},
"credit_msat": {},
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {},
"fees_msat": {
"type": "msat",
"description": [
"Amount paid in fees."
]
},
"is_rebalance": {
"type": "boolean",
"description": [
"Is this payment part of a rebalance."
]
},
"payment_id": {
"type": "hex",
"description": [
"Lightning payment identifier. For an htlc, this will be the preimage."
]
},
"part_id": {
"type": "u32",
"description": [
"Counter for multi-part payments."
]
}
},
"additionalProperties": false
}
}
]
}
}
}
},
"author": [
"Lisa Neigut <<niftynei@gmail.com>> is mainly responsible."
],
"see_also": [
"lightning-bkpr-listaccountevents(7)",
"lightning-bkpr-listincome(7)"
],
"resources": [
"Main web site: <https://github.com/ElementsProject/lightning>"
],
"examples": [
]
}

View file

@ -125,6 +125,12 @@
"debit_msat": {},
"currency": {},
"timestamp": {},
"description": {
"type": "string",
"description": [
"The description of this event."
]
},
"outpoint": {
"type": "string",
"description": [
@ -154,12 +160,6 @@
"description": [
"The txid of the transaction that created this event."
]
},
"description": {
"type": "string",
"description": [
"The description of this event."
]
}
},
"required": [

View file

@ -475,15 +475,41 @@ static struct command_result *json_list_account_events(struct command *cmd,
res = jsonrpc_stream_success(cmd);
json_array_start(res, "events");
json_add_events(res, channel_events, chain_events, onchain_fees);
json_array_end(res);
return command_finished(cmd, res);
}
static struct command_result *json_edit_desc_payment_id(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
struct json_stream *res;
struct sha256 *identifier;
const char *new_desc;
struct channel_event **channel_events;
struct chain_event **chain_events;
if (!param(cmd, buf, params,
p_req("payment_id", param_sha256, &identifier),
p_req("description", param_string, &new_desc),
NULL))
return command_param_failed();
db_begin_transaction(db);
add_payment_hash_desc(db, identifier, new_desc);
chain_events = get_chain_events_by_id(cmd, db, identifier);
channel_events = get_channel_events_by_id(cmd, db, identifier);
db_commit_transaction(db);
res = jsonrpc_stream_success(cmd);
json_array_start(res, "updated");
json_add_events(res, channel_events, chain_events, NULL);
json_array_end(res);
return command_finished(cmd, res);
}
static struct command_result *json_list_balances(struct command *cmd,
const char *buf,
const jsmntok_t *params)
@ -2001,6 +2027,10 @@ static const struct plugin_command commands[] = {
"bkpr-channelsapy",
json_channel_apy
},
{
"bkpr-editdescriptionbypaymentid",
json_edit_desc_payment_id
},
};
static const char *init(struct command *init_cmd, const char *b, const jsmntok_t *t)

View file

@ -723,6 +723,41 @@ struct chain_event *find_chain_event_by_id(const tal_t *ctx,
return e;
}
struct chain_event **get_chain_events_by_id(const tal_t *ctx,
struct db *db,
const struct sha256 *id)
{
struct db_stmt *stmt;
stmt = db_prepare_v2(db, SQL("SELECT"
" e.id"
", e.account_id"
", a.name"
", e.origin"
", e.tag"
", e.credit"
", e.debit"
", e.output_value"
", e.currency"
", e.timestamp"
", e.blockheight"
", e.utxo_txid"
", e.outnum"
", e.spending_txid"
", e.payment_id"
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
" WHERE "
" e.payment_id = ?"));
db_bind_sha256(stmt, id);
return find_chain_events(ctx, take(stmt));
}
static struct chain_event *find_chain_event(const tal_t *ctx,
struct db *db,
const struct account *acct,

View file

@ -107,6 +107,11 @@ struct chain_event **list_chain_events_timebox(const tal_t *ctx,
u64 start_time,
u64 end_time);
/* Get all chain events for a payment hash */
struct chain_event **get_chain_events_by_id(const tal_t *ctx,
struct db *db,
const struct sha256 *id);
/* Calculate the balances for an account
*
* @calc_sum - compute the total balance. error if negative

View file

@ -4054,6 +4054,8 @@ def test_sql(node_factory, bitcoind):
'type': 'string'},
{'name': 'timestamp',
'type': 'u32'},
{'name': 'description',
'type': 'string'},
{'name': 'outpoint',
'type': 'string'},
{'name': 'blockheight',
@ -4064,8 +4066,6 @@ def test_sql(node_factory, bitcoind):
'type': 'hex'},
{'name': 'txid',
'type': 'txid'},
{'name': 'description',
'type': 'string'},
{'name': 'fees_msat',
'type': 'msat'},
{'name': 'is_rebalance',