Skip to content

Commit

Permalink
wallet/db: don't use migration_context.
Browse files Browse the repository at this point in the history
`struct lightningd` is not completely initialized, so we added a
"migration_context" which only had some of the fields.  But we ended
up handing in `struct lightningd` anyway, so I don't think this
complexity is worthwhile.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Mar 22, 2023
1 parent 3db3dc9 commit df085a8
Showing 1 changed file with 26 additions and 62 deletions.
88 changes: 26 additions & 62 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,38 @@
#include <wallet/db.h>
#include <wire/wire_sync.h>

/* Small container for things that are needed by migrations. The
* fields are guaranteed to be initialized and can be relied upon when
* migrating.
*/
struct migration_context {
const struct ext_key *bip32_base;
int hsm_fd;
};

struct migration {
const char *sql;
void (*func)(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
void (*func)(struct lightningd *ld, struct db *db);
};

static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db);

static void migrate_our_funding(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
static void migrate_our_funding(struct lightningd *ld, struct db *db);

static void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
static void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db);

static void
migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db);

static void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
static void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db);

static void fillin_missing_channel_id(struct lightningd *ld, struct db *db,
const struct migration_context *mc);
static void fillin_missing_channel_id(struct lightningd *ld, struct db *db);

static void fillin_missing_local_basepoints(struct lightningd *ld,
struct db *db,
const struct migration_context *mc);
struct db *db);

static void fillin_missing_channel_blockheights(struct lightningd *ld,
struct db *db,
const struct migration_context *mc);
struct db *db);

static void migrate_channels_scids_as_integers(struct lightningd *ld,
struct db *db,
const struct migration_context *mc);
struct db *db);

static void migrate_payments_scids_as_integers(struct lightningd *ld,
struct db *db,
const struct migration_context *mc);
struct db *db);

static void fillin_missing_lease_satoshi(struct lightningd *ld,
struct db *db,
const struct migration_context *mc);
struct db *db);

/* Do not reorder or remove elements from this array, it is used to
* migrate existing databases from a previous state, based on the
Expand Down Expand Up @@ -966,10 +945,6 @@ static bool db_migrate(struct lightningd *ld, struct db *db,
int current, orig, available;
char *err_msg;
struct db_stmt *stmt;
const struct migration_context mc = {
.bip32_base = bip32_base,
.hsm_fd = ld->hsm_fd,
};

orig = current = db_get_version(db);
available = ARRAY_SIZE(dbmigrations) - 1;
Expand Down Expand Up @@ -1005,7 +980,7 @@ static bool db_migrate(struct lightningd *ld, struct db *db,
tal_free(stmt);
}
if (dbmigrations[current].func)
dbmigrations[current].func(ld, db, &mc);
dbmigrations[current].func(ld, db);
}

/* Finally update the version number in the version table */
Expand Down Expand Up @@ -1052,8 +1027,7 @@ struct db *db_setup(const tal_t *ctx, struct lightningd *ld,
}

/* Will apply the current config fee settings to all channels */
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt = db_prepare_v2(
db, SQL("UPDATE channels SET feerate_base = ?, feerate_ppm = ?;"));
Expand All @@ -1071,8 +1045,7 @@ static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db
* is the same as the funding_satoshi for every channel where we are
* the `funder`
*/
static void migrate_our_funding(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
static void migrate_our_funding(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt;

Expand All @@ -1088,8 +1061,7 @@ static void migrate_our_funding(struct lightningd *ld, struct db *db,
tal_free(stmt);
}

void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt;

Expand Down Expand Up @@ -1175,8 +1147,7 @@ void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db,
* could simply derive the channel_id whenever it was required, but since there
* are now two ways to do it, we save the derived channel id.
*/
static void fillin_missing_channel_id(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
static void fillin_missing_channel_id(struct lightningd *ld, struct db *db)
{

struct db_stmt *stmt;
Expand Down Expand Up @@ -1213,8 +1184,7 @@ static void fillin_missing_channel_id(struct lightningd *ld, struct db *db,
}

static void fillin_missing_local_basepoints(struct lightningd *ld,
struct db *db,
const struct migration_context *mc)
struct db *db)
{

struct db_stmt *stmt;
Expand All @@ -1240,12 +1210,12 @@ static void fillin_missing_local_basepoints(struct lightningd *ld,
dbid = db_col_u64(stmt, "channels.id");
db_col_node_id(stmt, "peers.node_id", &peer_id);

if (!wire_sync_write(mc->hsm_fd,
if (!wire_sync_write(ld->hsm_fd,
towire_hsmd_get_channel_basepoints(
tmpctx, &peer_id, dbid)))
fatal("could not retrieve basepoint from hsmd");

msg = wire_sync_read(tmpctx, mc->hsm_fd);
msg = wire_sync_read(tmpctx, ld->hsm_fd);
if (!fromwire_hsmd_get_channel_basepoints_reply(
msg, &base, &funding_pubkey))
fatal("malformed hsmd_get_channel_basepoints_reply "
Expand Down Expand Up @@ -1277,8 +1247,7 @@ static void fillin_missing_local_basepoints(struct lightningd *ld,
/* New 'channel_blockheights' table, every existing channel gets a
* 'initial blockheight' of 0 */
static void fillin_missing_channel_blockheights(struct lightningd *ld,
struct db *db,
const struct migration_context *mc)
struct db *db)
{
struct db_stmt *stmt;

Expand All @@ -1302,8 +1271,7 @@ static void fillin_missing_channel_blockheights(struct lightningd *ld,
}

void
migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt, *update_stmt;
stmt = db_prepare_v2(db, SQL("SELECT "
Expand Down Expand Up @@ -1399,8 +1367,7 @@ migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db,
* This migration loads all of the last_tx's and 're-formats' them into psbts,
* adds the required input witness utxo information, and then saves it back to disk
* */
void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db,
const struct migration_context *mc)
void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt, *update_stmt;

Expand Down Expand Up @@ -1490,8 +1457,7 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db,

/* We used to store scids as strings... */
static void migrate_channels_scids_as_integers(struct lightningd *ld,
struct db *db,
const struct migration_context *mc)
struct db *db)
{
struct db_stmt *stmt;
char **scids = tal_arr(tmpctx, char *, 0);
Expand Down Expand Up @@ -1548,8 +1514,7 @@ static void migrate_channels_scids_as_integers(struct lightningd *ld,
}

static void migrate_payments_scids_as_integers(struct lightningd *ld,
struct db *db,
const struct migration_context *mc)
struct db *db)
{
struct db_stmt *stmt;
const char *colnames[] = {"failchannel"};
Expand Down Expand Up @@ -1585,8 +1550,7 @@ static void migrate_payments_scids_as_integers(struct lightningd *ld,
}

static void fillin_missing_lease_satoshi(struct lightningd *ld,
struct db *db,
const struct migration_context *mc)
struct db *db)
{
struct db_stmt *stmt;

Expand Down

0 comments on commit df085a8

Please sign in to comment.