Browse Source

hsmd/wallet: pass the bip32_key down into migrations

we're about to add a migration that requires access to the bip32_key
in order to calculate missing scriptpubkeys.

prior to this patch, we don't have access to the bip32 key in the db
migration, as it's set on the wallet but after the db migrations are
run.

here we patch it through so that every migration can access it
v0.9.0.1
niftynei 5 years ago
committed by Christian Decker
parent
commit
65c2bac2f3
  1. 3
      lightningd/lightningd.c
  2. 3
      lightningd/test/run-find_my_abspath.c
  3. 33
      wallet/db.c
  4. 6
      wallet/db.h
  5. 6
      wallet/test/run-db.c
  6. 3
      wallet/test/run-wallet.c
  7. 7
      wallet/wallet.c
  8. 3
      wallet/wallet.h

3
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. */

3
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 */

33
wallet/db.c

@ -14,17 +14,25 @@
#include <lightningd/log.h>
#include <lightningd/plugin_hook.h>
#include <wallet/db_common.h>
#include <wally_bip32.h>
#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;

6
wallet/db.h

@ -15,6 +15,7 @@
#include <secp256k1_ecdh.h>
#include <stdbool.h>
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

6
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);

3
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");

7
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);

3
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.

Loading…
Cancel
Save