From 6f6d7a5e4494ef30b8e55861f2e3892be1237985 Mon Sep 17 00:00:00 2001
From: Rusty Russell <rusty@rustcorp.com.au>
Date: Thu, 21 Dec 2017 21:37:55 +1030
Subject: [PATCH] chaintopology: get fees using a timer, not on each block.

It definitely changes when we get a block, but it also changes between
blocks as mempool fills.  So put it on its own timer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
 lightningd/chaintopology.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c
index 92b9a2ac3989..53179f3ab62d 100644
--- a/lightningd/chaintopology.c
+++ b/lightningd/chaintopology.c
@@ -308,6 +308,9 @@ static const char *feerate_name(enum feerate feerate)
 		: feerate == FEERATE_NORMAL ? "Normal" : "Slow";
 }
 
+/* Mutual recursion via timer. */
+static void next_updatefee_timer(struct chain_topology *topo);
+
 /* We sanitize feerates if necessary to put them in descending order. */
 static void update_feerates(struct bitcoind *bitcoind,
 			    const u32 *satoshi_per_kw,
@@ -340,12 +343,11 @@ static void update_feerates(struct bitcoind *bitcoind,
 
 	if (changed)
 		notify_feerate_change(bitcoind->ld);
+
+	next_updatefee_timer(topo);
 }
 
-/* B is the new chain (linked by ->next); update topology */
-static void topology_changed(struct chain_topology *topo,
-			     struct block *prev,
-			     struct block *b)
+static void start_fee_estimate(struct chain_topology *topo)
 {
 	/* FEERATE_IMMEDIATE, FEERATE_NORMAL, FEERATE_SLOW */
 	const char *estmodes[] = { "CONSERVATIVE", "ECONOMICAL", "ECONOMICAL" };
@@ -353,6 +355,23 @@ static void topology_changed(struct chain_topology *topo,
 
 	BUILD_ASSERT(ARRAY_SIZE(blocks) == NUM_FEERATES);
 
+	/* Once per new block head, update fee estimates. */
+	bitcoind_estimate_fees(topo->bitcoind, blocks, estmodes, NUM_FEERATES,
+			       update_feerates, topo);
+}
+
+static void next_updatefee_timer(struct chain_topology *topo)
+{
+	/* This takes care of its own lifetime. */
+	notleak(new_reltimer(topo->timers, topo, topo->poll_time,
+			     start_fee_estimate, topo));
+}
+
+/* B is the new chain (linked by ->next); update topology */
+static void topology_changed(struct chain_topology *topo,
+			     struct block *prev,
+			     struct block *b)
+{
 	/* Eliminate any old chain. */
 	if (prev->next)
 		free_blocks(topo, prev->next);
@@ -369,10 +388,6 @@ static void topology_changed(struct chain_topology *topo,
 
 	/* Maybe need to rebroadcast. */
 	rebroadcast_txs(topo, NULL);
-
-	/* Once per new block head, update fee estimates. */
-	bitcoind_estimate_fees(topo->bitcoind, blocks, estmodes, NUM_FEERATES,
-			       update_feerates, topo);
 }
 
 static struct block *new_block(struct chain_topology *topo,
@@ -745,6 +760,9 @@ void setup_topology(struct chain_topology *topo,
 
 	tal_add_destructor(topo, destroy_outgoing_txs);
 
+	/* Begin fee estimation. */
+	start_fee_estimate(topo);
+
 	/* Once it gets topology, it calls io_break() and we return. */
 	io_loop(NULL, NULL);
 	assert(!topo->startup);