Skip to content

Commit 85a0e9c

Browse files
committed
WIP: Defer adding first hops after all blinded hints are processed
1 parent 8cee3bb commit 85a0e9c

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

lightning/src/routing/router.rs

+35-21
Original file line numberDiff line numberDiff line change
@@ -2367,7 +2367,7 @@ where L::Target: Logger {
23672367
.map_or(0, |used_liquidity_msat| {
23682368
available_value_contribution_msat = available_value_contribution_msat
23692369
.saturating_sub(*used_liquidity_msat);
2370-
*used_liquidity_msat
2370+
(*used_liquidity_msat)
23712371
});
23722372

23732373
// Verify the liquidity offered by this channel complies to the minimal contribution.
@@ -2809,6 +2809,7 @@ where L::Target: Logger {
28092809
introduction_node_id_cache.len(),
28102810
"introduction_node_id_cache was built by iterating the blinded_route_hints, so they should be the same len"
28112811
);
2812+
let mut value_contributions = Vec::with_capacity(payment_params.payee.blinded_route_hints().len());
28122813
for (hint_idx, hint) in payment_params.payee.blinded_route_hints().iter().enumerate() {
28132814
// Only add the hops in this route to our candidate set if either
28142815
// we have a direct channel to the first hop or the first hop is
@@ -2823,29 +2824,39 @@ where L::Target: Logger {
28232824
} else {
28242825
CandidateRouteHop::Blinded(BlindedPathCandidate { source_node_counter, source_node_id, hint, hint_idx })
28252826
};
2826-
let mut path_contribution_msat = path_value_msat;
28272827
if let Some(hop_used_msat) = add_entry!(&candidate,
2828-
0, path_contribution_msat, 0, 0_u64, 0, 0)
2828+
0, path_value_msat, 0, 0_u64, 0, 0)
28292829
{
2830-
path_contribution_msat = hop_used_msat;
2830+
value_contributions.push((hint_idx, hop_used_msat));
28312831
} else { continue }
2832-
if let Some((first_channels, peer_node_counter)) = first_hop_targets.get_mut(source_node_id) {
2833-
sort_first_hop_channels(
2834-
first_channels, &used_liquidities, recommended_value_msat, our_node_pubkey
2835-
);
2836-
for details in first_channels {
2837-
let first_hop_candidate = CandidateRouteHop::FirstHop(FirstHopCandidate {
2838-
details, payer_node_id: &our_node_id, payer_node_counter,
2839-
target_node_counter: *peer_node_counter,
2840-
});
2841-
let blinded_path_fee = match compute_fees(path_contribution_msat, candidate.fees()) {
2842-
Some(fee) => fee,
2843-
None => continue
2844-
};
2845-
let path_min = candidate.htlc_minimum_msat().saturating_add(
2846-
compute_fees_saturating(candidate.htlc_minimum_msat(), candidate.fees()));
2847-
add_entry!(&first_hop_candidate, blinded_path_fee, path_contribution_msat, path_min,
2848-
0_u64, candidate.cltv_expiry_delta(), 0);
2832+
}
2833+
for (hint_idx, path_contribution_msat) in value_contributions {
2834+
// Only add the hops in this route to our candidate set if either
2835+
// we have a direct channel to the first hop or the first hop is
2836+
// in the regular network graph.
2837+
let source_node_opt = introduction_node_id_cache[hint_idx];
2838+
let (source_node_id, source_node_counter) = if let Some(v) = source_node_opt { v } else { continue };
2839+
if our_node_id == *source_node_id { continue }
2840+
if let Some(hop_candidate) = dist[source_node_counter as usize].as_ref() {
2841+
let candidate = hop_candidate.candidate.clone();
2842+
if let Some((first_channels, peer_node_counter)) = first_hop_targets.get_mut(source_node_id) {
2843+
sort_first_hop_channels(
2844+
first_channels, &used_liquidities, recommended_value_msat, our_node_pubkey
2845+
);
2846+
for details in first_channels {
2847+
let first_hop_candidate = CandidateRouteHop::FirstHop(FirstHopCandidate {
2848+
details, payer_node_id: &our_node_id, payer_node_counter,
2849+
target_node_counter: *peer_node_counter,
2850+
});
2851+
let blinded_path_fee = match compute_fees(path_contribution_msat, candidate.fees()) {
2852+
Some(fee) => fee,
2853+
None => continue
2854+
};
2855+
let path_min = candidate.htlc_minimum_msat().saturating_add(
2856+
compute_fees_saturating(candidate.htlc_minimum_msat(), candidate.fees()));
2857+
add_entry!(&first_hop_candidate, blinded_path_fee, path_contribution_msat, path_min,
2858+
0_u64, candidate.cltv_expiry_delta(), 0);
2859+
}
28492860
}
28502861
}
28512862
}
@@ -3114,17 +3125,20 @@ where L::Target: Logger {
31143125
let mut prevented_redundant_path_selection = false;
31153126
for (hop, _) in payment_path.hops.iter() {
31163127
let spent_on_hop_msat = value_contribution_msat + hop.next_hops_fee_msat;
3128+
println!("SPENT: {} = VALUE {} + NEXT_FEE {}", spent_on_hop_msat, value_contribution_msat, hop.next_hops_fee_msat);
31173129
let used_liquidity_msat = used_liquidities
31183130
.entry(hop.candidate.id())
31193131
.and_modify(|used_liquidity_msat| *used_liquidity_msat += spent_on_hop_msat)
31203132
.or_insert(spent_on_hop_msat);
31213133
let hop_capacity = hop.candidate.effective_capacity();
3134+
println!("CAP: {}, USED: {}", spent_on_hop_msat, used_liquidity_msat);
31223135
let hop_max_msat = max_htlc_from_capacity(hop_capacity, channel_saturation_pow_half);
31233136
if *used_liquidity_msat == hop_max_msat {
31243137
// If this path used all of this channel's available liquidity, we know
31253138
// this path will not be selected again in the next loop iteration.
31263139
prevented_redundant_path_selection = true;
31273140
}
3141+
println!("USED: {}, hop_max_msat: {}", used_liquidity_msat, hop_max_msat);
31283142
debug_assert!(*used_liquidity_msat <= hop_max_msat);
31293143
}
31303144
if !prevented_redundant_path_selection {

0 commit comments

Comments
 (0)