X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=lightning%2Fsrc%2Fln%2Ffunctional_test_utils.rs;h=ee4c027a10ec56c8ceb02beaf99bced52b7f71b4;hb=59778dac488cff735004671cdefb3f4ac1f920fd;hp=5840fead943049a6ac2a26eb4e784253ad3bcffb;hpb=8354e0c9ba383d4f4df14c9735847a0b19a37f91;p=rust-lightning diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 5840fead..ee4c027a 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -415,6 +415,7 @@ type TestOnionMessenger<'chan_man, 'node_cfg, 'chan_mon_cfg> = OnionMessenger< DedicatedEntropy, &'node_cfg test_utils::TestKeysInterface, &'chan_mon_cfg test_utils::TestLogger, + &'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>, &'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>, &'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>, IgnoringMessageHandler, @@ -1553,11 +1554,29 @@ macro_rules! check_warn_msg { }} } +/// Checks if at least one peer is connected. +fn is_any_peer_connected(node: &Node) -> bool { + let peer_state = node.node.per_peer_state.read().unwrap(); + for (_, peer_mutex) in peer_state.iter() { + let peer = peer_mutex.lock().unwrap(); + if peer.is_connected { return true; } + } + false +} + /// Check that a channel's closing channel update has been broadcasted, and optionally /// check whether an error message event has occurred. pub fn check_closed_broadcast(node: &Node, num_channels: usize, with_error_msg: bool) -> Vec { + let mut dummy_connected = false; + if !is_any_peer_connected(node) { + connect_dummy_node(&node); + dummy_connected = true; + } let msg_events = node.node.get_and_clear_pending_msg_events(); assert_eq!(msg_events.len(), if with_error_msg { num_channels * 2 } else { num_channels }); + if dummy_connected { + disconnect_dummy_node(&node); + } msg_events.into_iter().filter_map(|msg_event| { match msg_event { MessageSendEvent::BroadcastChannelUpdate { ref msg } => { @@ -1827,14 +1846,9 @@ macro_rules! expect_htlc_handling_failed_destinations { /// there are any [`Event::HTLCHandlingFailed`] events their [`HTLCDestination`] is included in the /// `expected_failures` set. pub fn expect_pending_htlcs_forwardable_conditions(events: Vec, expected_failures: &[HTLCDestination]) { - match events[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event {:?}", events), - }; - let count = expected_failures.len() + 1; assert_eq!(events.len(), count); - + assert!(events.iter().find(|event| matches!(event, Event::PendingHTLCsForwardable { .. })).is_some()); if expected_failures.len() > 0 { expect_htlc_handling_failed_destinations!(events, expected_failures) } @@ -2512,6 +2526,7 @@ pub struct PassAlongPathArgs<'a, 'b, 'c, 'd> { pub clear_recipient_events: bool, pub expected_preimage: Option, pub is_probe: bool, + pub custom_tlvs: Vec<(u64, Vec)>, } impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> { @@ -2522,7 +2537,7 @@ impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> { Self { origin_node, expected_path, recv_value, payment_hash, payment_secret: None, event, payment_claimable_expected: true, clear_recipient_events: true, expected_preimage: None, - is_probe: false, + is_probe: false, custom_tlvs: Vec::new(), } } pub fn without_clearing_recipient_events(mut self) -> Self { @@ -2546,13 +2561,17 @@ impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> { self.expected_preimage = Some(payment_preimage); self } + pub fn with_custom_tlvs(mut self, custom_tlvs: Vec<(u64, Vec)>) -> Self { + self.custom_tlvs = custom_tlvs; + self + } } pub fn do_pass_along_path<'a, 'b, 'c>(args: PassAlongPathArgs) -> Option { let PassAlongPathArgs { origin_node, expected_path, recv_value, payment_hash: our_payment_hash, payment_secret: our_payment_secret, event: ev, payment_claimable_expected, - clear_recipient_events, expected_preimage, is_probe + clear_recipient_events, expected_preimage, is_probe, custom_tlvs } = args; let mut payment_event = SendEvent::from_event(ev); @@ -2585,6 +2604,7 @@ pub fn do_pass_along_path<'a, 'b, 'c>(args: PassAlongPathArgs) -> Option assert_eq!(our_payment_hash, *payment_hash); assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap()); assert!(onion_fields.is_some()); + assert_eq!(onion_fields.as_ref().unwrap().custom_tlvs, custom_tlvs); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert_eq!(expected_preimage, *payment_preimage); @@ -3180,8 +3200,8 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec(node_count: usize, cfgs: &'b Vec(node: &Node<'a, 'b, 'c>) { + let node_id_dummy = PublicKey::from_slice(&[2; 33]).unwrap(); + + let mut dummy_init_features = InitFeatures::empty(); + dummy_init_features.set_static_remote_key_required(); + + let init_dummy = msgs::Init { + features: dummy_init_features, + networks: None, + remote_network_address: None + }; + + node.node.peer_connected(&node_id_dummy, &init_dummy, true).unwrap(); + node.onion_messenger.peer_connected(&node_id_dummy, &init_dummy, true).unwrap(); +} + +pub fn disconnect_dummy_node<'a, 'b: 'a, 'c: 'b>(node: &Node<'a, 'b, 'c>) { + let node_id_dummy = PublicKey::from_slice(&[2; 33]).unwrap(); + node.node.peer_disconnected(&node_id_dummy); + node.onion_messenger.peer_disconnected(&node_id_dummy); +} + // Note that the following only works for CLTV values up to 128 pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 137; // Here we have a diff due to HTLC CLTV expiry being < 2^15 in test pub const ACCEPTED_HTLC_SCRIPT_WEIGHT_ANCHORS: usize = 140; // Here we have a diff due to HTLC CLTV expiry being < 2^15 in test @@ -3340,15 +3382,21 @@ pub fn check_preimage_claim<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, prev_txn: &Vec< } pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec>, a: usize, b: usize, needs_err_handle: bool, expected_error: &str) { + let mut dummy_connected = false; + if !is_any_peer_connected(&nodes[a]) { + connect_dummy_node(&nodes[a]); + dummy_connected = true + } + let events_1 = nodes[a].node.get_and_clear_pending_msg_events(); assert_eq!(events_1.len(), 2); - let as_update = match events_1[0] { + let as_update = match events_1[1] { MessageSendEvent::BroadcastChannelUpdate { ref msg } => { msg.clone() }, _ => panic!("Unexpected event"), }; - match events_1[1] { + match events_1[0] { MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => { assert_eq!(node_id, nodes[b].node.get_our_node_id()); assert_eq!(msg.data, expected_error); @@ -3365,17 +3413,24 @@ pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec panic!("Unexpected event"), } - + if dummy_connected { + disconnect_dummy_node(&nodes[a]); + dummy_connected = false; + } + if !is_any_peer_connected(&nodes[b]) { + connect_dummy_node(&nodes[b]); + dummy_connected = true; + } let events_2 = nodes[b].node.get_and_clear_pending_msg_events(); assert_eq!(events_2.len(), if needs_err_handle { 1 } else { 2 }); - let bs_update = match events_2[0] { + let bs_update = match events_2.last().unwrap() { MessageSendEvent::BroadcastChannelUpdate { ref msg } => { msg.clone() }, _ => panic!("Unexpected event"), }; if !needs_err_handle { - match events_2[1] { + match events_2[0] { MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => { assert_eq!(node_id, nodes[a].node.get_our_node_id()); assert_eq!(msg.data, expected_error); @@ -3387,7 +3442,9 @@ pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec panic!("Unexpected event"), } } - + if dummy_connected { + disconnect_dummy_node(&nodes[b]); + } for node in nodes { node.gossip_sync.handle_channel_update(&as_update).unwrap(); node.gossip_sync.handle_channel_update(&bs_update).unwrap();