Skip to content

Commit 45d35f8

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 f10708d commit 45d35f8

File tree

4 files changed

+468
-192
lines changed

4 files changed

+468
-192
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: 46 additions & 29 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
}
@@ -1062,7 +1071,7 @@ impl<'a> CandidateRouteHop<'a> {
10621071
/// For `Blinded` and `OneHopBlinded` we return `None` because next hop is not known.
10631072
pub fn short_channel_id(&self) -> Option<u64> {
10641073
match self {
1065-
CandidateRouteHop::FirstHop { details, .. } => Some(details.get_outbound_payment_scid().unwrap()),
1074+
CandidateRouteHop::FirstHop { details, .. } => details.get_outbound_payment_scid(),
10661075
CandidateRouteHop::PublicHop { short_channel_id, .. } => Some(*short_channel_id),
10671076
CandidateRouteHop::PrivateHop { hint, .. } => Some(hint.short_channel_id),
10681077
CandidateRouteHop::Blinded { .. } => None,
@@ -1168,7 +1177,7 @@ impl<'a> CandidateRouteHop<'a> {
11681177
CandidateRouteHop::PublicHop { source_node_id, .. } => *source_node_id,
11691178
CandidateRouteHop::PrivateHop { hint, .. } => hint.src_node_id.into(),
11701179
CandidateRouteHop::Blinded { hint, .. } => hint.1.introduction_node_id.into(),
1171-
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into()
1180+
CandidateRouteHop::OneHopBlinded { hint, .. } => hint.1.introduction_node_id.into(),
11721181
}
11731182
}
11741183
/// Returns the target node id of this hop, if known.
@@ -1795,7 +1804,7 @@ where L::Target: Logger {
17951804
let mut num_ignored_htlc_minimum_msat_limit: u32 = 0;
17961805

17971806
macro_rules! add_entry {
1798-
// Adds entry which goes from $src_node_id to $dest_node_id over the $candidate hop.
1807+
// Adds entry which goes from candidate.source() to candiadte.target() over the $candidate hop.
17991808
// $next_hops_fee_msat represents the fees paid for using all the channels *after* this one,
18001809
// since that value has to be transferred over this channel.
18011810
// Returns the contribution amount of $candidate if the channel caused an update to `targets`.
@@ -1811,7 +1820,7 @@ where L::Target: Logger {
18111820
// - for first and last hops early in get_route
18121821
let src_node_id = $candidate.source();
18131822
let dest_node_id = $candidate.target().unwrap_or(maybe_dummy_payee_node_id);
1814-
if src_node_id != dest_node_id {
1823+
if Some($candidate.source()) != $candidate.target() {
18151824
let scid_opt = $candidate.short_channel_id();
18161825
let effective_capacity = $candidate.effective_capacity();
18171826
let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, channel_saturation_pow_half);
@@ -1976,9 +1985,10 @@ where L::Target: Logger {
19761985
inflight_htlc_msat: used_liquidity_msat,
19771986
effective_capacity,
19781987
};
1979-
let channel_penalty_msat = scid_opt.map_or(0,
1980-
|scid| scorer.channel_penalty_msat(scid, &src_node_id, &dest_node_id,
1981-
channel_usage, score_params));
1988+
let channel_penalty_msat =
1989+
scorer.channel_penalty_msat($candidate,
1990+
channel_usage,
1991+
score_params);
19821992
let path_penalty_msat = $next_hops_path_penalty_msat
19831993
.saturating_add(channel_penalty_msat);
19841994
let new_graph_node = RouteGraphNode {
@@ -1991,7 +2001,7 @@ where L::Target: Logger {
19912001
path_length_to_node,
19922002
};
19932003

1994-
// Update the way of reaching $src_node_id with the given short_channel_id (from $dest_node_id),
2004+
// Update the way of reaching $candidate.source() with the given short_channel_id (from $candidate.target()),
19952005
// if this way is cheaper than the already known
19962006
// (considering the cost to "reach" this channel from the route destination,
19972007
// the cost of using this channel,
@@ -2285,7 +2295,7 @@ where L::Target: Logger {
22852295
effective_capacity: candidate.effective_capacity(),
22862296
};
22872297
let channel_penalty_msat = scorer.channel_penalty_msat(
2288-
hop.short_channel_id, &source, &target, channel_usage, score_params
2298+
&candidate, channel_usage, score_params
22892299
);
22902300
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
22912301
.saturating_add(channel_penalty_msat);
@@ -2649,7 +2659,6 @@ where L::Target: Logger {
26492659
let mut paths = Vec::new();
26502660
for payment_path in selected_route {
26512661
let mut hops = Vec::with_capacity(payment_path.hops.len());
2652-
let mut prev_hop_node_id = our_node_id;
26532662
for (hop, node_features) in payment_path.hops.iter()
26542663
.filter(|(h, _)| h.candidate.short_channel_id().is_some())
26552664
{
@@ -2666,7 +2675,7 @@ where L::Target: Logger {
26662675
// an alias, in which case we don't take any chances here.
26672676
network_graph.node(&hop.node_id).map_or(false, |hop_node|
26682677
hop_node.channels.iter().any(|scid| network_graph.channel(*scid)
2669-
.map_or(false, |c| c.as_directed_from(&prev_hop_node_id).is_some()))
2678+
.map_or(false, |c| c.as_directed_from(&hop.candidate.source()).is_some()))
26702679
)
26712680
};
26722681

@@ -2679,8 +2688,6 @@ where L::Target: Logger {
26792688
cltv_expiry_delta: hop.candidate.cltv_expiry_delta(),
26802689
maybe_announced_channel,
26812690
});
2682-
2683-
prev_hop_node_id = hop.node_id;
26842691
}
26852692
let mut final_cltv_delta = final_cltv_expiry_delta;
26862693
let blinded_tail = payment_path.hops.last().and_then(|(h, _)| {
@@ -2843,13 +2850,13 @@ fn build_route_from_hops_internal<L: Deref>(
28432850

28442851
impl ScoreLookUp for HopScorer {
28452852
type ScoreParams = ();
2846-
fn channel_penalty_msat(&self, _short_channel_id: u64, source: &NodeId, target: &NodeId,
2853+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop,
28472854
_usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64
28482855
{
28492856
let mut cur_id = self.our_node_id;
28502857
for i in 0..self.hop_ids.len() {
28512858
if let Some(next_id) = self.hop_ids[i] {
2852-
if cur_id == *source && next_id == *target {
2859+
if cur_id == candidate.source() && Some(next_id) == candidate.target() {
28532860
return 0;
28542861
}
28552862
cur_id = next_id;
@@ -2925,6 +2932,8 @@ mod tests {
29252932

29262933
use core::convert::TryInto;
29272934

2935+
use super::CandidateRouteHop;
2936+
29282937
fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
29292938
features: InitFeatures, outbound_capacity_msat: u64) -> channelmanager::ChannelDetails {
29302939
channelmanager::ChannelDetails {
@@ -6197,8 +6206,8 @@ mod tests {
61976206
}
61986207
impl ScoreLookUp for BadChannelScorer {
61996208
type ScoreParams = ();
6200-
fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6201-
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
6209+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6210+
if candidate.short_channel_id() == Some(self.short_channel_id) { u64::max_value() } else { 0 }
62026211
}
62036212
}
62046213

@@ -6213,8 +6222,8 @@ mod tests {
62136222

62146223
impl ScoreLookUp for BadNodeScorer {
62156224
type ScoreParams = ();
6216-
fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6217-
if *target == self.node_id { u64::max_value() } else { 0 }
6225+
fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
6226+
if candidate.target() == Some(self.node_id) { u64::max_value() } else { 0 }
62186227
}
62196228
}
62206229

@@ -6702,26 +6711,34 @@ mod tests {
67026711
};
67036712
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
67046713
scorer_params.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);
6705-
assert_eq!(scorer.channel_penalty_msat(42, &NodeId::from_pubkey(&nodes[3]), &NodeId::from_pubkey(&nodes[4]), usage, &scorer_params), 456);
6714+
let network_graph = network_graph.read_only();
6715+
let channels = network_graph.channels();
6716+
let channel = channels.get(&5).unwrap();
6717+
let info = channel.as_directed_from(&NodeId::from_pubkey(&nodes[3])).unwrap();
6718+
let candidate: CandidateRouteHop = CandidateRouteHop::PublicHop {
6719+
info: info.0,
6720+
short_channel_id: 5,
6721+
source_node_id: NodeId::from_pubkey(&nodes[3]),
6722+
target_node_id: NodeId::from_pubkey(&nodes[4]),
6723+
};
6724+
assert_eq!(scorer.channel_penalty_msat(&candidate, usage, &scorer_params), 456);
67066725

67076726
// Then check we can get a normal route
67086727
let payment_params = PaymentParameters::from_node_id(nodes[10], 42);
67096728
let route_params = RouteParameters::from_payment_params_and_value(
67106729
payment_params, 100);
6711-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6730+
let route = get_route(&our_id, &route_params, &network_graph, None,
67126731
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
67136732
assert!(route.is_ok());
67146733

67156734
// Then check that we can't get a route if we ban an intermediate node.
67166735
scorer_params.add_banned(&NodeId::from_pubkey(&nodes[3]));
6717-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6718-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6736+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67196737
assert!(route.is_err());
67206738

67216739
// Finally make sure we can route again, when we remove the ban.
67226740
scorer_params.remove_banned(&NodeId::from_pubkey(&nodes[3]));
6723-
let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
6724-
Arc::clone(&logger), &scorer, &scorer_params, &random_seed_bytes);
6741+
let route = get_route(&our_id, &route_params, &network_graph, None, Arc::clone(&logger), &scorer, &scorer_params,&random_seed_bytes);
67256742
assert!(route.is_ok());
67266743
}
67276744

0 commit comments

Comments
 (0)