48
48
49
49
use routing;
50
50
51
+ use ln:: msgs:: DecodeError ;
51
52
use routing:: network_graph:: NodeId ;
52
53
use routing:: router:: RouteHop ;
54
+ use util:: ser:: { Readable , Writeable , Writer } ;
53
55
54
56
use prelude:: * ;
57
+ use core:: ops:: Sub ;
55
58
use core:: time:: Duration ;
59
+ use io:: { self , Read } ;
56
60
57
61
/// [`routing::Score`] implementation that provides reasonable default behavior.
58
62
///
@@ -75,7 +79,7 @@ pub type DefaultClock = AlwaysPresent;
75
79
/// [`routing::Score`] implementation parameterized by a [`Clock`].
76
80
///
77
81
/// See [`Scorer`] for details.
78
- pub struct ScorerUsingClock < C : Clock > {
82
+ pub struct ScorerUsingClock < C : Clock + Sub < Duration , Output = C > > {
79
83
params : ScoringParameters ,
80
84
channel_failures : HashMap < u64 , ChannelFailure < C > > ,
81
85
}
@@ -96,10 +100,16 @@ pub struct ScoringParameters {
96
100
pub failure_penalty_half_life : Duration ,
97
101
}
98
102
103
+ impl_writeable_tlv_based ! ( ScoringParameters , {
104
+ ( 0 , base_penalty_msat, required) ,
105
+ ( 2 , failure_penalty_msat, required) ,
106
+ ( 4 , failure_penalty_half_life, required) ,
107
+ } ) ;
108
+
99
109
/// Accounting for penalties from channel failures.
100
110
///
101
111
/// Penalties decay over time, though accumulated as more failures occur.
102
- struct ChannelFailure < C : Clock > {
112
+ struct ChannelFailure < C : Clock + Sub < Duration , Output = C > > {
103
113
/// Accumulated penalty in msats for the channel as of `last_failed`.
104
114
undecayed_penalty_msat : u64 ,
105
115
@@ -116,7 +126,7 @@ pub trait Clock {
116
126
fn elapsed ( & self ) -> Duration ;
117
127
}
118
128
119
- impl < C : Clock > ScorerUsingClock < C > {
129
+ impl < C : Clock + Sub < Duration , Output = C > > ScorerUsingClock < C > {
120
130
/// Creates a new scorer using the given scoring parameters.
121
131
pub fn new ( params : ScoringParameters ) -> Self {
122
132
Self {
@@ -136,7 +146,7 @@ impl<C: Clock> ScorerUsingClock<C> {
136
146
}
137
147
}
138
148
139
- impl < C : Clock > ChannelFailure < C > {
149
+ impl < C : Clock + Sub < Duration , Output = C > > ChannelFailure < C > {
140
150
fn new ( failure_penalty_msat : u64 ) -> Self {
141
151
Self {
142
152
undecayed_penalty_msat : failure_penalty_msat,
@@ -158,7 +168,7 @@ impl<C: Clock> ChannelFailure<C> {
158
168
}
159
169
}
160
170
161
- impl < C : Clock > Default for ScorerUsingClock < C > {
171
+ impl < C : Clock + Sub < Duration , Output = C > > Default for ScorerUsingClock < C > {
162
172
fn default ( ) -> Self {
163
173
Self :: new ( ScoringParameters :: default ( ) )
164
174
}
@@ -174,7 +184,7 @@ impl Default for ScoringParameters {
174
184
}
175
185
}
176
186
177
- impl < C : Clock > routing:: Score for ScorerUsingClock < C > {
187
+ impl < C : Clock + Sub < Duration , Output = C > > routing:: Score for ScorerUsingClock < C > {
178
188
fn channel_penalty_msat (
179
189
& self , short_channel_id : u64 , _source : & NodeId , _target : & NodeId
180
190
) -> u64 {
@@ -218,3 +228,57 @@ impl Clock for AlwaysPresent {
218
228
Duration :: from_secs ( 0 )
219
229
}
220
230
}
231
+
232
+ impl Sub < Duration > for AlwaysPresent {
233
+ type Output = Self ;
234
+
235
+ fn sub ( self , _other : Duration ) -> Self {
236
+ self
237
+ }
238
+ }
239
+
240
+ impl < C : Clock + Sub < Duration , Output = C > > Writeable for ScorerUsingClock < C > {
241
+ #[ inline]
242
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
243
+ self . params . write ( w) ?;
244
+ self . channel_failures . write ( w)
245
+ }
246
+ }
247
+
248
+ impl < C : Clock + Sub < Duration , Output = C > > Readable for ScorerUsingClock < C > {
249
+ #[ inline]
250
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
251
+ Ok ( Self {
252
+ params : Readable :: read ( r) ?,
253
+ channel_failures : Readable :: read ( r) ?,
254
+ } )
255
+ }
256
+ }
257
+
258
+ impl < C : Clock + Sub < Duration , Output = C > > Writeable for ChannelFailure < C > {
259
+ #[ inline]
260
+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
261
+ self . undecayed_penalty_msat . write ( w) ?;
262
+ ( duration_since_epoch ( ) - self . last_failed . elapsed ( ) ) . write ( w)
263
+ }
264
+ }
265
+
266
+ impl < C : Clock + Sub < Duration , Output = C > > Readable for ChannelFailure < C > {
267
+ #[ inline]
268
+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
269
+ Ok ( Self {
270
+ undecayed_penalty_msat : Readable :: read ( r) ?,
271
+ last_failed : C :: now ( ) - ( duration_since_epoch ( ) - Readable :: read ( r) ?) ,
272
+ } )
273
+ }
274
+ }
275
+
276
+ fn duration_since_epoch ( ) -> Duration {
277
+ #[ cfg( not( feature = "no-std" ) ) ]
278
+ {
279
+ use std:: time:: SystemTime ;
280
+ SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
281
+ }
282
+ #[ cfg( feature = "no-std" ) ]
283
+ return Duration :: from_secs ( 0 ) ;
284
+ }
0 commit comments