Skip to content

Commit 63e0b00

Browse files
committed
Add GetRouteParameters struct for get_route
Group const after initialization values used across the `get_route` method into single `GetRouteParameters` struct.
1 parent 612f17c commit 63e0b00

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

lightning/src/routing/router.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,17 @@ where L::Target: Logger, GL::Target: Logger {
21232123
Ok(route)
21242124
}
21252125

2126+
// Const after initialization params which are used across the [`get_route`].
2127+
struct GetRouteParameters<'a> {
2128+
payment: &'a PaymentParameters,
2129+
channel_saturation_pow_half: u8,
2130+
max_total_routing_fee_msat: u64,
2131+
minimal_value_contribution_msat: u64,
2132+
final_cltv_expiry_delta: u32,
2133+
recommended_value_msat: u64,
2134+
max_path_length: u8,
2135+
}
2136+
21262137
pub(crate) fn get_route<L: Deref, S: ScoreLookUp>(
21272138
our_node_pubkey: &PublicKey, route_params: &RouteParameters, network_graph: &ReadOnlyNetworkGraph,
21282139
first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, score_params: &S::ScoreParams,
@@ -2375,6 +2386,11 @@ where L::Target: Logger {
23752386
// Remember how many candidates we ignored to allow for some logging afterwards.
23762387
let mut ignored_stats = IgnoredCandidatesStats::default();
23772388

2389+
// Common parameters used across this function.
2390+
let params = GetRouteParameters { channel_saturation_pow_half,
2391+
max_total_routing_fee_msat, minimal_value_contribution_msat, final_cltv_expiry_delta,
2392+
recommended_value_msat, max_path_length, payment: payment_params };
2393+
23782394
macro_rules! add_entry {
23792395
// Adds entry which goes from $candidate.source() to $candidate.target() over the $candidate hop.
23802396
// $next_hops_fee_msat represents the fees paid for using all the channels *after* this one,
@@ -2384,22 +2400,16 @@ where L::Target: Logger {
23842400
$next_hops_value_contribution: expr, $next_hops_path_htlc_minimum_msat: expr,
23852401
$next_hops_path_penalty_msat: expr, $next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => { {
23862402
add_entry_internal(
2387-
channel_saturation_pow_half,
2403+
&params,
23882404
&used_liquidities,
2389-
minimal_value_contribution_msat,
2390-
&payment_params,
2391-
final_cltv_expiry_delta,
2392-
recommended_value_msat,
23932405
&logger,
23942406
&mut ignored_stats,
23952407
&mut hit_minimum_limit,
23962408
&mut dist,
23972409
our_node_id,
2398-
max_total_routing_fee_msat,
23992410
&mut targets,
24002411
scorer,
24012412
score_params,
2402-
max_path_length,
24032413
$candidate,
24042414
$next_hops_fee_msat,
24052415
$next_hops_value_contribution,
@@ -3113,22 +3123,16 @@ where L::Target: Logger {
31133123
// Returns the contribution amount of candidate if the channel caused an update to `targets`.
31143124
fn add_entry_internal<'a, L: Deref, S: ScoreLookUp>(
31153125
// parameters that were captured from the original macro add_entry:
3116-
channel_saturation_pow_half: u8,
3126+
params: &GetRouteParameters,
31173127
used_liquidities: &HashMap<CandidateHopId, u64>,
3118-
minimal_value_contribution_msat: u64,
3119-
payment_params: &PaymentParameters,
3120-
final_cltv_expiry_delta: u32,
3121-
recommended_value_msat: u64,
31223128
logger: &L,
31233129
ignored_stats: &mut IgnoredCandidatesStats,
31243130
hit_minimum_limit: &mut bool,
31253131
dist: &mut Vec<Option<PathBuildingHop<'a>>>,
31263132
our_node_id: NodeId,
3127-
max_total_routing_fee_msat: u64,
31283133
targets: &mut BinaryHeap<RouteGraphNode>,
31293134
scorer: &S,
31303135
score_params: &S::ScoreParams,
3131-
max_path_length: u8,
31323136
// original add_entry params:
31333137
candidate: &CandidateRouteHop<'a>,
31343138
next_hops_fee_msat: u64,
@@ -3152,7 +3156,7 @@ where
31523156
if Some(src_node_id) != candidate.target() {
31533157
let scid_opt = candidate.short_channel_id();
31543158
let effective_capacity = candidate.effective_capacity();
3155-
let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, channel_saturation_pow_half);
3159+
let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, params.channel_saturation_pow_half);
31563160

31573161
// It is tricky to subtract $next_hops_fee_msat from available liquidity here.
31583162
// It may be misleading because we might later choose to reduce the value transferred
@@ -3171,19 +3175,19 @@ where
31713175
});
31723176

31733177
// Verify the liquidity offered by this channel complies to the minimal contribution.
3174-
let contributes_sufficient_value = available_value_contribution_msat >= minimal_value_contribution_msat;
3178+
let contributes_sufficient_value = available_value_contribution_msat >= params.minimal_value_contribution_msat;
31753179
// Do not consider candidate hops that would exceed the maximum path length.
31763180
let path_length_to_node = next_hops_path_length
31773181
+ if candidate.blinded_hint_idx().is_some() { 0 } else { 1 };
3178-
let exceeds_max_path_length = path_length_to_node > max_path_length;
3182+
let exceeds_max_path_length = path_length_to_node > params.max_path_length;
31793183

31803184
// Do not consider candidates that exceed the maximum total cltv expiry limit.
31813185
// In order to already account for some of the privacy enhancing random CLTV
31823186
// expiry delta offset we add on top later, we subtract a rough estimate
31833187
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
3184-
let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
3188+
let max_total_cltv_expiry_delta = (params.payment.max_total_cltv_expiry_delta - params.final_cltv_expiry_delta)
31853189
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
3186-
.unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
3190+
.unwrap_or(params.payment.max_total_cltv_expiry_delta - params.final_cltv_expiry_delta);
31873191
let hop_total_cltv_delta = (next_hops_cltv_delta as u32)
31883192
.saturating_add(candidate.cltv_expiry_delta());
31893193
let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
@@ -3202,15 +3206,15 @@ where
32023206
#[allow(unused_comparisons)] // next_hops_path_htlc_minimum_msat is 0 in some calls so rustc complains
32033207
let may_overpay_to_meet_path_minimum_msat =
32043208
(amount_to_transfer_over_msat < candidate.htlc_minimum_msat() &&
3205-
recommended_value_msat >= candidate.htlc_minimum_msat()) ||
3209+
params.recommended_value_msat >= candidate.htlc_minimum_msat()) ||
32063210
(amount_to_transfer_over_msat < next_hops_path_htlc_minimum_msat &&
3207-
recommended_value_msat >= next_hops_path_htlc_minimum_msat);
3211+
params.recommended_value_msat >= next_hops_path_htlc_minimum_msat);
32083212

32093213
let payment_failed_on_this_channel = match scid_opt {
3210-
Some(scid) => payment_params.previously_failed_channels.contains(&scid),
3214+
Some(scid) => params.payment.previously_failed_channels.contains(&scid),
32113215
None => match candidate.blinded_hint_idx() {
32123216
Some(idx) => {
3213-
payment_params.previously_failed_blinded_path_idxs.contains(&(idx as u64))
3217+
params.payment.previously_failed_blinded_path_idxs.contains(&(idx as u64))
32143218
},
32153219
None => false,
32163220
},
@@ -3324,15 +3328,15 @@ where
33243328
}
33253329

33263330
// Ignore hops if augmenting the current path to them would put us over `max_total_routing_fee_msat`
3327-
if total_fee_msat > max_total_routing_fee_msat {
3331+
if total_fee_msat > params.max_total_routing_fee_msat {
33283332
if should_log_candidate {
33293333
log_trace!(logger, "Ignoring {} due to exceeding max total routing fee limit.", LoggedCandidateHop(&candidate));
33303334

33313335
if let Some(_) = first_hop_details {
33323336
log_trace!(logger,
33333337
"First hop candidate routing fee: {}. Limit: {}",
33343338
total_fee_msat,
3335-
max_total_routing_fee_msat,
3339+
params.max_total_routing_fee_msat,
33363340
);
33373341
}
33383342
}

0 commit comments

Comments
 (0)