Skip to content

Commit 6df8d12

Browse files
committed
Make expect_payment_failed_conditions a function
This reduces macro generated code in tests a good bit, and moves us one step further away from using macros everywhere when we don't need to.
1 parent 75ca50f commit 6df8d12

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use chain::channelmonitor::ChannelMonitor;
1515
use chain::transaction::OutPoint;
1616
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1717
use ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, PaymentId, MIN_CLTV_EXPIRY_DELTA};
18-
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
18+
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph, NetworkUpdate};
1919
use routing::router::{PaymentParameters, Route, get_route};
2020
use ln::features::{InitFeatures, InvoiceFeatures};
2121
use ln::msgs;
@@ -1394,7 +1394,7 @@ impl<'a> PaymentFailedConditions<'a> {
13941394
#[cfg(test)]
13951395
macro_rules! expect_payment_failed_with_update {
13961396
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $scid: expr, $chan_closed: expr) => {
1397-
expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest,
1397+
$crate::ln::functional_test_utils::expect_payment_failed_conditions(&$node, $expected_payment_hash, $rejected_by_dest,
13981398
$crate::ln::functional_test_utils::PaymentFailedConditions::new().blamed_scid($scid).blamed_chan_closed($chan_closed));
13991399
}
14001400
}
@@ -1407,64 +1407,71 @@ macro_rules! expect_payment_failed {
14071407
$(
14081408
conditions = conditions.expected_htlc_error_data($expected_error_code, &$expected_error_data);
14091409
)*
1410-
expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest, conditions);
1410+
$crate::ln::functional_test_utils::expect_payment_failed_conditions(&$node, $expected_payment_hash, $rejected_by_dest, conditions);
14111411
};
14121412
}
14131413

