From 2971b2af79d7ab5a3e67cfe27ac735cf7e9612ae Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 1 Aug 2022 09:55:05 +0930 Subject: [PATCH] bkpr: insert obscure 60s pop references. The initial snapshots on an already-running lightningd are expected to be unbalanced, but this shouldn't cause users to long for the green, green grass of home. This controls the Art of Noise. Signed-off-by: Rusty Russell --- db/utils.c | 2 +- db/utils.h | 2 +- plugins/bkpr/bookkeeper.c | 8 ++++++-- plugins/bkpr/db.c | 13 ++++++++----- plugins/bkpr/db.h | 3 ++- plugins/bkpr/test/run-bkpr_db.c | 14 +++++++++++--- plugins/bkpr/test/run-recorder.c | 21 ++++++++++++++------- 7 files changed, 43 insertions(+), 20 deletions(-) diff --git a/db/utils.c b/db/utils.c index 11d2654f6..96f442fbb 100644 --- a/db/utils.c +++ b/db/utils.c @@ -290,7 +290,7 @@ void db_prepare_for_changes(struct db *db) db->changes = tal_arr(db, const char *, 0); } -struct db *db_open(const tal_t *ctx, char *filename) +struct db *db_open(const tal_t *ctx, const char *filename) { struct db *db; diff --git a/db/utils.h b/db/utils.h index 2b5a21dd7..308a302f9 100644 --- a/db/utils.h +++ b/db/utils.h @@ -80,7 +80,7 @@ struct db_stmt *db_prepare_v2_(const char *location, struct db *db, /** * db_open - Open or create a database */ -struct db *db_open(const tal_t *ctx, char *filename); +struct db *db_open(const tal_t *ctx, const char *filename); /** * Report a statement that changes the wallet diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c index 7af8916a2..84fade036 100644 --- a/plugins/bkpr/bookkeeper.c +++ b/plugins/bkpr/bookkeeper.c @@ -36,6 +36,7 @@ static struct db *db ; static char *db_dsn; static char *datadir; +static bool tom_jones; static struct fee_sum *find_sum_for_txid(struct fee_sum **sums, struct bitcoin_txid *txid) @@ -1061,7 +1062,9 @@ static struct command_result *json_balance_snapshot(struct command *cmd, struct channel_event *ev; u64 timestamp; - plugin_log(cmd->plugin, LOG_UNUSUAL, + /* This is *expected* on first run of bookkeeper! */ + plugin_log(cmd->plugin, + tom_jones ? LOG_DBG : LOG_UNUSUAL, "Snapshot balance does not equal ondisk" " reported %s, off by (+%s/-%s) (account %s)" " Logging journal entry.", @@ -1838,7 +1841,8 @@ static const char *init(struct plugin *p, const char *b, const jsmntok_t *t) 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)); + /* Final flag tells us What's New, Pussycat. */ + db = notleak(db_setup(p, p, db_dsn, &tom_jones)); db_dsn = tal_free(db_dsn); return NULL; diff --git a/plugins/bkpr/db.c b/plugins/bkpr/db.c index fdd9ebd2b..1db77327b 100644 --- a/plugins/bkpr/db.c +++ b/plugins/bkpr/db.c @@ -100,7 +100,7 @@ static struct migration db_migrations[] = { {SQL("ALTER TABLE channel_events ADD ev_desc TEXT DEFAULT NULL;"), NULL}, }; -static bool db_migrate(struct plugin *p, struct db *db) +static bool db_migrate(struct plugin *p, struct db *db, bool *created) { /* Read current version from database */ int current, orig, available; @@ -109,9 +109,11 @@ static bool db_migrate(struct plugin *p, struct db *db) orig = current = db_get_version(db); available = ARRAY_SIZE(db_migrations) - 1; - if (current == -1) + *created = false; + if (current == -1) { + *created = true; plugin_log(p, LOG_INFORM, "Creating database"); - else if (available < current) + } else if (available < current) plugin_err(p, "Refusing to migrate down from version %u to %u", current, available); @@ -154,7 +156,8 @@ void db_fatal(const char *fmt, ...) } #endif /* DB_FATAL */ -struct db *db_setup(const tal_t *ctx, struct plugin *p, char *db_dsn) +struct db *db_setup(const tal_t *ctx, struct plugin *p, + const char *db_dsn, bool *created) { bool migrated; struct db *db; @@ -165,7 +168,7 @@ struct db *db_setup(const tal_t *ctx, struct plugin *p, char *db_dsn) db->report_changes_fn = NULL; db_begin_transaction(db); - migrated = db_migrate(p, db); + migrated = db_migrate(p, db, created); db->data_version = db_data_version_get(db); db_commit_transaction(db); diff --git a/plugins/bkpr/db.h b/plugins/bkpr/db.h index 0c9663e94..b4eb50273 100644 --- a/plugins/bkpr/db.h +++ b/plugins/bkpr/db.h @@ -6,6 +6,7 @@ struct plugin; struct db; -struct db *db_setup(const tal_t *ctx, struct plugin *p, char *db_dsn); +struct db *db_setup(const tal_t *ctx, struct plugin *p, const char *db_dsn, + bool *created); #endif /* LIGHTNING_PLUGINS_BKPR_DB_H */ diff --git a/plugins/bkpr/test/run-bkpr_db.c b/plugins/bkpr/test/run-bkpr_db.c index 198512849..09116bcb0 100644 --- a/plugins/bkpr/test/run-bkpr_db.c +++ b/plugins/bkpr/test/run-bkpr_db.c @@ -258,19 +258,27 @@ static struct db *create_test_db(void) return db; } -static bool test_empty_db_migrate(struct plugin *plugin) +static bool test_db_migrate(struct plugin *plugin) { struct db *db = create_test_db(); + bool created; + CHECK(db); db_begin_transaction(db); CHECK(db_get_version(db) == -1); - db_migrate(plugin, db); + CHECK(db_migrate(plugin, db, &created) == true); + CHECK(created); db_commit_transaction(db); db_begin_transaction(db); CHECK(db_get_version(db) == ARRAY_SIZE(db_migrations) - 1); db_commit_transaction(db); + db_begin_transaction(db); + CHECK(db_migrate(plugin, db, &created) == false); + CHECK(!created); + db_commit_transaction(db); + tal_free(db); return true; } @@ -284,7 +292,7 @@ int main(int argc, char *argv[]) common_setup(argv[0]); - ok &= test_empty_db_migrate(plugin); + ok &= test_db_migrate(plugin); tal_free(plugin); common_shutdown(); diff --git a/plugins/bkpr/test/run-recorder.c b/plugins/bkpr/test/run-recorder.c index c71f040fe..762b99dbb 100644 --- a/plugins/bkpr/test/run-recorder.c +++ b/plugins/bkpr/test/run-recorder.c @@ -393,7 +393,8 @@ static struct chain_event *make_chain_event(const tal_t *ctx, static bool test_onchain_fee_wallet_spend(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id node_id, peer_id; struct account *wal_acct, *ext_acct; struct bitcoin_txid txid; @@ -470,7 +471,8 @@ static bool test_onchain_fee_wallet_spend(const tal_t *ctx, struct plugin *p) static bool test_onchain_fee_chan_close(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id node_id, peer_id; struct account *acct, *wal_acct, *ext_acct; struct onchain_fee **ofs, **ofs1; @@ -744,7 +746,8 @@ static bool test_onchain_fee_chan_close(const tal_t *ctx, struct plugin *p) static bool test_onchain_fee_chan_open(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id node_id, peer_id; struct account *acct, *acct2, *wal_acct, *ext_acct; struct bitcoin_txid txid; @@ -868,7 +871,8 @@ static bool test_onchain_fee_chan_open(const tal_t *ctx, struct plugin *p) static bool test_channel_event_crud(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id peer_id; struct account *acct, *acct2; struct channel_event *ev1, *ev2, *ev3, **chan_evs; @@ -954,7 +958,8 @@ static bool test_channel_event_crud(const tal_t *ctx, struct plugin *p) static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id peer_id; struct account *acct, *acct2; struct chain_event *ev1, *ev2, *ev3, **chain_evs; @@ -1088,7 +1093,8 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p) static bool test_account_balances(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id peer_id; struct account *acct, *acct2; struct chain_event *ev1; @@ -1193,7 +1199,8 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p) static bool test_account_crud(const tal_t *ctx, struct plugin *p) { - struct db *db = db_setup(ctx, p, tmp_dsn(ctx)); + bool created; + struct db *db = db_setup(ctx, p, tmp_dsn(ctx), &created); struct node_id *peer_id; struct account *acct, *acct2, **acct_list; struct chain_event *ev1;