Skip to content

Commit f0ecc3e

Browse files
committed
Use CandidateRouteHop as input for channel_penalty_msat
We remove `source`, `target` and `scid` from `channel_penalty_msat` inputs to consume them from `candidate` of type `CandidateRouteHop`
1 parent 04e93fc commit f0ecc3e

File tree

4 files changed

+399
-194
lines changed

4 files changed

+399
-194
lines changed

lightning-background-processor/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ mod tests {
863863
use lightning::ln::msgs::{ChannelMessageHandler, Init};
864864
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
865865
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
866-
use lightning::routing::router::{DefaultRouter, Path, RouteHop};
867866
use lightning::routing::scoring::{ChannelUsage, ScoreUpdate, ScoreLookUp, LockableScore};
867+
use lightning::routing::router::{DefaultRouter, Path, RouteHop, CandidateRouteHop};
868868
use lightning::util::config::UserConfig;
869869
use lightning::util::ser::Writeable;
870870
use lightning::util::test_utils;
@@ -1071,7 +1071,7 @@ mod tests {
10711071
impl ScoreLookUp for TestScorer {
10721072
type ScoreParams = ();
10731073
fn channel_penalty_msat(
1074-
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage, _score_params: &Self::ScoreParams
1074+
&self, _candidate: &CandidateRouteHop, _usage: ChannelUsage, _score_params: &Self::ScoreParams
10751075
) -> u64 { unimplemented!(); }
10761076
}
10771077

lightning/src/routing/router.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,27 @@ impl<'a, S: Deref> ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: Scor
130130

131131
impl<'a, S: Deref> ScoreLookUp for ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: ScoreLookUp {
132132
type ScoreParams = <S::Target as ScoreLookUp>::ScoreParams;
133-
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
133+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
134+
let target = match candidate.target() {
135+
Some(target) => target,
136+
None => return self.scorer.channel_penalty_msat(candidate, usage, score_params),
137+
};
138+
let short_channel_id = match candidate.short_channel_id() {
139+
Some(short_channel_id) => short_channel_id,
140+
None => return self.scorer.channel_penalty_msat(candidate, usage, score_params),
141+
};
142+
let source = candidate.source();
134143
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
135-
source, target, short_channel_id
144+
&source, &target, short_channel_id
136145
) {
137146
let usage = ChannelUsage {
138147
inflight_htlc_msat: usage.inflight_htlc_msat.saturating_add(used_liquidity),
139148
..usage
140149
};
141150

142-
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage, score_params)
151+
self.scorer.channel_penalty_msat(candidate, usage, score_params)
143152
} else {
144-
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage, score_params)
153+
self.scorer.channel_penalty_msat(candidate, usage, score_params)
145154
}
146155
}
147156
}
@@ -1068,7 +1077,7 @@ impl<'a> CandidateRouteHop<'a> {
10681077
/// For `Blinded` and `OneHopBlinded` we return `None` because next hop is not known.
10691078
pub fn short_channel_id(&self) -> Option<u64> {
10701079
match self {
1071-
CandidateRouteHop::FirstHop { details, .. } => Some(details.get_outbound_payment_scid().unwrap()),
1080+
CandidateRouteHop::FirstHop { details, .. } => details.get_outbound_payment_scid(),
10721081
CandidateRouteHop::PublicHop { short_channel_id, .. } => Some(*short_channel_id),
10731082
CandidateRouteHop::PrivateHop { hint, .. } => Some(hint.short_channel_id),
10741083
CandidateRouteHop::Blinded { .. } => None,
@@ -1173,7 +1182,7 @@ impl<'a> CandidateRouteHop<'a> {
11731182
CandidateRouteHop::PublicHop { info, .. } => *info.source(),
11741183
CandidateRouteHop::PrivateHop { hint, .. } => hint.src_node_id.into(),
11751184
CandidateRouteHop::Blinded { hint, .. } => hint.1.introduction_node_id.into(),
1176-
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into()
1185+
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into(),
11771186
}
11781187
}
11791188
/// Returns the target node id of this hop, if known.
@@ -2011,9 +2020,10 @@ where L::Target: Logger {
20112020
inflight_htlc_msat: used_liquidity_msat,
20122021
effective_capacity,
20132022
};
2014-
let channel_penalty_msat = scid_opt.map_or(0,
2015-
|scid| scorer.channel_penalty_msat(scid, &src_node_id, &dest_node_id,
2016-
channel_usage, score_params));
2023+
let channel_penalty_msat =
2024+
scorer.channel_penalty_msat($candidate,
2025+
channel_usage,
2026+
score_params);
20172027
let path_penalty_msat = $next_hops_path_penalty_msat
20182028
.saturating_add(channel_penalty_msat);
20192029
let new_graph_node = RouteGraphNode {
@@ -2324,7 +2334,7 @@ where L::Target: Logger {
23242334
effective_capacity: candidate.effective_capacity(),
23252335
};
23262336
let channel_penalty_msat = scorer.channel_penalty_msat(
2327-
hop.short_channel_id, &source, &target, channel_usage, score_params
2337+
&candidate, channel_usage, score_params
23282338
);
23292339
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
23302340
.saturating_add(channel_penalty_msat);
@@ -2879,13 +2889,13 @@ fn build_route_from_hops_internal<L: Deref>(
28792889

28802890
impl ScoreLookUp for HopScorer {
28812891
type ScoreParams = ();
2882-
fn channel_penalty_msat(&self, _short_channel_id: u64, source: &NodeId, target: &NodeId,
2892+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop,
28832893
_usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64
28842894
{
28852895
let mut cur_id = self.our_node_id;
28862896
for i in 0..self.hop_ids.len() {
28872897
if let Some(next_id) = self.hop_ids[i] {
2888-
if cur_id == *source && next_id == *target {
2898+
if cur_id == candidate.source() && Some(next_id) == candidate.target() {
28892899
return 0;
28902900
}
28912901
cur_id = next_id;
@@ -2926,7 +2936,7 @@ mod tests {
29262936
use crate::routing::utxo::UtxoResult;
29272937
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
29282938
BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
2929-
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE, RouteParameters};
2939+
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE, RouteParameters, CandidateRouteHop};
29302940
use crate::routing::scoring::{ChannelUsage, FixedPenaltyScorer, ScoreLookUp, ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
29312941
use crate::routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel};
29322942
use crate::chain::transaction::OutPoint;
@@ -6231,8 +6241,8 @@ mod tests {
62316241
}
62326242
impl ScoreLookUp for BadChannelScorer {
62336243
type ScoreParams = ();
6234-
fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6235-
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
6244+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6245+
if candidate.short_channel_id() == Some(self.short_channel_id) { u64::max_value() } else { 0 }
62366246
}
62376247
}
62386248

@@ -6247,8 +6257,8 @@ mod tests {
62476257

62486258
impl ScoreLookUp for BadNodeScorer {
62496259
type ScoreParams = ();
6250-
fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6251-
if *target == self.node_id { u64::max_value() } else { 0 }
6260+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6261+
if candidate.target() == Some(self.node_id) { u64::max_value() } else { 0 }
62526262
}
62536263
}
62546264

@@ -6736,26 +6746,32 @@ mod tests {
67366746
};
67376747
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
67386748
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);
6739-
assert_eq!(scorer.channel_penalty_msat(42, &NodeId::from_pubkey(&nodes[3]), &NodeId::from_pubkey(&nodes[4]), usage, &scorer_params), 456);
6749+
let network_graph = network_graph.read_only();
6750+
let channels = network_graph.channels();
6751+
let channel = channels.get(&5).unwrap();
6752+
let info = channel.as_directed_from(&NodeId::from_pubkey(&nodes[3])).unwrap();
6753+
let candidate: CandidateRouteHop = CandidateRouteHop::PublicHop {
6754+
info: info.0,
6755+
short_channel_id: 5,
6756+
};
6757+
assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &scorer_params), 456);
67406758

67416759
// Then check we can get a normal route
67426760
let payment_params = PaymentParameters::from_node_id(nodes[10], 42);
67436761
let route_params = RouteParameters::from_payment_params_and_value(
67446762
payment_params, 100);
6745-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6763+
let route = get_route(&our_id, &route_params, &network_graph, None,
67466764
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
67476765
assert!(route.is_ok());
67486766

67496767
// Then check that we can't get a route if we ban an intermediate node.
67506768
scorer_params.add_banned(&NodeId::from_pubkey(&nodes[3]));
6751-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6752-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6769+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67536770
assert!(route.is_err());
67546771

67556772
// Finally make sure we can route again, when we remove the ban.
67566773
scorer_params.remove_banned(&NodeId::from_pubkey(&nodes[3]));
6757-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6758-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6774+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67596775
assert!(route.is_ok());
67606776
}
67616777

0 commit comments

Comments
 (0)