@@ -75,6 +75,16 @@ impl chaininterface::FeeEstimator for TestFeeEstimator {
75
75
76
76
pub struct TestRouter < ' a > {
77
77
pub network_graph : Arc < NetworkGraph < & ' a TestLogger > > ,
78
+ pub next_routes : Mutex < VecDeque < Route > > ,
79
+ }
80
+
81
+ impl < ' a > TestRouter < ' a > {
82
+ pub fn new ( network_graph : Arc < NetworkGraph < & ' a TestLogger > > ) -> Self {
83
+ Self {
84
+ network_graph,
85
+ next_routes : Mutex :: new ( VecDeque :: new ( ) ) ,
86
+ }
87
+ }
78
88
}
79
89
80
90
impl < ' a > TestRouter < ' a > {
@@ -88,6 +98,9 @@ impl<'a> Router for TestRouter<'a> {
88
98
& self , payer : & PublicKey , params : & RouteParameters , first_hops : Option < & [ & channelmanager:: ChannelDetails ] > ,
89
99
inflight_htlcs : & InFlightHtlcs
90
100
) -> Result < Route , msgs:: LightningError > {
101
+ if let Some ( route) = self . next_routes . lock ( ) . unwrap ( ) . pop_front ( ) {
102
+ return Ok ( route)
103
+ }
91
104
let logger = TestLogger :: new ( ) ;
92
105
find_route (
93
106
payer, params, & self . network_graph , first_hops, & logger,
@@ -101,6 +114,15 @@ impl<'a> Router for TestRouter<'a> {
101
114
fn notify_payment_probe_failed ( & self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
102
115
}
103
116
117
+ impl < ' a > Drop for TestRouter < ' a > {
118
+ fn drop ( & mut self ) {
119
+ if std:: thread:: panicking ( ) {
120
+ return ;
121
+ }
122
+ assert ! ( self . next_routes. lock( ) . unwrap( ) . is_empty( ) ) ;
123
+ }
124
+ }
125
+
104
126
pub struct OnlyReadsKeysInterface { }
105
127
106
128
impl EntropySource for OnlyReadsKeysInterface {
@@ -224,10 +246,9 @@ impl<'a> chain::Watch<EnforcingSigner> for TestChainMonitor<'a> {
224
246
}
225
247
226
248
pub struct TestPersister {
227
- pub update_ret : Mutex < chain:: ChannelMonitorUpdateStatus > ,
228
- /// If this is set to Some(), after the next return, we'll always return this until update_ret
229
- /// is changed:
230
- pub next_update_ret : Mutex < Option < chain:: ChannelMonitorUpdateStatus > > ,
249
+ /// The queue of update statuses we'll return. If only one is queued, we'll always return it. If
250
+ /// none are queued, ::Completed will always be returned.
251
+ pub update_rets : Mutex < VecDeque < chain:: ChannelMonitorUpdateStatus > > ,
231
252
/// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the
232
253
/// MonitorUpdateId here.
233
254
pub chain_sync_monitor_persistences : Mutex < HashMap < OutPoint , HashSet < MonitorUpdateId > > > ,
@@ -238,35 +259,39 @@ pub struct TestPersister {
238
259
impl TestPersister {
239
260
pub fn new ( ) -> Self {
240
261
Self {
241
- update_ret : Mutex :: new ( chain:: ChannelMonitorUpdateStatus :: Completed ) ,
242
- next_update_ret : Mutex :: new ( None ) ,
262
+ update_rets : Mutex :: new ( VecDeque :: new ( ) ) ,
243
263
chain_sync_monitor_persistences : Mutex :: new ( HashMap :: new ( ) ) ,
244
264
offchain_monitor_updates : Mutex :: new ( HashMap :: new ( ) ) ,
245
265
}
246
266
}
247
267
268
+ /// Clear the queue of update statuses and set the one we'll always return.
248
269
pub fn set_update_ret ( & self , ret : chain:: ChannelMonitorUpdateStatus ) {
249
- * self . update_ret . lock ( ) . unwrap ( ) = ret;
270
+ let mut update_rets = self . update_rets . lock ( ) . unwrap ( ) ;
271
+ update_rets. clear ( ) ;
272
+ update_rets. push_front ( ret) ;
250
273
}
251
274
252
- pub fn set_next_update_ret ( & self , next_ret : Option < chain:: ChannelMonitorUpdateStatus > ) {
253
- * self . next_update_ret . lock ( ) . unwrap ( ) = next_ret;
275
+ /// Queue an update status to return.
276
+ pub fn set_next_update_ret ( & self , next_ret : chain:: ChannelMonitorUpdateStatus ) {
277
+ self . update_rets . lock ( ) . unwrap ( ) . push_back ( next_ret) ;
254
278
}
255
279
}
256
280
impl < Signer : keysinterface:: Sign > chainmonitor:: Persist < Signer > for TestPersister {
257
281
fn persist_new_channel ( & self , _funding_txo : OutPoint , _data : & channelmonitor:: ChannelMonitor < Signer > , _id : MonitorUpdateId ) -> chain:: ChannelMonitorUpdateStatus {
258
- let ret = self . update_ret . lock ( ) . unwrap ( ) . clone ( ) ;
259
- if let Some ( next_ret) = self . next_update_ret . lock ( ) . unwrap ( ) . take ( ) {
260
- * self . update_ret . lock ( ) . unwrap ( ) = next_ret;
261
- }
262
- ret
282
+ let mut update_rets = self . update_rets . lock ( ) . unwrap ( ) ;
283
+ if update_rets. len ( ) > 1 { return update_rets. pop_front ( ) . unwrap ( ) }
284
+ else if update_rets. len ( ) == 1 { return * update_rets. front ( ) . clone ( ) . unwrap ( ) }
285
+ chain:: ChannelMonitorUpdateStatus :: Completed
263
286
}
264
287
265
288
fn update_persisted_channel ( & self , funding_txo : OutPoint , update : & Option < channelmonitor:: ChannelMonitorUpdate > , _data : & channelmonitor:: ChannelMonitor < Signer > , update_id : MonitorUpdateId ) -> chain:: ChannelMonitorUpdateStatus {
266
- let ret = self . update_ret . lock ( ) . unwrap ( ) . clone ( ) ;
267
- if let Some ( next_ret) = self . next_update_ret . lock ( ) . unwrap ( ) . take ( ) {
268
- * self . update_ret . lock ( ) . unwrap ( ) = next_ret;
269
- }
289
+ let ret = {
290
+ let mut update_rets = self . update_rets . lock ( ) . unwrap ( ) ;
291
+ if update_rets. len ( ) > 1 { update_rets. pop_front ( ) . unwrap ( ) }
292
+ else if update_rets. len ( ) == 1 { * update_rets. front ( ) . clone ( ) . unwrap ( ) }
293
+ else { chain:: ChannelMonitorUpdateStatus :: Completed }
294
+ } ;
270
295
if update. is_none ( ) {
271
296
self . chain_sync_monitor_persistences . lock ( ) . unwrap ( ) . entry ( funding_txo) . or_insert ( HashSet :: new ( ) ) . insert ( update_id) ;
272
297
} else {
0 commit comments