+
+#[test]
+fn test_disconnects_peer_awaiting_response_ticks() {
+ // Tests that nodes which are awaiting on a response critical for channel responsiveness
+ // disconnect their counterparty after `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+ let mut chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ // Asserts a disconnect event is queued to the user.
+ let check_disconnect_event = |node: &Node, should_disconnect: bool| {
+ let disconnect_event = node.node.get_and_clear_pending_msg_events().iter().find_map(|event|
+ if let MessageSendEvent::HandleError { action, .. } = event {
+ if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
+ Some(())
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+ );
+ assert_eq!(disconnect_event.is_some(), should_disconnect);
+ };
+
+ // Fires timer ticks ensuring we only attempt to disconnect peers after reaching
+ // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+ let check_disconnect = |node: &Node| {
+ // No disconnect without any timer ticks.
+ check_disconnect_event(node, false);
+
+ // No disconnect with 1 timer tick less than required.
+ for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS - 1 {
+ node.node.timer_tick_occurred();
+ check_disconnect_event(node, false);
+ }
+
+ // Disconnect after reaching the required ticks.
+ node.node.timer_tick_occurred();
+ check_disconnect_event(node, true);
+
+ // Disconnect again on the next tick if the peer hasn't been disconnected yet.
+ node.node.timer_tick_occurred();
+ check_disconnect_event(node, true);
+ };
+
+ create_chan_between_nodes(&nodes[0], &nodes[1]);
+
+ // We'll start by performing a fee update with Alice (nodes[0]) on the channel.
+ *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
+ nodes[0].node.timer_tick_occurred();
+ check_added_monitors!(&nodes[0], 1);
+ let alice_fee_update = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
+ nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), alice_fee_update.update_fee.as_ref().unwrap());
+ nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &alice_fee_update.commitment_signed);
+ check_added_monitors!(&nodes[1], 1);
+
+ // This will prompt Bob (nodes[1]) to respond with his `CommitmentSigned` and `RevokeAndACK`.
+ let (bob_revoke_and_ack, bob_commitment_signed) = get_revoke_commit_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
+ nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revoke_and_ack);
+ check_added_monitors!(&nodes[0], 1);
+ nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_commitment_signed);
+ check_added_monitors(&nodes[0], 1);
+
+ // Alice then needs to send her final `RevokeAndACK` to complete the commitment dance. We
+ // pretend Bob hasn't received the message and check whether he'll disconnect Alice after
+ // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+ let alice_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
+ check_disconnect(&nodes[1]);
+
+ // Now, we'll reconnect them to test awaiting a `ChannelReestablish` message.
+ //
+ // Note that since the commitment dance didn't complete above, Alice is expected to resend her
+ // final `RevokeAndACK` to Bob to complete it.
+ nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
+ nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
+ let bob_init = msgs::Init {
+ features: nodes[1].node.init_features(), networks: None, remote_network_address: None
+ };
+ nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &bob_init, true).unwrap();
+ let alice_init = msgs::Init {
+ features: nodes[0].node.init_features(), networks: None, remote_network_address: None
+ };
+ nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &alice_init, true).unwrap();
+
+ // Upon reconnection, Alice sends her `ChannelReestablish` to Bob. Alice, however, hasn't
+ // received Bob's yet, so she should disconnect him after reaching
+ // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+ let alice_channel_reestablish = get_event_msg!(
+ nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()
+ );
+ nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &alice_channel_reestablish);
+ check_disconnect(&nodes[0]);
+
+ // Bob now sends his `ChannelReestablish` to Alice to resume the channel and consider it "live".
+ let bob_channel_reestablish = nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(|event|
+ if let MessageSendEvent::SendChannelReestablish { node_id, msg } = event {
+ assert_eq!(*node_id, nodes[0].node.get_our_node_id());
+ Some(msg.clone())
+ } else {
+ None
+ }
+ ).unwrap();
+ nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bob_channel_reestablish);
+
+ // Sanity check that Alice won't disconnect Bob since she's no longer waiting for any messages.
+ for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
+ nodes[0].node.timer_tick_occurred();
+ check_disconnect_event(&nodes[0], false);
+ }
+
+ // However, Bob is still waiting on Alice's `RevokeAndACK`, so he should disconnect her after
+ // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
+ check_disconnect(&nodes[1]);
+
+ // Finally, have Bob process the last message.
+ nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &alice_revoke_and_ack);
+ check_added_monitors(&nodes[1], 1);
+
+ // At this point, neither node should attempt to disconnect each other, since they aren't
+ // waiting on any messages.
+ for node in &nodes {
+ for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
+ node.node.timer_tick_occurred();
+ check_disconnect_event(node, false);
+ }
+ }
+}
+
+#[test]
+fn test_remove_expired_outbound_unfunded_channels() {
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
+ let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+ nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
+
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ Event::FundingGenerationReady { .. } => (),
+ _ => panic!("Unexpected event"),
+ };
+
+ // Asserts the outbound channel has been removed from a nodes[0]'s peer state map.
+ let check_outbound_channel_existence = |should_exist: bool| {
+ let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
+ let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
+ assert_eq!(chan_lock.outbound_v1_channel_by_id.contains_key(&temp_channel_id), should_exist);
+ };
+
+ // Channel should exist without any timer ticks.
+ check_outbound_channel_existence(true);
+
+ // Channel should exist with 1 timer tick less than required.
+ for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
+ nodes[0].node.timer_tick_occurred();
+ check_outbound_channel_existence(true)
+ }
+
+ // Remove channel after reaching the required ticks.
+ nodes[0].node.timer_tick_occurred();
+ check_outbound_channel_existence(false);
+
+ check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
+}
+
+#[test]
+fn test_remove_expired_inbound_unfunded_channels() {
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
+ let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
+ let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
+ nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
+
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ Event::FundingGenerationReady { .. } => (),
+ _ => panic!("Unexpected event"),
+ };
+
+ // Asserts the inbound channel has been removed from a nodes[1]'s peer state map.
+ let check_inbound_channel_existence = |should_exist: bool| {
+ let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
+ let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
+ assert_eq!(chan_lock.inbound_v1_channel_by_id.contains_key(&temp_channel_id), should_exist);
+ };
+
+ // Channel should exist without any timer ticks.
+ check_inbound_channel_existence(true);
+
+ // Channel should exist with 1 timer tick less than required.
+ for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
+ nodes[1].node.timer_tick_occurred();
+ check_inbound_channel_existence(true)
+ }
+
+ // Remove channel after reaching the required ticks.
+ nodes[1].node.timer_tick_occurred();
+ check_inbound_channel_existence(false);
+
+ check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
+}