Expand the Route object to include multiple paths.
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index 472e50aae27b1cedfeccccd880b1cd2958099d4a..76aeffd2ead6449d3bcd3dfd607fc83600925cea 100644 (file)
@@ -310,10 +310,33 @@ pub fn create_announced_chan_between_nodes<'a, 'b, 'c>(nodes: &'a Vec<Node<'b, '
 
 pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c>(nodes: &'a Vec<Node<'b, 'c>>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
        let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
+
+       nodes[a].node.broadcast_node_announcement([0, 0, 0], [0; 32], msgs::NetAddressSet::new());
+       let a_events = nodes[a].node.get_and_clear_pending_msg_events();
+       assert_eq!(a_events.len(), 1);
+       let a_node_announcement = match a_events[0] {
+               MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
+                       (*msg).clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
+       nodes[b].node.broadcast_node_announcement([1, 1, 1], [1; 32], msgs::NetAddressSet::new());
+       let b_events = nodes[b].node.get_and_clear_pending_msg_events();
+       assert_eq!(b_events.len(), 1);
+       let b_node_announcement = match b_events[0] {
+               MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
+                       (*msg).clone()
+               },
+               _ => panic!("Unexpected event"),
+       };
+
        for node in nodes {
                assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
                node.router.handle_channel_update(&chan_announcement.1).unwrap();
                node.router.handle_channel_update(&chan_announcement.2).unwrap();
+               node.router.handle_node_announcement(&a_node_announcement).unwrap();
+               node.router.handle_node_announcement(&b_node_announcement).unwrap();
        }
        (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
 }
@@ -591,8 +614,9 @@ macro_rules! expect_payment_received {
                let events = $node.node.get_and_clear_pending_events();
                assert_eq!(events.len(), 1);
                match events[0] {
-                       Event::PaymentReceived { ref payment_hash, amt } => {
+                       Event::PaymentReceived { ref payment_hash, ref payment_secret, amt } => {
                                assert_eq!($expected_payment_hash, *payment_hash);
+                               assert_eq!(*payment_secret, None);
                                assert_eq!($expected_recv_value, amt);
                        },
                        _ => panic!("Unexpected event"),
@@ -613,9 +637,9 @@ macro_rules! expect_payment_sent {
        }
 }
 
-pub fn send_along_route_with_hash<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64, our_payment_hash: PaymentHash) {
+pub fn send_along_route_with_secret<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: Option<[u8; 32]>) {
        let mut payment_event = {
-               origin_node.node.send_payment(route, our_payment_hash).unwrap();
+               origin_node.node.send_payment(route, our_payment_hash, our_payment_secret.as_ref()).unwrap();
                check_added_monitors!(origin_node, 1);
 
                let mut events = origin_node.node.get_and_clear_pending_msg_events();
@@ -637,8 +661,9 @@ pub fn send_along_route_with_hash<'a, 'b>(origin_node: &Node<'a, 'b>, route: Rou
                        let events_2 = node.node.get_and_clear_pending_events();
                        assert_eq!(events_2.len(), 1);
                        match events_2[0] {
-                               Event::PaymentReceived { ref payment_hash, amt } => {
+                               Event::PaymentReceived { ref payment_hash, ref payment_secret, amt } => {
                                        assert_eq!(our_payment_hash, *payment_hash);
+                                       assert_eq!(our_payment_secret, *payment_secret);
                                        assert_eq!(amt, recv_value);
                                },
                                _ => panic!("Unexpected event"),
@@ -655,14 +680,18 @@ pub fn send_along_route_with_hash<'a, 'b>(origin_node: &Node<'a, 'b>, route: Rou
        }
 }
 
+pub fn send_along_route_with_hash<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64, our_payment_hash: PaymentHash) {
+       send_along_route_with_secret(origin_node, route, expected_route, recv_value, our_payment_hash, None);
+}
+
 pub fn send_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
        let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
        send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
        (our_payment_preimage, our_payment_hash)
 }
 
-pub fn claim_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_preimage: PaymentPreimage, expected_amount: u64) {
-       assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage, expected_amount));
+pub fn claim_payment_along_route_with_secret<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_preimage: PaymentPreimage, our_payment_secret: Option<[u8; 32]>, expected_amount: u64) {
+       assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage, &our_payment_secret, expected_amount));
        check_added_monitors!(expected_route.last().unwrap(), 1);
 
        let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
@@ -739,6 +768,10 @@ pub fn claim_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_ro
        }
 }
 
+pub fn claim_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_preimage: PaymentPreimage, expected_amount: u64) {
+       claim_payment_along_route_with_secret(origin_node, expected_route, skip_last, our_payment_preimage, None, expected_amount);
+}
+
 pub fn claim_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], our_payment_preimage: PaymentPreimage, expected_amount: u64) {
        claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
 }
@@ -747,8 +780,9 @@ pub const TEST_FINAL_CLTV: u32 = 32;
 
 pub fn route_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
        let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
-       assert_eq!(route.hops.len(), expected_route.len());
-       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+       assert_eq!(route.paths.len(), 1);
+       assert_eq!(route.paths[0].len(), expected_route.len());
+       for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
                assert_eq!(hop.pubkey, node.node.get_our_node_id());
        }
 
@@ -757,14 +791,15 @@ pub fn route_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node
 
 pub fn route_over_limit<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64)  {
        let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
-       assert_eq!(route.hops.len(), expected_route.len());
-       for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
+       assert_eq!(route.paths.len(), 1);
+       assert_eq!(route.paths[0].len(), expected_route.len());
+       for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
                assert_eq!(hop.pubkey, node.node.get_our_node_id());
        }
 
        let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
 
-       let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
+       let err = origin_node.node.send_payment(route, our_payment_hash, None).err().unwrap();
        match err {
                APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
                _ => panic!("Unknown error variants"),
@@ -777,7 +812,7 @@ pub fn send_payment<'a, 'b>(origin: &Node<'a, 'b>, expected_route: &[&Node<'a, '
 }
 
 pub fn fail_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_hash: PaymentHash)  {
-       assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
+       assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, &None));
        expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
        check_added_monitors!(expected_route.last().unwrap(), 1);