From fe405f49beba3686d324aae0e46ec61e77e9621c Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 30 Jun 2018 14:42:09 +0200 Subject: [PATCH] bitcoind: Smooth fee changes over a number of estimates Implements an EWMA for the fee estimation. Achieves 90% influence of the newer fee after 5 minutes, and adjusts to the polling rate that is configured. --- lightningd/chaintopology.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 6e16c6a65..bd7f0cd80 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -237,6 +237,8 @@ static void update_feerates(struct bitcoind *bitcoind, { u32 old_feerates[NUM_FEERATES]; bool changed = false; + /* Rate of change of the fee smoothing, depending on the poll-interval */ + double change_rate = (double)topo->poll_seconds / 150 * 0.9; for (size_t i = 0; i < NUM_FEERATES; i++) { u32 feerate = satoshi_per_kw[i]; @@ -248,7 +250,14 @@ static void update_feerates(struct bitcoind *bitcoind, if (!feerate) continue; - if (feerate < feerate_floor()) + /* Smooth the feerate to avoid spikes. The goal is to have the + * fee consist of 0.9 * feerate + 0.1 * old_feerate after 300 + * seconds. The following will do that in a polling interval + * independent manner. */ + feerate = + feerate * (1 - change_rate) + old_feerates[i] * change_rate; + + if (feerate < feerate_floor()) feerate = feerate_floor(); if (feerate != topo->feerate[i]) {