Skip to content

Commit b0278d7

Browse files
committed
peer: fix maximum htlc value when we disconnect.
If we send an HTLC #1, then get disconnected before a confirm, we will forget it. But we've incremented peer->htlc_id_counter, so when we offer it again we'll make it HTLC #2, which is non-consecutive. To make this clear, we always start htlc ids at 0 now. That revealed the bugs handled in the previous patch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 555a753 commit b0278d7

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

daemon/peer.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,15 @@ static void forget_uncommitted_changes(struct peer *peer)
19321932
if (!cstate_add_htlc(peer->remote.staging_cstate, h))
19331933
fatal("Could not add HTLC?");
19341934
break;
1935-
} /* Fall thru */
1935+
}
1936+
/* Adjust counter to lowest HTLC removed */
1937+
if (peer->htlc_id_counter > h->id) {
1938+
log_debug(peer->log,
1939+
"Lowering htlc_id_counter to %"PRIu64,
1940+
h->id);
1941+
peer->htlc_id_counter = h->id;
1942+
}
1943+
/* Fall thru */
19361944
case RCVD_ADD_HTLC:
19371945
log_debug(peer->log, "Forgetting %s %"PRIu64,
19381946
htlc_state_name(h->state), h->id);
@@ -1975,6 +1983,11 @@ static void forget_uncommitted_changes(struct peer *peer)
19751983
= tal_free(peer->feechanges[SENT_FEECHANGE]);
19761984
peer->feechanges[RCVD_FEECHANGE]
19771985
= tal_free(peer->feechanges[RCVD_FEECHANGE]);
1986+
1987+
/* Make sure our HTLC counter is correct. */
1988+
if (peer->htlc_id_counter != 0)
1989+
assert(htlc_get(&peer->htlcs, peer->htlc_id_counter-1, LOCAL));
1990+
assert(!htlc_get(&peer->htlcs, peer->htlc_id_counter, LOCAL));
19781991
}
19791992

19801993
static void retransmit_pkts(struct peer *peer, s64 ack)
@@ -2417,9 +2430,7 @@ static bool peer_first_connected(struct peer *peer,
24172430
peer->id = tal_dup(peer, struct pubkey, id);
24182431
peer->local.commit_fee_rate = desired_commit_feerate(peer->dstate);
24192432

2420-
/* Make it different from other node (to catch bugs!), but a
2421-
* round number for simple eyeballing. */
2422-
peer->htlc_id_counter = pseudorand(1ULL << 32) * 1000;
2433+
peer->htlc_id_counter = 0;
24232434

24242435
/* If we free peer, conn should be closed, but can't be freed
24252436
* immediately so don't make peer a parent. */

0 commit comments

Comments
 (0)