Skip to content

Commit fc834d8

Browse files
Move ScorerAccountingForInFlightHtlcs to scoring + make public
1 parent d8844b7 commit fc834d8

File tree

3 files changed

+67
-53
lines changed

3 files changed

+67
-53
lines changed

lightning-invoice/src/payment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ 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;
@@ -740,7 +740,7 @@ mod tests {
740740
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
741741
use lightning::routing::gossip::{EffectiveCapacity, NodeId};
742742
use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, Router};
743-
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score};
743+
use lightning::routing::scoring::{ChannelUsage, LockableScore, Score, ScorerAccountingForInFlightHtlcs};
744744
use lightning::util::test_utils::TestLogger;
745745
use lightning::util::errors::APIError;
746746
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;
@@ -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/scoring.rs

Lines changed: 63 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,68 @@ 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+
/// [`Router`]: crate::routing::router::Router
325+
/// [`find_route`]: crate::routing::router::find_route
326+
pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score>
327+
{
328+
scorer: &'a mut S,
329+
// Maps a channel's short channel id and its direction to the liquidity used up.
330+
inflight_htlcs: InFlightHtlcs,
331+
}
332+
333+
impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
334+
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
335+
pub fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self {
336+
ScorerAccountingForInFlightHtlcs {
337+
scorer,
338+
inflight_htlcs
339+
}
340+
}
341+
}
342+
343+
#[cfg(c_bindings)]
344+
impl<'a, S:Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
345+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
346+
}
347+
348+
impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
349+
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
350+
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(
351+
source, target, short_channel_id
352+
) {
353+
let usage = ChannelUsage {
354+
inflight_htlc_msat: usage.inflight_htlc_msat + used_liquidity,
355+
..usage
356+
};
357+
358+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
359+
} else {
360+
self.scorer.channel_penalty_msat(short_channel_id, source, target, usage)
361+
}
362+
}
363+
364+
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
365+
self.scorer.payment_path_failed(path, short_channel_id)
366+
}
367+
368+
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
369+
self.scorer.payment_path_successful(path)
370+
}
371+
372+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
373+
self.scorer.probe_failed(path, short_channel_id)
374+
}
375+
376+
fn probe_successful(&mut self, path: &[&RouteHop]) {
377+
self.scorer.probe_successful(path)
378+
}
379+
}
380+
319381
#[cfg(not(feature = "no-std"))]
320382
type ConfiguredTime = std::time::Instant;
321383
#[cfg(feature = "no-std")]

0 commit comments

Comments
 (0)