Skip to content

Commit e6415c6

Browse files
committed
Split claim and fail payment functions to be able to skip one hop
1 parent 617db24 commit e6415c6

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
@@ -2794,7 +2794,7 @@ mod tests {
27942794
(our_payment_preimage, our_payment_hash)
27952795
}
27962796

2797-
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2797+
fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: [u8; 32]) {
27982798
assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
27992799
{
28002800
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2823,40 +2823,51 @@ mod tests {
28232823

28242824
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
28252825
let mut prev_node = expected_route.last().unwrap();
2826-
for node in expected_route.iter().rev() {
2826+
for (idx, node) in expected_route.iter().rev().enumerate() {
28272827
assert_eq!(expected_next_node, node.node.get_our_node_id());
28282828
if next_msgs.is_some() {
28292829
update_fulfill_dance!(node, prev_node, false);
28302830
}
28312831

28322832
let events = node.node.get_and_clear_pending_events();
2833+
if !skip_last || idx != expected_route.len() - 1 {
2834+
assert_eq!(events.len(), 1);
2835+
match events[0] {
2836+
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 } } => {
2837+
assert!(update_add_htlcs.is_empty());
2838+
assert_eq!(update_fulfill_htlcs.len(), 1);
2839+
assert!(update_fail_htlcs.is_empty());
2840+
assert!(update_fail_malformed_htlcs.is_empty());
2841+
expected_next_node = node_id.clone();
2842+
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2843+
},
2844+
_ => panic!("Unexpected event"),
2845+
}
2846+
} else {
2847+
assert!(events.is_empty());
2848+
}
2849+
if !skip_last && idx == expected_route.len() - 1 {
2850+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2851+
}
2852+
2853+
prev_node = node;
2854+
}
2855+
2856+
if !skip_last {
2857+
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2858+
let events = origin_node.node.get_and_clear_pending_events();
28332859
assert_eq!(events.len(), 1);
28342860
match events[0] {
2835-
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 } } => {
2836-
assert!(update_add_htlcs.is_empty());
2837-
assert_eq!(update_fulfill_htlcs.len(), 1);
2838-
assert!(update_fail_htlcs.is_empty());
2839-
assert!(update_fail_malformed_htlcs.is_empty());
2840-
expected_next_node = node_id.clone();
2841-
next_msgs = Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()));
2861+
Event::PaymentSent { payment_preimage } => {
2862+
assert_eq!(payment_preimage, our_payment_preimage);
28422863
},
28432864
_ => panic!("Unexpected event"),
2844-
};
2845-
2846-
prev_node = node;
2865+
}
28472866
}
2867+
}
28482868

2849-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2850-
update_fulfill_dance!(origin_node, expected_route.first().unwrap(), true);
2851-
2852-
let events = origin_node.node.get_and_clear_pending_events();
2853-
assert_eq!(events.len(), 1);
2854-
match events[0] {
2855-
Event::PaymentSent { payment_preimage } => {
2856-
assert_eq!(payment_preimage, our_payment_preimage);
2857-
},
2858-
_ => panic!("Unexpected event"),
2859-
}
2869+
fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: [u8; 32]) {
2870+
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
28602871
}
28612872

28622873
const TEST_FINAL_CLTV: u32 = 32;
@@ -2900,7 +2911,7 @@ mod tests {
29002911
claim_payment(&origin, expected_route, our_payment_preimage);
29012912
}
29022913

2903-
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2914+
fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) {
29042915
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
29052916
{
29062917
let mut added_monitors = expected_route.last().unwrap().chan_monitor.added_monitors.lock().unwrap();
@@ -2920,42 +2931,57 @@ mod tests {
29202931

29212932
let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
29222933
let mut prev_node = expected_route.last().unwrap();
2923-
for node in expected_route.iter().rev() {
2934+
for (idx, node) in expected_route.iter().rev().enumerate() {
29242935
assert_eq!(expected_next_node, node.node.get_our_node_id());
29252936
if next_msgs.is_some() {
2926-
update_fail_dance!(node, prev_node, false);
2937+
// We may be the "last node" for the purpose of the commitment dance if we're
2938+
// skipping the last node (implying it is disconnected) and we're the
2939+
// second-to-last node!
2940+
update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
29272941
}
29282942

29292943
let events = node.node.get_and_clear_pending_events();
2930-
assert_eq!(events.len(), 1);
2931-
match events[0] {
2932-
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 } } => {
2933-
assert!(update_add_htlcs.is_empty());
2934-
assert!(update_fulfill_htlcs.is_empty());
2935-
assert_eq!(update_fail_htlcs.len(), 1);
2936-
assert!(update_fail_malformed_htlcs.is_empty());
2937-
expected_next_node = node_id.clone();
2938-
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2939-
},
2940-
_ => panic!("Unexpected event"),
2941-
};
2944+
if !skip_last || idx != expected_route.len() - 1 {
2945+
assert_eq!(events.len(), 1);
2946+
match events[0] {
2947+
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 } } => {
2948+
assert!(update_add_htlcs.is_empty());
2949+
assert!(update_fulfill_htlcs.is_empty());
2950+
assert_eq!(update_fail_htlcs.len(), 1);
2951+
assert!(update_fail_malformed_htlcs.is_empty());
2952+
expected_next_node = node_id.clone();
2953+
next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
2954+
},
2955+
_ => panic!("Unexpected event"),
2956+
}
2957+
} else {
2958+
assert!(events.is_empty());
2959+
}
2960+
if !skip_last && idx == expected_route.len() - 1 {
2961+
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2962+
}
29422963

29432964
prev_node = node;
29442965
}
29452966

2946-
assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
2947-
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
2967+
if !skip_last {
2968+
update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
29482969

2949-
let events = origin_node.node.get_and_clear_pending_events();
2950-
assert_eq!(events.len(), 1);
2951-
match events[0] {
2952-
Event::PaymentFailed { payment_hash } => {
2953-
assert_eq!(payment_hash, our_payment_hash);
2954-
},
2955-
_ => panic!("Unexpected event"),
2970+
let events = origin_node.node.get_and_clear_pending_events();
2971+
assert_eq!(events.len(), 1);
2972+
match events[0] {
2973+
Event::PaymentFailed { payment_hash } => {
2974+
assert_eq!(payment_hash, our_payment_hash);
2975+
},
2976+
_ => panic!("Unexpected event"),
2977+
}
29562978
}
29572979
}
29582980

2981+
fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: [u8; 32]) {
2982+
fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
2983+
}
2984+
29592985
fn create_network(node_count: usize) -> Vec<Node> {
29602986
let mut nodes = Vec::new();
29612987
let mut rng = thread_rng();

0 commit comments

Comments
 (0)