38
38
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
39
39
//! # use lightning::ln::msgs::LightningError;
40
40
//! # use lightning::routing::gossip::NodeId;
41
- //! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
41
+ //! # use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters, Router };
42
42
//! # use lightning::routing::scoring::{ChannelUsage, Score};
43
43
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
44
44
//! # use lightning::util::logger::{Logger, Record};
45
45
//! # use lightning::util::ser::{Writeable, Writer};
46
46
//! # use lightning_invoice::Invoice;
47
- //! # use lightning_invoice::payment::{InFlightHtlcs, InvoicePayer, Payer, Retry, Router };
47
+ //! # use lightning_invoice::payment::{InvoicePayer, Payer, Retry, ScoringRouter };
48
48
//! # use secp256k1::PublicKey;
49
49
//! # use std::cell::RefCell;
50
50
//! # use std::ops::Deref;
74
74
//! # struct FakeRouter {}
75
75
//! # impl Router for FakeRouter {
76
76
//! # fn find_route(
77
- //! # &self, payer: &PublicKey, params: &RouteParameters, payment_hash: &PaymentHash,
77
+ //! # &self, payer: &PublicKey, params: &RouteParameters,
78
78
//! # first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs
79
79
//! # ) -> Result<Route, LightningError> { unimplemented!() }
80
- //! #
80
+ //! # }
81
+ //! # impl ScoringRouter for FakeRouter {
81
82
//! # fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { unimplemented!() }
82
83
//! # fn notify_payment_path_successful(&self, path: &[&RouteHop]) { unimplemented!() }
83
84
//! # fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { unimplemented!() }
@@ -141,16 +142,14 @@ use bitcoin_hashes::Hash;
141
142
use bitcoin_hashes:: sha256:: Hash as Sha256 ;
142
143
143
144
use crate :: prelude:: * ;
144
- use lightning:: io;
145
145
use lightning:: ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
146
146
use lightning:: ln:: channelmanager:: { ChannelDetails , PaymentId , PaymentSendFailure } ;
147
147
use lightning:: ln:: msgs:: LightningError ;
148
148
use lightning:: routing:: gossip:: NodeId ;
149
- use lightning:: routing:: router:: { PaymentParameters , Route , RouteHop , RouteParameters } ;
149
+ use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , RouteParameters , Router } ;
150
150
use lightning:: util:: errors:: APIError ;
151
151
use lightning:: util:: events:: { Event , EventHandler } ;
152
152
use lightning:: util:: logger:: Logger ;
153
- use lightning:: util:: ser:: Writeable ;
154
153
use crate :: time_utils:: Time ;
155
154
use crate :: sync:: Mutex ;
156
155
@@ -178,7 +177,7 @@ use crate::time_utils;
178
177
type ConfiguredTime = time_utils:: Eternity ;
179
178
180
179
/// (C-not exported) generally all users should use the [`InvoicePayer`] type alias.
181
- pub struct InvoicePayerUsingTime < P : Deref , R : Router , L : Deref , E : EventHandler , T : Time >
180
+ pub struct InvoicePayerUsingTime < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time >
182
181
where
183
182
P :: Target : Payer ,
184
183
L :: Target : Logger ,
@@ -280,13 +279,20 @@ pub trait Payer {
280
279
fn abandon_payment ( & self , payment_id : PaymentId ) ;
281
280
}
282
281
283
- /// A trait defining behavior for routing an [`Invoice`] payment.
284
- pub trait Router {
285
- /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
286
- fn find_route (
287
- & self , payer : & PublicKey , route_params : & RouteParameters , payment_hash : & PaymentHash ,
288
- first_hops : Option < & [ & ChannelDetails ] > , inflight_htlcs : InFlightHtlcs
289
- ) -> Result < Route , LightningError > ;
282
+ /// A trait defining behavior for a [`Router`] implementation that also supports scoring channels
283
+ /// based on payment and probe success/failure.
284
+ ///
285
+ /// [`Router`]: lightning::routing::router::Router
286
+ pub trait ScoringRouter : Router {
287
+ /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes
288
+ /// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment.
289
+ fn find_route_with_id (
290
+ & self , payer : & PublicKey , route_params : & RouteParameters ,
291
+ first_hops : Option < & [ & ChannelDetails ] > , inflight_htlcs : InFlightHtlcs ,
292
+ _payment_hash : PaymentHash , _payment_id : PaymentId
293
+ ) -> Result < Route , LightningError > {
294
+ self . find_route ( payer, route_params, first_hops, inflight_htlcs)
295
+ }
290
296
/// Lets the router know that payment through a specific path has failed.
291
297
fn notify_payment_path_failed ( & self , path : & [ & RouteHop ] , short_channel_id : u64 ) ;
292
298
/// Lets the router know that payment through a specific path was successful.
@@ -336,7 +342,7 @@ pub enum PaymentError {
336
342
Sending ( PaymentSendFailure ) ,
337
343
}
338
344
339
- impl < P : Deref , R : Router , L : Deref , E : EventHandler , T : Time > InvoicePayerUsingTime < P , R , L , E , T >
345
+ impl < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time > InvoicePayerUsingTime < P , R , L , E , T >
340
346
where
341
347
P :: Target : Payer ,
342
348
L :: Target : Logger ,
@@ -529,8 +535,7 @@ where
529
535
let first_hops = self . payer . first_hops ( ) ;
530
536
let inflight_htlcs = self . create_inflight_map ( ) ;
531
537
let route = self . router . find_route (
532
- & payer, & params, & payment_hash, Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) ,
533
- inflight_htlcs
538
+ & payer, & params, Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , inflight_htlcs
534
539
) . map_err ( |e| PaymentError :: Routing ( e) ) ?;
535
540
536
541
match send_payment ( & route) {
@@ -634,8 +639,7 @@ where
634
639
let inflight_htlcs = self . create_inflight_map ( ) ;
635
640
636
641
let route = self . router . find_route (
637
- & payer, & params, & payment_hash, Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) ,
638
- inflight_htlcs
642
+ & payer, & params, Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , inflight_htlcs
639
643
) ;
640
644
641
645
if route. is_err ( ) {
@@ -688,9 +692,8 @@ where
688
692
self . payment_cache . lock ( ) . unwrap ( ) . remove ( payment_hash) ;
689
693
}
690
694
691
- /// Given a [`PaymentHash`], this function looks up inflight path attempts in the payment_cache.
692
- /// Then, it uses the path information inside the cache to construct a HashMap mapping a channel's
693
- /// short channel id and direction to the amount being sent through it.
695
+ /// Use path information in the payment_cache to construct a HashMap mapping a channel's short
696
+ /// channel id and direction to the amount being sent through it.
694
697
///
695
698
/// This function should be called whenever we need information about currently used up liquidity
696
699
/// across payments.
@@ -726,7 +729,7 @@ where
726
729
}
727
730
}
728
731
729
- InFlightHtlcs ( total_inflight_map)
732
+ InFlightHtlcs :: new ( total_inflight_map)
730
733
}
731
734
}
732
735
@@ -741,7 +744,7 @@ fn has_expired(route_params: &RouteParameters) -> bool {
741
744
} else { false }
742
745
}
743
746
744
- impl < P : Deref , R : Router , L : Deref , E : EventHandler , T : Time > EventHandler for InvoicePayerUsingTime < P , R , L , E , T >
747
+ impl < P : Deref , R : ScoringRouter , L : Deref , E : EventHandler , T : Time > EventHandler for InvoicePayerUsingTime < P , R , L , E , T >
745
748
where
746
749
P :: Target : Payer ,
747
750
L :: Target : Logger ,
@@ -815,31 +818,6 @@ where
815
818
}
816
819
}
817
820
818
- /// A map with liquidity value (in msat) keyed by a short channel id and the direction the HTLC
819
- /// is traveling in. The direction boolean is determined by checking if the HTLC source's public
820
- /// key is less than its destination. See [`InFlightHtlcs::used_liquidity_msat`] for more
821
- /// details.
822
- pub struct InFlightHtlcs ( HashMap < ( u64 , bool ) , u64 > ) ;
823
-
824
- impl InFlightHtlcs {
825
- /// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
826
- /// id.
827
- pub fn used_liquidity_msat ( & self , source : & NodeId , target : & NodeId , channel_scid : u64 ) -> Option < u64 > {
828
- self . 0 . get ( & ( channel_scid, source < target) ) . map ( |v| * v)
829
- }
830
- }
831
-
832
- impl Writeable for InFlightHtlcs {
833
- fn write < W : lightning:: util:: ser:: Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > { self . 0 . write ( writer) }
834
- }
835
-
836
- impl lightning:: util:: ser:: Readable for InFlightHtlcs {
837
- fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
838
- let infight_map: HashMap < ( u64 , bool ) , u64 > = lightning:: util:: ser:: Readable :: read ( reader) ?;
839
- Ok ( Self ( infight_map) )
840
- }
841
- }
842
-
843
821
#[ cfg( test) ]
844
822
mod tests {
845
823
use super :: * ;
@@ -852,7 +830,7 @@ mod tests {
852
830
use lightning:: ln:: functional_test_utils:: * ;
853
831
use lightning:: ln:: msgs:: { ChannelMessageHandler , ErrorAction , LightningError } ;
854
832
use lightning:: routing:: gossip:: { EffectiveCapacity , NodeId } ;
855
- use lightning:: routing:: router:: { PaymentParameters , Route , RouteHop } ;
833
+ use lightning:: routing:: router:: { InFlightHtlcs , PaymentParameters , Route , RouteHop , Router } ;
856
834
use lightning:: routing:: scoring:: { ChannelUsage , LockableScore , Score } ;
857
835
use lightning:: util:: test_utils:: TestLogger ;
858
836
use lightning:: util:: errors:: APIError ;
@@ -1895,7 +1873,7 @@ mod tests {
1895
1873
1896
1874
impl Router for TestRouter {
1897
1875
fn find_route (
1898
- & self , payer : & PublicKey , route_params : & RouteParameters , _payment_hash : & PaymentHash ,
1876
+ & self , payer : & PublicKey , route_params : & RouteParameters ,
1899
1877
_first_hops : Option < & [ & ChannelDetails ] > , inflight_htlcs : InFlightHtlcs
1900
1878
) -> Result < Route , LightningError > {
1901
1879
// Simulate calling the Scorer just as you would in find_route
@@ -1926,7 +1904,9 @@ mod tests {
1926
1904
payment_params : Some ( route_params. payment_params . clone ( ) ) , ..Self :: route_for_value ( route_params. final_value_msat )
1927
1905
} )
1928
1906
}
1907
+ }
1929
1908
1909
+ impl ScoringRouter for TestRouter {
1930
1910
fn notify_payment_path_failed ( & self , path : & [ & RouteHop ] , short_channel_id : u64 ) {
1931
1911
self . scorer . lock ( ) . payment_path_failed ( path, short_channel_id) ;
1932
1912
}
@@ -1948,12 +1928,14 @@ mod tests {
1948
1928
1949
1929
impl Router for FailingRouter {
1950
1930
fn find_route (
1951
- & self , _payer : & PublicKey , _params : & RouteParameters , _payment_hash : & PaymentHash ,
1952
- _first_hops : Option < & [ & ChannelDetails ] > , _inflight_htlcs : InFlightHtlcs
1931
+ & self , _payer : & PublicKey , _params : & RouteParameters , _first_hops : Option < & [ & ChannelDetails ] > ,
1932
+ _inflight_htlcs : InFlightHtlcs ,
1953
1933
) -> Result < Route , LightningError > {
1954
1934
Err ( LightningError { err : String :: new ( ) , action : ErrorAction :: IgnoreError } )
1955
1935
}
1936
+ }
1956
1937
1938
+ impl ScoringRouter for FailingRouter {
1957
1939
fn notify_payment_path_failed ( & self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
1958
1940
1959
1941
fn notify_payment_path_successful ( & self , _path : & [ & RouteHop ] ) { }
@@ -2210,12 +2192,13 @@ mod tests {
2210
2192
2211
2193
impl Router for ManualRouter {
2212
2194
fn find_route (
2213
- & self , _payer : & PublicKey , _params : & RouteParameters , _payment_hash : & PaymentHash ,
2214
- _first_hops : Option < & [ & ChannelDetails ] > , _inflight_htlcs : InFlightHtlcs
2195
+ & self , _payer : & PublicKey , _params : & RouteParameters , _first_hops : Option < & [ & ChannelDetails ] > ,
2196
+ _inflight_htlcs : InFlightHtlcs
2215
2197
) -> Result < Route , LightningError > {
2216
2198
self . 0 . borrow_mut ( ) . pop_front ( ) . unwrap ( )
2217
2199
}
2218
-
2200
+ }
2201
+ impl ScoringRouter for ManualRouter {
2219
2202
fn notify_payment_path_failed ( & self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
2220
2203
2221
2204
fn notify_payment_path_successful ( & self , _path : & [ & RouteHop ] ) { }
0 commit comments