Skip to content

Commit f04fa4d

Browse files
authored
Merge pull request #895 from Jolah1/peer-handling-on-force-close
Fix peer store management on channel closure
2 parents 4479a62 + 5d4afa3 commit f04fa4d

4 files changed

Lines changed: 107 additions & 5 deletions

File tree

‎src/event.rs‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,45 @@ where
16141614
} => {
16151615
log_info!(self.logger, "Channel {} closed due to: {}", channel_id, reason);
16161616

1617+
// `counterparty_node_id` has been set on every `ChannelClosed` since LDK 0.0.117.
1618+
let counterparty_node_id = counterparty_node_id
1619+
.expect("counterparty_node_id is always set since LDK 0.0.117");
1620+
1621+
// Drop the peer once its last channel with us has reached a terminal state
1622+
// that reconnection cannot recover. Every closure reason is terminal except
1623+
// `HolderForceClosed`: when *we* force-close, we keep reconnecting so that
1624+
// `channel_reestablish` can drive recovery (see `Node::close_channel_internal`).
1625+
// This also cleans up peers persisted for a channel that closed before funding
1626+
// (e.g. `CounterpartyCoopClosedUnfundedChannel`), which would otherwise be
1627+
// retried forever.
1628+
// We exclude `channel_id` from the count because LDK emits `ChannelClosed`
1629+
// before removing it from its internal list.
1630+
let dont_reconnect = !matches!(reason, ClosureReason::HolderForceClosed { .. });
1631+
1632+
if dont_reconnect {
1633+
let has_other_channels = self
1634+
.channel_manager
1635+
.list_channels_with_counterparty(&counterparty_node_id)
1636+
.iter()
1637+
.any(|c| c.channel_id != channel_id);
1638+
1639+
if !has_other_channels {
1640+
if let Err(e) = self.peer_store.remove_peer(&counterparty_node_id).await {
1641+
log_error!(
1642+
self.logger,
1643+
"Failed to remove peer {} from peer store: {}",
1644+
counterparty_node_id,
1645+
e
1646+
);
1647+
return Err(ReplayEvent());
1648+
}
1649+
}
1650+
}
1651+
16171652
let event = Event::ChannelClosed {
16181653
channel_id,
16191654
user_channel_id: UserChannelId(user_channel_id),
1620-
counterparty_node_id,
1655+
counterparty_node_id: Some(counterparty_node_id),
16211656
reason: Some(reason),
16221657
};
16231658

‎src/lib.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,10 +2010,13 @@ impl Node {
20102010
})?;
20112011
}
20122012

2013-
// Check if this was the last open channel, if so, forget the peer.
2014-
if open_channels.len() == 1 {
2015-
self.runtime.block_on(self.peer_store.remove_peer(&counterparty_node_id))?;
2016-
}
2013+
// Peer store cleanup is handled centrally in the `ChannelClosed` event handler,
2014+
// which drops the peer once its last channel reaches a terminal state that
2015+
// reconnection cannot recover. We intentionally do nothing here so that a
2016+
// force-closed peer is retained, letting the background reconnection task keep
2017+
// firing and drive the `channel_reestablish` recovery flow. This is especially
2018+
// important against LND peers, which don't always handle force-closure error
2019+
// messages correctly.
20172020
}
20182021

20192022
Ok(())

‎tests/common/mod.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,20 @@ pub(crate) async fn do_channel_full_cycle<E: ElectrumApi>(
16101610
assert!(node_b.list_balances().pending_balances_from_channel_closures.is_empty());
16111611
}
16121612

1613+
if force_close {
1614+
// Peer retained after local force-close to allow channel_reestablish recovery.
1615+
assert!(
1616+
node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
1617+
"node_b should remain persisted in node_a peer store after locally-initiated force-close"
1618+
);
1619+
} else {
1620+
// Peer removed after cooperative close — no further reason to reconnect.
1621+
assert!(
1622+
!node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
1623+
"node_b should be removed from node_a peer store after cooperative close"
1624+
);
1625+
}
1626+
16131627
let sum_of_all_payments_sat = (push_msat
16141628
+ invoice_amount_1_msat
16151629
+ overpaid_amount_msat

‎tests/integration_tests_rust.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,56 @@ async fn channel_full_cycle_force_close_trusted_no_reserve() {
127127
.await;
128128
}
129129

130+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
131+
async fn peer_removed_when_counterparty_force_closes_last_channel() {
132+
// When we open a channel outbound, we persist the counterparty so the background
133+
// reconnection task can reach them. If the counterparty then force-closes what turns out
134+
// to be their last channel with us, the channel is terminal and there is nothing left for
135+
// `channel_reestablish` to recover, so the peer should be dropped from the store rather
136+
// than reconnected to forever.
137+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
138+
let chain_source = random_chain_source(&bitcoind, &electrsd);
139+
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
140+
141+
let address_a = node_a.onchain_payment().new_address().unwrap();
142+
let premine_amount_sat = 5_000_000;
143+
premine_and_distribute_funds(
144+
&bitcoind.client,
145+
&electrsd.client,
146+
vec![address_a],
147+
Amount::from_sat(premine_amount_sat),
148+
)
149+
.await;
150+
node_a.sync_wallets().unwrap();
151+
152+
// node_a opens the channel, so node_a persists node_b in its peer store.
153+
open_channel(&node_a, &node_b, 4_000_000, false, &electrsd).await;
154+
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6).await;
155+
node_a.sync_wallets().unwrap();
156+
node_b.sync_wallets().unwrap();
157+
158+
let _user_channel_id_a = expect_channel_ready_event!(node_a, node_b.node_id());
159+
let user_channel_id_b = expect_channel_ready_event!(node_b, node_a.node_id());
160+
161+
assert!(
162+
node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
163+
"node_a should persist node_b after opening a channel to it"
164+
);
165+
166+
// The counterparty force-closes their last channel with us.
167+
node_b.force_close_channel(&user_channel_id_b, node_a.node_id(), None).unwrap();
168+
169+
expect_event!(node_a, ChannelClosed);
170+
expect_event!(node_b, ChannelClosed);
171+
172+
// node_a should have dropped node_b from its peer store. We assert on `is_persisted` rather
173+
// than peer presence so a lingering transient TCP connection doesn't mask the removal.
174+
assert!(
175+
!node_a.list_peers().iter().any(|p| p.node_id == node_b.node_id() && p.is_persisted),
176+
"node_a should drop node_b from its peer store after node_b force-closed the last channel"
177+
);
178+
}
179+
130180
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
131181
async fn channel_full_cycle_0conf() {
132182
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();

0 commit comments

Comments
 (0)