Skip to content

Commit 01b1d09

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 50b8ba8 commit 01b1d09

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 83 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<usize>,
1260+
pub expected_destinations: Vec<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_destinations: vec![],
1269+
}
1270+
}
1271+
1272+
pub fn with_reason(mut self, reason: HTLCDestination) -> Self {
1273+
self.expected_htlc_processing_failed = Some(1);
1274+
self.expected_destinations = vec![reason];
1275+
self
1276+
}
1277+
1278+
pub fn with_reasons(mut self, reasons: Vec<HTLCDestination>) -> Self {
1279+
self.expected_htlc_processing_failed = Some(reasons.len());
1280+
self.expected_destinations = reasons;
1281+
self
1282+
}
1283+
}
1284+
12571285
#[macro_export]
1258-
/// Clears (and ignores) a PendingHTLCsForwardable event
1259-
macro_rules! expect_pending_htlcs_forwardable_ignore {
1260-
($node: expr) => {{
1286+
macro_rules! expect_pending_htlcs_forwardable_conditions {
1287+
($node: expr, $conditions: expr) => {{
1288+
let conditions = $conditions;
12611289
let events = $node.node.get_and_clear_pending_events();
1262-
assert_eq!(events.len(), 1);
12631290
match events[0] {
12641291
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
12651292
_ => panic!("Unexpected event"),
12661293
};
1294+
1295+
let count = conditions.expected_htlc_processing_failed.unwrap_or(0) + 1;
1296+
assert_eq!(events.len(), count);
1297+
1298+
if conditions.expected_destinations.len() > 0 {
1299+
expect_htlc_handling_failed_destinations!(events, conditions.expected_destinations)
1300+
}
1301+
}}
1302+
}
1303+
1304+
#[macro_export]
1305+
macro_rules! expect_htlc_handling_failed_destinations {
1306+
($events: expr, $destinations: expr) => {{
1307+
for event in $events {
1308+
match event {
1309+
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
1310+
$crate::util::events::Event::HTLCHandlingFailed { ref failed_next_destination, .. } => {
1311+
assert!($destinations.contains(&failed_next_destination))
1312+
},
1313+
_ => panic!("Unexpected destination"),
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.

0 commit comments

Comments
 (0)