Skip to content

Commit 8c99e34

Browse files
committed
Assert query's and route's final_value_msat are equal
1 parent fb2c959 commit 8c99e34

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

lightning/src/ln/outbound_payment.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ impl OutboundPayments {
875875
}
876876
}
877877

878-
let route = router.find_route_with_id(
878+
let mut route = router.find_route_with_id(
879879
&node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
880880
Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),
881881
payment_hash, payment_id,
@@ -885,6 +885,14 @@ impl OutboundPayments {
885885
RetryableSendFailure::RouteNotFound
886886
})?;
887887

888+
if let Some(route_route_params) = route.route_params.as_mut() {
889+
if route_route_params.final_value_msat != route_params.final_value_msat {
890+
debug_assert!(false,
891+
"Routers are expected to return a route which includes the requested final_value_msat");
892+
route_route_params.final_value_msat = route_params.final_value_msat;
893+
}
894+
}
895+
888896
let onion_session_privs = self.add_new_pending_payment(payment_hash,
889897
recipient_onion.clone(), payment_id, keysend_preimage, &route, Some(retry_strategy),
890898
Some(route_params.payment_params.clone()), entropy_source, best_block_height)
@@ -926,7 +934,7 @@ impl OutboundPayments {
926934
}
927935
}
928936

929-
let route = match router.find_route_with_id(
937+
let mut route = match router.find_route_with_id(
930938
&node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
931939
Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),
932940
payment_hash, payment_id,
@@ -938,6 +946,15 @@ impl OutboundPayments {
938946
return
939947
}
940948
};
949+
950+
if let Some(route_route_params) = route.route_params.as_mut() {
951+
if route_route_params.final_value_msat != route_params.final_value_msat {
952+
debug_assert!(false,
953+
"Routers are expected to return a route which includes the requested final_value_msat");
954+
route_route_params.final_value_msat = route_params.final_value_msat;
955+
}
956+
}
957+
941958
for path in route.paths.iter() {
942959
if path.hops.len() == 0 {
943960
log_error!(logger, "Unusable path in route (path.hops.len() must be at least 1");

lightning/src/ln/payment_tests.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ fn mpp_retry_overpay() {
253253

254254
route.paths.remove(0);
255255
route_params.final_value_msat -= first_path_value;
256+
route.route_params.as_mut().map(|p| p.final_value_msat -= first_path_value);
256257
route_params.payment_params.previously_failed_channels.push(chan_4_update.contents.short_channel_id);
257258

258259
// Check the remaining max total routing fee for the second attempt accounts only for 1_000 msat
@@ -2395,10 +2396,11 @@ fn auto_retry_partial_failure() {
23952396
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
23962397
.with_expiry_time(payment_expiry_secs as u64)
23972398
.with_bolt11_features(invoice_features).unwrap();
2399+
2400+
// Configure the initial send path
23982401
let mut route_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat);
23992402
route_params.max_total_routing_fee_msat = None;
24002403

2401-
// Configure the initial send, retry1 and retry2's paths.
24022404
let send_route = Route {
24032405
paths: vec![
24042406
Path { hops: vec![RouteHop {
@@ -2422,6 +2424,14 @@ fn auto_retry_partial_failure() {
24222424
],
24232425
route_params: Some(route_params.clone()),
24242426
};
2427+
nodes[0].router.expect_find_route(route_params.clone(), Ok(send_route));
2428+
2429+
// Configure the retry1 paths
2430+
let mut payment_params = route_params.payment_params.clone();
2431+
payment_params.previously_failed_channels.push(chan_2_id);
2432+
let mut retry_1_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 2);
2433+
retry_1_params.max_total_routing_fee_msat = None;
2434+
24252435
let retry_1_route = Route {
24262436
paths: vec![
24272437
Path { hops: vec![RouteHop {
@@ -2443,8 +2453,16 @@ fn auto_retry_partial_failure() {
24432453
maybe_announced_channel: true,
24442454
}], blinded_tail: None },
24452455
],
2446-
route_params: Some(route_params.clone()),
2456+
route_params: Some(retry_1_params.clone()),
24472457
};
2458+
nodes[0].router.expect_find_route(retry_1_params.clone(), Ok(retry_1_route));
2459+
2460+
// Configure the retry2 path
2461+
let mut payment_params = retry_1_params.payment_params.clone();
2462+
payment_params.previously_failed_channels.push(chan_3_id);
2463+
let mut retry_2_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 4);
2464+
retry_2_params.max_total_routing_fee_msat = None;
2465+
24482466
let retry_2_route = Route {
24492467
paths: vec![
24502468
Path { hops: vec![RouteHop {
@@ -2457,20 +2475,8 @@ fn auto_retry_partial_failure() {
24572475
maybe_announced_channel: true,
24582476
}], blinded_tail: None },
24592477
],
2460-
route_params: Some(route_params.clone()),
2478+
route_params: Some(retry_2_params.clone()),
24612479
};
2462-
nodes[0].router.expect_find_route(route_params.clone(), Ok(send_route));
2463-
let mut payment_params = route_params.payment_params.clone();
2464-
payment_params.previously_failed_channels.push(chan_2_id);
2465-
2466-
let mut retry_1_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 2);
2467-
retry_1_params.max_total_routing_fee_msat = None;
2468-
nodes[0].router.expect_find_route(retry_1_params, Ok(retry_1_route));
2469-
2470-
let mut payment_params = route_params.payment_params.clone();
2471-
payment_params.previously_failed_channels.push(chan_3_id);
2472-
let mut retry_2_params = RouteParameters::from_payment_params_and_value(payment_params, amt_msat / 4);
2473-
retry_2_params.max_total_routing_fee_msat = None;
24742480
nodes[0].router.expect_find_route(retry_2_params, Ok(retry_2_route));
24752481

24762482
// Send a payment that will partially fail on send, then partially fail on retry, then succeed.
@@ -2734,6 +2740,7 @@ fn retry_multi_path_single_failed_payment() {
27342740
// not the amount remaining on the full payment, which should be changed.
27352741
let mut retry_params = RouteParameters::from_payment_params_and_value(pay_params, 100_000_001);
27362742
retry_params.max_total_routing_fee_msat = None;
2743+
route.route_params.as_mut().unwrap().final_value_msat = 100_000_000;
27372744
nodes[0].router.expect_find_route(retry_params, Ok(route.clone()));
27382745

27392746
{
@@ -2917,9 +2924,7 @@ fn no_extra_retries_on_back_to_back_fail() {
29172924
maybe_announced_channel: true,
29182925
}], blinded_tail: None }
29192926
],
2920-
route_params: Some(RouteParameters::from_payment_params_and_value(
2921-
PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
2922-
100_000_000)),
2927+
route_params: Some(route_params.clone()),
29232928
};
29242929
route.route_params.as_mut().unwrap().max_total_routing_fee_msat = None;
29252930
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
@@ -3123,18 +3128,18 @@ fn test_simple_partial_retry() {
31233128
maybe_announced_channel: true,
31243129
}], blinded_tail: None }
31253130
],
3126-
route_params: Some(RouteParameters::from_payment_params_and_value(
3127-
PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV),
3128-
100_000_000)),
3131+
route_params: Some(route_params.clone()),
31293132
};
3130-
route.route_params.as_mut().unwrap().max_total_routing_fee_msat = None;
3133+
31313134
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
3135+
31323136
let mut second_payment_params = route_params.payment_params.clone();
31333137
second_payment_params.previously_failed_channels = vec![chan_2_scid];
31343138
// On retry, we'll only be asked for one path (or 100k sats)
31353139
route.paths.remove(0);
31363140
let mut retry_params = RouteParameters::from_payment_params_and_value(second_payment_params, amt_msat / 2);
31373141
retry_params.max_total_routing_fee_msat = None;
3142+
route.route_params.as_mut().unwrap().final_value_msat = amt_msat / 2;
31383143
nodes[0].router.expect_find_route(retry_params, Ok(route.clone()));
31393144

