Skip to content

Commit f640b59

Browse files
committed
plugins/sql: add bkpr-listaccountevents and bkpr-listincome support.
This *would* be a 1-line change (add it to Makefile) except that we previously assumed a "list" prefix on commands. These use the default refreshing, but they could be done better using the time-range parameters. Suggested-by: @niftynei Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent e07f461 commit f640b59

File tree

5 files changed

+121
-13
lines changed

5 files changed

+121
-13
lines changed

doc/lightning-sql.7.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ Note that most column names reflect the JSON field names, with the exception of
6161
Sub-arrays are represented in their own tables, and sub-objects are flattened with `_`.
6262

6363
The following tables are currently supported:
64+
- bkpr_accountevents (see lightning-bkpr-listaccountevents(7))
65+
- `account` (type `string`, sqltype `TEXT`)
66+
- `type` (type `string`, sqltype `TEXT`)
67+
- `tag` (type `string`, sqltype `TEXT`)
68+
- `credit_msat` (type `msat`, sqltype `INTEGER`)
69+
- `debit_msat` (type `msat`, sqltype `INTEGER`)
70+
- `currency` (type `string`, sqltype `TEXT`)
71+
- `timestamp` (type `u32`, sqltype `INTEGER`)
72+
- `outpoint` (type `string`, sqltype `TEXT`)
73+
- `blockheight` (type `u32`, sqltype `INTEGER`)
74+
- `origin` (type `string`, sqltype `TEXT`)
75+
- `payment_id` (type `hex`, sqltype `BLOB`)
76+
- `txid` (type `txid`, sqltype `BLOB`)
77+
- `description` (type `string`, sqltype `TEXT`)
78+
- `fees_msat` (type `msat`, sqltype `INTEGER`)
79+
- `is_rebalance` (type `boolean`, sqltype `INTEGER`)
80+
- `part_id` (type `u32`, sqltype `INTEGER`)
81+
82+
- bkpr_income (see lightning-bkpr-listincome(7))
83+
- `account` (type `string`, sqltype `TEXT`)
84+
- `tag` (type `string`, sqltype `TEXT`)
85+
- `credit_msat` (type `msat`, sqltype `INTEGER`)
86+
- `debit_msat` (type `msat`, sqltype `INTEGER`)
87+
- `currency` (type `string`, sqltype `TEXT`)
88+
- `timestamp` (type `u32`, sqltype `INTEGER`)
89+
- `description` (type `string`, sqltype `TEXT`)
90+
- `outpoint` (type `string`, sqltype `TEXT`)
91+
- `txid` (type `txid`, sqltype `BLOB`)
92+
- `payment_id` (type `hex`, sqltype `BLOB`)
93+
6494
- channels indexed by `short_channel_id` (see lightning-listchannels(7))
6595
- `source` (type `pubkey`, sqltype `BLOB`)
6696
- `destination` (type `pubkey`, sqltype `BLOB`)
@@ -270,4 +300,4 @@ RESOURCES
270300
---------
271301

272302
Main web site: <https://github.com/ElementsProject/lightning>
273-
[comment]: # ( SHA256STAMP:03aee4b1270fce52618a6261f79308927e18df1f490df0da3972840d7f9df1cf)
303+
[comment]: # ( SHA256STAMP:86fec37b6a343e04c2d8f482692aab62e2ba0f3d1196ddd052e06f13952fa6d2)

