From 0d4b7eaa2c1fc69e580e36ea9da0627847375fb0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 4 Jun 2018 14:46:05 +0200 Subject: [PATCH] topo: Have chain_topology track both min and max block heights Signed-off-by: Christian Decker --- lightningd/chaintopology.c | 23 +++++++++++++---------- lightningd/chaintopology.h | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 3b19bcde9..767de73ab 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -422,6 +422,7 @@ static void add_tip(struct chain_topology *topo, struct block *b) filter_block_txs(topo, b); block_map_add(&topo->block_map, b); + topo->max_blockheight = b->height; } static struct block *new_block(struct chain_topology *topo, @@ -509,7 +510,7 @@ static void init_topo(struct bitcoind *bitcoind UNUSED, struct bitcoin_block *blk, struct chain_topology *topo) { - topo->root = new_block(topo, blk, topo->first_blocknum); + topo->root = new_block(topo, blk, topo->max_blockheight); block_map_add(&topo->block_map, topo->root); topo->tip = topo->prev_tip = topo->root; @@ -533,32 +534,32 @@ static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount, /* If bitcoind's current blockheight is below the requested height, just * go back to that height. This might be a new node catching up, or * bitcoind is processing a reorg. */ - if (blockcount < topo->first_blocknum) { + if (blockcount < topo->max_blockheight) { if (bitcoind->ld->config.rescan < 0) { /* Absolute blockheight, but bitcoind's blockheight isn't there yet */ /* Protect against underflow in subtraction. * Possible in regtest mode. */ if (blockcount < 1) - topo->first_blocknum = 0; + topo->max_blockheight = 0; else - topo->first_blocknum = blockcount - 1; - } else if (topo->first_blocknum == UINT32_MAX) { + topo->max_blockheight = blockcount - 1; + } else if (topo->max_blockheight == UINT32_MAX) { /* Relative rescan, but we didn't know the blockheight */ /* Protect against underflow in subtraction. * Possible in regtest mode. */ if (blockcount < bitcoind->ld->config.rescan) - topo->first_blocknum = 0; + topo->max_blockheight = 0; else - topo->first_blocknum = blockcount - bitcoind->ld->config.rescan; + topo->max_blockheight = blockcount - bitcoind->ld->config.rescan; } } /* Rollback to the given blockheight, so we start track * correctly again */ - wallet_blocks_rollback(topo->wallet, topo->first_blocknum); + wallet_blocks_rollback(topo->wallet, topo->max_blockheight); /* Get up to speed with topology. */ - bitcoind_getblockhash(bitcoind, topo->first_blocknum, + bitcoind_getblockhash(bitcoind, topo->max_blockheight, get_init_block, topo); } @@ -749,7 +750,9 @@ void setup_topology(struct chain_topology *topo, memset(&topo->feerate, 0, sizeof(topo->feerate)); topo->timers = timers; - topo->first_blocknum = first_blocknum; + /* FIXME(cdecker) Actually load this from DB */ + topo->min_blockheight = first_blocknum; + topo->max_blockheight = first_blocknum; /* Make sure bitcoind is started, and ready */ wait_for_bitcoind(topo->bitcoind); diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 26ee80afc..ce63bc4ff 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -89,8 +89,8 @@ struct chain_topology { /* Where to log things. */ struct log *log; - /* How far back (in blocks) to go. */ - unsigned int first_blocknum; + /* What range of blocks do we have in our database? */ + u32 min_blockheight, max_blockheight; /* How often to poll. */ u32 poll_seconds;