Skip to content

Commit a773553

Browse files
Respect route hint max_htlc in pathfinding
1 parent 88821cb commit a773553

File tree

3 files changed

+123
-15
lines changed

3 files changed

+123
-15
lines changed

lightning/src/routing/gossip.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,11 @@ pub enum EffectiveCapacity {
10331033
/// A capacity sufficient to route any payment, typically used for private channels provided by
10341034
/// an invoice.
10351035
Infinite,
1036+
/// The maximum HTLC amount as provided by an invoice route hint.
1037+
HintMaxHTLC {
1038+
/// The maximum HTLC amount denominated in millisatoshi.
1039+
amount_msat: u64,
1040+
},
10361041
/// A capacity that is unknown possibly because either the chain state is unavailable to know
10371042
/// the total capacity or the `htlc_maximum_msat` was not advertised on the gossip network.
10381043
Unknown,
@@ -1049,6 +1054,7 @@ impl EffectiveCapacity {
10491054
EffectiveCapacity::ExactLiquidity { liquidity_msat } => *liquidity_msat,
10501055
EffectiveCapacity::AdvertisedMaxHTLC { amount_msat } => *amount_msat,
10511056
EffectiveCapacity::Total { capacity_msat, .. } => *capacity_msat,
1057+
EffectiveCapacity::HintMaxHTLC { amount_msat } => *amount_msat,
10521058
EffectiveCapacity::Infinite => u64::max_value(),
10531059
EffectiveCapacity::Unknown => UNKNOWN_CHANNEL_CAPACITY_MSAT,
10541060
}

lightning/src/routing/router.rs

Lines changed: 113 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,10 @@ impl<'a> CandidateRouteHop<'a> {
951951
liquidity_msat: details.next_outbound_htlc_limit_msat,
952952
},
953953
CandidateRouteHop::PublicHop { info, .. } => info.effective_capacity(),
954-
CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite,
954+
CandidateRouteHop::PrivateHop { hint } => {
955+
hint.htlc_maximum_msat.map_or(EffectiveCapacity::Infinite,
956+
|max| EffectiveCapacity::HintMaxHTLC { amount_msat: max })
957+
},
955958
}
956959
}
957960
}
@@ -965,6 +968,7 @@ fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_po
965968
EffectiveCapacity::Unknown => EffectiveCapacity::Unknown.as_msat(),
966969
EffectiveCapacity::AdvertisedMaxHTLC { amount_msat } =>
967970
amount_msat.checked_shr(saturation_shift).unwrap_or(0),
971+
EffectiveCapacity::HintMaxHTLC { amount_msat } => amount_msat,
968972
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } =>
969973
cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_maximum_msat),
970974
}
@@ -1832,11 +1836,12 @@ where L::Target: Logger {
18321836
short_channel_id: hop.short_channel_id,
18331837
})
18341838
.unwrap_or_else(|| CandidateRouteHop::PrivateHop { hint: hop });
1839+
let hint_candidate_contribution_msat = cmp::min(path_value_msat, candidate.effective_capacity().as_msat());
18351840

