Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ void setup_topology(struct chain_topology *topo)
fixup = db_get_intvar(topo->ld->wallet->db, "fixup_block_scan", -1);
if (fixup == -1) {
/* Never done fixup: this is set to non-zero if we have blocks. */
topo->old_block_scan = wallet_blocks_minheight(topo->ld->wallet);
topo->old_block_scan = wallet_blocks_contig_minheight(topo->ld->wallet);
db_set_intvar(topo->ld->wallet->db, "fixup_block_scan",
topo->old_block_scan);
} else {
Expand Down
16 changes: 11 additions & 5 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2554,16 +2554,22 @@ u32 wallet_blocks_maxheight(struct wallet *w)
return max;
}

u32 wallet_blocks_minheight(struct wallet *w)
u32 wallet_blocks_contig_minheight(struct wallet *w)
{
u32 min = 0;
struct db_stmt *stmt = db_prepare_v2(w->db, SQL("SELECT MIN(height) FROM blocks;"));
struct db_stmt *stmt = db_prepare_v2(w->db, SQL("SELECT MAX(b.height)"
" FROM blocks b"
" WHERE NOT EXISTS ("
" SELECT 1"
" FROM blocks b2"
" WHERE b2.height = b.height - 1)"));
db_query_prepared(stmt);

/* If we ever processed a block we'll get the latest block in the chain */
/* If we ever processed a block we'll get the first block in
* the last run of blocks */
if (db_step(stmt)) {
if (!db_col_is_null(stmt, "MIN(height)")) {
min = db_col_int(stmt, "MIN(height)");
if (!db_col_is_null(stmt, "MAX(b.height)")) {
min = db_col_int(stmt, "MAX(b.height)");
}
}
tal_free(stmt);
Expand Down
5 changes: 3 additions & 2 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,14 @@ void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 cdbid, struct
u32 wallet_blocks_maxheight(struct wallet *w);

/**
* Retrieve the blockheight of the first block processed by lightningd.
* Retrieve the blockheight of the first block processed by lightningd (ignoring
* backfilled blocks for gossip).
*
* Will return the 0 if the wallet was never used before.
*
* @w: wallet to load from.
*/
u32 wallet_blocks_minheight(struct wallet *w);
u32 wallet_blocks_contig_minheight(struct wallet *w);

/**
* wallet_extract_owned_outputs - given a tx, extract all of our outputs
Expand Down
Loading