]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Refactor check_closed_event for multiple events
authorWillem Van Lint <noreply@wvanlint.dev>
Wed, 27 Sep 2023 23:12:35 +0000 (16:12 -0700)
committerWillem Van Lint <noreply@wvanlint.dev>
Thu, 19 Oct 2023 03:46:02 +0000 (20:46 -0700)
The check_closed_event function verified closure events against multiple
counterparty nodes, but only a single closure reason and channel
capacity. This commit introduces a check_closed_events function to
verify events against descriptions of each expected event, and refactors
check_closed_event in function of check_closed_events.

lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs

index 42403f89c7d17b267af97633a6e2b3b895315fa7..bf9f3704417c342cfaf1ee2d2383f8dfd95ce531 100644 (file)
@@ -1478,27 +1478,61 @@ macro_rules! check_closed_broadcast {
        }
 }
 
+#[derive(Default)]
+pub struct ExpectedCloseEvent {
+       pub channel_capacity_sats: Option<u64>,
+       pub channel_id: Option<ChannelId>,
+       pub counterparty_node_id: Option<PublicKey>,
+       pub discard_funding: bool,
+       pub reason: Option<ClosureReason>,
+}
+
+/// Check that multiple channel closing events have been issued.
+pub fn check_closed_events(node: &Node, expected_close_events: &[ExpectedCloseEvent]) {
+       let closed_events_count = expected_close_events.len();
+       let discard_events_count = expected_close_events.iter().filter(|e| e.discard_funding).count();
+       let events = node.node.get_and_clear_pending_events();
+       assert_eq!(events.len(), closed_events_count + discard_events_count, "{:?}", events);
+       for expected_event in expected_close_events {
+               assert!(events.iter().any(|e| matches!(
+                       e,
+                       Event::ChannelClosed {
+                               channel_id,
+                               reason,
+                               counterparty_node_id,
+                               channel_capacity_sats,
+                               ..
+                       } if (
+                               expected_event.channel_id.map(|expected| *channel_id == expected).unwrap_or(true) &&
+                               expected_event.reason.as_ref().map(|expected| reason == expected).unwrap_or(true) &&
+                               expected_event.counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) &&
+                               expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true)
+                       )
+               )));
+       }
+       assert_eq!(events.iter().filter(|e| matches!(
+               e,
+               Event::DiscardFunding { .. },
+       )).count(), discard_events_count);
+}
+
 /// Check that a channel's closing channel events has been issued
 pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: ClosureReason, is_check_discard_funding: bool,
        expected_counterparty_node_ids: &[PublicKey], expected_channel_capacity: u64) {
-       let events = node.node.get_and_clear_pending_events();
-       assert_eq!(events.len(), events_count, "{:?}", events);
-       let mut issues_discard_funding = false;
-       for event in events {
-               match event {
-                       Event::ChannelClosed { ref reason, counterparty_node_id,
-                               channel_capacity_sats, .. } => {
-                               assert_eq!(*reason, expected_reason);
-                               assert!(expected_counterparty_node_ids.iter().any(|id| id == &counterparty_node_id.unwrap()));
-                               assert_eq!(channel_capacity_sats.unwrap(), expected_channel_capacity);
-                       },
-                       Event::DiscardFunding { .. } => {
-                               issues_discard_funding = true;
-                       }
-                       _ => panic!("Unexpected event"),
-               }
-       }
-       assert_eq!(is_check_discard_funding, issues_discard_funding);
+       let expected_events_count = if is_check_discard_funding {
+               2 * expected_counterparty_node_ids.len()
+       } else {
+               expected_counterparty_node_ids.len()
+       };
+       assert_eq!(events_count, expected_events_count);
+       let expected_close_events = expected_counterparty_node_ids.iter().map(|node_id| ExpectedCloseEvent {
+               channel_capacity_sats: Some(expected_channel_capacity),
+               channel_id: None,
+               counterparty_node_id: Some(*node_id),
+               discard_funding: is_check_discard_funding,
+               reason: Some(expected_reason.clone()),
+       }).collect::<Vec<_>>();
+       check_closed_events(node, expected_close_events.as_slice());
 }
 
 /// Check that a channel's closing channel events has been issued
index 714fe2f248a828a273f373eb7607285a2bac3953..6d547b7a9e23fed8b724487ed4ef5db0eb1ae85e 100644 (file)
@@ -8653,7 +8653,7 @@ fn test_pre_lockin_no_chan_closed_update() {
        nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
        assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
        check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("Hi".to_string()) }, true,
-               [nodes[1].node.get_our_node_id(); 2], 100000);
+               [nodes[1].node.get_our_node_id()], 100000);
 }
 
 #[test]
@@ -10498,26 +10498,18 @@ fn test_disconnect_in_funding_batch() {
        // The channels in the batch will close immediately.
        let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
        let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 4);
-       assert!(events.iter().any(|e| matches!(
-               e,
-               Event::ChannelClosed {
-                       channel_id,
-                       ..
-               } if channel_id == &channel_id_1
-       )));
-       assert!(events.iter().any(|e| matches!(
-               e,
-               Event::ChannelClosed {
-                       channel_id,
-                       ..
-               } if channel_id == &channel_id_2
-       )));
-       assert_eq!(events.iter().filter(|e| matches!(
-               e,
-               Event::DiscardFunding { .. },
-       )).count(), 2);
+       check_closed_events(&nodes[0], &[
+               ExpectedCloseEvent {
+                       channel_id: Some(channel_id_1),
+                       discard_funding: true,
+                       ..Default::default()
+               },
+               ExpectedCloseEvent {
+                       channel_id: Some(channel_id_2),
+                       discard_funding: true,
+                       ..Default::default()
+               },
+       ]);
 
        // The monitor should become closed.
        check_added_monitors(&nodes[0], 1);
@@ -10601,26 +10593,18 @@ fn test_batch_funding_close_after_funding_signed() {
        }
 
        // All channels in the batch should close immediately.
-       let events = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(events.len(), 4);
-       assert!(events.iter().any(|e| matches!(
-               e,
-               Event::ChannelClosed {
-                       channel_id,
-                       ..
-               } if channel_id == &channel_id_1
-       )));
-       assert!(events.iter().any(|e| matches!(
-               e,
-               Event::ChannelClosed {
-                       channel_id,
-                       ..
-               } if channel_id == &channel_id_2
-       )));
-       assert_eq!(events.iter().filter(|e| matches!(
-               e,
-               Event::DiscardFunding { .. },
-       )).count(), 2);
+       check_closed_events(&nodes[0], &[
+               ExpectedCloseEvent {
+                       channel_id: Some(channel_id_1),
+                       discard_funding: true,
+                       ..Default::default()
+               },
+               ExpectedCloseEvent {
+                       channel_id: Some(channel_id_2),
+                       discard_funding: true,
+                       ..Default::default()
+               },
+       ]);
 
        // Ensure the channels don't exist anymore.
        assert!(nodes[0].node.list_channels().is_empty());