@@ -38,7 +38,7 @@ use util::events::EventHandler;
38
38
use ln:: channelmanager:: ChannelDetails ;
39
39
40
40
use prelude:: * ;
41
- use sync:: RwLock ;
41
+ use sync:: { RwLock , RwLockReadGuard } ;
42
42
use core:: ops:: Deref ;
43
43
44
44
/// `Persist` defines behavior for persisting channel monitors: this could mean
@@ -92,6 +92,26 @@ pub trait Persist<ChannelSigner: Sign> {
92
92
fn update_persisted_channel ( & self , id : OutPoint , update : & ChannelMonitorUpdate , data : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > ;
93
93
}
94
94
95
+ struct MonitorHolder < ChannelSigner : Sign > {
96
+ monitor : ChannelMonitor < ChannelSigner > ,
97
+ }
98
+
99
+ /// A read-only reference to a current ChannelMonitor.
100
+ ///
101
+ /// Note that this holds a mutex in [`ChainMonitor`] and may block other events until it is
102
+ /// released.
103
+ pub struct LockedChannelMonitor < ' a , ChannelSigner : Sign > {
104
+ lock : RwLockReadGuard < ' a , HashMap < OutPoint , MonitorHolder < ChannelSigner > > > ,
105
+ funding_txo : OutPoint ,
106
+ }
107
+
108
+ impl < ChannelSigner : Sign > Deref for LockedChannelMonitor < ' _ , ChannelSigner > {
109
+ type Target = ChannelMonitor < ChannelSigner > ;
110
+ fn deref ( & self ) -> & ChannelMonitor < ChannelSigner > {
111
+ & self . lock . get ( & self . funding_txo ) . expect ( "Checked at construction" ) . monitor
112
+ }
113
+ }
114
+
95
115
/// An implementation of [`chain::Watch`] for monitoring channels.
96
116
///
97
117
/// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
@@ -108,8 +128,7 @@ pub struct ChainMonitor<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: De
108
128
L :: Target : Logger ,
109
129
P :: Target : Persist < ChannelSigner > ,
110
130
{
111
- /// The monitors
112
- pub monitors : RwLock < HashMap < OutPoint , ChannelMonitor < ChannelSigner > > > ,
131
+ monitors : RwLock < HashMap < OutPoint , MonitorHolder < ChannelSigner > > > ,
113
132
chain_source : Option < C > ,
114
133
broadcaster : T ,
115
134
logger : L ,
@@ -139,8 +158,8 @@ where C::Target: chain::Filter,
139
158
{
140
159
let mut dependent_txdata = Vec :: new ( ) ;
141
160
let monitors = self . monitors . read ( ) . unwrap ( ) ;
142
- for monitor in monitors. values ( ) {
143
- let mut txn_outputs = process ( monitor, txdata) ;
161
+ for monitor_state in monitors. values ( ) {
162
+ let mut txn_outputs = process ( & monitor_state . monitor , txdata) ;
144
163
145
164
// Register any new outputs with the chain source for filtering, storing any dependent
146
165
// transactions from within the block that previously had not been included in txdata.
@@ -203,19 +222,41 @@ where C::Target: chain::Filter,
203
222
pub fn get_claimable_balances ( & self , ignored_channels : & [ & ChannelDetails ] ) -> Vec < Balance > {
204
223
let mut ret = Vec :: new ( ) ;
205
224
let monitors = self . monitors . read ( ) . unwrap ( ) ;
206
- for ( _, monitor ) in monitors. iter ( ) . filter ( |( funding_outpoint, _) | {
225
+ for ( _, monitor_state ) in monitors. iter ( ) . filter ( |( funding_outpoint, _) | {
207
226
for chan in ignored_channels {
208
227
if chan. funding_txo . as_ref ( ) == Some ( funding_outpoint) {
209
228
return false ;
210
229
}
211
230
}
212
231
true
213
232
} ) {
214
- ret. append ( & mut monitor. get_claimable_balances ( ) ) ;
233
+ ret. append ( & mut monitor_state . monitor . get_claimable_balances ( ) ) ;
215
234
}
216
235
ret
217
236
}
218
237
238
+ /// Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
239
+ /// such [`ChannelMonitor`] is currently being monitored for.
240
+ pub fn get_current_monitor ( & self , funding_txo : OutPoint ) -> Result < LockedChannelMonitor < ' _ , ChannelSigner > , ( ) > {
241
+ let lock = self . monitors . read ( ) . unwrap ( ) ;
242
+ if lock. get ( & funding_txo) . is_some ( ) {
243
+ Ok ( LockedChannelMonitor { lock, funding_txo } )
244
+ } else {
245
+ Err ( ( ) )
246
+ }
247
+ }
248
+
249
+ /// Gets the current list of [`ChannelMonitor`]s being monitored for, by their funding
250
+ /// outpoint.
251
+ pub fn list_current_monitors ( & self ) -> Vec < OutPoint > {
252
+ self . monitors . read ( ) . unwrap ( ) . keys ( ) . map ( |outpoint| * outpoint) . collect ( )
253
+ }
254
+
255
+ #[ cfg( test) ]
256
+ pub fn remove_monitor ( & self , funding_txo : & OutPoint ) -> ChannelMonitor < ChannelSigner > {
257
+ self . monitors . write ( ) . unwrap ( ) . remove ( funding_txo) . unwrap ( ) . monitor
258
+ }
259
+
219
260
#[ cfg( any( test, feature = "fuzztarget" , feature = "_test_utils" ) ) ]
220
261
pub fn get_and_clear_pending_events ( & self ) -> Vec < events:: Event > {
221
262
use util:: events:: EventsProvider ;
@@ -248,8 +289,8 @@ where
248
289
fn block_disconnected ( & self , header : & BlockHeader , height : u32 ) {
249
290
let monitors = self . monitors . read ( ) . unwrap ( ) ;
250
291
log_debug ! ( self . logger, "Latest block {} at height {} removed via block_disconnected" , header. block_hash( ) , height) ;
251
- for monitor in monitors. values ( ) {
252
- monitor. block_disconnected (
292
+ for monitor_state in monitors. values ( ) {
293
+ monitor_state . monitor . block_disconnected (
253
294
header, height, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
254
295
}
255
296
}
@@ -275,8 +316,8 @@ where
275
316
fn transaction_unconfirmed ( & self , txid : & Txid ) {
276
317
log_debug ! ( self . logger, "Transaction {} reorganized out of chain" , txid) ;
277
318
let monitors = self . monitors . read ( ) . unwrap ( ) ;
278
- for monitor in monitors. values ( ) {
279
- monitor. transaction_unconfirmed ( txid, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
319
+ for monitor_state in monitors. values ( ) {
320
+ monitor_state . monitor . transaction_unconfirmed ( txid, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
280
321
}
281
322
}
282
323
@@ -294,8 +335,8 @@ where
294
335
fn get_relevant_txids ( & self ) -> Vec < Txid > {
295
336
let mut txids = Vec :: new ( ) ;
296
337
let monitors = self . monitors . read ( ) . unwrap ( ) ;
297
- for monitor in monitors. values ( ) {
298
- txids. append ( & mut monitor. get_relevant_txids ( ) ) ;
338
+ for monitor_state in monitors. values ( ) {
339
+ txids. append ( & mut monitor_state . monitor . get_relevant_txids ( ) ) ;
299
340
}
300
341
301
342
txids. sort_unstable ( ) ;
@@ -338,7 +379,7 @@ where C::Target: chain::Filter,
338
379
monitor. load_outputs_to_watch ( chain_source) ;
339
380
}
340
381
}
341
- entry. insert ( monitor) ;
382
+ entry. insert ( MonitorHolder { monitor } ) ;
342
383
Ok ( ( ) )
343
384
}
344
385
@@ -359,7 +400,8 @@ where C::Target: chain::Filter,
359
400
#[ cfg( not( any( test, feature = "fuzztarget" ) ) ) ]
360
401
Err ( ChannelMonitorUpdateErr :: PermanentFailure )
361
402
} ,
362
- Some ( monitor) => {
403
+ Some ( monitor_state) => {
404
+ let monitor = & monitor_state. monitor ;
363
405
log_trace ! ( self . logger, "Updating Channel Monitor for channel {}" , log_funding_info!( monitor) ) ;
364
406
let update_res = monitor. update_monitor ( & update, & self . broadcaster , & self . fee_estimator , & self . logger ) ;
365
407
if let Err ( e) = & update_res {
@@ -382,8 +424,8 @@ where C::Target: chain::Filter,
382
424
383
425
fn release_pending_monitor_events ( & self ) -> Vec < MonitorEvent > {
384
426
let mut pending_monitor_events = Vec :: new ( ) ;
385
- for monitor in self . monitors . read ( ) . unwrap ( ) . values ( ) {
386
- pending_monitor_events. append ( & mut monitor. get_and_clear_pending_monitor_events ( ) ) ;
427
+ for monitor_state in self . monitors . read ( ) . unwrap ( ) . values ( ) {
428
+ pending_monitor_events. append ( & mut monitor_state . monitor . get_and_clear_pending_monitor_events ( ) ) ;
387
429
}
388
430
pending_monitor_events
389
431
}
@@ -404,8 +446,8 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
404
446
/// [`SpendableOutputs`]: events::Event::SpendableOutputs
405
447
fn process_pending_events < H : Deref > ( & self , handler : H ) where H :: Target : EventHandler {
406
448
let mut pending_events = Vec :: new ( ) ;
407
- for monitor in self . monitors . read ( ) . unwrap ( ) . values ( ) {
408
- pending_events. append ( & mut monitor. get_and_clear_pending_events ( ) ) ;
449
+ for monitor_state in self . monitors . read ( ) . unwrap ( ) . values ( ) {
450
+ pending_events. append ( & mut monitor_state . monitor . get_and_clear_pending_events ( ) ) ;
409
451
}
410
452
for event in pending_events. drain ( ..) {
411
453
handler. handle_event ( & event) ;
0 commit comments