Skip to content

Commit 2148262

Browse files
committed
event: drop peer on counterparty or on-chain close
When the counterparty drives closure or a commitment transaction confirms on chain, no further channel state can be recovered with the peer. Retaining them in the peer store would only spin the reconnection task forever. Remove the peer once their last channel with us reaches one of these terminal states: - `CounterpartyForceClosed` - `CounterpartyInitiatedCooperativeClosure` - `CommitmentTxConfirmed` `CommitmentTxConfirmed` covers the case where a remote commitment confirms while we are disconnected; LDK may also report our own commitment confirming under the same variant, but the channel is gone on chain in either case. `HolderForceClosed` is intentionally excluded so the reconnection loop can keep driving `channel_reestablish` recovery (handled in `Node::close_channel_internal`). `counterparty_node_id` is unwrapped with `expect` since LDK has always populated it on `ChannelClosed` from 0.0.117 onward, well before the minimum version this crate targets. If peer removal fails we now return `ReplayEvent` so the event is retried instead of being silently dropped.
1 parent 8647a56 commit 2148262

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/event.rs‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,49 @@ where
16091609
} => {
16101610
log_info!(self.logger, "Channel {} closed due to: {}", channel_id, reason);
16111611

1612+
// `counterparty_node_id` has been set on every `ChannelClosed` since LDK 0.0.117.
1613+
let counterparty_node_id = counterparty_node_id
1614+
.expect("counterparty_node_id is always set since LDK 0.0.117");
1615+
1616+
// Drop the peer once their last channel with us has reached a terminal
1617+
// state reconnection cannot recover. `CommitmentTxConfirmed` is included
1618+
// because LDK reports a remote (or our own) commitment confirming on-chain
1619+
// via this variant, leaving nothing for `channel_reestablish` to recover.
1620+
// `HolderForceClosed` is deliberately excluded so reconnection can still
1621+
// drive recovery (see `Node::close_channel_internal`).
1622+
// We exclude `channel_id` from the count because LDK emits `ChannelClosed`
1623+
// before removing it from its internal list.
1624+
let reconnect_unneeded = matches!(
1625+
reason,
1626+
ClosureReason::CounterpartyForceClosed { .. }
1627+
| ClosureReason::CounterpartyInitiatedCooperativeClosure
1628+
| ClosureReason::CommitmentTxConfirmed
1629+
);
1630+
1631+
if reconnect_unneeded {
1632+
let has_other_channels = self
1633+
.channel_manager
1634+
.list_channels_with_counterparty(&counterparty_node_id)
1635+
.iter()
1636+
.any(|c| c.channel_id != channel_id);
1637+
1638+
if !has_other_channels {
1639+
if let Err(e) = self.peer_store.remove_peer(&counterparty_node_id) {
1640+
log_error!(
1641+
self.logger,
1642+
"Failed to remove peer {} from peer store: {}",
1643+
counterparty_node_id,
1644+
e
1645+
);
1646+
return Err(ReplayEvent());
1647+
}
1648+
}
1649+
}
1650+
16121651
let event = Event::ChannelClosed {
16131652
channel_id,
16141653
user_channel_id: UserChannelId(user_channel_id),
1615-
counterparty_node_id,
1654+
counterparty_node_id: Some(counterparty_node_id),
16161655
reason: Some(reason),
16171656
};
16181657

0 commit comments

Comments
 (0)