Skip to content

Commit 9034bc4

Browse files
Move ScorerAccountingForInFlightHtlcs to scoring + make public
1 parent 5f49011 commit 9034bc4

File tree

3 files changed

+66
-53
lines changed

3 files changed

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

0 commit comments

Comments
 (0)