Skip to content

Commit 231a0bd

Browse files
committed
Add tests to check the introduced behaviour
- The first test make sure that the OutboundV1Channel is not immediately removed when peers disconnect, but is removed after N timer ticks. - The second test makes sure that the SendOpenChannel is rebroadcasted for the OutboundV1Channel if peer reconnects within time.
1 parent 7aa38e3 commit 231a0bd

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lightning/src/ln/channelmanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11146,7 +11146,7 @@ mod tests {
1114611146
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1114711147
use core::sync::atomic::Ordering;
1114811148
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
11149-
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
11149+
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1115011150
use crate::ln::ChannelId;
1115111151
use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
1115211152
use crate::ln::functional_test_utils::*;

lightning/src/ln/functional_tests.rs

+92
Original file line numberDiff line numberDiff line change
@@ -10511,6 +10511,98 @@ fn test_remove_expired_inbound_unfunded_channels() {
1051110511
check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false, &[nodes[0].node.get_our_node_id()], 100000);
1051210512
}
1051310513

10514+
#[test]
10515+
fn test_channel_close_when_not_timely_accepted() {
10516+
// Create network of two nodes
10517+
let chanmon_cfgs = create_chanmon_cfgs(2);
10518+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10519+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10520+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10521+
10522+
// Simulate peer-diconnects mid-handshake
10523+
// The channel is initiated from the node 0 side,
10524+
// But the nodes disconnects before node 1 could send accept channel
10525+
let create_chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
10526+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10527+
assert_eq!(open_channel_msg.temporary_channel_id, create_chan_id);
10528+
10529+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10530+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10531+
10532+
// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
10533+
assert_eq!(nodes[0].node.list_channels().len(), 1);
10534+
10535+
{
10536+
// Since channel was inbound from node[1] perspective, it should have been immediately dropped.
10537+
let node_1_per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10538+
let per_peer_state = node_1_per_peer_state.get(&nodes[0].node.get_our_node_id());
10539+
assert!(per_peer_state.is_none());
10540+
}
10541+
10542+
// In the meantime, some time passes.
10543+
for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS {
10544+
nodes[0].node.timer_tick_occurred();
10545+
}
10546+
10547+
// Since we disconnected from peer and did not connect back within time
10548+
// We should have forced-closed the channel by now.
10549+
check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
10550+
10551+
{
10552+
// Since accept channel message was never received
10553+
// The channel should be forced close by now from node 0 side
10554+
// and the peer removed from per_peer_state
10555+
let node_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
10556+
assert_eq!(node_0_per_peer_state.len(), 0);
10557+
}
10558+
10559+
}
10560+
10561+
#[test]
10562+
fn test_rebroadcast_open_channel_when_reconnect_mid_handshake() {
10563+
// Create network of two nodes
10564+
let chanmon_cfgs = create_chanmon_cfgs(2);
10565+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10566+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10567+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10568+
10569+
// Simulate peer-diconnects mid-handshake
10570+
// The channel is initiated from the node 0 side,
10571+
// But the nodes disconnects before node 1 could send accept channel
10572+
let create_chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
10573+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10574+
assert_eq!(open_channel_msg.temporary_channel_id, create_chan_id);
10575+
10576+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10577+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10578+
10579+
// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
10580+
assert_eq!(nodes[0].node.list_channels().len(), 1);
10581+
10582+
{
10583+
// Since channel was inbound from node[1] perspective, it should have been immediately dropped.
10584+
let node_1_per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10585+
let per_peer_state = node_1_per_peer_state.get(&nodes[0].node.get_our_node_id());
10586+
assert!(per_peer_state.is_none());
10587+
}
10588+
10589+
// The peers now reconnect
10590+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
10591+
features: nodes[1].node.init_features(), networks: None, remote_network_address: None
10592+
}, true).unwrap();
10593+
nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
10594+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
10595+
}, false).unwrap();
10596+
10597+
// Make sure the SendOpenChannel message is added to node_0 pending message events
10598+
let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
10599+
assert_eq!(msg_events.len(), 1);
10600+
match msg_events[0] {
10601+
MessageSendEvent::SendOpenChannel { .. } => (),
10602+
_ => panic!("Unexpected message"),
10603+
}
10604+
}
10605+
1051410606
fn do_test_multi_post_event_actions(do_reload: bool) {
1051510607
// Tests handling multiple post-Event actions at once.
1051610608
// There is specific code in ChannelManager to handle channels where multiple post-Event

0 commit comments

Comments
 (0)