3838//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
3939//! # use lightning::ln::msgs::LightningError;
4040//! # use lightning::routing::gossip::NodeId;
41- //! # use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters};
41+ //! # use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters, Router };
4242//! # use lightning::routing::scoring::{ChannelUsage, Score};
4343//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
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, ScoringRouter };
4848//! # use secp256k1::PublicKey;
4949//! # use std::cell::RefCell;
5050//! # use std::ops::Deref;
7676//! # &self, payer: &PublicKey, params: &RouteParameters,
7777//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs
7878//! # ) -> Result<Route, LightningError> { unimplemented!() }
79- //! #
79+ //! # }
80+ //! # impl ScoringRouter for FakeRouter {
8081//! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() }
8182//! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() }
8283//! # fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { unimplemented!() }
@@ -144,7 +145,7 @@ use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
144145use lightning:: ln:: channelmanager:: { ChannelDetails , PaymentId , PaymentSendFailure } ;
145146use lightning:: ln:: msgs:: LightningError ;
146147use lightning:: routing:: gossip:: NodeId ;
147- use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , RouteParameters } ;
148+ use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , RouteParameters , Router } ;
148149use lightning:: util:: errors:: APIError ;
149150use lightning:: util:: events:: { Event , EventHandler } ;
150151use lightning:: util:: logger:: Logger ;
@@ -175,7 +176,7 @@ use crate::time_utils;
175176type ConfiguredTime = time_utils:: Eternity ;
176177
177178/// (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 >
179+ pub struct InvoicePayerUsingTime < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time >
179180where
180181 P :: Target : Payer ,
181182 L :: Target : Logger ,
@@ -264,13 +265,20 @@ pub trait Payer {
264265 fn abandon_payment ( & self , payment_id : PaymentId ) ;
265266}
266267
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 (
268+ /// A trait defining behavior for a [`Router`] implementation that also supports scoring channels
269+ /// based on payment and probe success/failure.
270+ ///
271+ /// [`Router`]: lightning::routing::router::Router
272+ pub trait ScoringRouter : Router {
273+ /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes
274+ /// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment.
275+ fn find_route_with_id (
271276 & self , payer : & PublicKey , route_params : & RouteParameters ,
272- first_hops : Option < & [ & ChannelDetails ] > , inflight_htlcs : InFlightHtlcs
273- ) -> Result < Route , LightningError > ;
277+ first_hops : Option < & [ & ChannelDetails ] > , inflight_htlcs : InFlightHtlcs ,
278+ _payment_hash : PaymentHash , _payment_id : PaymentId
279+ ) -> Result < Route , LightningError > {
280+ self . find_route ( payer, route_params, first_hops, inflight_htlcs)
281+ }
274282 /// Lets the router know that payment through a specific path has failed.
275283 fn notify_payment_path_failed ( & self , path : & [ & RouteHop ] , short_channel_id : u64 ) ;
276284 /// Lets the router know that payment through a specific path was successful.
@@ -320,7 +328,7 @@ pub enum PaymentError {
320328 Sending ( PaymentSendFailure ) ,
321329}
322330
323- impl < P : Deref , R : Router , L : Deref , E : EventHandler , T : Time > InvoicePayerUsingTime < P , R , L , E , T >
331+ impl < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time > InvoicePayerUsingTime < P , R , L , E , T >
324332where
325333 P :: Target : Payer ,
326334 L :: Target : Logger ,
@@ -654,7 +662,7 @@ fn has_expired(route_params: &RouteParameters) -> bool {
654662 } else { false }
655663}
656664
657- impl < P : Deref , R : Router , L : Deref , E : EventHandler , T : Time > EventHandler for InvoicePayerUsingTime < P , R , L , E , T >
665+ impl < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time > EventHandler for InvoicePayerUsingTime < P , R , L , E , T >
658666where
659667 P :: Target : Payer ,
660668 L :: Target : Logger ,
@@ -740,7 +748,7 @@ mod tests {
740748 use lightning:: ln:: functional_test_utils:: * ;
741749 use lightning:: ln:: msgs:: { ChannelMessageHandler , ErrorAction , LightningError } ;
742750 use lightning:: routing:: gossip:: { EffectiveCapacity , NodeId } ;
743- use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop } ;
751+ use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , Router } ;
744752 use lightning:: routing:: scoring:: { ChannelUsage , LockableScore , Score } ;
745753 use lightning:: util:: test_utils:: TestLogger ;
746754 use lightning:: util:: errors:: APIError ;
@@ -1814,7 +1822,9 @@ mod tests {
18141822 payment_params : Some ( route_params. payment_params . clone ( ) ) , ..Self :: route_for_value ( route_params. final_value_msat )
18151823 } )
18161824 }
1825+ }
18171826
1827+ impl ScoringRouter for TestRouter {
18181828 fn notify_payment_path_failed ( & self , path : & [ & RouteHop ] , short_channel_id : u64 ) {
18191829 self . scorer . lock ( ) . payment_path_failed ( path, short_channel_id) ;
18201830 }
@@ -1841,7 +1851,9 @@ mod tests {
18411851 ) -> Result < Route , LightningError > {
18421852 Err ( LightningError { err : String :: new ( ) , action : ErrorAction :: IgnoreError } )
18431853 }
1854+ }
18441855
1856+ impl ScoringRouter for FailingRouter {
18451857 fn notify_payment_path_failed ( & self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
18461858
18471859 fn notify_payment_path_successful ( & self , _path : & [ & RouteHop ] ) { }
@@ -2103,7 +2115,8 @@ mod tests {
21032115 ) -> Result < Route , LightningError > {
21042116 self . 0 . borrow_mut ( ) . pop_front ( ) . unwrap ( )
21052117 }
2106-
2118+ }
2119+ impl ScoringRouter for ManualRouter {
21072120 fn notify_payment_path_failed ( & self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
21082121
21092122 fn notify_payment_path_successful ( & self , _path : & [ & RouteHop ] ) { }
0 commit comments