Skip to content

Commit 056fe35

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 17e6c37 commit 056fe35

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 74 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,92 @@ macro_rules! get_route_and_payment_hash {
12541254
}}
12551255
}
12561256

1257+
pub struct HTLCHandlingFailedConditions {
1258+
pub expected_destinations: Vec<HTLCDestination>,
1259+
}
1260+
1261+
impl HTLCHandlingFailedConditions {
1262+
pub fn new() -> Self {
1263+
Self {
1264+
expected_destinations: vec![],
1265+
}
1266+
}
1267+
1268+
pub fn with_reason(mut self, reason: HTLCDestination) -> Self {
1269+
self.expected_destinations = vec![reason];
1270+
self
1271+
}
1272+
1273+
pub fn with_reasons(mut self, reasons: Vec<HTLCDestination>) -> Self {
1274+
self.expected_destinations = reasons;
1275+
self
1276+
}
1277+
}
1278+
12571279
#[macro_export]
1258-
/// Clears (and ignores) a PendingHTLCsForwardable event
1259-
macro_rules! expect_pending_htlcs_forwardable_ignore {
1260-
($node: expr) => {{
1280+
macro_rules! expect_pending_htlcs_forwardable_conditions {
1281+
($node: expr, $conditions: expr) => {{
1282+
let conditions = $conditions;
12611283
let events = $node.node.get_and_clear_pending_events();
1262-
assert_eq!(events.len(), 1);
12631284
match events[0] {
12641285
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
12651286
_ => panic!("Unexpected event"),
12661287
};
1288+
1289+
if conditions.expected_destinations.len() > 0 {
1290+
expect_htlc_handling_failed_destinations!(events, conditions.expected_destinations)
1291+
}
12671292
}}
12681293
}
12691294

1295+
#[macro_export]
1296+
macro_rules! expect_htlc_handling_failed_destinations {
1297+
($events: expr, $destinations: expr) => {{
1298+
for event in $events {
1299+
match event {
1300+
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
1301+
$crate::util::events::Event::HTLCHandlingFailed { ref failed_next_destination, .. } => {
1302+
assert!($destinations.contains(&failed_next_destination))
1303+
},
1304+
_ => panic!("Unexpected destination"),
1305+
}
1306+
}
1307+
}}
1308+
}
1309+
1310+
#[macro_export]
1311+
/// Clears (and ignores) a PendingHTLCsForwardable event
1312+
macro_rules! expect_pending_htlcs_forwardable_ignore {
1313+
($node: expr) => {{
1314+
expect_pending_htlcs_forwardable_conditions!($node, $crate::ln::functional_test_utils::HTLCHandlingFailedConditions::new());
1315+
}};
1316+
}
1317+
1318+
#[macro_export]
1319+
/// Clears (and ignores) PendingHTLCsForwardable and HTLCHandlingFailed events
1320+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore {
1321+
($node: expr, $conditions: expr) => {{
1322+
expect_pending_htlcs_forwardable_conditions!($node, $conditions);
1323+
}};
1324+
}
1325+
12701326
#[macro_export]
12711327
/// Handles a PendingHTLCsForwardable event
12721328
macro_rules! expect_pending_htlcs_forwardable {
12731329
($node: expr) => {{
1274-
$crate::expect_pending_htlcs_forwardable_ignore!($node);
1330+
expect_pending_htlcs_forwardable_ignore!($node);
1331+
$node.node.process_pending_htlc_forwards();
1332+
1333+
// Ensure process_pending_htlc_forwards is idempotent.
1334+
$node.node.process_pending_htlc_forwards();
1335+
}};
1336+
}
1337+
1338+
#[macro_export]
1339+
/// Handles a PendingHTLCsForwardable and HTLCHandlingFailed event
1340+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
1341+
($node: expr, $conditions: expr) => {{
1342+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!($node, $conditions);
12751343
$node.node.process_pending_htlc_forwards();
12761344

12771345
// Ensure process_pending_htlc_forwards is idempotent.

0 commit comments

Comments
 (0)