diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 1c9af6556..bdffd4e25 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -306,7 +306,7 @@ void notify_new_block(struct lightningd *ld, int main(int argc, char *argv[]) { struct lightningd *ld; - u32 blockheight; + u32 blockheight, first_block; setup_locale(); daemon_setup(argv[0], log_backtrace_print, log_backtrace_exit); @@ -388,7 +388,7 @@ int main(int argc, char *argv[]) /* Get the blockheight we are currently at, UINT32_MAX is used to signal * an unitialized wallet and that we should start off of bitcoind's * current height */ - blockheight = wallet_blocks_height(ld->wallet, UINT32_MAX); + wallet_blocks_heights(ld->wallet, UINT32_MAX, &first_block, &blockheight); /* If we were asked to rescan from an absolute height (--rescan < 0) * then just go there. Otherwise take compute the diff to our current diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index dd123f81b..38c630371 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -121,9 +121,9 @@ struct txfilter *txfilter_new(const tal_t *ctx UNNEEDED) /* Generated stub for version */ const char *version(void) { fprintf(stderr, "version called!\n"); abort(); } -/* Generated stub for wallet_blocks_height */ -u32 wallet_blocks_height(struct wallet *w UNNEEDED, u32 def UNNEEDED) -{ fprintf(stderr, "wallet_blocks_height called!\n"); abort(); } +/* Generated stub for wallet_blocks_heights */ +void wallet_blocks_heights(struct wallet *w UNNEEDED, u32 def UNNEEDED, u32 *min UNNEEDED, u32 *max UNNEEDED) +{ fprintf(stderr, "wallet_blocks_heights called!\n"); abort(); } /* Generated stub for wallet_channels_load_active */ bool wallet_channels_load_active(const tal_t *ctx UNNEEDED, struct wallet *w UNNEEDED) { fprintf(stderr, "wallet_channels_load_active called!\n"); abort(); } diff --git a/wallet/wallet.c b/wallet/wallet.c index cb32eb250..300a84b99 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -769,20 +769,20 @@ void wallet_channel_stats_load(struct wallet *w, db_stmt_done(stmt); } -u32 wallet_blocks_height(struct wallet *w, u32 def) +void wallet_blocks_heights(struct wallet *w, u32 def, u32 *min, u32 *max) { - u32 blockheight; - sqlite3_stmt *stmt = db_prepare(w->db, "SELECT MAX(height) FROM blocks;"); + assert(min != NULL && max != NULL); + sqlite3_stmt *stmt = db_prepare(w->db, "SELECT MIN(height), MAX(height) FROM blocks;"); /* If we ever processed a block we'll get the latest block in the chain */ if (sqlite3_step(stmt) == SQLITE_ROW && sqlite3_column_type(stmt, 0) != SQLITE_NULL) { - blockheight = sqlite3_column_int(stmt, 0); - db_stmt_done(stmt); - return blockheight; + *min = sqlite3_column_int(stmt, 0); + *max = sqlite3_column_int(stmt, 1); } else { - db_stmt_done(stmt); - return def; + *min = def; + *max = def; } + db_stmt_done(stmt); } static void wallet_channel_config_insert(struct wallet *w, diff --git a/wallet/wallet.h b/wallet/wallet.h index 164f6eb82..aa359be7c 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -316,13 +316,15 @@ void wallet_channel_stats_load(struct wallet *w, u64 cdbid, struct channel_stats /** * Retrieve the blockheight of the last block processed by lightningd. * - * Will return either the maximal blockheight or the default value if the wallet - * was never used before. + * Will set min/max either the minimal/maximal blockheight or the default value + * if the wallet was never used before. * * @w: wallet to load from. * @def: the default value to return if we've never used the wallet before + * @min(out): height of the first block we track + * @max(out): height of the last block we added */ -u32 wallet_blocks_height(struct wallet *w, u32 def); +void wallet_blocks_heights(struct wallet *w, u32 def, u32 *min, u32 *max); /** * wallet_extract_owned_outputs - given a tx, extract all of our outputs