Skip to content

Commit 3f9868f

Browse files
Move ScorerAccountingForInFlightHtlcs to router + make public
We move it to router instead of scoring because it pairs with the InFlightHtlcs struct in router and is useful for custom Router trait implementations
1 parent 5f49011 commit 3f9868f

File tree

3 files changed

+65
-53
lines changed

3 files changed

+65
-53
lines changed

lightning-invoice/src/payment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,15 @@ where
731731
mod tests {
732732
use super::*;
733733
use crate::{InvoiceBuilder, Currency};
734-
use crate::utils::{ScorerAccountingForInFlightHtlcs, create_invoice_from_channelmanager_and_duration_since_epoch};
734+
use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
735735
use bitcoin_hashes::sha256::Hash as Sha256;
736736
use lightning::ln::PaymentPreimage;
737737
use lightning::ln::channelmanager;
738738
use lightning::ln::features::{ChannelFeatures, NodeFeatures};
739739
use lightning::ln::functional_test_utils::*;
740740
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
741741
use lightning::routing::gossip::{EffectiveCapacity, NodeId};
742-
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router};
742+
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router, ScorerAccountingForInFlightHtlcs};
743743
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
744744
use lightning::util::test_utils::TestLogger;
745745
use lightning::util::errors::APIError;

lightning-invoice/src/utils.rs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, P
1515
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1616
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
1717
use lightning::ln::msgs::LightningError;
18-
use lightning::routing::gossip::{NetworkGraph, NodeId, RoutingFees};
19-
use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router};
20-
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
18+
use lightning::routing::gossip::{NetworkGraph, RoutingFees};
19+
use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router, ScorerAccountingForInFlightHtlcs};
20+
use lightning::routing::scoring::{LockableScore, Score};
2121
use lightning::util::logger::Logger;
2222
use secp256k1::PublicKey;
2323
use core::ops::Deref;
@@ -627,54 +627,6 @@ where
627627
fn inflight_htlcs(&self) -> InFlightHtlcs { self.compute_inflight_htlcs() }
628628
}
629629

630-
631-
/// Used to store information about all the HTLCs that are inflight across all payment attempts.
632-
pub(crate) struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
633-
scorer: &'a mut S,
634-
/// Maps a channel's short channel id and its direction to the liquidity used up.
635-
inflight_htlcs: InFlightHtlcs,
636-
}
637-
638-
impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
639-
pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
640-
ScorerAccountingForInFlightHtlcs {
641-
scorer,
642-
inflight_htlcs
643-
}
644-
}
645-
}
646-
647-
#[cfg(c_bindings)]
648-
impl<'a, S:Score> lightning::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
649-
fn write<W: lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> { self.scorer.write(writer) }
650-
}
651-
652-
impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
653-
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
654-
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
655-
source, target, short_channel_id
656-
) {
657-
let usage = ChannelUsage {
658-
inflight_htlc_msat: usage.inflight_htlc_msat + used_liquidity,
659-
..usage
660-
};
661-
662-
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
663-
} else {
664-
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
665-
}
666-
}
667-
668-
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
669-
670-
fn payment_path_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
671-
672-
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() }
673-
674-
fn probe_successful(&mut self, _path: &[&RouteHop]) { unreachable!() }
675-
}
676-
677-
678630
#[cfg(test)]
679631
mod test {
680632
use core::time::Duration;

lightning/src/routing/router.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,66 @@ pub trait Router {
5656
fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64);
5757
}
5858

59+
/// [`Score`] implementation that factors in in-flight HTLC liquidity.
60+
///
61+
/// Useful for custom [`Router`] implementations to wrap their [`Score`] on-the-fly when calling
62+
/// [`find_route`].
63+
///
64+
/// [`Score`]: crate::routing::scoring::Score
65+
pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
66+
scorer: &'a mut S,
67+
// Maps a channel's short channel id and its direction to the liquidity used up.
68+
inflight_htlcs: InFlightHtlcs,
69+
}
70+
71+
impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
72+
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
73+
pub fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
74+
ScorerAccountingForInFlightHtlcs {
75+
scorer,
76+
inflight_htlcs
77+
}
78+
}
79+
}
80+
81+
#[cfg(c_bindings)]
82+
impl<'a, S:Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
83+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
84+
}
85+
86+
impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
87+
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
88+
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
89+
source, target, short_channel_id
90+
) {
91+
let usage = ChannelUsage {
92+
inflight_htlc_msat: usage.inflight_htlc_msat + used_liquidity,
93+
..usage
94+
};
95+
96+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
97+
} else {
98+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
99+
}
100+
}
101+
102+
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
103+
self.scorer.payment_path_failed(path, short_channel_id)
104+
}
105+
106+
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
107+
self.scorer.payment_path_successful(path)
108+
}
109+
110+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
111+
self.scorer.probe_failed(path, short_channel_id)
112+
}
113+
114+
fn probe_successful(&mut self, path: &[&RouteHop]) {
115+
self.scorer.probe_successful(path)
116+
}
117+
}
118+
59119
/// A data structure for tracking in-flight HTLCs. May be used during pathfinding to account for
60120
/// in-use channel liquidity.
61121
pub struct InFlightHtlcs(

0 commit comments

Comments
 (0)