Browse Source

topo: Have chain_topology track both min and max block heights

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
0d4b7eaa2c
  1. 23
      lightningd/chaintopology.c
  2. 4
      lightningd/chaintopology.h

23
lightningd/chaintopology.c

@ -422,6 +422,7 @@ static void add_tip(struct chain_topology *topo, struct block *b)
filter_block_txs(topo, b); filter_block_txs(topo, b);
block_map_add(&topo->block_map, b); block_map_add(&topo->block_map, b);
topo->max_blockheight = b->height;
} }
static struct block *new_block(struct chain_topology *topo, 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 bitcoin_block *blk,
struct chain_topology *topo) 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); block_map_add(&topo->block_map, topo->root);
topo->tip = topo->prev_tip = 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 /* 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 * go back to that height. This might be a new node catching up, or
* bitcoind is processing a reorg. */ * bitcoind is processing a reorg. */
if (blockcount < topo->first_blocknum) { if (blockcount < topo->max_blockheight) {
if (bitcoind->ld->config.rescan < 0) { if (bitcoind->ld->config.rescan < 0) {
/* Absolute blockheight, but bitcoind's blockheight isn't there yet */ /* Absolute blockheight, but bitcoind's blockheight isn't there yet */
/* Protect against underflow in subtraction. /* Protect against underflow in subtraction.
* Possible in regtest mode. */ * Possible in regtest mode. */
if (blockcount < 1) if (blockcount < 1)
topo->first_blocknum = 0; topo->max_blockheight = 0;
else else
topo->first_blocknum = blockcount - 1; topo->max_blockheight = blockcount - 1;
} else if (topo->first_blocknum == UINT32_MAX) { } else if (topo->max_blockheight == UINT32_MAX) {
/* Relative rescan, but we didn't know the blockheight */ /* Relative rescan, but we didn't know the blockheight */
/* Protect against underflow in subtraction. /* Protect against underflow in subtraction.
* Possible in regtest mode. */ * Possible in regtest mode. */
if (blockcount < bitcoind->ld->config.rescan) if (blockcount < bitcoind->ld->config.rescan)
topo->first_blocknum = 0; topo->max_blockheight = 0;
else 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 /* Rollback to the given blockheight, so we start track
* correctly again */ * correctly again */
wallet_blocks_rollback(topo->wallet, topo->first_blocknum); wallet_blocks_rollback(topo->wallet, topo->max_blockheight);
/* Get up to speed with topology. */ /* Get up to speed with topology. */
bitcoind_getblockhash(bitcoind, topo->first_blocknum, bitcoind_getblockhash(bitcoind, topo->max_blockheight,
get_init_block, topo); get_init_block, topo);
} }
@ -749,7 +750,9 @@ void setup_topology(struct chain_topology *topo,
memset(&topo->feerate, 0, sizeof(topo->feerate)); memset(&topo->feerate, 0, sizeof(topo->feerate));
topo->timers = timers; 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 */ /* Make sure bitcoind is started, and ready */
wait_for_bitcoind(topo->bitcoind); wait_for_bitcoind(topo->bitcoind);

4
lightningd/chaintopology.h

@ -89,8 +89,8 @@ struct chain_topology {
/* Where to log things. */ /* Where to log things. */
struct log *log; struct log *log;
/* How far back (in blocks) to go. */ /* What range of blocks do we have in our database? */
unsigned int first_blocknum; u32 min_blockheight, max_blockheight;
/* How often to poll. */ /* How often to poll. */
u32 poll_seconds; u32 poll_seconds;

Loading…
Cancel
Save