Skip to content

Commit 7811ea1

Browse files
committed
channeld: tweak function to allow testing for pending *uncommitted* changes.
For quiescence, we can't have sent any updates at all. But for upgrades on reconnection, we may have already added uncommitted HTLCs for retransmission, but they don't count towards "are we quiesced" since they're not sent yet. Signed-off-by: Rusty Russell <[email protected]>
1 parent 9e8b78f commit 7811ea1

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

channeld/channeld.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static void maybe_send_stfu(struct peer *peer)
290290
if (!peer->stfu)
291291
return;
292292

293-
if (!peer->stfu_sent[LOCAL] && !pending_updates(peer->channel, LOCAL)) {
293+
if (!peer->stfu_sent[LOCAL] && !pending_updates(peer->channel, LOCAL, false)) {
294294
u8 *msg = towire_stfu(NULL, &peer->channel_id,
295295
peer->stfu_initiator == LOCAL);
296296
sync_crypto_write(peer->pps, take(msg));
@@ -323,7 +323,7 @@ static void handle_stfu(struct peer *peer, const u8 *stfu)
323323
}
324324

325325
/* Sanity check */
326-
if (pending_updates(peer->channel, REMOTE))
326+
if (pending_updates(peer->channel, REMOTE, false))
327327
peer_failed_warn(peer->pps, &peer->channel_id,
328328
"STFU but you still have updates pending?");
329329

@@ -1141,7 +1141,7 @@ static void send_commit(struct peer *peer)
11411141
/* FIXME: We occasionally desynchronize with LND here, so
11421142
* don't stress things by having more than one feerate change
11431143
* in-flight! */
1144-
if (feerate_changes_done(peer->channel->fee_states)) {
1144+
if (feerate_changes_done(peer->channel->fee_states, false)) {
11451145
u8 *msg;
11461146

11471147
if (!channel_update_feerate(peer->channel, feerate_target))

channeld/full_channel.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,26 +1252,38 @@ static bool adjust_balance(struct balance view_owed[NUM_SIDES][NUM_SIDES],
12521252
return true;
12531253
}
12541254

1255-
bool pending_updates(const struct channel *channel, enum side side)
1255+
bool pending_updates(const struct channel *channel,
1256+
enum side side,
1257+
bool uncommitted_ok)
12561258
{
12571259
struct htlc_map_iter it;
12581260
const struct htlc *htlc;
12591261

12601262
/* Initiator might have fee changes in play. */
12611263
if (side == channel->opener) {
1262-
if (!feerate_changes_done(channel->fee_states))
1264+
if (!feerate_changes_done(channel->fee_states, uncommitted_ok))
12631265
return true;
12641266
}
12651267

12661268
for (htlc = htlc_map_first(channel->htlcs, &it);
12671269
htlc;
12681270
htlc = htlc_map_next(channel->htlcs, &it)) {
1269-
/* If it's still being added, it's owner added it. */
1270-
if (htlc_state_flags(htlc->state) & HTLC_ADDING) {
1271+
int flags = htlc_state_flags(htlc->state);
1272+
1273+
/* If it's still being added, its owner added it. */
1274+
if (flags & HTLC_ADDING) {
1275+
/* It might be OK if it's added, but not committed */
1276+
if (uncommitted_ok
1277+
&& (flags & HTLC_FLAG(!side, HTLC_F_PENDING)))
1278+
continue;
12711279
if (htlc_owner(htlc) == side)
12721280
return true;
12731281
/* If it's being removed, non-owner removed it */
12741282
} else if (htlc_state_flags(htlc->state) & HTLC_REMOVING) {
1283+
/* It might be OK if it's removed, but not committed */
1284+
if (uncommitted_ok
1285+
&& (flags & HTLC_FLAG(!side, HTLC_F_PENDING)))
1286+
continue;
12751287
if (htlc_owner(htlc) != side)
12761288
return true;
12771289
}

channeld/full_channel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ void dump_htlcs(const struct channel *channel, const char *prefix);
258258
* pending_updates: does this side have updates pending in channel?
259259
* @channel: the channel
260260
* @side: the side who is offering or failing/fulfilling HTLC, or feechange
261+
* @uncommitted_ok: don't count uncommitted changes.
261262
*/
262-
bool pending_updates(const struct channel *channel, enum side side);
263+
bool pending_updates(const struct channel *channel, enum side side,
264+
bool uncommitted_ok);
263265

264266
const char *channel_add_err_name(enum channel_add_err e);
265267
const char *channel_remove_err_name(enum channel_remove_err e);

common/fee_states.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ u32 get_feerate(const struct fee_states *fee_states,
7474
}
7575

7676
/* Are feerates all agreed by both sides? */
77-
bool feerate_changes_done(const struct fee_states *fee_states)
77+
bool feerate_changes_done(const struct fee_states *fee_states,
78+
bool ignore_uncommitted)
7879
{
7980
size_t num_feerates = 0;
8081
for (size_t i = 0; i < ARRAY_SIZE(fee_states->feerate); i++) {
82+
if (ignore_uncommitted
83+
&& (i == RCVD_ADD_HTLC || i == SENT_ADD_HTLC))
84+
continue;
8185
num_feerates += (fee_states->feerate[i] != NULL);
8286
}
8387
return num_feerates == 1;

common/fee_states.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ struct fee_states *fromwire_fee_states(const tal_t *ctx,
8686
*/
8787
bool fee_states_valid(const struct fee_states *fee_states, enum side opener);
8888

89-
/* Are therre no more fee changes in-flight? */
90-
bool feerate_changes_done(const struct fee_states *fee_states);
89+
/* Are there no more fee changes in-flight? */
90+
bool feerate_changes_done(const struct fee_states *fee_states,
91+
bool ignore_uncommitted);
9192

9293
#endif /* LIGHTNING_COMMON_FEE_STATES_H */

0 commit comments

Comments
 (0)