Skip to content

Commit 0357f6a

Browse files
authored
Merge pull request #1407 from jkczyz/0.0.106-bindings
[0.0.106-bindings] Restrict ChannelInfo::as_directed_from visibility
2 parents 0a0f87c + 128685e commit 0357f6a

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

lightning-invoice/src/payment.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ use crate::prelude::*;
142142
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
143143
use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
144144
use lightning::ln::msgs::LightningError;
145-
use lightning::routing::scoring::{LockableScore, Score};
145+
use lightning::routing::scoring::{LockableScore, MultiThreadedLockableScore, Score};
146146
use lightning::routing::router::{PaymentParameters, Route, RouteParameters};
147147
use lightning::util::events::{Event, EventHandler};
148148
use lightning::util::logger::Logger;
@@ -160,16 +160,15 @@ use std::time::SystemTime;
160160
/// See [module-level documentation] for details.
161161
///
162162
/// [module-level documentation]: crate::payment
163-
pub struct InvoicePayer<P: Deref, R, S: Deref, L: Deref, E: EventHandler>
163+
pub struct InvoicePayer<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler>
164164
where
165165
P::Target: Payer,
166-
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
167-
S::Target: for <'a> LockableScore<'a>,
166+
R::Target: Router<S>,
168167
L::Target: Logger,
169168
{
170169
payer: P,
171170
router: R,
172-
scorer: S,
171+
scorer: SR,
173172
logger: L,
174173
event_handler: E,
175174
/// Caches the overall attempts at making a payment, which is updated prior to retrying.
@@ -230,19 +229,18 @@ pub enum PaymentError {
230229
Sending(PaymentSendFailure),
231230
}
232231

233-
impl<P: Deref, R, S: Deref, L: Deref, E: EventHandler> InvoicePayer<P, R, S, L, E>
232+
impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> InvoicePayer<P, S, R, SR, L, E>
234233
where
235234
P::Target: Payer,
236-
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
237-
S::Target: for <'a> LockableScore<'a>,
235+
R::Target: Router<S>,
238236
L::Target: Logger,
239237
{
240238
/// Creates an invoice payer that retries failed payment paths.
241239
///
242240
/// Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
243241
/// `retry_attempts` has been exceeded for a given [`Invoice`].
244242
pub fn new(
245-
payer: P, router: R, scorer: S, logger: L, event_handler: E, retry_attempts: RetryAttempts
243+
payer: P, router: R, scorer: SR, logger: L, event_handler: E, retry_attempts: RetryAttempts
246244
) -> Self {
247245
Self {
248246
payer,
@@ -468,11 +466,10 @@ fn has_expired(route_params: &RouteParameters) -> bool {
468466
} else { false }
469467
}
470468

471-
impl<P: Deref, R, S: Deref, L: Deref, E: EventHandler> EventHandler for InvoicePayer<P, R, S, L, E>
469+
impl<P: Deref, S: Score, R: Deref, SR: Deref<Target = MultiThreadedLockableScore<S>>, L: Deref, E: EventHandler> EventHandler for InvoicePayer<P, S, R, SR, L, E>
472470
where
473471
P::Target: Payer,
474-
R: for <'a> Router<<<S as Deref>::Target as LockableScore<'a>>::Locked>,
475-
S::Target: for <'a> LockableScore<'a>,
472+
R::Target: Router<S>,
476473
L::Target: Logger,
477474
{
478475
fn handle_event(&self, event: &Event) {

lightning/src/routing/network_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ pub struct ChannelInfo {
685685
impl ChannelInfo {
686686
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target` from a
687687
/// returned `source`, or `None` if `target` is not one of the channel's counterparties.
688-
pub fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
688+
pub(crate) fn as_directed_to(&self, target: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
689689
let (direction, source) = {
690690
if target == &self.node_one {
691691
(self.two_to_one.as_ref(), &self.node_two)
@@ -700,7 +700,7 @@ impl ChannelInfo {
700700

701701
/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
702702
/// returned `target`, or `None` if `source` is not one of the channel's counterparties.
703-
pub fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
703+
pub(crate) fn as_directed_from(&self, source: &NodeId) -> Option<(DirectedChannelInfo, &NodeId)> {
704704
let (direction, target) = {
705705
if source == &self.node_one {
706706
(self.one_to_two.as_ref(), &self.node_two)

lightning/src/routing/scoring.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
230230
}
231231
}
232232

233+
#[cfg(not(feature = "no-std"))]
233234
/// [`Score`] implementation that provides reasonable default behavior.
234235
///
235236
/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
@@ -247,12 +248,22 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
247248
since = "0.0.105",
248249
note = "ProbabilisticScorer should be used instead of Scorer.",
249250
)]
250-
pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
251-
252-
#[cfg(not(feature = "no-std"))]
253-
type ConfiguredTime = std::time::Instant;
251+
pub type Scorer = ScorerUsingTime::<std::time::Instant>;
254252
#[cfg(feature = "no-std")]
255-
type ConfiguredTime = time::Eternity;
253+
/// [`Score`] implementation that provides reasonable default behavior.
254+
///
255+
/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
256+
/// slightly higher fees are available. Will further penalize channels that fail to relay payments.
257+
///
258+
/// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
259+
///
260+
/// # Note
261+
///
262+
/// Mixing the `no-std` feature between serialization and deserialization results in undefined
263+
/// behavior.
264+
///
265+
/// [module-level documentation]: crate::routing::scoring
266+
pub type Scorer = ScorerUsingTime::<time::Eternity>;
256267

257268
// Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
258269
// doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -481,6 +492,31 @@ impl<T: Time> Readable for ChannelFailure<T> {
481492
}
482493
}
483494

495+
#[cfg(not(feature = "no-std"))]
496+
/// [`Score`] implementation using channel success probability distributions.
497+
///
498+
/// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
499+
/// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
500+
/// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
501+
/// Then the negative `log10` of the success probability is used to determine the cost of routing a
502+
/// specific HTLC amount through a channel.
503+
///
504+
/// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
505+
/// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
506+
/// [`ProbabilisticScoringParameters`] for details.
507+
///
508+
/// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
509+
/// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
510+
/// volume are more likely to experience failed payment paths, which would need to be retried.
511+
///
512+
/// # Note
513+
///
514+
/// Mixing the `no-std` feature between serialization and deserialization results in undefined
515+
/// behavior.
516+
///
517+
/// [1]: https://arxiv.org/abs/2107.05322
518+
pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, std::time::Instant>;
519+
#[cfg(feature = "no-std")]
484520
/// [`Score`] implementation using channel success probability distributions.
485521
///
486522
/// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
@@ -503,7 +539,7 @@ impl<T: Time> Readable for ChannelFailure<T> {
503539
/// behavior.
504540
///
505541
/// [1]: https://arxiv.org/abs/2107.05322
506-
pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, ConfiguredTime>;
542+
pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, time::Eternity>;
507543

508544
/// Probabilistic [`Score`] implementation.
509545
///

0 commit comments

Comments
 (0)