Skip to content

Commit 52d69df

Browse files
committed
lightningd: migrate events from bookkeeper at startup.
We take over the --bookkeeper-dir and --bookkeeper-db options, and then if we can find the bookkeeper db we extract the records to initialize our chain_moves and channel_moves tables. Of course, bookkeeper now needs to not register those options. When bookkeeper gets invoked the first time, it will reconstruct everything from listchannelmoves and listcoinmoves. It cannot preserve manually-added descriptions, so we put those in the datastore for it ready to go. Note that the order of onchain_fee changes slightly from the original. But this is fine. Signed-off-by: Rusty Russell <[email protected]>
1 parent 1958efd commit 52d69df

25 files changed

+710
-98
lines changed

common/coin_mvt.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const char *mvt_tag_str(enum mvt_tag tag)
155155
return mvt_tags[tag];
156156
}
157157

158-
static void tag_set(struct mvt_tags *tags, enum mvt_tag tag)
158+
void mvt_tag_set(struct mvt_tags *tags, enum mvt_tag tag)
159159
{
160160
u64 bitnum = mvt_tag_in_db(tag);
161161
assert(bitnum < NUM_MVT_TAGS);
@@ -394,10 +394,10 @@ struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
394394

395395
/* If we're the opener, add to the tag list */
396396
if (is_opener)
397-
tag_set(&tags, MVT_OPENER);
397+
mvt_tag_set(&tags, MVT_OPENER);
398398

399399
if (is_leased)
400-
tag_set(&tags, MVT_LEASED);
400+
mvt_tag_set(&tags, MVT_LEASED);
401401

402402
mvt = new_chain_coin_mvt(ctx, channel, NULL, time_now().ts.tv_sec,
403403
NULL, out, NULL, 0,
@@ -423,10 +423,10 @@ struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
423423

424424
/* If we're the opener, add to the tag list */
425425
if (is_opener)
426-
tag_set(&tags, MVT_OPENER);
426+
mvt_tag_set(&tags, MVT_OPENER);
427427

428428
if (is_leased)
429-
tag_set(&tags, MVT_LEASED);
429+
mvt_tag_set(&tags, MVT_LEASED);
430430

431431
mvt = new_chain_coin_mvt(ctx, channel, NULL, time_now().ts.tv_sec,
432432
NULL, out, NULL, blockheight,
@@ -684,10 +684,10 @@ struct mvt_tags mk_mvt_tags_(enum mvt_tag tag, ...)
684684
va_list ap;
685685
struct mvt_tags ret = { 0 };
686686

687-
tag_set(&ret, tag);
687+
mvt_tag_set(&ret, tag);
688688
va_start(ap, tag);
689689
while ((tag = va_arg(ap, enum mvt_tag)) != 999)
690-
tag_set(&ret, mvt_tag_in_db(tag));
690+
mvt_tag_set(&ret, mvt_tag_in_db(tag));
691691
va_end(ap);
692692
return ret;
693693
}

common/coin_mvt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ static inline struct mvt_tags tag_to_mvt_tags(enum mvt_tag tag)
130130
return tags;
131131
}
132132

133+
/* Add a tag */
134+
void mvt_tag_set(struct mvt_tags *tags, enum mvt_tag tag);
135+
133136
/* Extract the primary tag */
134137
enum mvt_tag primary_mvt_tag(struct mvt_tags tags);
135138

doc/lightningd-config.5.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,6 @@ authenticate with username `user` and password `pass`, and then use the
293293
database `db_name`. The database must exist, but the schema will be managed
294294
automatically by `lightningd`.
295295

296-
* **bookkeeper-dir**=*DIR* [plugin `bookkeeper`]
297-
298-
Directory to keep the accounts.sqlite3 database file in.
299-
Defaults to lightning-dir.
300-
301-
* **bookkeeper-db**=*DSN* [plugin `bookkeeper`]
302-
303-
Identify the location of the bookkeeper data. This is a fully qualified data source
304-
name, including a scheme such as `sqlite3` or `postgres` followed by the
305-
connection parameters.
306-
Defaults to `sqlite3://accounts.sqlite3` in the `bookkeeper-dir`.
307-
308296
* **encrypted-hsm**
309297

310298
If set, you will be prompted to enter a password used to encrypt the `hsm_secret`.

lightningd/coin_mvts.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,35 +258,38 @@ void json_add_channel_mvt_fields(struct json_stream *stream,
258258
}
259259

260260
static u64 coinmvt_index_inc(struct lightningd *ld,
261+
struct db *db,
261262
enum wait_subsystem subsys,
262263
const struct mvt_account_id *account,
263264
struct amount_msat credit,
264265
struct amount_msat debit,
265266
enum wait_index idx)
266267
{
267-
return wait_index_increment(ld, subsys, idx,
268+
return wait_index_increment(ld, db, subsys, idx,
268269
"account", account->channel ? fmt_channel_id(tmpctx, &account->channel->cid) : account->alt_account,
269270
"=credit_msat", tal_fmt(tmpctx, "%"PRIu64, credit.millisatoshis), /* Raw: JSON output */
270271
"=debit_msat", tal_fmt(tmpctx, "%"PRIu64, debit.millisatoshis), /* Raw: JSON output */
271272
NULL);
272273
}
273274

274275
u64 chain_mvt_index_created(struct lightningd *ld,
276+
struct db *db,
275277
const struct mvt_account_id *account,
276278
struct amount_msat credit,
277279
struct amount_msat debit)
278280
{
279-
return coinmvt_index_inc(ld, WAIT_SUBSYSTEM_CHAINMOVES,
281+
return coinmvt_index_inc(ld, db, WAIT_SUBSYSTEM_CHAINMOVES,
280282
account, credit, debit,
281283
WAIT_INDEX_CREATED);
282284
}
283285

284286
u64 channel_mvt_index_created(struct lightningd *ld,
287+
struct db *db,
285288
const struct mvt_account_id *account,
286289
struct amount_msat credit,
287290
struct amount_msat debit)
288291
{
289-
return coinmvt_index_inc(ld, WAIT_SUBSYSTEM_CHANNELMOVES,
292+
return coinmvt_index_inc(ld, db, WAIT_SUBSYSTEM_CHANNELMOVES,
290293
account, credit, debit,
291294
WAIT_INDEX_CREATED);
292295
}

lightningd/coin_mvts.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include <common/coin_mvt.h>
66

7+
struct htlc_in;
8+
struct htlc_out;
9+
struct json_stream;
710
struct lightningd;
811

912
struct account_balance {
@@ -54,11 +57,13 @@ void json_add_channel_mvt_fields(struct json_stream *stream,
5457

5558
/* For db code to get incremental ids */
5659
u64 chain_mvt_index_created(struct lightningd *ld,
60+
struct db *db,
5761
const struct mvt_account_id *account,
5862
struct amount_msat credit,
5963
struct amount_msat debit);
6064

6165
u64 channel_mvt_index_created(struct lightningd *ld,
66+
struct db *db,
6267
const struct mvt_account_id *account,
6368
struct amount_msat credit,
6469
struct amount_msat debit);

lightningd/forwards.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ static u64 forward_index_inc(struct lightningd *ld,
1919
const struct short_channel_id *out_channel,
2020
enum wait_index idx)
2121
{
22-
return wait_index_increment(ld, WAIT_SUBSYSTEM_FORWARD, idx,
22+
return wait_index_increment(ld, ld->wallet->db,
23+
WAIT_SUBSYSTEM_FORWARD, idx,
2324
"status", forward_status_name(status),
2425
"in_channel", fmt_short_channel_id(tmpctx, in_channel),
2526
"=in_htlc_id", tal_fmt(tmpctx, "%"PRIu64, in_htlc_id),

lightningd/lightningd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
271271
ld->try_reexec = false;
272272
ld->recover_secret = NULL;
273273
ld->db_upgrade_ok = NULL;
274+
ld->old_bookkeeper_dir = NULL;
275+
ld->old_bookkeeper_db = NULL;
274276

275277
/* --invoices-onchain-fallback */
276278
ld->unified_invoices = false;

lightningd/lightningd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ struct lightningd {
383383

384384
char *wallet_dsn;
385385

386+
/* For migration from old accounts.db */
387+
char *old_bookkeeper_dir;
388+
char *old_bookkeeper_db;
389+
386390
bool encrypted_hsm;
387391
/* What (additional) messages the HSM accepts */
388392
u32 *hsm_capabilities;

lightningd/options.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,16 @@ static void register_opts(struct lightningd *ld)
16031603
"Re-enable a long-deprecated API (which will be removed entirely next version!)");
16041604
opt_register_logging(ld);
16051605

1606+
/* Old bookkeeper migration flags. */
1607+
opt_register_early_arg("--bookkeeper-dir",
1608+
opt_set_talstr, NULL,
1609+
&ld->old_bookkeeper_dir,
1610+
opt_hidden);
1611+
opt_register_early_arg("--bookkeeper-db",
1612+
opt_set_talstr, NULL,
1613+
&ld->old_bookkeeper_db,
1614+
opt_hidden);
1615+
16061616
dev_register_opts(ld);
16071617
}
16081618

lightningd/pay.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,8 @@ static u64 sendpay_index_inc(struct lightningd *ld,
21182118
enum payment_status status,
21192119
enum wait_index idx)
21202120
{
2121-
return wait_index_increment(ld, WAIT_SUBSYSTEM_SENDPAY, idx,
2121+
return wait_index_increment(ld, ld->wallet->db,
2122+
WAIT_SUBSYSTEM_SENDPAY, idx,
21222123
"status", payment_status_to_string(status),
21232124
"=partid", tal_fmt(tmpctx, "%"PRIu64, partid),
21242125
"=groupid", tal_fmt(tmpctx, "%"PRIu64, groupid),

0 commit comments

Comments
 (0)