18361841
if !add_entry!(candidate, source, target, aggregate_next_hops_fee_msat,
1837-
path_value_msat, aggregate_next_hops_path_htlc_minimum_msat,
1838-
aggregate_next_hops_path_penalty_msat,
1839-
aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length) {
1842+
hint_candidate_contribution_msat, aggregate_next_hops_path_htlc_minimum_msat,
1843+
aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta,
1844+
aggregate_next_hops_path_length) {
18401845
// If this hop was not used then there is no use checking the preceding
18411846
// hops in the RouteHint. We can break by just searching for a direct
18421847
// channel between last checked hop and first_hop_targets.
@@ -1865,12 +1870,11 @@ where L::Target: Logger {
18651870
// Searching for a direct channel between last checked hop and first_hop_targets
18661871
if let Some(first_channels) = first_hop_targets.get(&NodeId::from_pubkey(&prev_hop_id)) {
18671872
for details in first_channels {
1868-
let candidate = CandidateRouteHop::FirstHop { details };
1869-
add_entry!(candidate, our_node_id, NodeId::from_pubkey(&prev_hop_id),
1870-
aggregate_next_hops_fee_msat, path_value_msat,
1871-
aggregate_next_hops_path_htlc_minimum_msat,
1872-
aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta,
1873-
aggregate_next_hops_path_length);
1873+
let first_hop_candidate = CandidateRouteHop::FirstHop { details };
1874+
add_entry!(first_hop_candidate, our_node_id, NodeId::from_pubkey(&prev_hop_id),
1875+
aggregate_next_hops_fee_msat, hint_candidate_contribution_msat,
1876+
aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
1877+
aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length);
18741878
}
18751879
}
18761880

@@ -1905,10 +1909,11 @@ where L::Target: Logger {
19051909
// path.
19061910
if let Some(first_channels) = first_hop_targets.get(&NodeId::from_pubkey(&hop.src_node_id)) {
19071911
for details in first_channels {
1908-
let candidate = CandidateRouteHop::FirstHop { details };
1909-
add_entry!(candidate, our_node_id,
1912+
let first_hop_candidate = CandidateRouteHop::FirstHop { details };
1913+
add_entry!(first_hop_candidate, our_node_id,
19101914
NodeId::from_pubkey(&hop.src_node_id),
1911-
aggregate_next_hops_fee_msat, path_value_msat,
1915+
aggregate_next_hops_fee_msat,
1916+
hint_candidate_contribution_msat,
19121917
aggregate_next_hops_path_htlc_minimum_msat,
19131918
aggregate_next_hops_path_penalty_msat,
19141919
aggregate_next_hops_cltv_delta,
@@ -5906,6 +5911,101 @@ mod tests {
59065911
assert!(route.is_ok());
59075912
}
59085913

5914+
#[test]
5915+
fn respect_route_hint_max_htlc() {
5916+
// Make sure that any max_htlc provided in the route hints of the payment params is respected in
5917+
// the final route.
5918+
let (secp_ctx, network_graph, _, _, logger) = build_graph();
5919+
let netgraph = network_graph.read_only();
5920+
let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
5921+
let scorer = ln_test_utils::TestScorer::new();
5922+
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
5923+
let random_seed_bytes = keys_manager.get_secure_random_bytes();
5924+
let config = UserConfig::default();
5925+
5926+
let max_htlc_msat = 50_000;
5927+
let route_hint_1 = RouteHint(vec![RouteHintHop {
5928+
src_node_id: nodes[2],
5929+
short_channel_id: 42,
5930+
fees: RoutingFees {
5931+
base_msat: 100,
5932+
proportional_millionths: 0,
5933+
},
5934+
cltv_expiry_delta: 10,
5935+
htlc_minimum_msat: None,
5936+
htlc_maximum_msat: Some(max_htlc_msat),
5937+
}]);
5938+
let dest_node_id = ln_test_utils::pubkey(42);
5939+
let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
5940+
.with_route_hints(vec![route_hint_1.clone()]).unwrap()
5941+
.with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
5942+
5943+
// Make sure we'll error if our route hints don't have enough liquidity according to their
5944+
// max_htlc.
5945+
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id,
5946+
&payment_params, &netgraph, None, max_htlc_msat + 1, Arc::clone(&logger), &scorer, &(),
5947+
&random_seed_bytes)
5948+
{
5949+
assert_eq!(err, "Failed to find a sufficient route to the given destination");
5950+
} else { panic!(); }
5951+
5952+
// Make sure we'll split an MPP payment across route hints if their max_htlcs warrant it.
5953+
let mut route_hint_2 = route_hint_1.clone();
5954+
route_hint_2.0[0].short_channel_id = 43;
5955+
let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
5956+
.with_route_hints(vec![route_hint_1, route_hint_2]).unwrap()
5957+
.with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
5958+
let route = get_route(&our_id, &payment_params, &netgraph, None, max_htlc_msat + 1,
5959+
Arc::clone(&logger), &scorer, &(), &random_seed_bytes).unwrap();
5960+
assert_eq!(route.paths.len(), 2);
5961+
assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
5962+
assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
5963+
}
5964+
5965+
#[test]
5966+
fn direct_channel_to_hints_with_max_htlc() {
5967+
// Check that if we have a first hop channel peer that's connected to multiple provided route
5968+
// hints, that we properly split the payment between the route hints if needed.
5969+
let logger = Arc::new(ln_test_utils::TestLogger::new());
5970+
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
5971+
let scorer = ln_test_utils::TestScorer::new();
5972+
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
5973+
let random_seed_bytes = keys_manager.get_secure_random_bytes();
5974+
let config = UserConfig::default();
5975+
5976+
let our_node_id = ln_test_utils::pubkey(42);
5977+
let intermed_node_id = ln_test_utils::pubkey(43);
5978+
let first_hop = vec![get_channel_details(Some(42), intermed_node_id, InitFeatures::from_le_bytes(vec![0b11]), 10_000_000)];
5979+
5980+
let amt_msat = 900_000;
5981+
let max_htlc_msat = 500_000;
5982+
let route_hint_1 = RouteHint(vec![RouteHintHop {
5983+
src_node_id: intermed_node_id,
5984+
short_channel_id: 43,
5985+
fees: RoutingFees {
5986+
base_msat: 100,
5987+
proportional_millionths: 0,
5988+
},
5989+
cltv_expiry_delta: 10,
5990+
htlc_minimum_msat: None,
5991+
htlc_maximum_msat: Some(max_htlc_msat),
5992+
}]);
5993+
let mut route_hint_2 = route_hint_1.clone();
5994+
route_hint_2.0[0].short_channel_id = 44;
5995+
route_hint_2.0[0].htlc_maximum_msat = Some(max_htlc_msat);
5996+
let dest_node_id = ln_test_utils::pubkey(44);
5997+
let payment_params = PaymentParameters::from_node_id(dest_node_id, 42)
5998+
.with_route_hints(vec![route_hint_1, route_hint_2]).unwrap()
5999+
.with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
6000+
6001+
let route = get_route(&our_node_id, &payment_params, &network_graph.read_only(),
6002+
Some(&first_hop.iter().collect::<Vec<_>>()), amt_msat, Arc::clone(&logger), &scorer, &(),
6003+
&random_seed_bytes).unwrap();
6004+
assert_eq!(route.paths.len(), 2);
6005+
assert!(route.paths[0].hops.last().unwrap().fee_msat <= max_htlc_msat);
6006+
assert!(route.paths[1].hops.last().unwrap().fee_msat <= max_htlc_msat);
6007+
}
6008+
59096009
#[test]
59106010
fn blinded_route_ser() {
59116011
let blinded_path_1 = BlindedPath {

lightning/src/routing/scoring.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
12431243

12441244
let mut anti_probing_penalty_msat = 0;
12451245
match usage.effective_capacity {
1246-
EffectiveCapacity::ExactLiquidity { liquidity_msat } => {
1247-
if usage.amount_msat > liquidity_msat {
1246+
EffectiveCapacity::ExactLiquidity { liquidity_msat: amount_msat } |
1247+
EffectiveCapacity::HintMaxHTLC { amount_msat } =>
1248+
{
1249+
if usage.amount_msat > amount_msat {
12481250
return u64::max_value();
12491251
} else {
12501252
return base_penalty_msat;

0 commit comments

Comments
 (0)