Skip to content

Commit 3284e03

Browse files
test utils: refactor fail_payment_along_route for mpp
1 parent 24065c8 commit 3284e03

File tree

2 files changed

+77
-53
lines changed

2 files changed

+77
-53
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,77 +1287,101 @@ pub fn send_payment<'a, 'b, 'c>(origin: &Node<'a, 'b, 'c>, expected_route: &[&No
12871287
claim_payment(&origin, expected_route, our_payment_preimage);
12881288
}
12891289

1290-
pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], skip_last: bool, our_payment_hash: PaymentHash) {
1291-
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1292-
expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
1293-
check_added_monitors!(expected_route.last().unwrap(), 1);
1294-
1295-
let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
1296-
macro_rules! update_fail_dance {
1297-
($node: expr, $prev_node: expr, $last_node: expr) => {
1298-
{
1299-
$node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1300-
commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
1301-
if skip_last && $last_node {
1302-
expect_pending_htlcs_forwardable!($node);
1303-
}
1304-
}
1305-
}
1290+
pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_paths_slice: &[&[&Node<'a, 'b, 'c>]], skip_last: bool, our_payment_hash: PaymentHash) {
1291+
let mut expected_paths: Vec<_> = expected_paths_slice.iter().collect();
1292+
for path in expected_paths.iter() {
1293+
assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
13061294
}
1295+
assert!(expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1296+
expect_pending_htlcs_forwardable!(expected_paths[0].last().unwrap());
1297+
check_added_monitors!(expected_paths[0].last().unwrap(), expected_paths.len());
13071298

1308-
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
1309-
let mut prev_node = expected_route.last().unwrap();
1310-
for (idx, node) in expected_route.iter().rev().enumerate() {
1311-
assert_eq!(expected_next_node, node.node.get_our_node_id());
1312-
if next_msgs.is_some() {
1313-
// We may be the "last node" for the purpose of the commitment dance if we're
1314-
// skipping the last node (implying it is disconnected) and we're the
1315-
// second-to-last node!
1316-
update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
1317-
}
1318-
1319-
let events = node.node.get_and_clear_pending_msg_events();
1320-
if !skip_last || idx != expected_route.len() - 1 {
1321-
assert_eq!(events.len(), 1);
1322-
match events[0] {
1323-
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
1299+
macro_rules! msgs_from_ev {
1300+
($ev: expr) => {
1301+
match $ev {
1302+
&MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
13241303
assert!(update_add_htlcs.is_empty());
13251304
assert!(update_fulfill_htlcs.is_empty());
13261305
assert_eq!(update_fail_htlcs.len(), 1);
13271306
assert!(update_fail_malformed_htlcs.is_empty());
13281307
assert!(update_fee.is_none());
1329-
expected_next_node = node_id.clone();
1330-
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
1308+
((update_fail_htlcs[0].clone(), commitment_signed.clone()), node_id.clone())
13311309
},
13321310
_ => panic!("Unexpected event"),
13331311
}
1334-
} else {
1335-
assert!(events.is_empty());
1336-
}
1337-
if !skip_last && idx == expected_route.len() - 1 {
1338-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
13391312
}
1313+
}
13401314

1341-
prev_node = node;
1315+
let mut per_path_msgs: Vec<((msgs::UpdateFailHTLC, msgs::CommitmentSigned), PublicKey)> = Vec::with_capacity(expected_paths.len());
1316+
let events = expected_paths[0].last().unwrap().node.get_and_clear_pending_msg_events();
1317+
assert_eq!(events.len(), expected_paths.len());
1318+
for ev in events.iter() {
1319+
per_path_msgs.push(msgs_from_ev!(ev));
13421320
}
1321+
per_path_msgs.sort_unstable_by(|(_, node_id_a), (_, node_id_b)| node_id_a.cmp(node_id_b));
1322+
expected_paths.sort_unstable_by(|path_a, path_b| path_a[path_a.len() - 2].node.get_our_node_id().cmp(&path_b[path_b.len() - 2].node.get_our_node_id()));
13431323

1344-
if !skip_last {
1345-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
1324+
for (i, (expected_route, (path_msgs, next_hop))) in expected_paths.iter().zip(per_path_msgs.drain(..)).enumerate() {
1325+
let mut next_msgs = Some(path_msgs);
1326+
let mut expected_next_node = next_hop;
1327+
let mut prev_node = expected_route.last().unwrap();
13461328

1347-
let events = origin_node.node.get_and_clear_pending_events();
1348-
assert_eq!(events.len(), 1);
1349-
match events[0] {
1350-
Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1351-
assert_eq!(payment_hash, our_payment_hash);
1352-
assert!(rejected_by_dest);
1353-
},
1354-
_ => panic!("Unexpected event"),
1329+
for (idx, node) in expected_route.iter().rev().enumerate().skip(1) {
1330+
assert_eq!(expected_next_node, node.node.get_our_node_id());
1331+
if next_msgs.is_some() {
1332+
node.node.handle_update_fail_htlc(&prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1333+
let last_node = !(skip_last && idx == expected_route.len() - 1);
1334+
commitment_signed_dance!(node, prev_node, next_msgs.as_ref().unwrap().1, last_node);
1335+
if !last_node {
1336+
expect_pending_htlcs_forwardable!(node);
1337+
}
1338+
}
1339+
let events = node.node.get_and_clear_pending_msg_events();
1340+
if !skip_last || idx != expected_route.len() - 1 {
1341+
assert_eq!(events.len(), 1);
1342+
match events[0] {
1343+
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
1344+
assert!(update_add_htlcs.is_empty());
1345+
assert!(update_fulfill_htlcs.is_empty());
1346+
assert_eq!(update_fail_htlcs.len(), 1);
1347+
assert!(update_fail_malformed_htlcs.is_empty());
1348+
assert!(update_fee.is_none());
1349+
expected_next_node = node_id.clone();
1350+
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
1351+
},
1352+
_ => panic!("Unexpected event"),
1353+
}
1354+
} else {
1355+
assert!(events.is_empty());
1356+
}
1357+
if !skip_last && idx == expected_route.len() - 1 {
1358+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
1359+
}
1360+
1361+
prev_node = node;
1362+
}
1363+
1364+
if !skip_last {
1365+
let prev_node = expected_route.first().unwrap();
1366+
origin_node.node.handle_update_fail_htlc(&prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1367+
check_added_monitors!(origin_node, 0);
1368+
assert!(origin_node.node.get_and_clear_pending_msg_events().is_empty());
1369+
commitment_signed_dance!(origin_node, prev_node, next_msgs.as_ref().unwrap().1, false);
1370+
let events = origin_node.node.get_and_clear_pending_events();
1371+
assert_eq!(events.len(), 1);
1372+
match events[0] {
1373+
Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1374+
assert_eq!(payment_hash, our_payment_hash);
1375+
assert!(rejected_by_dest);
1376+
},
1377+
_ => panic!("Unexpected event"),
1378+
}
13551379
}
13561380
}
13571381
}
13581382

1359-
pub fn fail_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], our_payment_hash: PaymentHash) {
1360-
fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
1383+
pub fn fail_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path: &[&Node<'a, 'b, 'c>], our_payment_hash: PaymentHash) {
1384+
fail_payment_along_route(origin_node, &[&expected_path[..]], false, our_payment_hash);
13611385
}
13621386

13631387
pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3308,7 +3308,7 @@ fn test_simple_peer_disconnect() {
33083308
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
33093309

33103310
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_preimage_3);
3311-
fail_payment_along_route(&nodes[0], &[&nodes[1], &nodes[2]], true, payment_hash_5);
3311+
fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_hash_5);
33123312

33133313
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (1, 0), (1, 0), (false, false));
33143314
{

0 commit comments

Comments
 (0)