Skip to content

Commit 5e6e3bc

Browse files
committed
Introduce new code changes properly handling this new behavior
- Do not remove channel immediately when peer_disconnect, instead removed it after some time if peer doesn't reconnect soon (handled in previous commit). - Do not mark per ok_to_remove if we have some OutboundV1Channels too. - Rebroadcast SendOpenChannel for outboundV1Channel when peer reconnects. - Also, update the relevant tests to account for the new behavior change
1 parent 5c14929 commit 5e6e3bc

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
874874
return false
875875
}
876876
self.channel_by_id.iter().filter(|(_, phase)| matches!(phase, ChannelPhase::Funded(_))).count() == 0
877+
&& self.channel_by_id.iter().filter(|(_, phase)| matches!(phase, ChannelPhase::UnfundedOutboundV1(_))).count() == 0
877878
&& self.monitor_update_blocked_actions.is_empty()
878879
&& self.in_flight_monitor_updates.is_empty()
879880
}
@@ -8734,10 +8735,12 @@ where
87348735
}
87358736
&mut chan.context
87368737
},
8737-
// Unfunded channels will always be removed.
8738-
ChannelPhase::UnfundedOutboundV1(chan) => {
8739-
&mut chan.context
8738+
// We retain UnfundedOutboundV1 channel for some time in case
8739+
// peer unexpectedly disconnects, and intends to reconnect again.
8740+
ChannelPhase::UnfundedOutboundV1(_) => {
8741+
return true;
87408742
},
8743+
// Unfunded inbound channels will always be removed.
87418744
ChannelPhase::UnfundedInboundV1(chan) => {
87428745
&mut chan.context
87438746
},
@@ -8877,21 +8880,27 @@ where
88778880
let peer_state = &mut *peer_state_lock;
88788881
let pending_msg_events = &mut peer_state.pending_msg_events;
88798882

8880-
peer_state.channel_by_id.iter_mut().filter_map(|(_, phase)|
8881-
if let ChannelPhase::Funded(chan) = phase { Some(chan) } else {
8882-
// Since unfunded channel maps are cleared upon disconnecting a peer, and they're not persisted
8883-
// (so won't be recovered after a crash), they shouldn't exist here and we would never need to
8884-
// worry about closing and removing them.
8883+
for (_, phase) in peer_state.channel_by_id.iter_mut() {
8884+
if let ChannelPhase::Funded(chan) = phase {
8885+
let logger = WithChannelContext::from(&self.logger, &chan.context);
8886+
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
8887+
node_id: chan.context.get_counterparty_node_id(),
8888+
msg: chan.get_channel_reestablish(&&logger),
8889+
});
8890+
}
8891+
else if let ChannelPhase::UnfundedOutboundV1(chan) = phase {
8892+
pending_msg_events.push(events::MessageSendEvent::SendOpenChannel {
8893+
node_id: chan.context.get_counterparty_node_id(),
8894+
msg: chan.get_open_channel(self.chain_hash),
8895+
});
8896+
}
8897+
else {
8898+
// Since unfunded inbound channel maps are cleared upon disconnecting a peer, and they're not
8899+
// persisted (so won't be recovered after a crash), they shouldn't exist here and we would never
8900+
// need to worry about closing and removing them.
88858901
debug_assert!(false);
8886-
None
88878902
}
8888-
).for_each(|chan| {
8889-
let logger = WithChannelContext::from(&self.logger, &chan.context);
8890-
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
8891-
node_id: chan.context.get_counterparty_node_id(),
8892-
msg: chan.get_channel_reestablish(&&logger),
8893-
});
8894-
});
8903+
}
88958904
}
88968905

88978906
return NotifyOption::SkipPersistHandleEvents;

lightning/src/ln/functional_tests.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,7 +3692,7 @@ fn test_dup_events_on_peer_disconnect() {
36923692
#[test]
36933693
fn test_peer_disconnected_before_funding_broadcasted() {
36943694
// Test that channels are closed with `ClosureReason::DisconnectedPeer` if the peer disconnects
3695-
// before the funding transaction has been broadcasted.
3695+
// before the funding transaction has been broadcasted, and doesn't reconnect back within time.
36963696
let chanmon_cfgs = create_chanmon_cfgs(2);
36973697
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
36983698
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -3721,11 +3721,15 @@ fn test_peer_disconnected_before_funding_broadcasted() {
37213721
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
37223722
}
37233723

3724-
// Ensure that the channel is closed with `ClosureReason::DisconnectedPeer` when the peers are
3725-
// disconnected before the funding transaction was broadcasted.
3724+
// Ensure that the channel is closed after timeout with `ClosureReason::DisconnectedPeer`
3725+
// when the peers are disconnected before the funding transaction was broadcasted.
37263726
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
37273727
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
37283728

3729+
// The timer ticks while waiting for peer to connect back
3730+
nodes[0].node.timer_tick_occurred();
3731+
nodes[0].node.timer_tick_occurred();
3732+
37293733
check_closed_event!(&nodes[0], 2, ClosureReason::DisconnectedPeer, true
37303734
, [nodes[1].node.get_our_node_id()], 1000000);
37313735
check_closed_event!(&nodes[1], 1, ClosureReason::DisconnectedPeer, false
@@ -10526,6 +10530,10 @@ fn test_disconnect_in_funding_batch() {
1052610530
// The remaining peer in the batch disconnects.
1052710531
nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());
1052810532

10533+
// After the time expires for allowing peer to connect back
10534+
nodes[0].node.timer_tick_occurred();
10535+
nodes[0].node.timer_tick_occurred();
10536+
1052910537
// The channels in the batch will close immediately.
1053010538
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
1053110539
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();

0 commit comments

Comments
 (0)