Skip to content

Commit d62d9bc

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 6d0ed09 commit d62d9bc

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11437,6 +11437,87 @@ mod tests {
1143711437
}
1143811438
}
1143911439

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

0 commit comments

Comments
 (0)