From bdd11e07fee70de61079f109aba70dee0ee039ec Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 16 Feb 2018 09:37:04 +1030 Subject: [PATCH] chaintopology: fix 100 block subtraction. We do a complicated dance because we don't know the current block height before setting up the topology. If we're starting at a particular block, we want to go back 100 blocks before that to cover any reorgs. If we're not (fresh startup), we still want to go back 100 blocks because we don't bother handling a reorg which removes all the blocks we know. Signed-off-by: Rusty Russell --- lightningd/chaintopology.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 3907680f8..1fff03724 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -470,17 +470,18 @@ static void get_init_block(struct bitcoind *bitcoind, static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount, struct chain_topology *topo) { - /* FIXME: Because we don't handle our root disappearing, we go - * 100 blocks back */ - if (blockcount < 100) - blockcount = 0; - else - blockcount -= 100; - - /* This happens if first_blocknum is UINTMAX-1 */ + /* This can happen if bitcoind still syncing, or first_blocknum is MAX */ if (blockcount < topo->first_blocknum) topo->first_blocknum = blockcount; + /* For fork protection (esp. because we don't handle our own first + * block getting reorged out), we always go 100 blocks further back + * than we need. */ + if (topo->first_blocknum < 100) + topo->first_blocknum = 0; + else + topo->first_blocknum -= 100; + /* Get up to speed with topology. */ bitcoind_getblockhash(bitcoind, topo->first_blocknum, get_init_block, topo); @@ -719,9 +720,8 @@ void setup_topology(struct chain_topology *topo, memset(&topo->feerate, 0, sizeof(topo->feerate)); topo->timers = timers; topo->poll_time = poll_time; - /* Start one before the block we are interested in (as we won't - * get notifications on txs in that block). */ - topo->first_blocknum = first_blocknum - 1; + + topo->first_blocknum = first_blocknum; /* Make sure bitcoind is started, and ready */ wait_for_bitcoind(topo->bitcoind);