Skip to content

Commit 8828fce

Browse files
Move InvoicePayer's Router into ChannelManager
This helps prepare to parameterize ChannelManager with a Router, to eventually use in trampoline payments.
1 parent 14061ff commit 8828fce

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

lightning-invoice/src/payment.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! #
3636
//! # use lightning::io;
3737
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
38-
//! # use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure};
38+
//! # use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure, Router};
3939
//! # use lightning::ln::msgs::LightningError;
4040
//! # use lightning::routing::gossip::NodeId;
4141
//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
@@ -44,7 +44,7 @@
4444
//! # use lightning::util::logger::{Logger, Record};
4545
//! # use lightning::util::ser::{Writeable, Writer};
4646
//! # use lightning_invoice::Invoice;
47-
//! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry, Router};
47+
//! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry, ProbingRouter};
4848
//! # use secp256k1::PublicKey;
4949
//! # use std::cell::RefCell;
5050
//! # use std::ops::Deref;
@@ -79,6 +79,8 @@
7979
//! #
8080
//! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() }
8181
//! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() }
82+
//! # }
83+
//! # impl ProbingRouter for FakeRouter {
8284
//! # fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { unimplemented!() }
8385
//! # fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() }
8486
//! # }
@@ -141,7 +143,7 @@ use bitcoin_hashes::sha256::Hash as Sha256;
141143

142144
use crate::prelude::*;
143145
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
144-
use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure};
146+
use lightning::ln::channelmanager::{ChannelDetails, InFlightHtlcs, PaymentId, PaymentSendFailure, Router};
145147
use lightning::ln::msgs::LightningError;
146148
use lightning::routing::gossip::NodeId;
147149
use lightning::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters};
@@ -175,7 +177,7 @@ use crate::time_utils;
175177
type ConfiguredTime = time_utils::Eternity;
176178

177179
/// (C-not exported) generally all users should use the [`InvoicePayer`] type alias.
178-
pub struct InvoicePayerUsingTime<P: Deref, R: Router, L: Deref, E: EventHandler, T: Time>
180+
pub struct InvoicePayerUsingTime<P: Deref, R: ProbingRouter, L: Deref, E: EventHandler, T: Time>
179181
where
180182
P::Target: Payer,
181183
L::Target: Logger,
@@ -264,17 +266,10 @@ pub trait Payer {
264266
fn abandon_payment(&self, payment_id: PaymentId);
265267
}
266268

267-
/// A trait defining behavior for routing an [`Invoice`] payment.
268-
pub trait Router {
269-
/// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
270-
fn find_route(
271-
&self, payer: &PublicKey, route_params: &RouteParameters,
272-
first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs
273-
) -> Result<Route, LightningError>;
274-
/// Lets the router know that payment through a specific path has failed.
275-
fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64);
276-
/// Lets the router know that payment through a specific path was successful.
277-
fn notify_payment_path_successful(&self, path: &[&RouteHop]);
269+
/// A trait defining behavior for a [`Router`] implementation that also supports probing.
270+
///
271+
/// [`Router`]: lightning::ln::channelmanager::Router
272+
pub trait ProbingRouter: Router {
278273
/// Lets the router know that a payment probe was successful.
279274
fn notify_payment_probe_successful(&self, path: &[&RouteHop]);
280275
/// Lets the router know that a payment probe failed.
@@ -320,7 +315,7 @@ pub enum PaymentError {
320315
Sending(PaymentSendFailure),
321316
}
322317

323-
impl<P: Deref, R: Router, L: Deref, E: EventHandler, T: Time> InvoicePayerUsingTime<P, R, L, E, T>
318+
impl<P: Deref, R: ProbingRouter, L: Deref, E: EventHandler, T: Time> InvoicePayerUsingTime<P, R, L, E, T>
324319
where
325320
P::Target: Payer,
326321
L::Target: Logger,
@@ -654,7 +649,7 @@ fn has_expired(route_params: &RouteParameters) -> bool {
654649
} else { false }
655650
}
656651

657-
impl<P: Deref, R: Router, L: Deref, E: EventHandler, T: Time> EventHandler for InvoicePayerUsingTime<P, R, L, E, T>
652+
impl<P: Deref, R: ProbingRouter, L: Deref, E: EventHandler, T: Time> EventHandler for InvoicePayerUsingTime<P, R, L, E, T>
658653
where
659654
P::Target: Payer,
660655
L::Target: Logger,
@@ -1781,7 +1776,7 @@ mod tests {
17811776
}
17821777
}
17831778

