X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Ffunctional_test_utils.rs;h=8f9c206f108c66c6c23bb0fc006ecc13caa20532;hb=55da9c434eeb6f108463e1c8d78914c759f43999;hp=42403f89c7d17b267af97633a6e2b3b895315fa7;hpb=0357cafbbb6fc9f1d69f9513e89c66107aaa8a27;p=rust-lightning diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 42403f89..8f9c206f 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1478,27 +1478,61 @@ macro_rules! check_closed_broadcast { } } +#[derive(Default)] +pub struct ExpectedCloseEvent { + pub channel_capacity_sats: Option, + pub channel_id: Option, + pub counterparty_node_id: Option, + pub discard_funding: bool, + pub reason: Option, +} + +/// 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::>(); + check_closed_events(node, expected_close_events.as_slice()); } /// Check that a channel's closing channel events has been issued @@ -1904,7 +1938,7 @@ macro_rules! get_route { macro_rules! get_route_and_payment_hash { ($send_node: expr, $recv_node: expr, $recv_value: expr) => {{ let payment_params = $crate::routing::router::PaymentParameters::from_node_id($recv_node.node.get_our_node_id(), TEST_FINAL_CLTV) - .with_bolt11_features($recv_node.node.invoice_features()).unwrap(); + .with_bolt11_features($recv_node.node.bolt11_invoice_features()).unwrap(); $crate::get_route_and_payment_hash!($send_node, $recv_node, payment_params, $recv_value) }}; ($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr) => {{ @@ -2517,7 +2551,7 @@ pub const TEST_FINAL_CLTV: u32 = 70; pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret, PaymentId) { let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV) - .with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap(); + .with_bolt11_features(expected_route.last().unwrap().node.bolt11_invoice_features()).unwrap(); let route_params = RouteParameters::from_payment_params_and_value(payment_params, recv_value); let route = get_route(origin_node, &route_params).unwrap(); assert_eq!(route.paths.len(), 1); @@ -2532,7 +2566,7 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) { let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV) - .with_bolt11_features(expected_route.last().unwrap().node.invoice_features()).unwrap(); + .with_bolt11_features(expected_route.last().unwrap().node.bolt11_invoice_features()).unwrap(); let route_params = RouteParameters::from_payment_params_and_value(payment_params, recv_value); let network_graph = origin_node.network_graph.read_only(); let scorer = test_utils::TestScorer::new();