]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add new expect_payment_failure_chan_update!() macro in tests
authorMatt Corallo <git@bluematt.me>
Fri, 25 Jun 2021 16:48:34 +0000 (16:48 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 2 Jul 2021 17:16:12 +0000 (17:16 +0000)
This further DRYs up some functional_test code and increases
coverage.

lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs

index 166e943e66ddb1007dafbab2f49c843e6db63467..98086e11b46bc54144305ab6632b3250d7b2fd2d 100644 (file)
@@ -997,6 +997,30 @@ macro_rules! expect_payment_sent {
        }
 }
 
+#[cfg(test)]
+macro_rules! expect_payment_failure_chan_update {
+       ($node: expr, $scid: expr, $chan_closed: expr) => {
+               let events = $node.node.get_and_clear_pending_msg_events();
+               assert_eq!(events.len(), 1);
+               match events[0] {
+                       MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
+                               match update {
+                                       &HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } if !$chan_closed => {
+                                               assert_eq!(msg.contents.short_channel_id, $scid);
+                                               assert_eq!(msg.contents.flags & 2, 0);
+                                       },
+                                       &HTLCFailChannelUpdate::ChannelClosed { short_channel_id, is_permanent } if $chan_closed => {
+                                               assert_eq!(short_channel_id, $scid);
+                                               assert!(is_permanent);
+                                       },
+                                       _ => panic!("Unexpected update type"),
+                               }
+                       },
+                       _ => panic!("Unexpected event"),
+               }
+       }
+}
+
 #[cfg(test)]
 macro_rules! expect_payment_failed {
        ($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
index cea886ae21b16f879314e65f7a238bd7b6a6eb38..060097c5f4a30abe18139ebb9ab14800a38d50f3 100644 (file)
@@ -1360,15 +1360,7 @@ fn holding_cell_htlc_counting() {
        nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
        commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
 
-       let events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
-       match events[0] {
-               MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
-                       assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
-               },
-               _ => panic!("Unexpected event"),
-       }
-
+       expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, false);
        expect_payment_failed!(nodes[0], payment_hash_2, false);
 
        // Now forward all the pending HTLCs and claim them back
@@ -3095,13 +3087,7 @@ fn test_simple_commitment_revoked_fail_backward() {
 
                        nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
                        commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
-
-                       let events = nodes[0].node.get_and_clear_pending_msg_events();
-                       assert_eq!(events.len(), 1);
-                       match events[0] {
-                               MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-                               _ => panic!("Unexpected event"),
-                       }
+                       expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, true);
                        expect_payment_failed!(nodes[0], payment_hash, false);
                },
                _ => panic!("Unexpected event"),
@@ -4204,7 +4190,7 @@ fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
        let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
        create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
-       create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
+       let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
 
        // Make sure all nodes are at the same starting height
        connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
@@ -4260,14 +4246,7 @@ fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
                        _ => unreachable!(),
                }
                expect_payment_failed!(nodes[0], second_payment_hash, false);
-               if let &MessageSendEvent::PaymentFailureNetworkUpdate { ref update } = &nodes[0].node.get_and_clear_pending_msg_events()[0] {
-                       match update {
-                               &HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {},
-                               _ => panic!("Unexpected event"),
-                       }
-               } else {
-                       panic!("Unexpected event");
-               }
+               expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, false);
        } else {
                expect_payment_failed!(nodes[1], second_payment_hash, true);
        }
@@ -5452,13 +5431,7 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
        assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
        {
                commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
-               let events = nodes[0].node.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match events[0] {
-                       MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelClosed { .. }  } => {
-                       },
-                       _ => { panic!("Unexpected event"); }
-               }
+               expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, true);
        }
        expect_payment_failed!(nodes[0], duplicate_payment_hash, false);
 
@@ -6517,20 +6490,8 @@ fn test_fail_holding_cell_htlc_upon_free_multihop() {
                _ => panic!("Unexpected event"),
        };
        nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
-       let fail_msg_event = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(fail_msg_event.len(), 1);
-       match &fail_msg_event[0] {
-               &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
-               _ => panic!("Unexpected event"),
-       }
-       let failure_event = nodes[0].node.get_and_clear_pending_events();
-       assert_eq!(failure_event.len(), 1);
-       match &failure_event[0] {
-               &Event::PaymentFailed { rejected_by_dest, .. } => {
-                       assert!(!rejected_by_dest);
-               },
-               _ => panic!("Unexpected event"),
-       }
+       expect_payment_failure_chan_update!(nodes[0], chan_1_2.0.contents.short_channel_id, false);
+       expect_payment_failed!(nodes[0], our_payment_hash, false);
        check_added_monitors!(nodes[0], 1);
 }