31403145
nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret),
@@ -3294,11 +3299,7 @@ fn test_threaded_payment_retries() {
32943299
maybe_announced_channel: true,
32953300
}], blinded_tail: None }
32963301
],
3297-
route_params: Some(RouteParameters {
3298-
payment_params: PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV),
3299-
final_value_msat: amt_msat - amt_msat / 1000,
3300-
max_total_routing_fee_msat: Some(500_000),
3301-
}),
3302+
route_params: Some(route_params.clone()),
33023303
};
33033304
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
33043305

@@ -3317,6 +3318,7 @@ fn test_threaded_payment_retries() {
33173318

33183319
// from here on out, the retry `RouteParameters` amount will be amt/1000
33193320
route_params.final_value_msat /= 1000;
3321+
route.route_params.as_mut().unwrap().final_value_msat /= 1000;
33203322
route.paths.pop();
33213323

33223324
let end_time = Instant::now() + Duration::from_secs(1);

lightning/src/util/test_utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<'a> Router for TestRouter<'a> {
124124
if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() {
125125
assert_eq!(find_route_query, *params);
126126
if let Ok(ref route) = find_route_res {
127+
assert_eq!(route.route_params.as_ref().unwrap().final_value_msat, find_route_query.final_value_msat);
127128
let scorer = self.scorer.read().unwrap();
128129
let scorer = ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs);
129130
for path in &route.paths {

0 commit comments

Comments
 (0)