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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-04-21 15:56:45 +09:30
parent e64e6ba283
commit 934fded0e8

View File

@@ -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));