Skip to content

Commit

Permalink
chaintopology: get fees using a timer, not on each block.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Dec 21, 2017
1 parent 985a0b4 commit 6f6d7a5
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -340,19 +343,35 @@ 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" };
const u32 blocks[] = { 2, 4, 100 };

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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6f6d7a5

Please sign in to comment.