Skip to content

Commit 5737b32

Browse files
committed
Split claim and fail payment functions to be able to skip one hop
1 parent 4f36f37 commit 5737b32

File tree

1 file changed

+73
-47
lines changed

1 file changed

+73
-47
lines changed

src/ln/channelmanager.rs

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,7 @@ mod tests {
27412741
(our_payment_preimage, our_payment_hash)
27422742
}
27432743

2744-
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2744+
fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: [u8; 32]) {
27452745
assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
27462746
{
27472747
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2770,40 +2770,51 @@ mod tests {
27702770

27712771
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
27722772
let mut prev_node = expected_route.last().unwrap();
2773-
for node in expected_route.iter().rev() {
2773+
for (idx, node) in expected_route.iter().rev().enumerate() {
27742774
assert_eq!(expected_next_node, node.node.get_our_node_id());
27752775
if next_msgs.is_some() {
27762776
update_fulfill_dance!(node, prev_node, false);
27772777
}
27782778

27792779
let events = node.node.get_and_clear_pending_events();
2780+
if !skip_last || idx != expected_route.len() - 1 {
2781+
assert_eq!(events.len(), 1);
2782+
match events[0] {
2783+
Event::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 commitment_signed } } => {
2784+
assert!(update_add_htlcs.is_empty());
2785+
assert_eq!(update_fulfill_htlcs.len(), 1);
2786+
assert!(update_fail_htlcs.is_empty());
2787+
assert!(update_fail_malformed_htlcs.is_empty());
2788+
expected_next_node = node_id.clone();
2789+
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2790+
},
2791+
_ => panic!("Unexpected event"),
2792+
}
2793+
} else {
2794+
assert!(events.is_empty());
2795+
}
2796+
if !skip_last && idx == expected_route.len() - 1 {
2797+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2798+
}
2799+
2800+
prev_node = node;
2801+
}
2802+
2803+
if !skip_last {
2804+
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2805+
let events = origin_node.node.get_and_clear_pending_events();
27802806
assert_eq!(events.len(), 1);
27812807
match events[0] {
2782-
Event::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 commitment_signed } } => {
2783-
assert!(update_add_htlcs.is_empty());
2784-
assert_eq!(update_fulfill_htlcs.len(), 1);
2785-
assert!(update_fail_htlcs.is_empty());
2786-
assert!(update_fail_malformed_htlcs.is_empty());
2787-
expected_next_node = node_id.clone();
2788-
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2808+
Event::PaymentSent { payment_preimage } => {
2809+
assert_eq!(payment_preimage, our_payment_preimage);
27892810
},
27902811
_ => panic!("Unexpected event"),
2791-
};
2792-
2793-
prev_node = node;
2812+
}
27942813
}
2814+
}
27952815

2796-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2797-
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2798-
2799-
let events = origin_node.node.get_and_clear_pending_events();
2800-
assert_eq!(events.len(), 1);
2801-
match events[0] {
2802-
Event::PaymentSent { payment_preimage } => {
2803-
assert_eq!(payment_preimage, our_payment_preimage);
2804-
},
2805-
_ => panic!("Unexpected event"),
2806-
}
2816+
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2817+
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
28072818
}
28082819

28092820
const TEST_FINAL_CLTV: u32 = 32;
@@ -2847,7 +2858,7 @@ mod tests {
28472858
claim_payment(&origin, expected_route, our_payment_preimage);
28482859
}
28492860

2850-
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2861+
fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) {
28512862
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
28522863
{
28532864
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2867,42 +2878,57 @@ mod tests {
28672878

28682879
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
28692880
let mut prev_node = expected_route.last().unwrap();
2870-
for node in expected_route.iter().rev() {
2881+
for (idx, node) in expected_route.iter().rev().enumerate() {
28712882
assert_eq!(expected_next_node, node.node.get_our_node_id());
28722883
if next_msgs.is_some() {
2873-
update_fail_dance!(node, prev_node, false);
2884+
// We may be the "last node" for the purpose of the commitment dance if we're
2885+
// skipping the last node (implying it is disconnected) and we're the
2886+
// second-to-last node!
2887+
update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
28742888
}
28752889

28762890
let events = node.node.get_and_clear_pending_events();
2877-
assert_eq!(events.len(), 1);
2878-
match events[0] {
2879-
Event::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 commitment_signed } } => {
2880-
assert!(update_add_htlcs.is_empty());
2881-
assert!(update_fulfill_htlcs.is_empty());
2882-
assert_eq!(update_fail_htlcs.len(), 1);
2883-
assert!(update_fail_malformed_htlcs.is_empty());
2884-
expected_next_node = node_id.clone();
2885-
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2886-
},
2887-
_ => panic!("Unexpected event"),
2888-
};
2891+
if !skip_last || idx != expected_route.len() - 1 {
2892+
assert_eq!(events.len(), 1);
2893+
match events[0] {
2894+
Event::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 commitment_signed } } => {
2895+
assert!(update_add_htlcs.is_empty());
2896+
assert!(update_fulfill_htlcs.is_empty());
2897+
assert_eq!(update_fail_htlcs.len(), 1);
2898+
assert!(update_fail_malformed_htlcs.is_empty());
2899+
expected_next_node = node_id.clone();
2900+
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2901+
},
2902+
_ => panic!("Unexpected event"),
2903+
}
2904+
} else {
2905+
assert!(events.is_empty());
2906+
}
2907+
if !skip_last && idx == expected_route.len() - 1 {
2908+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2909+
}
28892910

28902911
prev_node = node;
28912912
}
28922913

2893-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2894-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
2914+
if !skip_last {
2915+
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
28952916

2896-
let events = origin_node.node.get_and_clear_pending_events();
2897-
assert_eq!(events.len(), 1);
2898-
match events[0] {
2899-
Event::PaymentFailed { payment_hash } => {
2900-
assert_eq!(payment_hash, our_payment_hash);
2901-
},
2902-
_ => panic!("Unexpected event"),
2917+
let events = origin_node.node.get_and_clear_pending_events();
2918+
assert_eq!(events.len(), 1);
2919+
match events[0] {
2920+
Event::PaymentFailed { payment_hash } => {
2921+
assert_eq!(payment_hash, our_payment_hash);
2922+
},
2923+
_ => panic!("Unexpected event"),
2924+
}
29032925
}
29042926
}
29052927

2928+
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2929+
fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
2930+
}
2931+
29062932
fn create_network(node_count: usize) -> Vec<Node> {
29072933
let mut nodes = Vec::new();
29082934
let mut rng = thread_rng();

0 commit comments

Comments
 (0)