diff --git a/wallet/db.c b/wallet/db.c index 575ee67ff..a9a2b380d 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -145,6 +145,11 @@ char *dbmigrations[] = { " VALUES('next_pay_index', " " COALESCE((SELECT MAX(pay_index) FROM invoices WHERE state=1), 0) + 1" " );", + /* Create first_block field; initialize from channel id if any. + * This fails for channels still awaiting lockin, but that only applies to + * pre-release software, so it's forgivable. */ + "ALTER TABLE channels ADD first_blocknum INTEGER;", + "UPDATE channels SET first_blocknum=CAST(short_channel_id AS INTEGER) WHERE short_channel_id IS NOT NULL;", NULL, }; diff --git a/wallet/wallet.c b/wallet/wallet.c index c5a700055..f5a7ec4f0 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -597,6 +597,25 @@ bool wallet_channels_load_active(struct wallet *w, struct list_head *peers) return ok; } +u32 wallet_channels_first_blocknum(struct wallet *w) +{ + int err; + u32 first_blocknum; + sqlite3_stmt *stmt = + db_query(__func__, w->db, + "SELECT MIN(first_blocknum) FROM channels WHERE state >= %d AND state != %d;", + OPENINGD, CLOSINGD_COMPLETE); + + err = sqlite3_step(stmt); + if (err == SQLITE_ROW && sqlite3_column_type(stmt, 1) != SQLITE_NULL) + first_blocknum = sqlite3_column_int(stmt, 1); + else + first_blocknum = UINT32_MAX; + + sqlite3_finalize(stmt); + return first_blocknum; +} + void wallet_channel_config_save(struct wallet *w, struct channel_config *cc) { sqlite3_stmt *stmt; diff --git a/wallet/wallet.h b/wallet/wallet.h index ae681d4a6..96ae5b181 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -242,6 +242,15 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, */ bool wallet_channels_load_active(struct wallet *w, struct list_head *peers); +/** + * wallet_channels_first_blocknum - get first block we're interested in. + * + * @w: wallet to load from. + * + * Returns UINT32_MAX if nothing interesting. + */ +u32 wallet_channels_first_blocknum(struct wallet *w); + /** * wallet_extract_owned_outputs - given a tx, extract all of our outputs */