diff --git a/wallet/db.c b/wallet/db.c index 45fefef7c..843d27bd9 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -67,6 +67,9 @@ static void migrate_fill_in_channel_type(struct lightningd *ld, static void migrate_normalize_invstr(struct lightningd *ld, struct db *db); +static void migrate_initialize_wait_indexes(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 * string indices */ @@ -959,6 +962,7 @@ static struct migration dbmigrations[] = { {SQL("CREATE TABLE runes (id BIGSERIAL, rune TEXT, PRIMARY KEY (id));"), NULL}, {SQL("CREATE TABLE runes_blacklist (start_index BIGINT, end_index BIGINT);"), NULL}, {SQL("ALTER TABLE channels ADD ignore_fee_limits INTEGER DEFAULT 0;"), NULL}, + {NULL, migrate_initialize_wait_indexes}, }; /** @@ -1393,6 +1397,18 @@ migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db) tal_free(stmt); } +void load_indexes(struct db *db, struct indexes *indexes) +{ + for (size_t s = 0; s < NUM_WAIT_SUBSYSTEM; s++) { + for (size_t i = 0; i < NUM_WAIT_INDEX; i++) { + const char *fname = tal_fmt(tmpctx, "last_%s_%s_index", + wait_subsystem_name(s), + wait_index_name(i)); + indexes[s].i[i] = db_get_intvar(db, fname, 0); + } + } +} + /* We're moving everything over to PSBTs from tx's, particularly our last_tx's * which are commitment transactions for channels. * This migration loads all of the last_tx's and 're-formats' them into psbts, @@ -1635,6 +1651,24 @@ static void migrate_fill_in_channel_type(struct lightningd *ld, tal_free(stmt); } +static void migrate_initialize_wait_indexes(struct lightningd *ld, + struct db *db) +{ + struct db_stmt *stmt; + bool res; + + /* "invoices.id" serves as the created_index. It's never 0. */ + stmt = db_prepare_v2(db, SQL("SELECT MAX(id) FROM invoices;")); + db_query_prepared(stmt); + res = db_step(stmt); + assert(res); + + if (!db_col_is_null(stmt, "MAX(id)")) + db_set_intvar(db, "last_invoice_created_index", + db_col_u64(stmt, "MAX(id)")); + tal_free(stmt); +} + static void complain_unfixed(struct lightningd *ld, enum channel_state state, u64 id, diff --git a/wallet/db.h b/wallet/db.h index 3d76d097a..f891d18a6 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -3,6 +3,7 @@ #include "config.h" struct ext_key; +struct indexes; struct lightningd; struct db_stmt; struct db; @@ -22,4 +23,7 @@ struct db; struct db *db_setup(const tal_t *ctx, struct lightningd *ld, const struct ext_key *bip32_base); +/* We store last wait indices in our var table. */ +void load_indexes(struct db *db, struct indexes *indexes); + #endif /* LIGHTNING_WALLET_DB_H */ diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index b1dadd2fa..2308b1566 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -62,6 +62,12 @@ u8 *towire_hsmd_get_channel_basepoints(const tal_t *ctx UNNEEDED, const struct n /* Generated stub for towire_hsmd_get_output_scriptpubkey */ u8 *towire_hsmd_get_output_scriptpubkey(const tal_t *ctx UNNEEDED, u64 channel_id UNNEEDED, const struct node_id *peer_id UNNEEDED, const struct pubkey *commitment_point UNNEEDED) { fprintf(stderr, "towire_hsmd_get_output_scriptpubkey called!\n"); abort(); } +/* Generated stub for wait_index_name */ +const char *wait_index_name(enum wait_index index UNNEEDED) +{ fprintf(stderr, "wait_index_name called!\n"); abort(); } +/* Generated stub for wait_subsystem_name */ +const char *wait_subsystem_name(enum wait_subsystem subsystem UNNEEDED) +{ fprintf(stderr, "wait_subsystem_name called!\n"); abort(); } /* Generated stub for wire_sync_read */ u8 *wire_sync_read(const tal_t *ctx UNNEEDED, int fd UNNEEDED) { fprintf(stderr, "wire_sync_read called!\n"); abort(); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9caeb3ba8..d84c92b51 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -856,6 +856,12 @@ void try_reconnect(const tal_t *ctx UNNEEDED, struct peer *peer UNNEEDED, const struct wireaddr_internal *addrhint UNNEEDED) { fprintf(stderr, "try_reconnect called!\n"); abort(); } +/* Generated stub for wait_index_name */ +const char *wait_index_name(enum wait_index index UNNEEDED) +{ fprintf(stderr, "wait_index_name called!\n"); abort(); } +/* Generated stub for wait_subsystem_name */ +const char *wait_subsystem_name(enum wait_subsystem subsystem UNNEEDED) +{ fprintf(stderr, "wait_subsystem_name called!\n"); abort(); } /* Generated stub for watch_txid */ struct txwatch *watch_txid(const tal_t *ctx UNNEEDED, struct chain_topology *topo UNNEEDED, diff --git a/wallet/wallet.c b/wallet/wallet.c index 28155e6fd..85fefe497 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -114,6 +114,7 @@ struct wallet *wallet_new(struct lightningd *ld, struct timers *timers) db_begin_transaction(wallet->db); wallet->invoices = invoices_new(wallet, wallet, timers); outpointfilters_init(wallet); + load_indexes(wallet->db, ld->indexes); db_commit_transaction(wallet->db); return wallet; }