Skip to content

Commit 71b2c1e

Browse files
committed
test: Add test coverage
1 parent 989fa7e commit 71b2c1e

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lightning/src/ln/channelmanager.rs

+72
Original file line numberDiff line numberDiff line change
@@ -11353,6 +11353,78 @@ mod tests {
1135311353
}
1135411354
}
1135511355

11356+
fn do_test_peer_disconnect_just_before_create_channel(reconnect_on_time: bool) {
11357+
let chanmon_cfgs = create_chanmon_cfgs(2);
11358+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
11359+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
11360+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
11361+
11362+
// Create a channel to prevent removal of peer when disconnected.
11363+
let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
11364+
11365+
// Disconnect Peers
11366+
nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
11367+
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
11368+
11369+
// Call create_channel from node_0 to node_1
11370+
let _chan_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
11371+
11372+
// Accquire the peer state from node_0 perspective
11373+
let nodes_0_per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
11374+
let per_state_mutex = nodes_0_per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap();
11375+
let peer_state = per_state_mutex.lock().unwrap();
11376+
11377+
// Check that SendOpenChannel message is not added to pending_msg_events
11378+
assert_eq!(peer_state.pending_msg_events.len(), 0);
11379+
11380+
// Check that the number of channels from node_0 to node_1 is two: from previously created channel and the unsend channel
11381+
assert_eq!(peer_state.channel_by_id.len(), 2);
11382+
11383+
// Simulate not reconnected on time by ticking the timer
11384+
if !reconnect_on_time {
11385+
// Run timer twice
11386+
nodes[0].node.timer_tick_occurred();
11387+
nodes[0].node.timer_tick_occurred();
11388+
}
11389+
// If not reconnected on time..
11390+
if !reconnect_on_time {
11391+
// the pending_msg_events should not contain SendOpenChannel msg
11392+
assert_eq!(peer_state.pending_msg_events.len(), 0);
11393+
// And the number of channels from node_0 perspective should be reduced to 1
11394+
assert_eq!(peer_state.channel_by_id.len(), 1);
11395+
}
11396+
11397+
// TODO: Make the test part for reconnect work.
11398+
// // Now Reconnect
11399+
// {
11400+
// nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
11401+
// features: nodes[1].node.init_features(), networks: None, remote_network_address: None
11402+
// }, true).unwrap();
11403+
// let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
11404+
// assert_eq!(reestablish_1.len(), 1);
11405+
// nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
11406+
// features: nodes[0].node.init_features(), networks: None, remote_network_address: None
11407+
// }, false).unwrap();
11408+
// let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
11409+
// assert_eq!(reestablish_2.len(), 1);
11410+
// }
11411+
11412+
// // If reconnected on time..
11413+
// if reconnect_on_time {
11414+
// // the pending_msg_events should contain SendOpenChannel msg
11415+
// assert_eq!(peer_state.pending_msg_events.len(), 1);
11416+
// // the number of channels from node_0 perspective should be 2
11417+
// assert_eq!(peer_state.channel_by_id.len(), 2);
11418+
// }
11419+
11420+
}
11421+
11422+
#[test]
11423+
fn test_peer_disconnect_just_before_create_channel() {
11424+
// do_test_peer_disconnect_just_before_create_channel(false);
11425+
do_test_peer_disconnect_just_before_create_channel(true);
11426+
}
11427+
1135611428
#[test]
1135711429
fn bad_inbound_payment_hash() {
1135811430
// Add coverage for checking that a user-provided payment hash matches the payment secret.

0 commit comments

Comments
 (0)