From ee5f370e1152290c2fc3d96958b1d46f4dbabff0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 11 Feb 2024 11:14:34 +0100 Subject: [PATCH] channeld: Adjust the feerate security margin profile The feerate security margin is a multiplicative factor applied to the feerate of some transactions in order to guarantee that the transaction remains publishable and has a sufficient chance of being confirmed, that we can base some of our decisions on that. The multiplicative factor is >=1 and was so far a constant 2. This might have been sensible in the low-fee environment, where the fees are expected to oscillate, and almost guaranteeing that we will eventually have rising feerates but in high-fee environments that is no longer the case, and the 100% margin that the multiplicator 2 brings is excessive. We therefore opt to start out with 100%, then linearly interpolate up to a given maxfeerate (which does not have to be a real feerate ever reached, it just indicates the feerate after which we apply the constant 10% margin. Fixes #6974 Closes #6976 --- channeld/full_channel.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 97a7ccc823e1..7b12f80d0692 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -560,9 +560,29 @@ static bool local_opener_has_fee_headroom(const struct channel *channel, option_anchors_zero_fee_htlc_tx, committed, adding, removing); + /* Scale the feerate up by a margin. This ensures that we have + * some leeway even in rising fees. The calculation starts + * with a 100% margin at very low fees, since they are likely + * to rise, and can do so quickly, whereas on the higher fee + * side, asking for a 100% margin is excessive, so ask for a + * 10% margin. In-between these two regions we interpolate + * linearly. Notice that minfeerate and maxfeerate are just + * the markers of the linear interpolation, they don't have + * to correspond to actual feerates seen in the network. + * + * See [CLN6974] for details and discussion. + * + * [CLN6974]: https://github.com/ElementsProject/lightning/issues/6974 + */ + u64 minfeerate = 253, maxfeerate = 45000, + min = feerate - minfeerate > maxfeerate ? maxfeerate + : feerate - minfeerate; + double marginperc = 1 - min / (maxfeerate * 1.1); + u64 marginrate = 1 + marginperc; + /* Now, how much would it cost us if feerate increases 100% and we added * another HTLC? */ - fee = commit_tx_base_fee(2 * feerate, untrimmed + 1, + fee = commit_tx_base_fee(marginrate, untrimmed + 1, option_anchor_outputs, option_anchors_zero_fee_htlc_tx); if (amount_msat_greater_eq_sat(remainder, fee))