Merge pull request #1540 from wpaulino/rename-config-fields
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index 0873127179f82a9060c716b7429b7e69957419e5..38c05998e6a2769db297179d4f0ce0dc76589b9e 100644 (file)
@@ -15,7 +15,7 @@ use chain::channelmonitor::ChannelMonitor;
 use chain::transaction::OutPoint;
 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
 use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, PaymentId, MIN_CLTV_EXPIRY_DELTA};
-use routing::gossip::{P2PGossipSync, NetworkGraph};
+use routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate};
 use routing::router::{PaymentParameters, Route, get_route};
 use ln::features::{InitFeatures, InvoiceFeatures};
 use ln::msgs;
@@ -772,7 +772,7 @@ pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &'a
 
 pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelReady, Transaction) {
        let mut no_announce_cfg = test_default_channel_config();
-       no_announce_cfg.channel_options.announced_channel = false;
+       no_announce_cfg.channel_handshake_config.announced_channel = false;
        nodes[a].node.create_channel(nodes[b].node.get_our_node_id(), channel_value, push_msat, 42, Some(no_announce_cfg)).unwrap();
        let open_channel = get_event_msg!(nodes[a], MessageSendEvent::SendOpenChannel, nodes[b].node.get_our_node_id());
        nodes[b].node.handle_open_channel(&nodes[a].node.get_our_node_id(), a_flags, &open_channel);
@@ -1466,8 +1466,10 @@ impl<'a> PaymentFailedConditions<'a> {
 #[cfg(test)]
 macro_rules! expect_payment_failed_with_update {
        ($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $scid: expr, $chan_closed: expr) => {
-               expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest,
-                       $crate::ln::functional_test_utils::PaymentFailedConditions::new().blamed_scid($scid).blamed_chan_closed($chan_closed));
+               $crate::ln::functional_test_utils::expect_payment_failed_conditions(
+                       &$node, $expected_payment_hash, $rejected_by_dest,
+                       $crate::ln::functional_test_utils::PaymentFailedConditions::new()
+                               .blamed_scid($scid).blamed_chan_closed($chan_closed));
        }
 }
 
@@ -1479,64 +1481,72 @@ macro_rules! expect_payment_failed {
                $(
                        conditions = conditions.expected_htlc_error_data($expected_error_code, &$expected_error_data);
                )*
-               expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest, conditions);
+               $crate::ln::functional_test_utils::expect_payment_failed_conditions(&$node, $expected_payment_hash, $rejected_by_dest, conditions);
        };
 }
 
