From 71c03bc082d82c96ea81071ed741222756b5fdba Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 19 Jul 2022 17:04:40 +0930 Subject: [PATCH] bkpr: Add an option to set the database to something else (postgres) `lightningd` has an option --wallet that lets you supply a database dsn string to connect to a sqlite3/postgres database that's hosted/stored elsewhere. This adds the `--bookkeeper-db` option which does the same, except for the bookkeeping data for a node! Note that the default is to go in the `lightning-dir` in a database called `accounts.sqlite3` --- doc/lightning-listconfigs.7 | 4 +++- doc/lightning-listconfigs.7.md | 3 ++- doc/lightningd-config.5.md | 7 +++++++ doc/schemas/listconfigs.schema.json | 4 ++++ plugins/bkpr/bookkeeper.c | 16 +++++++++++++++- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/doc/lightning-listconfigs.7 b/doc/lightning-listconfigs.7 index 7b0af2ed4..068fb615e 100644 --- a/doc/lightning-listconfigs.7 +++ b/doc/lightning-listconfigs.7 @@ -81,6 +81,8 @@ On success, an object is returned, containing: .IP \[bu] \fBbookkeeper-dir\fR (string, optional): \fBbookkeeper-dir\fR field from config or cmdline, or default .IP \[bu] +\fBbookkeeper-db\fR (string, optional): \fBbookkeeper-db\fR field from config or cmdline, or default +.IP \[bu] \fBalways-use-proxy\fR (boolean, optional): \fBalways-use-proxy\fR field from config or cmdline, or default .IP \[bu] \fBdaemon\fR (boolean, optional): \fBdaemon\fR field from config or cmdline, or default @@ -286,4 +288,4 @@ Vincenzo Palazzo \fI wrote the initial versi Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -\" SHA256STAMP:b1148d7469f7bac293482be6f501031e4cac02f9bc113cf372a31747eb7a6055 +\" SHA256STAMP:acf490bbc9c764f67bf72de7383ea05422ce892dbf70de5ba8918fad15900bd8 diff --git a/doc/lightning-listconfigs.7.md b/doc/lightning-listconfigs.7.md index 2405cf602..a5cdf6173 100644 --- a/doc/lightning-listconfigs.7.md +++ b/doc/lightning-listconfigs.7.md @@ -51,6 +51,7 @@ On success, an object is returned, containing: - **disable-plugin** (array of strings, optional): - `disable-plugin` field from config or cmdline - **bookkeeper-dir** (string, optional): `bookkeeper-dir` field from config or cmdline, or default +- **bookkeeper-db** (string, optional): `bookkeeper-db` field from config or cmdline, or default - **always-use-proxy** (boolean, optional): `always-use-proxy` field from config or cmdline, or default - **daemon** (boolean, optional): `daemon` field from config or cmdline, or default - **wallet** (string, optional): `wallet` field from config or cmdline, or default @@ -213,4 +214,4 @@ RESOURCES --------- Main web site: -[comment]: # ( SHA256STAMP:4a4cacd309d9593b45cb27563c25d9d3df7df8aef2b281145abeee27eae57fa9) +[comment]: # ( SHA256STAMP:999502771ada48f32011ea4df2443a2a3385d27377d8e55ec82cf283f9acd0a6) diff --git a/doc/lightningd-config.5.md b/doc/lightningd-config.5.md index eb8bae77a..115a2d095 100644 --- a/doc/lightningd-config.5.md +++ b/doc/lightningd-config.5.md @@ -221,6 +221,13 @@ automatically by `lightningd`. Directory to keep the accounts.sqlite3 database file in. Defaults to lightning-dir. + **bookkeeper-db**=*DSN* +Identify the location of the bookkeeper data. This is a fully qualified data source +name, including a scheme such as `sqlite3` or `postgres` followed by the +connection parameters. +Defaults to `sqlite3://accounts.sqlite3` in the `bookkeeper-dir`. + + **encrypted-hsm** If set, you will be prompted to enter a password used to encrypt the `hsm_secret`. Note that once you encrypt the `hsm_secret` this option will be mandatory for diff --git a/doc/schemas/listconfigs.schema.json b/doc/schemas/listconfigs.schema.json index 46feb13ca..56a1cd7b1 100644 --- a/doc/schemas/listconfigs.schema.json +++ b/doc/schemas/listconfigs.schema.json @@ -97,6 +97,10 @@ "type": "string", "description": "`bookkeeper-dir` field from config or cmdline, or default" }, + "bookkeeper-db": { + "type": "string", + "description": "`bookkeeper-db` field from config or cmdline, or default" + }, "always-use-proxy": { "type": "boolean", "description": "`always-use-proxy` field from config or cmdline, or default" diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index 95a5fe8da..ef7f25c12 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -34,7 +34,7 @@ /* The database that we store all the accounting data in */ static struct db *db ; -static char *db_dsn = "sqlite3://accounts.sqlite3"; +static char *db_dsn; static char *datadir; static struct fee_sum *find_sum_for_txid(struct fee_sum **sums, @@ -1780,7 +1780,14 @@ static const char *init(struct plugin *p, const char *b, const jsmntok_t *t) "Unable to switch to 'bookkeeper-dir'=%s", datadir); } + + /* No user suppled db_dsn, set one up here */ + if (!db_dsn) + db_dsn = tal_fmt(NULL, "sqlite3://accounts.sqlite3"); + + plugin_log(p, LOG_DBG, "Setting up database at %s", db_dsn); db = notleak(db_setup(p, p, db_dsn)); + db_dsn = tal_free(db_dsn); return NULL; } @@ -1791,6 +1798,8 @@ int main(int argc, char *argv[]) /* No datadir is default */ datadir = NULL; + db_dsn = NULL; + plugin_main(argv, init, PLUGIN_STATIC, true, NULL, commands, ARRAY_SIZE(commands), notifs, ARRAY_SIZE(notifs), @@ -1800,6 +1809,11 @@ int main(int argc, char *argv[]) "string", "Location for bookkeeper records.", charp_option, &datadir), + plugin_option("bookkeeper-db", + "string", + "Location of the bookkeeper database", + charp_option, &db_dsn), NULL); + return 0; }