Skip to content

Commit cf5212d

Browse files
Move ScorerAccountingForInFlightHtlcs to scoring + make public
1 parent 9fd9533 commit cf5212d

File tree

3 files changed

+71
-53
lines changed

3 files changed

+71
-53
lines changed

lightning-invoice/src/payment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ where
848848
mod tests {
849849
use super::*;
850850
use crate::{InvoiceBuilder, Currency};
851-
use crate::utils::{ScorerAccountingForInFlightHtlcs, create_invoice_from_channelmanager_and_duration_since_epoch};
851+
use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
852852
use bitcoin_hashes::sha256::Hash as Sha256;
853853
use lightning::ln::PaymentPreimage;
854854
use lightning::ln::channelmanager;
@@ -857,7 +857,7 @@ mod tests {
857857
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
858858
use lightning::routing::gossip::{EffectiveCapacity, NodeId};
859859
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router};
860-
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
860+
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score, ScorerAccountingForInFlightHtlcs};
861861
use lightning::util::test_utils::TestLogger;
862862
use lightning::util::errors::APIError;
863863
use lightning::util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};

lightning-invoice/src/utils.rs

Lines changed: 2 additions & 50 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};
18+
use lightning::routing::gossip::{NetworkGraph, RoutingFees};
1919
use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router};
20-
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
20+
use lightning::routing::scoring::{LockableScore, Score, ScorerAccountingForInFlightHtlcs};
2121
use lightning::util::logger::Logger;
2222
use secp256k1::PublicKey;
2323
use core::ops::Deref;
@@ -625,54 +625,6 @@ where
625625
}
626626
}
627627

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

lightning/src/routing/scoring.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
5757
use crate::ln::msgs::DecodeError;
5858
use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
59-
use crate::routing::router::RouteHop;
59+
use crate::routing::router::{InFlightHtlcs, RouteHop};
6060
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
6161
use crate::util::logger::Logger;
6262
use crate::util::time::Time;
@@ -316,6 +316,72 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
316316
}
317317
}
318318

319+
/// [`Score`] implementation that factors in in-flight HTLC liquidity.
320+
///
321+
/// Useful for custom [`Router`] implementations to wrap their [`Score`] on-the-fly when calling
322+
/// [`find_route`].
323+
///
324+
/// Not to be used in conjunction with [`DefaultRouter`] because `DefaultRouter` automatically wraps
325+
/// its scorer with a `ScorerAccountingForInFlightHtlcs` when finding a route.
326+
///
327+
/// [`DefaultRouter`]: crate::routing::router::DefaultRouter
328+
/// [`Router`]: crate::routing::router::Router
329+
/// [`find_route`]: crate::routing::router::find_route
330+
pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score>
331+
{
332+
scorer: &'a mut S,
333+
// Maps a channel's short channel id and its direction to the liquidity used up.
334+
inflight_htlcs: InFlightHtlcs,
335+
}
336+
337+
impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
338+
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
339+
pub fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
340+
ScorerAccountingForInFlightHtlcs {
341+
scorer,
342+
inflight_htlcs
343+
}
344+
}
345+
}
346+
347+
#[cfg(c_bindings)]
348+
impl<'a, S:Score> crate::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
349+
fn write<W: crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), crate::io::Error> { self.scorer.write(writer) }
350+
}
351+
352+
impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
353+
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
354+
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
355+
source, target, short_channel_id
356+
) {
357+
let usage = ChannelUsage {
358+
inflight_htlc_msat: usage.inflight_htlc_msat + used_liquidity,
359+
..usage
360+
};
361+
362+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
363+
} else {
364+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
365+
}
366+
}
367+
368+
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
369+
self.scorer.payment_path_failed(path, short_channel_id)
370+
}
371+
372+
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
373+
self.scorer.payment_path_successful(path)
374+
}
375+
376+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
377+
self.scorer.probe_failed(path, short_channel_id)
378+
}
379+
380+
fn probe_successful(&mut self, path: &[&RouteHop]) {
381+
self.scorer.probe_successful(path)
382+
}
383+
}
384+
319385
#[cfg(not(feature = "no-std"))]
320386
type ConfiguredTime = std::time::Instant;
321387
#[cfg(feature = "no-std")]

0 commit comments

Comments
 (0)