Skip to content

Commit 5e97712

Browse files
committed
Split claim and fail payment functions to be able to skip one hop
1 parent 8bb1332 commit 5e97712

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
@@ -2742,7 +2742,7 @@ mod tests {
27422742
(our_payment_preimage, our_payment_hash)
27432743
}
27442744

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

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

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

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

28102821
const TEST_FINAL_CLTV: u32 = 32;
@@ -2848,7 +2859,7 @@ mod tests {
28482859
claim_payment(&origin, expected_route, our_payment_preimage);
28492860
}
28502861

2851-
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2862+
fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) {
28522863
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
28532864
{
28542865
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2868,42 +2879,57 @@ mod tests {
28682879

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

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

28912912
prev_node = node;
28922913
}
28932914

2894-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2895-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
2915+
if !skip_last {
2916+
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
28962917

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

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

0 commit comments

Comments
 (0)