Skip to content

Commit fed62bb

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 5e6e3bc commit fed62bb

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

lightning/src/ln/channelmanager.rs

+83
Original file line numberDiff line numberDiff line change
@@ -11441,6 +11441,89 @@ mod tests {
1144111441
}
1144211442
}
1144311443

11444+
#[test]
11445+
fn test_channel_close_when_not_timely_accepted() {
11446+
// Create network of two nodes
11447+
let chanmon_cfgs = create_chanmon_cfgs(2);
11448+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11449+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
11450+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11451+
11452+
// Simulate peer-diconnects mid-handshake
11453+
// The channel is initiated from the node 0 side,
11454+
// But the nodes disconnects before node 1 could send accept channel
11455+
let create_chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
11456+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11457+
assert_eq!(open_channel_msg.temporary_channel_id, create_chan_id);
11458+
11459+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
11460+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11461+
11462+
{
11463+
// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
11464+
let node_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
11465+
let per_peer_state = node_0_per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
11466+
assert_eq!(per_peer_state.channel_by_id.len(), 1);
11467+
}
11468+
11469+
// In the meantime, two timer tick passed
11470+
nodes[0].node.timer_tick_occurred();
11471+
nodes[0].node.timer_tick_occurred();
11472+
11473+
// Since we disconnected from peer and did not connect back within time
11474+
// We should have forced-closed the channel by now, with "ClosureReason::DisconnectedPeer"
11475+
// as the event.
11476+
check_closed_event!(nodes[0], 1, ClosureReason::DisconnectedPeer, [nodes[1].node.get_our_node_id()], 100000);
11477+
11478+
{
11479+
// Since accept channel message was never received
11480+
// The channel should be forced close by now from node 0 side
11481+
// and the peer removed from per_peer_state
11482+
let node_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
11483+
assert_eq!(node_0_per_peer_state.len(), 0);
11484+
}
11485+
11486+
}
11487+
11488+
#[test]
11489+
fn test_rebroadcast_open_channel_when_reconnect_mid_handshake() {
11490+
// Create network of two nodes
11491+
let chanmon_cfgs = create_chanmon_cfgs(2);
11492+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11493+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
11494+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11495+
11496+
// Simulate peer-diconnects mid-handshake
11497+
// The channel is initiated from the node 0 side,
11498+
// But the nodes disconnects before node 1 could send accept channel
11499+
let create_chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
11500+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
11501+
assert_eq!(open_channel_msg.temporary_channel_id, create_chan_id);
11502+
11503+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
11504+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11505+
11506+
{
11507+
// Make sure that we have not removed the OutboundV1Channel from node[0] immediately.
11508+
let node_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
11509+
let per_peer_state = node_0_per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
11510+
assert_eq!(per_peer_state.channel_by_id.len(), 1);
11511+
}
11512+
11513+
// The peers now reconnect
11514+
nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
11515+
features: nodes[1].node.init_features(), networks: None, remote_network_address: None
11516+
}, true).unwrap();
11517+
nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11518+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11519+
}, false).unwrap();
11520+
11521+
// Make sure the SendOpenChannel message is added to
11522+
// node_0 pending message events
11523+
let events = nodes[0].node.get_and_clear_pending_msg_events();
11524+
assert_eq!(events.len(), 1);
11525+
}
11526+
1144411527
#[test]
1144511528
fn test_drop_disconnected_peers_when_removing_channels() {
1144611529
let chanmon_cfgs = create_chanmon_cfgs(2);

0 commit comments

Comments
 (0)