Skip to content

Commit 5bccd2e

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 5bccd2e

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 77 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,95 @@ 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+
let count = conditions.expected_destinations.len() + 1;
1290+
assert_eq!(events.len(), count);
1291+
1292+
if conditions.expected_destinations.len() > 0 {
1293+
expect_htlc_handling_failed_destinations!(events, conditions.expected_destinations)
1294+
}
1295+
}}
1296+
}
1297+
1298+
#[macro_export]
1299+
macro_rules! expect_htlc_handling_failed_destinations {
1300+
($events: expr, $destinations: expr) => {{
1301+
for event in $events {
1302+
match event {
1303+
$crate::util::events::Event::PendingHTLCsForwardable { .. } => { },
1304+
$crate::util::events::Event::HTLCHandlingFailed { ref failed_next_destination, .. } => {
1305+
assert!($destinations.contains(&failed_next_destination))
1306+
},
1307+
_ => panic!("Unexpected destination"),
1308+
}
1309+
}
12671310
}}
12681311
}
12691312

1313+
#[macro_export]
1314+
/// Clears (and ignores) a PendingHTLCsForwardable event
1315+
macro_rules! expect_pending_htlcs_forwardable_ignore {
1316+
($node: expr) => {{
1317+
expect_pending_htlcs_forwardable_conditions!($node, $crate::ln::functional_test_utils::HTLCHandlingFailedConditions::new());
1318+
}};
1319+
}
1320+
1321+
#[macro_export]
1322+
/// Clears (and ignores) PendingHTLCsForwardable and HTLCHandlingFailed events
1323+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore {
1324+
($node: expr, $conditions: expr) => {{
1325+
expect_pending_htlcs_forwardable_conditions!($node, $conditions);
1326+
}};
1327+
}
1328+
12701329
#[macro_export]
12711330
/// Handles a PendingHTLCsForwardable event
12721331
macro_rules! expect_pending_htlcs_forwardable {
12731332
($node: expr) => {{
1274-
$crate::expect_pending_htlcs_forwardable_ignore!($node);
1333+
expect_pending_htlcs_forwardable_ignore!($node);
1334+
$node.node.process_pending_htlc_forwards();
1335+
1336+
// Ensure process_pending_htlc_forwards is idempotent.
1337+
$node.node.process_pending_htlc_forwards();
1338+
}};
1339+
}
1340+
1341+
#[macro_export]
1342+
/// Handles a PendingHTLCsForwardable and HTLCHandlingFailed event
1343+
macro_rules! expect_pending_htlcs_forwardable_and_htlc_handling_failed {
1344+
($node: expr, $conditions: expr) => {{
1345+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!($node, $conditions);
12751346
$node.node.process_pending_htlc_forwards();
12761347

12771348
// Ensure process_pending_htlc_forwards is idempotent.

0 commit comments

Comments
 (0)