-#[cfg(test)]
-macro_rules! expect_payment_failed_conditions {
-       ($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $conditions: expr) => {
-               let events = $node.node.get_and_clear_pending_events();
-               assert_eq!(events.len(), 1);
-               let expected_payment_id = match events[0] {
-                       Event::PaymentPathFailed { ref payment_hash, rejected_by_dest, ref error_code, ref error_data, ref path, ref retry, ref payment_id, ref network_update, .. } => {
-                               assert_eq!(*payment_hash, $expected_payment_hash, "unexpected payment_hash");
-                               assert_eq!(rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value");
-                               assert!(retry.is_some(), "expected retry.is_some()");
-                               assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
-                               assert_eq!(retry.as_ref().unwrap().payment_params.payee_pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
-
+pub fn expect_payment_failed_conditions<'a, 'b, 'c, 'd, 'e>(
+       node: &'a Node<'b, 'c, 'd>, expected_payment_hash: PaymentHash, expected_rejected_by_dest: bool,
+       conditions: PaymentFailedConditions<'e>
+) {
+       let mut events = node.node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       let expected_payment_id = match events.pop().unwrap() {
+               Event::PaymentPathFailed { payment_hash, rejected_by_dest, path, retry, payment_id, network_update,
+                       #[cfg(test)]
+                       error_code,
+                       #[cfg(test)]
+                       error_data, .. } => {
+                       assert_eq!(payment_hash, expected_payment_hash, "unexpected payment_hash");
+                       assert_eq!(rejected_by_dest, expected_rejected_by_dest, "unexpected rejected_by_dest value");
+                       assert!(retry.is_some(), "expected retry.is_some()");
+                       assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
+                       assert_eq!(retry.as_ref().unwrap().payment_params.payee_pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
+
+                       #[cfg(test)]
+                       {
                                assert!(error_code.is_some(), "expected error_code.is_some() = true");
                                assert!(error_data.is_some(), "expected error_data.is_some() = true");
-                               if let Some((code, data)) = $conditions.expected_htlc_error_data {
+                               if let Some((code, data)) = conditions.expected_htlc_error_data {
                                        assert_eq!(error_code.unwrap(), code, "unexpected error code");
                                        assert_eq!(&error_data.as_ref().unwrap()[..], data, "unexpected error data");
                                }
+                       }
 
-                               if let Some(chan_closed) = $conditions.expected_blamed_chan_closed {
-                                       match network_update {
-                                               &Some($crate::routing::gossip::NetworkUpdate::ChannelUpdateMessage { ref msg }) if !chan_closed => {
-                                                       if let Some(scid) = $conditions.expected_blamed_scid {
-                                                               assert_eq!(msg.contents.short_channel_id, scid);
-                                                       }
-                                                       assert_eq!(msg.contents.flags & 2, 0);
-                                               },
-                                               &Some($crate::routing::gossip::NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }) if chan_closed => {
-                                                       if let Some(scid) = $conditions.expected_blamed_scid {
-                                                               assert_eq!(short_channel_id, scid);
-                                                       }
-                                                       assert!(is_permanent);
-                                               },
-                                               Some(_) => panic!("Unexpected update type"),
-                                               None => panic!("Expected update"),
-                                       }
+                       if let Some(chan_closed) = conditions.expected_blamed_chan_closed {
+                               match network_update {
+                                       Some(NetworkUpdate::ChannelUpdateMessage { ref msg }) if !chan_closed => {
+                                               if let Some(scid) = conditions.expected_blamed_scid {
+                                                       assert_eq!(msg.contents.short_channel_id, scid);
+                                               }
+                                               const CHAN_DISABLED_FLAG: u8 = 2;
+                                               assert_eq!(msg.contents.flags & CHAN_DISABLED_FLAG, 0);
+                                       },
+                                       Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }) if chan_closed => {
+                                               if let Some(scid) = conditions.expected_blamed_scid {
+                                                       assert_eq!(short_channel_id, scid);
+                                               }
+                                               assert!(is_permanent);
+                                       },
+                                       Some(_) => panic!("Unexpected update type"),
+                                       None => panic!("Expected update"),
                                }
+                       }
 
-                               payment_id.unwrap()
-                       },
-                       _ => panic!("Unexpected event"),
-               };
-               if !$conditions.expected_mpp_parts_remain {
-                       $node.node.abandon_payment(expected_payment_id);
-                       let events = $node.node.get_and_clear_pending_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               Event::PaymentFailed { ref payment_hash, ref payment_id } => {
-                                       assert_eq!(*payment_hash, $expected_payment_hash, "unexpected second payment_hash");
-                                       assert_eq!(*payment_id, expected_payment_id);
-                               }
-                               _ => panic!("Unexpected second event"),
+                       payment_id.unwrap()
+               },
+               _ => panic!("Unexpected event"),
+       };
+       if !conditions.expected_mpp_parts_remain {
+               node.node.abandon_payment(expected_payment_id);
+               let events = node.node.get_and_clear_pending_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       Event::PaymentFailed { ref payment_hash, ref payment_id } => {
+                               assert_eq!(*payment_hash, expected_payment_hash, "unexpected second payment_hash");
+                               assert_eq!(*payment_id, expected_payment_id);
                        }
+                       _ => panic!("Unexpected second event"),
                }
        }
 }
@@ -1679,7 +1689,9 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
                        ($node: expr, $prev_node: expr, $next_node: expr, $new_msgs: expr) => {
                                {
                                        $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
-                                       let fee = $node.node.channel_state.lock().unwrap().by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap().config.forwarding_fee_base_msat;
+                                       let fee = $node.node.channel_state.lock().unwrap()
+                                               .by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap()
+                                               .config.options.forwarding_fee_base_msat;
                                        expect_payment_forwarded!($node, $next_node, $prev_node, Some(fee as u64), false, false);
                                        expected_total_fee_msat += fee as u64;
                                        check_added_monitors!($node, 1);
@@ -1955,15 +1967,15 @@ pub fn test_default_channel_config() -> UserConfig {
        let mut default_config = UserConfig::default();
        // Set cltv_expiry_delta slightly lower to keep the final CLTV values inside one byte in our
        // tests so that our script-length checks don't fail (see ACCEPTED_HTLC_SCRIPT_WEIGHT).
-       default_config.channel_options.cltv_expiry_delta = MIN_CLTV_EXPIRY_DELTA;
-       default_config.channel_options.announced_channel = true;
-       default_config.peer_channel_config_limits.force_announced_channel_preference = false;
+       default_config.channel_config.cltv_expiry_delta = MIN_CLTV_EXPIRY_DELTA;
+       default_config.channel_handshake_config.announced_channel = true;
+       default_config.channel_handshake_limits.force_announced_channel_preference = false;
        // When most of our tests were written, the default HTLC minimum was fixed at 1000.
        // It now defaults to 1, so we simply set it to the expected value here.
-       default_config.own_channel_config.our_htlc_minimum_msat = 1000;
+       default_config.channel_handshake_config.our_htlc_minimum_msat = 1000;
        // When most of our tests were written, we didn't have the notion of a `max_dust_htlc_exposure_msat`,
        // It now defaults to 5_000_000 msat; to avoid interfering with tests we bump it to 50_000_000 msat.
-       default_config.channel_options.max_dust_htlc_exposure_msat = 50_000_000;
+       default_config.channel_config.max_dust_htlc_exposure_msat = 50_000_000;
        default_config
 }