From 7b735e5de83b541fbaa08abf9f53778add9e8e82 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 3 Jan 2018 15:56:43 +1030 Subject: [PATCH] lightningd: scan blockchain from first possible block. Eventually we want to save blockchain in db to avoid this scan, but for the moment, we need to reload as far back as we may be interested in. Signed-off-by: Rusty Russell --- lightningd/chaintopology.c | 14 +++++++------- lightningd/lightningd.c | 6 ++++-- lightningd/test/run-find_my_path.c | 3 +++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index cce5bca76..b744852cb 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -467,13 +467,11 @@ static void get_init_block(struct bitcoind *bitcoind, static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount, struct chain_topology *topo) { - /* Start back before any reasonable forks. */ - if (blockcount < 100) - topo->first_blocknum = 0; - else if (!topo->first_blocknum || blockcount - 100 < topo->first_blocknum) - topo->first_blocknum = blockcount - 100; + /* This happens if first_blocknum is UINTMAX-1 */ + if (blockcount < topo->first_blocknum) + topo->first_blocknum = blockcount; - /* Start topology from 100 blocks back. */ + /* Get up to speed with topology. */ bitcoind_getblockhash(bitcoind, topo->first_blocknum, get_init_block, topo); } @@ -716,7 +714,9 @@ void setup_topology(struct chain_topology *topo, memset(&topo->feerate, 0, sizeof(topo->feerate)); topo->timers = timers; topo->poll_time = poll_time; - topo->first_blocknum = first_peer_block; + /* Start one before the block we are interested in (as we won't + * get notifications on txs in that block). */ + topo->first_blocknum = first_peer_block - 1; /* Make sure bitcoind is started, and ready */ wait_for_bitcoind(topo->bitcoind); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index a79d77106..335d9cde7 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -210,6 +210,7 @@ int main(int argc, char *argv[]) struct log_book *log_book; struct lightningd *ld; bool newdir; + u32 peer_first_blocknum; err_set_progname(argv[0]); @@ -291,14 +292,15 @@ int main(int argc, char *argv[]) if (!wallet_htlcs_reconnect(ld->wallet, &ld->htlcs_in, &ld->htlcs_out)) fatal("could not reconnect htlcs loaded from wallet, wallet may be inconsistent."); + peer_first_blocknum = wallet_channels_first_blocknum(ld->wallet); + db_commit_transaction(ld->wallet->db); /* Initialize block topology (does its own transaction) */ setup_topology(ld->topology, &ld->timers, ld->config.poll_time, - /* FIXME: Load from peers. */ - 0); + peer_first_blocknum); /* Create RPC socket (if any) */ setup_jsonrpc(ld, ld->rpc_filename); diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index d76d90987..2bcb8e5c6 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -87,6 +87,9 @@ struct txfilter *txfilter_new(const tal_t *ctx UNNEEDED) /* Generated stub for version */ const char *version(void) { fprintf(stderr, "version called!\n"); abort(); } +/* Generated stub for wallet_channels_first_blocknum */ +u32 wallet_channels_first_blocknum(struct wallet *w UNNEEDED) +{ fprintf(stderr, "wallet_channels_first_blocknum called!\n"); abort(); } /* Generated stub for wallet_channels_load_active */ bool wallet_channels_load_active(struct wallet *w UNNEEDED, struct list_head *peers UNNEEDED) { fprintf(stderr, "wallet_channels_load_active called!\n"); abort(); }