Skip to content

Commit 8c9615e

Browse files
committed
DRY up payment failure macros in functional_test_utils
... with a more extensible expectation-checking framework for them.
1 parent 3ec529d commit 8c9615e

File tree

5 files changed

+81
-41
lines changed

5 files changed

+81
-41
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,11 @@ pub fn create_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(node_a: &'a Node<'b,
336336
}
337337

338338
#[macro_export]
339+
/// Gets an RAA and CS which were sent in response to a commitment update
339340
macro_rules! get_revoke_commit_msgs {
340341
($node: expr, $node_id: expr) => {
341342
{
342-
use util::events::MessageSendEvent;
343+
use $crate::util::events::MessageSendEvent;
343344
let events = $node.node.get_and_clear_pending_msg_events();
344345
assert_eq!(events.len(), 2);
345346
(match events[0] {
@@ -400,8 +401,8 @@ macro_rules! get_event {
400401
}
401402
}
402403

403-
#[cfg(test)]
404404
#[macro_export]
405+
/// Gets an UpdateHTLCs MessageSendEvent
405406
macro_rules! get_htlc_update_msgs {
406407
($node: expr, $node_id: expr) => {
407408
{
@@ -933,6 +934,9 @@ impl SendEvent {
933934
}
934935
}
935936

937+
#[macro_export]
938+
/// Performs the "commitment signed dance" - the series of message exchanges which occur after a
939+
/// commitment update.
936940
macro_rules! commitment_signed_dance {
937941
($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
938942
{
@@ -999,7 +1003,7 @@ macro_rules! commitment_signed_dance {
9991003
{
10001004
commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
10011005
if $fail_backwards {
1002-
expect_pending_htlcs_forwardable!($node_a);
1006+
$crate::expect_pending_htlcs_forwardable!($node_a);
10031007
check_added_monitors!($node_a, 1);
10041008

10051009
let channel_state = $node_a.node.channel_state.lock().unwrap();
@@ -1057,6 +1061,8 @@ macro_rules! get_route_and_payment_hash {
10571061
}}
10581062
}
10591063

1064+
#[macro_export]
1065+
/// Clears (and ignores) a PendingHTLCsForwardable event
10601066
macro_rules! expect_pending_htlcs_forwardable_ignore {
10611067
($node: expr) => {{
10621068
let events = $node.node.get_and_clear_pending_events();
@@ -1068,9 +1074,11 @@ macro_rules! expect_pending_htlcs_forwardable_ignore {
10681074
}}
10691075
}
10701076

1077+
#[macro_export]
1078+
/// Handles a PendingHTLCsForwardable event
10711079
macro_rules! expect_pending_htlcs_forwardable {
10721080
($node: expr) => {{
1073-
expect_pending_htlcs_forwardable_ignore!($node);
1081+
$crate::expect_pending_htlcs_forwardable_ignore!($node);
10741082
$node.node.process_pending_htlc_forwards();
10751083

10761084
// Ensure process_pending_htlc_forwards is idempotent.
@@ -1199,60 +1207,95 @@ macro_rules! expect_payment_forwarded {
11991207
}
12001208
}
12011209

1210+
pub struct PaymentFailedConditions<'a> {
1211+
pub(crate) expected_htlc_error_data: Option<(u16, &'a [u8])>,
1212+
pub(crate) expected_blamed_scid: Option<u64>,
1213+
pub(crate) expected_blamed_chan_closed: Option<bool>,
1214+
}
1215+
1216+
impl<'a> PaymentFailedConditions<'a> {
1217+
pub fn new() -> Self {
1218+
Self {
1219+
expected_htlc_error_data: None,
1220+
expected_blamed_scid: None,
1221+
expected_blamed_chan_closed: None,
1222+
}
1223+
}
1224+
pub fn blamed_scid(mut self, scid: u64) -> Self {
1225+
self.expected_blamed_scid = Some(scid);
1226+
self
1227+
}
1228+
pub fn blamed_chan_closed(mut self, closed: bool) -> Self {
1229+
self.expected_blamed_chan_closed = Some(closed);
1230+
self
1231+
}
1232+
pub fn expected_htlc_error_data(mut self, code: u16, data: &'a [u8]) -> Self {
1233+
self.expected_htlc_error_data = Some((code, data));
1234+
self
1235+
}
1236+
}
1237+
12021238
#[cfg(test)]
12031239
macro_rules! expect_payment_failed_with_update {
12041240
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $scid: expr, $chan_closed: expr) => {
1205-
let events = $node.node.get_and_clear_pending_events();
1206-
assert_eq!(events.len(), 1);
1207-
match events[0] {
1208-
Event::PaymentPathFailed { ref payment_hash, rejected_by_dest, ref network_update, ref error_code, ref error_data, ref path, ref retry, .. } => {
1209-
assert_eq!(*payment_hash, $expected_payment_hash, "unexpected payment_hash");
1210-
assert_eq!(rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value");
1211-
assert!(retry.is_some(), "expected retry.is_some()");
1212-
assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
1213-
assert_eq!(retry.as_ref().unwrap().payee.pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
1214-
assert!(error_code.is_some(), "expected error_code.is_some() = true");
1215-
assert!(error_data.is_some(), "expected error_data.is_some() = true");
1216-
match network_update {
1217-
&Some(NetworkUpdate::ChannelUpdateMessage { ref msg }) if !$chan_closed => {
1218-
assert_eq!(msg.contents.short_channel_id, $scid);
1219-
assert_eq!(msg.contents.flags & 2, 0);
1220-
},
1221-
&Some(NetworkUpdate::ChannelClosed { short_channel_id, is_permanent }) if $chan_closed => {
1222-
assert_eq!(short_channel_id, $scid);
1223-
assert!(is_permanent);
1224-
},
1225-
Some(_) => panic!("Unexpected update type"),
1226-
None => panic!("Expected update"),
1227-
}
1228-
},
1229-
_ => panic!("Unexpected event"),
1230-
}
1241+
expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest,
1242+
$crate::ln::functional_test_utils::PaymentFailedConditions::new().blamed_scid($scid).blamed_chan_closed($chan_closed));
12311243
}
12321244
}
12331245

12341246
#[cfg(test)]
12351247
macro_rules! expect_payment_failed {
12361248
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
1249+
let mut conditions = $crate::ln::functional_test_utils::PaymentFailedConditions::new();
1250+
$(
1251+
conditions = conditions.expected_htlc_error_data($expected_error_code, &$expected_error_data);
1252+
)*
1253+
expect_payment_failed_conditions!($node, $expected_payment_hash, $rejected_by_dest, conditions);
1254+
};
1255+
}
1256+
1257+
#[cfg(test)]
1258+
macro_rules! expect_payment_failed_conditions {
1259+
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr, $conditions: expr) => {
12371260
let events = $node.node.get_and_clear_pending_events();
12381261
assert_eq!(events.len(), 1);
12391262
match events[0] {
1240-
Event::PaymentPathFailed { ref payment_hash, rejected_by_dest, network_update: _, ref error_code, ref error_data, ref path, ref retry, .. } => {
1263+
Event::PaymentPathFailed { ref payment_hash, rejected_by_dest, ref error_code, ref error_data, ref path, ref retry, ref network_update, .. } => {
12411264
assert_eq!(*payment_hash, $expected_payment_hash, "unexpected payment_hash");
12421265
assert_eq!(rejected_by_dest, $rejected_by_dest, "unexpected rejected_by_dest value");
12431266
assert!(retry.is_some(), "expected retry.is_some()");
12441267
assert_eq!(retry.as_ref().unwrap().final_value_msat, path.last().unwrap().fee_msat, "Retry amount should match last hop in path");
12451268
assert_eq!(retry.as_ref().unwrap().payee.pubkey, path.last().unwrap().pubkey, "Retry payee node_id should match last hop in path");
1269+
12461270
assert!(error_code.is_some(), "expected error_code.is_some() = true");
12471271
assert!(error_data.is_some(), "expected error_data.is_some() = true");
1248-
$(
1249-
assert_eq!(error_code.unwrap(), $expected_error_code, "unexpected error code");
1250-
assert_eq!(&error_data.as_ref().unwrap()[..], $expected_error_data, "unexpected error data");
1251-
)*
1272+
if let Some((code, data)) = $conditions.expected_htlc_error_data {
1273+
assert_eq!(error_code.unwrap(), code, "unexpected error code");
1274+
assert_eq!(&error_data.as_ref().unwrap()[..], data, "unexpected error data");
1275+
}
1276+
1277+
if let Some(chan_closed) = $conditions.expected_blamed_chan_closed {
1278+
match network_update {
1279+
&Some($crate::routing::network_graph::NetworkUpdate::ChannelUpdateMessage { ref msg }) if !chan_closed => {
1280+
if let Some(scid) = $conditions.expected_blamed_scid {
1281+
assert_eq!(msg.contents.short_channel_id, scid);
1282+
}
1283+
assert_eq!(msg.contents.flags & 2, 0);
1284+
},
1285+
&Some($crate::routing::network_graph::NetworkUpdate::ChannelClosed { short_channel_id, is_permanent }) if chan_closed => {
1286+
if let Some(scid) = $conditions.expected_blamed_scid {
1287+
assert_eq!(short_channel_id, scid);
1288+
}
1289+
assert!(is_permanent);
1290+
},
1291+
Some(_) => panic!("Unexpected update type"),
1292+
None => panic!("Expected update"),
1293+
}
1294+
}
12521295
},
12531296
_ => panic!("Unexpected event"),
1254-
}
1255-
}
1297+
};
1298+
};
12561299
}
12571300

12581301
pub fn send_along_route_with_secret<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, route: Route, expected_paths: &[&[&Node<'a, 'b, 'c>]], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: PaymentSecret) -> PaymentId {

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAAC
2323
use ln::channel::{Channel, ChannelError};
2424
use ln::{chan_utils, onion_utils};
2525
use ln::chan_utils::{HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, HTLCOutputInCommitment};
26-
use routing::network_graph::{NetworkUpdate, RoutingFees};
26+
use routing::network_graph::RoutingFees;
2727
use routing::router::{Payee, Route, RouteHop, RouteHint, RouteHintHop, RouteParameters, find_route, get_route};
2828
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
2929
use ln::msgs;

lightning/src/ln/monitor_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use ln::channelmanager::BREAKDOWN_TIMEOUT;
1616
use ln::features::InitFeatures;
1717
use ln::msgs::ChannelMessageHandler;
1818
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
19-
use routing::network_graph::NetworkUpdate;
2019

2120
use bitcoin::blockdata::script::Builder;
2221
use bitcoin::blockdata::opcodes;

lightning/src/ln/reorg_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use chain::{Confirm, Watch};
1515
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs};
1616
use ln::features::InitFeatures;
1717
use ln::msgs::ChannelMessageHandler;
18-
use routing::network_graph::NetworkUpdate;
1918
use util::enforcing_trait_impls::EnforcingSigner;
2019
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
2120
use util::test_utils;

lightning/src/ln/shutdown_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use chain::keysinterface::KeysInterface;
1313
use chain::transaction::OutPoint;
1414
use ln::channelmanager::PaymentSendFailure;
1515
use routing::router::{Payee, get_route};
16-
use routing::network_graph::NetworkUpdate;
1716
use ln::features::{InitFeatures, InvoiceFeatures};
1817
use ln::msgs;
1918
use ln::msgs::{ChannelMessageHandler, ErrorAction};

0 commit comments

Comments
 (0)