1414-
#[cfg(test)]
1415-
macro_rules! expect_payment_failed_conditions {
1416-
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $conditions: expr) => {
1417-
let events = $node.node.get_and_clear_pending_events();
1418-
assert_eq!(events.len(), 1);
1419-
let expected_payment_id = match events[0] {
1420-
Event::PaymentPathFailed { ref payment_hash, rejected_by_dest, ref error_code, ref error_data, ref path, ref retry, ref payment_id, ref network_update, .. } => {
1421-
assert_eq!(*payment_hash, $expected_payment_hash, "unexpected payment_hash");
1422-
assert_eq!(rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value");
1423-
assert!(retry.is_some(), "expected retry.is_some()");
1424-
assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
1425-
assert_eq!(retry.as_ref().unwrap().payment_params.payee_pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
1426-
1427-
assert!(error_code.is_some(), "expected error_code.is_some() = true");
1428-
assert!(error_data.is_some(), "expected error_data.is_some() = true");
1429-
if let Some((code, data)) = $conditions.expected_htlc_error_data {
1430-
assert_eq!(error_code.unwrap(), code, "unexpected error code");
1431-
assert_eq!(&error_data.as_ref().unwrap()[..], data, "unexpected error data");
1432-
}
1414+
pub fn expect_payment_failed_conditions<'a, 'b, 'c, 'd, 'e>(
1415+
node: &'a Node<'b, 'c, 'd>, expected_payment_hash: PaymentHash, rejected_by_dest: bool,
1416+
conditions: PaymentFailedConditions<'e>
1417+
) {
1418+
let mut events = node.node.get_and_clear_pending_events();
1419+
assert_eq!(events.len(), 1);
1420+
let expected_payment_id = match events.pop().unwrap() {
1421+
Event::PaymentPathFailed { payment_hash, rejected_by_dest:lulz, path, retry, payment_id, network_update,
1422+
#[cfg(test)]
1423+
error_code,
1424+
#[cfg(test)]
1425+
error_data, .. } => {
1426+
assert_eq!(payment_hash, expected_payment_hash, "unexpected payment_hash");
1427+
assert_eq!(rejected_by_dest, lulz, "unexpected rejected_by_dest value");
1428+
assert!(retry.is_some(), "expected retry.is_some()");
1429+
assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
1430+
assert_eq!(retry.as_ref().unwrap().payment_params.payee_pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
1431+
1432+
#[cfg(test)]
1433+
assert!(error_code.is_some(), "expected error_code.is_some() = true");
1434+
#[cfg(test)]
1435+
assert!(error_data.is_some(), "expected error_data.is_some() = true");
1436+
#[cfg(test)]
1437+
if let Some((code, data)) = conditions.expected_htlc_error_data {
1438+
assert_eq!(error_code.unwrap(), code, "unexpected error code");
1439+
assert_eq!(&error_data.as_ref().unwrap()[..], data, "unexpected error data");
1440+
}
14331441

1434-
if let Some(chan_closed) = $conditions.expected_blamed_chan_closed {
1435-
match network_update {
1436-
&Some($crate::routing::network_graph::NetworkUpdate::ChannelUpdateMessage { ref msg }) if !chan_closed => {
1437-
if let Some(scid) = $conditions.expected_blamed_scid {
1438-
assert_eq!(msg.contents.short_channel_id, scid);
1439-
}
1440-
assert_eq!(msg.contents.flags & 2, 0);
1441-
},
1442-
&Some($crate::routing::network_graph::NetworkUpdate::ChannelClosed { short_channel_id, is_permanent }) if chan_closed => {
1443-
if let Some(scid) = $conditions.expected_blamed_scid {
1444-
assert_eq!(short_channel_id, scid);
1445-
}
1446-
assert!(is_permanent);
1447-
},
1448-
Some(_) => panic!("Unexpected update type"),
1449-
None => panic!("Expected update"),
1450-
}
1442+
if let Some(chan_closed) = conditions.expected_blamed_chan_closed {
1443+
match network_update {
1444+
Some(NetworkUpdate::ChannelUpdateMessage { ref msg }) if !chan_closed => {
1445+
if let Some(scid) = conditions.expected_blamed_scid {
1446+
assert_eq!(msg.contents.short_channel_id, scid);
1447+
}
1448+
assert_eq!(msg.contents.flags & 2, 0);
1449+
},
1450+
Some(NetworkUpdate::ChannelClosed { short_channel_id, is_permanent }) if chan_closed => {
1451+
if let Some(scid) = conditions.expected_blamed_scid {
1452+
assert_eq!(short_channel_id, scid);
1453+
}
1454+
assert!(is_permanent);
1455+
},
1456+
Some(_) => panic!("Unexpected update type"),
1457+
None => panic!("Expected update"),
14511458
}
1459+
}
14521460

1453-
payment_id.unwrap()
1454-
},
1455-
_ => panic!("Unexpected event"),
1456-
};
1457-
if !$conditions.expected_mpp_parts_remain {
1458-
$node.node.abandon_payment(expected_payment_id);
1459-
let events = $node.node.get_and_clear_pending_events();
1460-
assert_eq!(events.len(), 1);
1461-
match events[0] {
1462-
Event::PaymentFailed { ref payment_hash, ref payment_id } => {
1463-
assert_eq!(*payment_hash, $expected_payment_hash, "unexpected second payment_hash");
1464-
assert_eq!(*payment_id, expected_payment_id);
1465-
}
1466-
_ => panic!("Unexpected second event"),
1461+
payment_id.unwrap()
1462+
},
1463+
_ => panic!("Unexpected event"),
1464+
};
1465+
if !conditions.expected_mpp_parts_remain {
1466+
node.node.abandon_payment(expected_payment_id);
1467+
let events = node.node.get_and_clear_pending_events();
1468+
assert_eq!(events.len(), 1);
1469+
match events[0] {
1470+
Event::PaymentFailed { ref payment_hash, ref payment_id } => {
1471+
assert_eq!(*payment_hash, expected_payment_hash, "unexpected second payment_hash");
1472+
assert_eq!(*payment_id, expected_payment_id);
14671473
}
1474+
_ => panic!("Unexpected second event"),
14681475
}
14691476
}
14701477
}

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9657,7 +9657,7 @@ fn do_test_dup_htlc_second_rejected(test_for_second_fail_panic: bool) {
96579657
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
96589658
commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
96599659

9660-
expect_payment_failed_conditions!(nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
9660+
expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
96619661

96629662
claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
96639663
}
@@ -9762,7 +9762,7 @@ fn test_inconsistent_mpp_params() {
97629762
nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
97639763
commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
97649764

9765-
expect_payment_failed_conditions!(nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
9765+
expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
97669766

97679767
nodes[0].node.send_payment_along_path(&route.paths[1], &payment_params_opt, &our_payment_hash, &Some(our_payment_secret), 15_000_000, cur_height, payment_id, &None).unwrap();
97689768
check_added_monitors!(nodes[0], 1);

lightning/src/ln/onion_route_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn test_phantom_onion_hmac_failure() {
854854
.blamed_scid(phantom_scid)
855855
.blamed_chan_closed(true)
856856
.expected_htlc_error_data(0x8000 | 0x4000 | 5, &sha256_of_onion);
857-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, fail_conditions);
857+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
858858
}
859859

860860
#[test]
@@ -927,7 +927,7 @@ fn test_phantom_invalid_onion_payload() {
927927
.blamed_scid(phantom_scid)
928928
.blamed_chan_closed(true)
929929
.expected_htlc_error_data(0x4000 | 22, &error_data);
930-
expect_payment_failed_conditions!(nodes[0], payment_hash, true, fail_conditions);
930+
expect_payment_failed_conditions(&nodes[0], payment_hash, true, fail_conditions);
931931
}
932932

933933
#[test]
@@ -983,7 +983,7 @@ fn test_phantom_final_incorrect_cltv_expiry() {
983983
let mut fail_conditions = PaymentFailedConditions::new()
984984
.blamed_scid(phantom_scid)
985985
.expected_htlc_error_data(18, &error_data);
986-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, fail_conditions);
986+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
987987
}
988988

989989
#[test]
@@ -1028,7 +1028,7 @@ fn test_phantom_failure_too_low_cltv() {
10281028
let mut fail_conditions = PaymentFailedConditions::new()
10291029
.blamed_scid(phantom_scid)
10301030
.expected_htlc_error_data(17, &error_data);
1031-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, fail_conditions);
1031+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
10321032
}
10331033

10341034
#[test]
@@ -1076,7 +1076,7 @@ fn test_phantom_failure_too_low_recv_amt() {
10761076
let mut fail_conditions = PaymentFailedConditions::new()
10771077
.blamed_scid(phantom_scid)
10781078
.expected_htlc_error_data(0x4000 | 15, &error_data);
1079-
expect_payment_failed_conditions!(nodes[0], payment_hash, true, fail_conditions);
1079+
expect_payment_failed_conditions(&nodes[0], payment_hash, true, fail_conditions);
10801080
}
10811081

10821082
#[test]
@@ -1123,7 +1123,7 @@ fn test_phantom_dust_exposure_failure() {
11231123
.blamed_scid(channel.0.contents.short_channel_id)
11241124
.blamed_chan_closed(false)
11251125
.expected_htlc_error_data(0x1000 | 7, &err_data);
1126-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, fail_conditions);
1126+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
11271127
}
11281128

11291129
#[test]
@@ -1174,5 +1174,5 @@ fn test_phantom_failure_reject_payment() {
11741174
let mut fail_conditions = PaymentFailedConditions::new()
11751175
.blamed_scid(phantom_scid)
11761176
.expected_htlc_error_data(0x4000 | 15, &error_data);
1177-
expect_payment_failed_conditions!(nodes[0], payment_hash, true, fail_conditions);
1177+
expect_payment_failed_conditions(&nodes[0], payment_hash, true, fail_conditions);
11781178
}

lightning/src/ln/payment_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn retry_single_path_payment() {
7070
check_added_monitors!(nodes[1], 1);
7171
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
7272
commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
73-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
73+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
7474

7575
// Rebalance the channel so the retry succeeds.
7676
send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
@@ -173,7 +173,7 @@ fn mpp_retry() {
173173
check_added_monitors!(nodes[2], 1);
174174
nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
175175
commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
176-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
176+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
177177

178178
// Rebalance the channel so the second half of the payment can succeed.
179179
send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
@@ -251,7 +251,7 @@ fn do_mpp_receive_timeout(send_partial_mpp: bool) {
251251
check_added_monitors!(nodes[1], 1);
252252
commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates_1_0.commitment_signed, false);
253253

254-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain().expected_htlc_error_data(23, &[][..]));
254+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain().expected_htlc_error_data(23, &[][..]));
255255
} else {
256256
// Pass half of the payment along the second path.
257257
pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash, Some(payment_secret), events.remove(0), true, None);
@@ -514,7 +514,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
514514
confirm_transaction(&nodes[0], &as_htlc_timeout_txn[0]);
515515
}
516516
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
517-
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
517+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
518518

519519
// Finally, retry the payment (which was reloaded from the ChannelMonitor when nodes[0] was
520520
// reloaded) via a route over the new channel, which work without issue and eventually be

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ fn test_inbound_scid_privacy() {
460460
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
461461
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
462462

463-
expect_payment_failed_conditions!(nodes[0], payment_hash_2, false,
463+
expect_payment_failed_conditions(&nodes[0], payment_hash_2, false,
464464
PaymentFailedConditions::new().blamed_scid(last_hop[0].short_channel_id.unwrap())
465465
.blamed_chan_closed(true).expected_htlc_error_data(0x4000|10, &[0; 0]));
466466
}
@@ -537,7 +537,7 @@ fn test_scid_alias_returned() {
537537
err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
538538
err_data.extend_from_slice(&msg.encode());
539539

540-
expect_payment_failed_conditions!(nodes[0], payment_hash, false,
540+
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
541541
PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
542542
.blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data));
543543

@@ -560,7 +560,7 @@ fn test_scid_alias_returned() {
560560
err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes());
561561
err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes());
562562
err_data.extend_from_slice(&msg.encode());
563-
expect_payment_failed_conditions!(nodes[0], payment_hash, false,
563+
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
564564
PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
565565
.blamed_chan_closed(false).expected_htlc_error_data(0x1000|12, &err_data));
566566
}

0 commit comments

Comments
 (0)