diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 4e0302bca..6e0fdab27 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -931,6 +931,15 @@ u32 get_block_height(const struct chain_topology *topo) return topo->tip->height; } +u32 get_network_blockheight(const struct chain_topology *topo) +{ + if (topo->tip->height > topo->headercount) + return topo->tip->height; + else + return topo->headercount; +} + + u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate) { return topo->feerate[feerate]; @@ -1068,6 +1077,8 @@ check_chain(struct bitcoind *bitcoind, const char *chain, fatal("Wrong network! Our Bitcoin backend is running on '%s'," " but we expect '%s'.", chain, chainparams->bip70_name); + topo->headercount = headercount; + if (first_call) { /* Has the Bitcoin backend gone backward ? */ check_blockcount(topo, blockcount); diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 962dd1456..8fd651283 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -120,6 +120,10 @@ struct chain_topology { /* Transactions/txos we are watching. */ struct txwatch_hash txwatches; struct txowatch_hash txowatches; + + /* The number of headers known to the bitcoin backend at startup. Not + * updated after the initial check. */ + u32 headercount; }; /* Information relevant to locating a TX in a blockchain. */ @@ -140,6 +144,13 @@ size_t get_tx_depth(const struct chain_topology *topo, /* Get highest block number. */ u32 get_block_height(const struct chain_topology *topo); +/* Get the highest block number in the network that we are aware of. Unlike + * `get_block_height` this takes into consideration the block header counter + * in the bitcoin backend as well. If an absolute time is required, rather + * than our current scan position this is preferable since it is far less + * likely to lag behind the rest of the network.*/ +u32 get_network_blockheight(const struct chain_topology *topo); + /* Get fee rate in satoshi per kiloweight, or 0 if unavailable! */ u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 5343c384e..8766459f8 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -265,7 +265,7 @@ wallet_commit_channel(struct lightningd *ld, NULL, /* No commit sent yet */ /* If we're fundee, could be a little before this * in theory, but it's only used for timing out. */ - get_block_height(ld->topology), + get_network_blockheight(ld->topology), feerate, feerate, /* We are connected */ true,