Skip to content

Commit 264f181

Browse files
committed
Add utils to handle HTLC handling failure reason
We add `HTLCHandlingFailedConditions` to express the failure parameters, that will be enforced by a new macro, `expect_pending_htlcs_forwardable_conditions`.
1 parent 77a7457 commit 264f181

File tree

1 file changed

+85
-6
lines changed

1 file changed

+85
-6
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use util::enforcing_trait_impls::EnforcingSigner;
2424
use util::scid_utils;
2525
use util::test_utils;
2626
use util::test_utils::{panicking, TestChainMonitor};
27-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
27+
use util::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
2828
use util::errors::APIError;
2929
use util::config::UserConfig;
3030
use util::ser::{ReadableArgs, Writeable};
@@ -1254,24 +1254,101 @@ macro_rules! get_route_and_payment_hash {
12541254
}}
12551255
}
12561256

1257+
pub struct HTLCHandlingFailedConditions {
1258+
pub expected_pending_htlcs_forwardable: bool,
1259+
pub expected_htlc_processing_failed: Option<u32>,
1260+
pub expected_destination: Option<HTLCDestination>,
1261+
}
1262+
1263+
impl HTLCHandlingFailedConditions {
1264+
pub fn new() -> Self {
1265+
Self {
1266+
expected_pending_htlcs_forwardable: false,
1267+
expected_htlc_processing_failed: None,
1268+
expected_destination: None,
1269+
}
1270+
}
1271+
1272+
pub fn htlc_processing_failed_with_count(mut self, count: u32) -> Self {
1273+
self.expected_htlc_processing_failed = Some(count);
1274+
self
1275+
}
1276+
1277+
pub fn htlc_processing_failed_with_reason(mut self, reason: HTLCDestination) -> Self {
1278+
self.expected_htlc_processing_failed = Some(1);
1279+
self.expected_destination = Some(reason);
1280+
self
1281+
}
1282+
1283+
pub fn htlc_processing_failed_with_count_and_reason(mut self, count: u32, reason: HTLCDestination) -> Self {
1284+
self.expected_htlc_processing_failed = Some(count);
1285+
self.expected_destination = Some(reason);
1286+
self
1287+
}
1288+
}
1289+
12571290
#[macro_export]
1258-
/// Clears (and ignores) a PendingHTLCsForwardable event
1259-
macro_rules! expect_pending_htlcs_forwardable_ignore {
1260-
($node: expr) => {{
1291+
macro_rules! expect_pending_htlcs_forwardable_conditions {
1292+
($node: expr, $conditions: expr) => {{
12611293
let events = $node.node.get_and_clear_pending_events();
1262-
assert_eq!(events.len(), 1);
12631294
match events[0] {
12641295
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
12651296
_ => panic!("Unexpected event"),
12661297
};
1298+
1299+
if let Some(count) = $conditions.expected_htlc_processing_failed {
1300+
assert_eq!(events.len() as u32, count + 1u32);
1301+
} else {
1302+
assert_eq!(events.len(), 1);
1303+
}
1304+
1305+
if let Some(reason) = $conditions.expected_destination {
1306+
for event in events {
1307+
match event {
1308+
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
1309+
$crate::util::events::Event::HTLCHandlingFailed { ref failed_next_destination, .. } => {
1310+
assert_eq!(reason, failed_next_destination.clone());
1311+
},
1312+
_ => panic!("Unexpected event"),
1313+
}
1314+
}
1315+
}
12671316
}}
12681317
}
12691318

1319+
#[macro_export]
1320+
/// Clears (and ignores) a PendingHTLCsForwardable event
1321+
macro_rules! expect_pending_htlcs_forwardable_ignore {
1322+
($node: expr) => {{
1323+
expect_pending_htlcs_forwardable_conditions!($node, $crate::ln::functional_test_utils::HTLCHandlingFailedConditions::new());
1324+
}};
1325+
}
1326+
1327+
#[macro_export]
1328+
/// Clears (and ignores) PendingHTLCsForwardable and HTLCHandlingFailed events
1329+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore {
1330+
($node: expr, $conditions: expr) => {{
1331+
expect_pending_htlcs_forwardable_conditions!($node, $conditions);
1332+
}};
1333+
}
1334+
12701335
#[macro_export]
12711336
/// Handles a PendingHTLCsForwardable event
12721337
macro_rules! expect_pending_htlcs_forwardable {
12731338
($node: expr) => {{
1274-
$crate::expect_pending_htlcs_forwardable_ignore!($node);
1339+
expect_pending_htlcs_forwardable_ignore!($node);
1340+
$node.node.process_pending_htlc_forwards();
1341+
1342+
// Ensure process_pending_htlc_forwards is idempotent.
1343+
$node.node.process_pending_htlc_forwards();
1344+
}};
1345+
}
1346+
1347+
#[macro_export]
1348+
/// Handles a PendingHTLCsForwardable and HTLCHandlingFailed event
1349+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
1350+
($node: expr, $conditions: expr) => {{
1351+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!($node, $conditions);
12751352
$node.node.process_pending_htlc_forwards();
12761353

12771354
// Ensure process_pending_htlc_forwards is idempotent.
@@ -1282,6 +1359,8 @@ macro_rules! expect_pending_htlcs_forwardable {
12821359
#[cfg(test)]
12831360
macro_rules! expect_pending_htlcs_forwardable_from_events {
12841361
($node: expr, $events: expr, $ignore: expr) => {{
1362+
// We need to clear pending events since there may possibly be `PaymentForwardingFailed` events here
1363+
$node.node.get_and_clear_pending_events();
12851364
assert_eq!($events.len(), 1);
12861365
match $events[0] {
12871366
Event::PendingHTLCsForwardable { .. } => { },

0 commit comments

Comments
 (0)