Implement multipath sends using payment_secret.
[rust-lightning] / lightning / src / ln / functional_test_utils.rs
index 4b73ad0ad058f3dd9c6b538e4d6a404c7d5d03e6..8a887e7fab0353b69d6ca1a9d70bb9b18931e83a 100644 (file)
@@ -4,7 +4,7 @@
 use chain::chaininterface;
 use chain::transaction::OutPoint;
 use chain::keysinterface::KeysInterface;
-use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret};
+use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure};
 use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor};
 use ln::router::{Route, Router, RouterReadArgs};
 use ln::features::InitFeatures;
@@ -273,6 +273,28 @@ macro_rules! get_local_commitment_txn {
        }
 }
 
+macro_rules! unwrap_send_err {
+       ($res: expr, $all_failed: expr, $type: pat, $check: expr) => {
+               match &$res {
+                       &Err(PaymentSendFailure::AllFailedRetrySafe(ref fails)) if $all_failed => {
+                               assert_eq!(fails.len(), 1);
+                               match fails[0] {
+                                       $type => { $check },
+                                       _ => panic!(),
+                               }
+                       },
+                       &Err(PaymentSendFailure::PartialFailure(ref fails)) if !$all_failed => {
+                               assert_eq!(fails.len(), 1);
+                               match fails[0] {
+                                       Err($type) => { $check },
+                                       _ => panic!(),
+                               }
+                       },
+                       _ => panic!(),
+               }
+       }
+}
+
 pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
        let chan_id = *node.network_chan_count.borrow();
 
@@ -897,8 +919,9 @@ pub const TEST_FINAL_CLTV: u32 = 32;
 
 pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], 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());
        }
 
@@ -907,18 +930,15 @@ 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 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, &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"),
-       };
+       unwrap_send_err!(origin_node.node.send_payment(route, our_payment_hash, &None), true, APIError::ChannelUnavailable { err },
+               assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"));
 }
 
 pub fn send_payment<'a, 'b, 'c>(origin: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64, expected_value: u64)  {