plugins/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ plugins/fetchinvoice: $(PLUGIN_FETCHINVOICE_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_CO
204204
plugins/funder: bitcoin/psbt.o common/psbt_open.o $(PLUGIN_FUNDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)
205205

206206
# This covers all the low-level list RPCs which return simple arrays of matching name (FIXME: NOT listsendpays!)
207-
SQL_LISTRPCS := listchannels listforwards listhtlcs listinvoices listnodes listoffers listpeers listtransactions listsendpays
207+
SQL_LISTRPCS := listchannels listforwards listhtlcs listinvoices listnodes listoffers listpeers listtransactions listsendpays bkpr-listaccountevents bkpr-listincome
208208
SQL_LISTRPCS_SCHEMAS := $(foreach l,$(SQL_LISTRPCS),doc/schemas/$l.schema.json)
209209
plugins/sql-flatschema_gen.h: plugins/Makefile tools/flattenschema.py $(SQL_LISTRPCS_SCHEMAS)
210210
@$(call VERBOSE,GEN $@, tools/flattenschema.py $(SQL_LISTRPCS_SCHEMAS) | sed 's/\(.*\)/"\1\\n"/' > $@)

plugins/sql.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ struct column {
149149
};
150150

151151
struct table_desc {
152+
/* e.g. listpeers */
153+
const char *cmdname;
152154
/* e.g. peers for listpeers */
153155
const char *name;
154156
/* e.g. "payments" for listsendpays */
@@ -618,8 +620,7 @@ static struct command_result *default_refresh(struct command *cmd,
618620
struct db_query *dbq)
619621
{
620622
struct out_req *req;
621-
req = jsonrpc_request_start(cmd->plugin, cmd,
622-
tal_fmt(tmpctx, "list%s", td->name),
623+
req = jsonrpc_request_start(cmd->plugin, cmd, td->cmdname,
623624
default_list_done, forward_error,
624625
dbq);
625626
return send_outreq(cmd->plugin, req);
@@ -1096,11 +1097,32 @@ static const char *db_column_name(const tal_t *ctx, const char *name)
10961097
return tal_strdup(ctx, name);
10971098
}
10981099

1100+
/* Remove 'list', turn - into _ in name */
1101+
static const char *db_table_name(const tal_t *ctx, const char *cmdname)
1102+
{
1103+
const char *list = strstr(cmdname, "list");
1104+
char *ret = tal_arr(ctx, char, strlen(cmdname) + 1), *dst = ret;
1105+
const char *src = cmdname;
1106+
1107+
while (*src) {
1108+
if (src == list)
1109+
src += strlen("list");
1110+
else if (cisalnum(*src))
1111+
*(dst++) = *(src++);
1112+
else {
1113+
(*dst++) = '_';
1114+
src++;
1115+
}
1116+
}
1117+
*dst = '\0';
1118+
return ret;
1119+
}
1120+
10991121
static const char *start_new_table_desc(const tal_t *ctx,
11001122
struct table_desc **tdp,
11011123
struct table_desc *parent,
11021124
bool is_subobject,
1103-
const char *name TAKES,
1125+
const char *cmdname TAKES,
11041126
const char *arrname TAKES,
11051127
struct command_result *
11061128
(*refresh)(struct command *,
@@ -1114,7 +1136,8 @@ static const char *start_new_table_desc(const tal_t *ctx,
11141136
return err;
11151137

11161138
*tdp = tal(NULL, struct table_desc);
1117-
(*tdp)->name = tal_strdup(*tdp, name);
1139+
(*tdp)->cmdname = tal_strdup(*tdp, cmdname);
1140+
(*tdp)->name = db_table_name(*tdp, cmdname);
11181141
(*tdp)->parent = parent;
11191142
(*tdp)->is_subobject = is_subobject;
11201143
(*tdp)->arrname = tal_strdup(*tdp, arrname);
@@ -1147,9 +1170,10 @@ static const char *init_tablemap(const tal_t *ctx)
11471170
if (!streq(sub[2], "array"))
11481171
continue;
11491172

1173+
/* FIXME: add specialized refreshers for bkpraccountevents and bkprincome */
11501174
err = start_new_table_desc(ctx, &td, NULL, false, sub[0], sub[1],
1151-
streq(sub[0], "channels") ? channels_refresh
1152-
: streq(sub[0], "nodes") ? nodes_refresh
1175+
streq(sub[0], "listchannels") ? channels_refresh
1176+
: streq(sub[0], "listnodes") ? nodes_refresh
11531177
: default_refresh);
11541178
if (err)
11551179
return err;
@@ -1317,8 +1341,8 @@ static bool print_one_table(const char *member,
13171341
if (td->parent)
13181342
return true;
13191343

1320-
printf("- %s%s (see lightning-list%s(7))\n",
1321-
member, fmt_indexes(tmpctx, member), member);
1344+
printf("- %s%s (see lightning-%s(7))\n",
1345+
member, fmt_indexes(tmpctx, member), td->cmdname);
13221346

13231347
for (size_t i = 0; i < tal_count(td->columns); i++) {
13241348
const char *origin;

tests/test_plugin.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,61 @@ def test_sql(node_factory, bitcoind):
35063506
{'name': 'type',
35073507
'type': 'string'},
35083508
{'name': 'channel',
3509-
'type': 'short_channel_id'}]}}
3509+
'type': 'short_channel_id'}]},
3510+
'bkpr_accountevents': {
3511+
'columns': [{'name': 'account',
3512+
'type': 'string'},
3513+
{'name': 'type',
3514+
'type': 'string'},
3515+
{'name': 'tag',
3516+
'type': 'string'},
3517+
{'name': 'credit_msat',
3518+
'type': 'msat'},
3519+
{'name': 'debit_msat',
3520+
'type': 'msat'},
3521+
{'name': 'currency',
3522+
'type': 'string'},
3523+
{'name': 'timestamp',
3524+
'type': 'u32'},
3525+
{'name': 'outpoint',
3526+
'type': 'string'},
3527+
{'name': 'blockheight',
3528+
'type': 'u32'},
3529+
{'name': 'origin',
3530+
'type': 'string'},
3531+
{'name': 'payment_id',
3532+
'type': 'hex'},
3533+
{'name': 'txid',
3534+
'type': 'txid'},
3535+
{'name': 'description',
3536+
'type': 'string'},
3537+
{'name': 'fees_msat',
3538+
'type': 'msat'},
3539+
{'name': 'is_rebalance',
3540+
'type': 'boolean'},
3541+
{'name': 'part_id',
3542+
'type': 'u32'}]},
3543+
'bkpr_income': {
3544+
'columns': [{'name': 'account',
3545+
'type': 'string'},
3546+
{'name': 'tag',
3547+
'type': 'string'},
3548+
{'name': 'credit_msat',
3549+
'type': 'msat'},
3550+
{'name': 'debit_msat',
3551+
'type': 'msat'},
3552+
{'name': 'currency',
3553+
'type': 'string'},
3554+
{'name': 'timestamp',
3555+
'type': 'u32'},
3556+
{'name': 'description',
3557+
'type': 'string'},
3558+
{'name': 'outpoint',
3559+
'type': 'string'},
3560+
{'name': 'txid',
3561+
'type': 'txid'},
3562+
{'name': 'payment_id',
3563+
'type': 'hex'}]}}
35103564

35113565
# Very rough checks of other list commands (make sure l2 has one of each)
35123566
l2.rpc.offer(1, 'desc')

tools/flattenschema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def flatten_schema(schema, prefixes):
3434
parsed_args = parser.parse_args()
3535

3636
for schemafile in parsed_args.schemafile:
37-
# Get basename of file, e.g. listfoo -> foo.
38-
prefix = os.path.basename(schemafile).split('.')[0][4:]
37+
# Get basename of file, e.g. listfoo
38+
prefix = os.path.basename(schemafile).split('.')[0]
3939
with open(schemafile, "r") as f:
4040
schema = json.load(f)
4141
# Python 3.6+ dicts maintain order, so use it to de-dup.

0 commit comments

Comments
 (0)