Skip to content

Commit

Permalink
channeld: increment fee states properly.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell committed Apr 24, 2021
1 parent e64e6ba commit 934fded
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

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

0 comments on commit 934fded

Please sign in to comment.