Skip to content

Commit

Permalink
lightningd: don't start if bitcoind is behind.
Browse files Browse the repository at this point in the history
This leads to all sorts of problems; in particular it's incredibly
slow (days, weeks!)  if bitcoind is a long way back.

We already ignore bitcoind if it goes backwards while we're running.

Also cover a false positive memleak.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Nov 19, 2019
1 parent 5158067 commit e09d68d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ static void bcli_failure(struct bitcoind *bitcoind,
bitcoind->error_count++;

/* Retry in 1 second (not a leak!) */
new_reltimer(bitcoind->ld->timers, notleak(bcli), time_from_sec(1),
retry_bcli, bcli);
notleak(new_reltimer(bitcoind->ld->timers, notleak(bcli),
time_from_sec(1),
retry_bcli, bcli));
}

static void bcli_finished(struct io_conn *conn UNUSED, struct bitcoin_cli *bcli)
Expand Down
19 changes: 7 additions & 12 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,11 @@ static void get_init_block(struct bitcoind *bitcoind,
static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount,
struct chain_topology *topo)
{
/* 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 bitcoind's current blockheight is below the requested
* height, refuse. You can always explicitly request a reindex from
* that block number using --rescan=. */
if (blockcount < topo->max_blockheight) {
/* UINT32_MAX == no blocks in database */
if (topo->max_blockheight == UINT32_MAX) {
/* Relative rescan, but we didn't know the blockheight */
/* Protect against underflow in subtraction.
Expand All @@ -816,15 +817,9 @@ static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount,
topo->max_blockheight = 0;
else
topo->max_blockheight = blockcount - bitcoind->ld->config.rescan;
} else {
/* Absolute blockheight, but bitcoind's blockheight isn't there yet */
/* Protect against underflow in subtraction.
* Possible in regtest mode. */
if (blockcount < 1)
topo->max_blockheight = 0;
else
topo->max_blockheight = blockcount - 1;
}
} else
fatal("bitcoind has gone backwards from %u to %u blocks!",
topo->max_blockheight, blockcount);
}

/* Rollback to the given blockheight, so we start track
Expand Down
1 change: 0 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,6 @@ def test_rescan(node_factory, bitcoind):
assert not l1.daemon.is_in_log(r'Adding block 102')


@pytest.mark.xfail(strict=True)
def test_bitcoind_goes_backwards(node_factory, bitcoind):
"""Check that we refuse to acknowledge bitcoind giving a shorter chain without explicit rescan"""
l1 = node_factory.get_node(may_fail=True, allow_broken_log=True)
Expand Down

0 comments on commit e09d68d

Please sign in to comment.