Skip to content

Commit becdf6f

Browse files
authored
Merge pull request #2551 from jbesraa/expose-CandidateRouteHop-to-channel_penalty_msat
Add CandidateRouteHop to channel_penalty_msat inputs
2 parents 78e88e9 + cbde4a7 commit becdf6f

File tree

5 files changed

+576
-280
lines changed

5 files changed

+576
-280
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/gossip.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -870,31 +870,31 @@ impl ChannelInfo {
870870
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
871871
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
872872
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
873-
let (direction, source) = {
873+
let (direction, source, outbound) = {
874874
if target == &self.node_one {
875-
(self.two_to_one.as_ref(), &self.node_two)
875+
(self.two_to_one.as_ref(), &self.node_two, false)
876876
} else if target == &self.node_two {
877-
(self.one_to_two.as_ref(), &self.node_one)
877+
(self.one_to_two.as_ref(), &self.node_one, true)
878878
} else {
879879
return None;
880880
}
881881
};
882-
direction.map(|dir| (DirectedChannelInfo::new(self, dir), source))
882+
direction.map(|dir| (DirectedChannelInfo::new(self, dir, outbound), source))
883883
}
884884

885885
/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
886886
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
887887
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
888-
let (direction, target) = {
888+
let (direction, target, outbound) = {
889889
if source == &self.node_one {
890-
(self.one_to_two.as_ref(), &self.node_two)
890+
(self.one_to_two.as_ref(), &self.node_two, true)
891891
} else if source == &self.node_two {
892-
(self.two_to_one.as_ref(), &self.node_one)
892+
(self.two_to_one.as_ref(), &self.node_one, false)
893893
} else {
894894
return None;
895895
}
896896
};
897-
direction.map(|dir| (DirectedChannelInfo::new(self, dir), target))
897+
direction.map(|dir| (DirectedChannelInfo::new(self, dir, outbound), target))
898898
}
899899

900900
/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
@@ -992,24 +992,32 @@ pub struct DirectedChannelInfo<'a> {
992992
direction: &'a ChannelUpdateInfo,
993993
htlc_maximum_msat: u64,
994994
effective_capacity: EffectiveCapacity,
995+
/// Outbound from the perspective of `node_one`.
996+
///
997+
/// If true, the channel is considered to be outbound from `node_one` perspective.
998+
/// If false, the channel is considered to be outbound from `node_two` perspective.
999+
///
1000+
/// [`ChannelInfo::node_one`]
1001+
/// [`ChannelInfo::node_two`]
1002+
outbound: bool,
9951003
}
9961004

9971005
impl<'a> DirectedChannelInfo<'a> {
9981006
#[inline]
999-
fn new(channel: &'a ChannelInfo, direction: &'a ChannelUpdateInfo) -> Self {
1007+
fn new(channel: &'a ChannelInfo, direction: &'a ChannelUpdateInfo, outbound: bool) -> Self {
10001008
let mut htlc_maximum_msat = direction.htlc_maximum_msat;
10011009
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);
10021010

10031011
let effective_capacity = match capacity_msat {
10041012
Some(capacity_msat) => {
10051013
htlc_maximum_msat = cmp::min(htlc_maximum_msat, capacity_msat);
1006-
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: htlc_maximum_msat }
1014+
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat }
10071015
},
10081016
None => EffectiveCapacity::AdvertisedMaxHTLC { amount_msat: htlc_maximum_msat },
10091017
};
10101018

10111019
Self {
1012-
channel, direction, htlc_maximum_msat, effective_capacity
1020+
channel, direction, htlc_maximum_msat, effective_capacity, outbound
10131021
}
10141022
}
10151023

@@ -1035,6 +1043,16 @@ impl<'a> DirectedChannelInfo<'a> {
10351043
/// Returns information for the direction.
10361044
#[inline]
10371045
pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
1046+
1047+
/// Returns the `node_id` of the source hop.
1048+
///
1049+
/// Refers to the `node_id` forwarding the payment to the next hop.
1050+
pub(super) fn source(&self) -> &'a NodeId { if self.outbound { &self.channel.node_one } else { &self.channel.node_two } }
1051+
1052+
/// Returns the `node_id` of the target hop.
1053+
///
1054+
/// Refers to the `node_id` receiving the payment from the previous hop.
1055+
pub(super) fn target(&self) -> &'a NodeId { if self.outbound { &self.channel.node_two } else { &self.channel.node_one } }
10381056
}
10391057

10401058
impl<'a> fmt::Debug for DirectedChannelInfo<'a> {

0 commit comments

Comments
 (0)