Skip to content

Commit e8b5a25

Browse files
committed
Use CandidateRouteHop as input for channel_penalty_msat
We remove `source`, `target` and `scid` from `channel_penalty_msat` to consume them from `candidate` of type `CandidateRouteHop`
1 parent 243bfb1 commit e8b5a25

File tree

4 files changed

+473
-191
lines changed

4 files changed

+473
-191
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 49 additions & 28 deletions
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
}
@@ -1065,7 +1074,7 @@ impl<'a> CandidateRouteHop<'a> {
10651074
/// For `Blinded` and `OneHopBlinded` we return `None` because next hop is not known.
10661075
pub fn short_channel_id(&self) -> Option<u64> {
10671076
match self {
1068-
CandidateRouteHop::FirstHop { details, .. } => Some(details.get_outbound_payment_scid().unwrap()),
1077+
CandidateRouteHop::FirstHop { details, .. } => details.get_outbound_payment_scid(),
10691078
CandidateRouteHop::PublicHop { short_channel_id, .. } => Some(*short_channel_id),
10701079
CandidateRouteHop::PrivateHop { hint, .. } => Some(hint.short_channel_id),
10711080
CandidateRouteHop::Blinded { .. } => None,
@@ -1171,7 +1180,7 @@ impl<'a> CandidateRouteHop<'a> {
11711180
CandidateRouteHop::PublicHop { source_node_id, .. } => *source_node_id,
11721181
CandidateRouteHop::PrivateHop { hint, .. } => hint.src_node_id.into(),
11731182
CandidateRouteHop::Blinded { hint, .. } => hint.1.introduction_node_id.into(),
1174-
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into()
1183+
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into(),
11751184
}
11761185
}
11771186
/// Returns the target node id of this hop, if known.
@@ -1798,7 +1807,7 @@ where L::Target: Logger {
17981807
let mut num_ignored_htlc_minimum_msat_limit: u32 = 0;
17991808

18001809
macro_rules! add_entry {
1801-
// Adds entry which goes from $src_node_id to $dest_node_id over the $candidate hop.
1810+
// Adds entry which goes from candidate.source() to candiadte.target() over the $candidate hop.
18021811
// $next_hops_fee_msat represents the fees paid for using all the channels *after* this one,
18031812
// since that value has to be transferred over this channel.
18041813
// Returns the contribution amount of $candidate if the channel caused an update to `targets`.
@@ -1814,7 +1823,7 @@ where L::Target: Logger {
18141823
// - for first and last hops early in get_route
18151824
let src_node_id = $candidate.source();
18161825
let dest_node_id = $candidate.target().unwrap_or(maybe_dummy_payee_node_id);
1817-
if src_node_id != dest_node_id {
1826+
if Some($candidate.source()) != $candidate.target() {
18181827
let scid_opt = $candidate.short_channel_id();
18191828
let effective_capacity = $candidate.effective_capacity();
18201829
let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, channel_saturation_pow_half);
@@ -1979,9 +1988,10 @@ where L::Target: Logger {
19791988
inflight_htlc_msat: used_liquidity_msat,
19801989
effective_capacity,
19811990
};
1982-
let channel_penalty_msat = scid_opt.map_or(0,
1983-
|scid| scorer.channel_penalty_msat(scid, &src_node_id, &dest_node_id,
1984-
channel_usage, score_params));
1991+
let channel_penalty_msat =
1992+
scorer.channel_penalty_msat($candidate,
1993+
channel_usage,
1994+
score_params);
19851995
let path_penalty_msat = $next_hops_path_penalty_msat
19861996
.saturating_add(channel_penalty_msat);
19871997
let new_graph_node = RouteGraphNode {
@@ -1994,7 +2004,7 @@ where L::Target: Logger {
19942004
path_length_to_node,
19952005
};
19962006

1997-
// Update the way of reaching $src_node_id with the given short_channel_id (from $dest_node_id),
2007+
// Update the way of reaching $candidate.source() with the given short_channel_id (from $candidate.target()),
19982008
// if this way is cheaper than the already known
19992009
// (considering the cost to "reach" this channel from the route destination,
20002010
// the cost of using this channel,
@@ -2288,7 +2298,7 @@ where L::Target: Logger {
22882298
effective_capacity: candidate.effective_capacity(),
22892299
};
22902300
let channel_penalty_msat = scorer.channel_penalty_msat(
2291-
hop.short_channel_id, &source, &target, channel_usage, score_params
2301+
&candidate, channel_usage, score_params
22922302
);
22932303
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
22942304
.saturating_add(channel_penalty_msat);
@@ -2652,7 +2662,6 @@ where L::Target: Logger {
26522662
let mut paths = Vec::new();
26532663
for payment_path in selected_route {
26542664
let mut hops = Vec::with_capacity(payment_path.hops.len());
2655-
let mut prev_hop_node_id = our_node_id;
26562665
for (hop, node_features) in payment_path.hops.iter()
26572666
.filter(|(h, _)| h.candidate.short_channel_id().is_some())
26582667
{
@@ -2669,7 +2678,7 @@ where L::Target: Logger {
26692678
// an alias, in which case we don't take any chances here.
26702679
network_graph.node(&hop.node_id).map_or(false, |hop_node|
26712680
hop_node.channels.iter().any(|scid| network_graph.channel(*scid)
2672-
.map_or(false, |c| c.as_directed_from(&prev_hop_node_id).is_some()))
2681+
.map_or(false, |c| c.as_directed_from(&hop.candidate.source()).is_some()))
26732682
)
26742683
};
26752684

@@ -2682,8 +2691,6 @@ where L::Target: Logger {
26822691
cltv_expiry_delta: hop.candidate.cltv_expiry_delta(),
26832692
maybe_announced_channel,
26842693
});
2685-
2686-
prev_hop_node_id = hop.node_id;
26872694
}
26882695
let mut final_cltv_delta = final_cltv_expiry_delta;
26892696
let blinded_tail = payment_path.hops.last().and_then(|(h, _)| {
@@ -2846,13 +2853,13 @@ fn build_route_from_hops_internal<L: Deref>(
28462853

28472854
impl ScoreLookUp for HopScorer {
28482855
type ScoreParams = ();
2849-
fn channel_penalty_msat(&self, _short_channel_id: u64, source: &NodeId, target: &NodeId,
2856+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop,
28502857
_usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64
28512858
{
28522859
let mut cur_id = self.our_node_id;
28532860
for i in 0..self.hop_ids.len() {
28542861
if let Some(next_id) = self.hop_ids[i] {
2855-
if cur_id == *source && next_id == *target {
2862+
if cur_id == candidate.source() && Some(next_id) == candidate.target() {
28562863
return 0;
28572864
}
28582865
cur_id = next_id;
@@ -2928,6 +2935,8 @@ mod tests {
29282935

29292936
use core::convert::TryInto;
29302937

2938+
use super::CandidateRouteHop;
2939+
29312940
fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
29322941
features: InitFeatures, outbound_capacity_msat: u64) -> channelmanager::ChannelDetails {
29332942
channelmanager::ChannelDetails {
@@ -6200,7 +6209,11 @@ mod tests {
62006209
}
62016210
impl ScoreLookUp for BadChannelScorer {
62026211
type ScoreParams = ();
6203-
fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6212+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6213+
let short_channel_id = match candidate.short_channel_id() {
6214+
Some(id) => id,
6215+
None => return 0,
6216+
};
62046217
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
62056218
}
62066219
}
@@ -6216,8 +6229,8 @@ mod tests {
62166229

62176230
impl ScoreLookUp for BadNodeScorer {
62186231
type ScoreParams = ();
6219-
fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6220-
if *target == self.node_id { u64::max_value() } else { 0 }
6232+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6233+
if candidate.target() == Some(self.node_id) { u64::max_value() } else { 0 }
62216234
}
62226235
}
62236236

@@ -6705,26 +6718,34 @@ mod tests {
67056718
};
67066719
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
67076720
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);
6708-
assert_eq!(scorer.channel_penalty_msat(42, &NodeId::from_pubkey(&nodes[3]), &NodeId::from_pubkey(&nodes[4]), usage, &scorer_params), 456);
6721+
let network_graph = network_graph.read_only();
6722+
let channels = network_graph.channels();
6723+
let channel = channels.get(&5).unwrap();
6724+
let info = channel.as_directed_from(&NodeId::from_pubkey(&nodes[3])).unwrap();
6725+
let candidate: CandidateRouteHop = CandidateRouteHop::PublicHop {
6726+
info: info.0,
6727+
short_channel_id: 5,
6728+
source_node_id: NodeId::from_pubkey(&nodes[3]),
6729+
target_node_id: NodeId::from_pubkey(&nodes[4]),
6730+
};
6731+
assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &scorer_params), 456);
67096732

67106733
// Then check we can get a normal route
67116734
let payment_params = PaymentParameters::from_node_id(nodes[10], 42);
67126735
let route_params = RouteParameters::from_payment_params_and_value(
67136736
payment_params, 100);
6714-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6737+
let route = get_route(&our_id, &route_params, &network_graph, None,
67156738
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
67166739
assert!(route.is_ok());
67176740

67186741
// Then check that we can't get a route if we ban an intermediate node.
67196742
scorer_params.add_banned(&NodeId::from_pubkey(&nodes[3]));
6720-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6721-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6743+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67226744
assert!(route.is_err());
67236745

67246746
// Finally make sure we can route again, when we remove the ban.
67256747
scorer_params.remove_banned(&NodeId::from_pubkey(&nodes[3]));
6726-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6727-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6748+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67286749
assert!(route.is_ok());
67296750
}
67306751

0 commit comments

Comments
 (0)