Skip to content

Commit f6f950d

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

File tree

2 files changed

+77
-58
lines changed

2 files changed

+77
-58
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,77 +1287,96 @@ 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);
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());
1294+
}
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());
1298+
1299+
let mut per_path_msgs: Vec<((msgs::UpdateFailHTLC, msgs::CommitmentSigned), PublicKey)> = Vec::with_capacity(expected_paths.len());
1300+
let events = expected_paths[0].last().unwrap().node.get_and_clear_pending_msg_events();
1301+
assert_eq!(events.len(), expected_paths.len());
1302+
for ev in events.iter() {
1303+
let (update_fail, commitment_signed, node_id) = match ev {
1304+
&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 } } => {
1305+
assert!(update_add_htlcs.is_empty());
1306+
assert!(update_fulfill_htlcs.is_empty());
1307+
assert_eq!(update_fail_htlcs.len(), 1);
1308+
assert!(update_fail_malformed_htlcs.is_empty());
1309+
assert!(update_fee.is_none());
1310+
(update_fail_htlcs[0].clone(), commitment_signed.clone(), node_id.clone())
1311+
},
1312+
_ => panic!("Unexpected event"),
1313+
};
1314+
per_path_msgs.push(((update_fail, commitment_signed), node_id));
1315+
}
1316+
per_path_msgs.sort_unstable_by(|(_, node_id_a), (_, node_id_b)| node_id_a.cmp(node_id_b));
1317+
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()));
1318+
1319+
for (i, (expected_route, (path_msgs, next_hop))) in expected_paths.iter().zip(per_path_msgs.drain(..)).enumerate() {
1320+
let mut next_msgs = Some(path_msgs);
1321+
let mut expected_next_node = next_hop;
1322+
let mut prev_node = expected_route.last().unwrap();
1323+
1324+
for (idx, node) in expected_route.iter().rev().enumerate().skip(1) {
1325+
assert_eq!(expected_next_node, node.node.get_our_node_id());
1326+
let update_next_node = !skip_last || idx != expected_route.len() - 1;
1327+
if next_msgs.is_some() {
1328+
node.node.handle_update_fail_htlc(&prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1329+
commitment_signed_dance!(node, prev_node, next_msgs.as_ref().unwrap().1, update_next_node);
1330+
if !update_next_node {
1331+
expect_pending_htlcs_forwardable!(node);
13031332
}
13041333
}
1305-
}
1306-
}
1334+
let events = node.node.get_and_clear_pending_msg_events();
1335+
if update_next_node {
1336+
assert_eq!(events.len(), 1);
1337+
match events[0] {
1338+
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 } } => {
1339+
assert!(update_add_htlcs.is_empty());
1340+
assert!(update_fulfill_htlcs.is_empty());
1341+
assert_eq!(update_fail_htlcs.len(), 1);
1342+
assert!(update_fail_malformed_htlcs.is_empty());
1343+
assert!(update_fee.is_none());
1344+
expected_next_node = node_id.clone();
1345+
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
1346+
},
1347+
_ => panic!("Unexpected event"),
1348+
}
1349+
} else {
1350+
assert!(events.is_empty());
1351+
}
1352+
if !skip_last && idx == expected_route.len() - 1 {
1353+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
1354+
}
13071355

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);
1356+
prev_node = node;
13171357
}
13181358

1319-
let events = node.node.get_and_clear_pending_msg_events();
1320-
if !skip_last || idx != expected_route.len() - 1 {
1359+
if !skip_last {
1360+
let prev_node = expected_route.first().unwrap();
1361+
origin_node.node.handle_update_fail_htlc(&prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1362+
check_added_monitors!(origin_node, 0);
1363+
assert!(origin_node.node.get_and_clear_pending_msg_events().is_empty());
1364+
commitment_signed_dance!(origin_node, prev_node, next_msgs.as_ref().unwrap().1, false);
1365+
let events = origin_node.node.get_and_clear_pending_events();
13211366
assert_eq!(events.len(), 1);
13221367
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 } } => {
1324-
assert!(update_add_htlcs.is_empty());
1325-
assert!(update_fulfill_htlcs.is_empty());
1326-
assert_eq!(update_fail_htlcs.len(), 1);
1327-
assert!(update_fail_malformed_htlcs.is_empty());
1328-
assert!(update_fee.is_none());
1329-
expected_next_node = node_id.clone();
1330-
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
1368+
Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1369+
assert_eq!(payment_hash, our_payment_hash);
1370+
assert!(rejected_by_dest);
13311371
},
13321372
_ => panic!("Unexpected event"),
13331373
}
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());
1339-
}
1340-
1341-
prev_node = node;
1342-
}
1343-
1344-
if !skip_last {
1345-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
1346-
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"),
13551374
}
13561375
}
13571376
}
13581377

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);
1378+
pub fn fail_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path: &[&Node<'a, 'b, 'c>], our_payment_hash: PaymentHash) {
1379+
fail_payment_along_route(origin_node, &[&expected_path[..]], false, our_payment_hash);
13611380
}
13621381

13631382
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)