From 934fded0e8ecd042626854ae4891376ca3a5aee9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 21 Apr 2021 15:56:45 +0930 Subject: [PATCH] channeld: increment fee states properly. By iterating through them forward, we would often increment them more than once! Always print feestate transitions, which is how I worked this out. Changelog-Fixed: Protocol: handle complex feerate transitions correctly. Signed-off-by: Rusty Russell --- channeld/full_channel.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 44ceb61196f2..53d504d5c8c0 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -937,7 +937,6 @@ static bool fee_incstate(struct channel *channel, enum htlc_state hstate) { int preflags, postflags; - const int committed_f = HTLC_FLAG(sidechanged, HTLC_F_COMMITTED); preflags = htlc_state_flags(hstate); postflags = htlc_state_flags(hstate + 1); @@ -953,12 +952,11 @@ static bool fee_incstate(struct channel *channel, if (!inc_fee_state(channel->fee_states, hstate)) return false; - if (!(preflags & committed_f) && (postflags & committed_f)) - status_debug("Feerate: %s->%s %s now %u", - htlc_state_name(hstate), - htlc_state_name(hstate+1), - side_to_str(sidechanged), - *channel->fee_states->feerate[hstate+1]); + status_debug("Feerate: %s->%s %s now %u", + htlc_state_name(hstate), + htlc_state_name(hstate+1), + side_to_str(sidechanged), + *channel->fee_states->feerate[hstate+1]); return true; } @@ -973,7 +971,7 @@ static int change_htlcs(struct channel *channel, struct htlc_map_iter it; struct htlc *h; int cflags = 0; - size_t i; + int i; struct balance owed[NUM_SIDES]; for (i = 0; i < NUM_SIDES; i++) @@ -1005,8 +1003,8 @@ static int change_htlcs(struct channel *channel, } } - /* Update fees. */ - for (i = 0; i < n_hstates; i++) { + /* Update fees (do backwards, to avoid double-increment!). */ + for (i = n_hstates - 1; i >= 0; i--) { if (fee_incstate(channel, sidechanged, htlc_states[i])) cflags |= (htlc_state_flags(htlc_states[i]) ^ htlc_state_flags(htlc_states[i]+1));