1784-
impl Router for TestRouter {
1779+
impl channelmanager::Router for TestRouter {
17851780
fn find_route(
17861781
&self, payer: &PublicKey, route_params: &RouteParameters,
17871782
_first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: channelmanager::InFlightHtlcs
@@ -1822,7 +1817,9 @@ mod tests {
18221817
fn notify_payment_path_successful(&self, path: &[&RouteHop]) {
18231818
self.scorer.lock().payment_path_successful(path);
18241819
}
1820+
}
18251821

1822+
impl ProbingRouter for TestRouter {
18261823
fn notify_payment_probe_successful(&self, path: &[&RouteHop]) {
18271824
self.scorer.lock().probe_successful(path);
18281825
}
@@ -1834,7 +1831,7 @@ mod tests {
18341831

18351832
struct FailingRouter;
18361833

1837-
impl Router for FailingRouter {
1834+
impl channelmanager::Router for FailingRouter {
18381835
fn find_route(
18391836
&self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>,
18401837
_inflight_htlcs: channelmanager::InFlightHtlcs
@@ -1845,7 +1842,9 @@ mod tests {
18451842
fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}
18461843

18471844
fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {}
1845+
}
18481846

1847+
impl ProbingRouter for FailingRouter {
18491848
fn notify_payment_probe_successful(&self, _path: &[&RouteHop]) {}
18501849

18511850
fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}
@@ -2096,7 +2095,7 @@ mod tests {
20962095
// *** Full Featured Functional Tests with a Real ChannelManager ***
20972096
struct ManualRouter(RefCell<VecDeque<Result<Route, LightningError>>>);
20982097

2099-
impl Router for ManualRouter {
2098+
impl channelmanager::Router for ManualRouter {
21002099
fn find_route(
21012100
&self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>,
21022101
_inflight_htlcs: channelmanager::InFlightHtlcs
@@ -2107,7 +2106,8 @@ mod tests {
21072106
fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}
21082107

21092108
fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {}
2110-
2109+
}
2110+
impl ProbingRouter for ManualRouter {
21112111
fn notify_payment_probe_successful(&self, _path: &[&RouteHop]) {}
21122112

21132113
fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}

lightning-invoice/src/utils.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Convenient utilities to create an invoice.
22
33
use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
4-
use crate::payment::{Payer, Router};
4+
use crate::payment::{Payer, ProbingRouter};
55

66
use crate::{prelude::*, Description, InvoiceDescription, Sha256};
77
use bech32::ToBase32;
@@ -10,7 +10,7 @@ use lightning::chain;
1010
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
1111
use lightning::chain::keysinterface::{Recipient, KeysInterface, Sign};
1212
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
13-
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, InFlightHtlcs, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY};
13+
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, InFlightHtlcs, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY, Router};
1414
#[cfg(feature = "std")]
1515
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1616
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
@@ -575,7 +575,12 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for DefaultR
575575
fn notify_payment_path_successful(&self, path: &[&RouteHop]) {
576576
self.scorer.lock().payment_path_successful(path);
577577
}
578+
}
578579

580+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> ProbingRouter for DefaultRouter<G, L, S> where
581+
L::Target: Logger,
582+
S::Target: for <'a> LockableScore<'a>,
583+
{
579584
fn notify_payment_probe_successful(&self, path: &[&RouteHop]) {
580585
self.scorer.lock().probe_successful(path);
581586
}

lightning/src/ln/channelmanager.rs

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ impl Readable for InFlightHtlcs {
106106
}
107107
}
108108

109+
/// A trait defining behavior for routing an payment.
110+
pub trait Router {
111+
/// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
112+
fn find_route(
113+
&self, payer: &PublicKey, route_params: &RouteParameters,
114+
first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs
115+
) -> Result<Route, LightningError>;
116+
/// Lets the router know that payment through a specific path has failed.
117+
fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64);
118+
/// Lets the router know that payment through a specific path was successful.
119+
fn notify_payment_path_successful(&self, path: &[&RouteHop]);
120+
}
121+
109122
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
110123
//
111124
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should

0 commit comments

Comments
 (0)