diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index b2f04cb38..397ce48d6 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -841,8 +841,7 @@ int main(int argc, char *argv[]) /*~ Our "wallet" code really wraps the db, which is more than a simple * bitcoin wallet (though it's that too). It also stores channel * states, invoices, payments, blocks and bitcoin transactions. */ - ld->wallet = wallet_new(ld, ld->timers); - ld->wallet->bip32_base = tal_steal(ld->wallet, bip32_base); + ld->wallet = wallet_new(ld, ld->timers, bip32_base); /*~ We keep track of how many 'coin moves' we've ever made. * Initialize the starting value from the database here. */ diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 41c74e071..f62e15697 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -250,7 +250,8 @@ void wallet_clean_utxos(struct wallet *w UNNEEDED, struct bitcoind *bitcoind UNN bool wallet_network_check(struct wallet *w UNNEEDED) { fprintf(stderr, "wallet_network_check called!\n"); abort(); } /* Generated stub for wallet_new */ -struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED) +struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED, + struct ext_key *bip32_base UNNEEDED) { fprintf(stderr, "wallet_new called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ diff --git a/wallet/db.c b/wallet/db.c index b3cca1b9e..9495b3b2c 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -14,17 +14,25 @@ #include #include #include +#include #define NSEC_IN_SEC 1000000000 struct migration { const char *sql; - void (*func)(struct lightningd *ld, struct db *db); + void (*func)(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base); }; -static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db); +static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base); + +static void migrate_our_funding(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base); + +static void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base); -static void migrate_our_funding(struct lightningd *ld, 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 @@ -970,7 +978,8 @@ static int db_get_version(struct db *db) /** * db_migrate - Apply all remaining migrations from the current version */ -static void db_migrate(struct lightningd *ld, struct db *db) +static void db_migrate(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base) { /* Attempt to read the version from the database */ int current, orig, available; @@ -997,7 +1006,7 @@ static void db_migrate(struct lightningd *ld, struct db *db) tal_free(stmt); } if (dbmigrations[current].func) - dbmigrations[current].func(ld, db); + dbmigrations[current].func(ld, db, bip32_base); } /* Finally update the version number in the version table */ @@ -1029,14 +1038,15 @@ u32 db_data_version_get(struct db *db) return version; } -struct db *db_setup(const tal_t *ctx, struct lightningd *ld) +struct db *db_setup(const tal_t *ctx, struct lightningd *ld, + const struct ext_key *bip32_base) { struct db *db = db_open(ctx, ld->wallet_dsn); db->log = new_log(db, ld->log_book, NULL, "database"); db_begin_transaction(db); - db_migrate(ld, db); + db_migrate(ld, db, bip32_base); db->data_version = db_data_version_get(db); db_commit_transaction(db); @@ -1082,7 +1092,8 @@ void db_set_intvar(struct db *db, char *varname, s64 val) } /* Will apply the current config fee settings to all channels */ -static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db) +static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base) { struct db_stmt *stmt = db_prepare_v2( db, SQL("UPDATE channels SET feerate_base = ?, feerate_ppm = ?;")); @@ -1100,7 +1111,8 @@ 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) +static void migrate_our_funding(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base) { struct db_stmt *stmt; @@ -1121,7 +1133,8 @@ static void migrate_our_funding(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) +void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db, + const struct ext_key *bip32_base) { struct db_stmt *stmt, *update_stmt; diff --git a/wallet/db.h b/wallet/db.h index f8b6c112e..5dadebd3a 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -15,6 +15,7 @@ #include #include +struct ext_key; struct lightningd; struct log; struct node_id; @@ -23,7 +24,6 @@ struct db_stmt; struct db; struct wally_psbt; -void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db); /** * Macro to annotate a named SQL query. * @@ -56,8 +56,10 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db); * Params: * @ctx: the tal_t context to allocate from * @ld: the lightningd context to hand to upgrade functions. + * @bip32_base: the base all of our pubkeys are constructed on */ -struct db *db_setup(const tal_t *ctx, struct lightningd *ld); +struct db *db_setup(const tal_t *ctx, struct lightningd *ld, + const struct ext_key *bip32_base); /** * db_begin_transaction - Begin a transaction diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index aeeed1ea2..d133563c7 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -72,10 +72,11 @@ static struct db *create_test_db(void) static bool test_empty_db_migrate(struct lightningd *ld) { struct db *db = create_test_db(); + const struct ext_key *bip32_base = NULL; CHECK(db); db_begin_transaction(db); CHECK(db_get_version(db) == -1); - db_migrate(ld, db); + db_migrate(ld, db, bip32_base); db_commit_transaction(db); db_begin_transaction(db); @@ -124,10 +125,11 @@ static bool test_vars(struct lightningd *ld) { struct db *db = create_test_db(); char *varname = "testvar"; + const struct ext_key *bip32_base = NULL; CHECK(db); db_begin_transaction(db); - db_migrate(ld, db); + db_migrate(ld, db, bip32_base); /* Check default behavior */ CHECK(db_get_intvar(db, varname, 42) == 42); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index c3ab72806..2f1d7a174 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -866,6 +866,7 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx int fd = mkstemp(filename); struct wallet *w = tal(ctx, struct wallet); static unsigned char badseed[BIP32_ENTROPY_LEN_128]; + const struct ext_key *bip32_base = NULL; CHECK_MSG(fd != -1, "Unable to generate temp filename"); close(fd); @@ -885,7 +886,7 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx CHECK_MSG(w->db, "Failed opening the db"); db_begin_transaction(w->db); - db_migrate(ld, w->db); + db_migrate(ld, w->db, bip32_base); w->db->data_version = 0; db_commit_transaction(w->db); CHECK_MSG(!wallet_err, "DB migration failed"); diff --git a/wallet/wallet.c b/wallet/wallet.c index bbbe43644..708932af7 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -58,16 +58,17 @@ static void outpointfilters_init(struct wallet *w) tal_free(stmt); } -struct wallet *wallet_new(struct lightningd *ld, struct timers *timers) +struct wallet *wallet_new(struct lightningd *ld, struct timers *timers, + struct ext_key *bip32_base STEALS) { struct wallet *wallet = tal(ld, struct wallet); wallet->ld = ld; - wallet->db = db_setup(wallet, ld); wallet->log = new_log(wallet, ld->log_book, NULL, "wallet"); - wallet->bip32_base = NULL; + wallet->bip32_base = tal_steal(wallet, bip32_base); wallet->keyscan_gap = 50; list_head_init(&wallet->unstored_payments); list_head_init(&wallet->unreleased_txs); + wallet->db = db_setup(wallet, ld, wallet->bip32_base); db_begin_transaction(wallet->db); wallet->invoices = invoices_new(wallet, wallet->db, timers); diff --git a/wallet/wallet.h b/wallet/wallet.h index 690bd7272..ba51401c4 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -331,7 +331,8 @@ struct wallet_transaction { * This is guaranteed to either return a valid wallet, or abort with * `fatal` if it cannot be initialized. */ -struct wallet *wallet_new(struct lightningd *ld, struct timers *timers); +struct wallet *wallet_new(struct lightningd *ld, struct timers *timers, + struct ext_key *bip32_base); /** * wallet_confirm_tx - Confirm a tx which contains a UTXO.