@@ -44,6 +44,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, Loca
44
44
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
45
45
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
46
46
use chain;
47
+ use chain:: Notify ;
47
48
use chain:: chaininterface:: { ChainWatchedUtil , BroadcasterInterface , FeeEstimator } ;
48
49
use chain:: transaction:: OutPoint ;
49
50
use chain:: keysinterface:: { SpendableOutputDescriptor , ChannelKeys } ;
@@ -181,26 +182,49 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
181
182
/// independently to monitor channels remotely.
182
183
///
183
184
/// [`chain::Watch`]: ../../chain/trait.Watch.html
184
- /// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html
185
- pub struct ChainMonitor < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref >
186
- where T :: Target : BroadcasterInterface ,
185
+ pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref >
186
+ where C :: Target : chain :: Notify ,
187
+ T :: Target : BroadcasterInterface ,
187
188
F :: Target : FeeEstimator ,
188
189
L :: Target : Logger ,
189
190
{
190
191
/// The monitors
191
192
pub monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
192
- watch_events : Mutex < WatchEventQueue > ,
193
+ watch_events : Mutex < WatchEventCache > ,
194
+ chain_source : Option < C > ,
193
195
broadcaster : T ,
194
196
logger : L ,
195
197
fee_estimator : F
196
198
}
197
199
198
- struct WatchEventQueue {
200
+ struct WatchEventCache {
199
201
watched : ChainWatchedUtil ,
200
- events : Vec < chain :: WatchEvent > ,
202
+ events : Vec < WatchEvent > ,
201
203
}
202
204
203
- impl WatchEventQueue {
205
+ /// An event indicating on-chain activity to watch for pertaining to a channel.
206
+ enum WatchEvent {
207
+ /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
208
+ /// condition.
209
+ WatchTransaction {
210
+ /// Identifier of the transaction.
211
+ txid : Txid ,
212
+
213
+ /// Spending condition for an output of the transaction.
214
+ script_pubkey : Script ,
215
+ } ,
216
+ /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
217
+ /// the spending condition.
218
+ WatchOutput {
219
+ /// Identifier for the output.
220
+ outpoint : OutPoint ,
221
+
222
+ /// Spending condition for the output.
223
+ script_pubkey : Script ,
224
+ }
225
+ }
226
+
227
+ impl WatchEventCache {
204
228
fn new ( ) -> Self {
205
229
Self {
206
230
watched : ChainWatchedUtil :: new ( ) ,
@@ -210,7 +234,7 @@ impl WatchEventQueue {
210
234
211
235
fn watch_tx ( & mut self , txid : & Txid , script_pubkey : & Script ) {
212
236
if self . watched . register_tx ( txid, script_pubkey) {
213
- self . events . push ( chain :: WatchEvent :: WatchTransaction {
237
+ self . events . push ( WatchEvent :: WatchTransaction {
214
238
txid : * txid,
215
239
script_pubkey : script_pubkey. clone ( )
216
240
} ) ;
@@ -220,7 +244,7 @@ impl WatchEventQueue {
220
244
fn watch_output ( & mut self , outpoint : ( & Txid , usize ) , script_pubkey : & Script ) {
221
245
let ( txid, index) = outpoint;
222
246
if self . watched . register_outpoint ( ( * txid, index as u32 ) , script_pubkey) {
223
- self . events . push ( chain :: WatchEvent :: WatchOutput {
247
+ self . events . push ( WatchEvent :: WatchOutput {
224
248
outpoint : OutPoint {
225
249
txid : * txid,
226
250
index : index as u16 ,
@@ -230,15 +254,30 @@ impl WatchEventQueue {
230
254
}
231
255
}
232
256
233
- fn dequeue_events ( & mut self ) -> Vec < chain:: WatchEvent > {
234
- let mut pending_events = Vec :: with_capacity ( self . events . len ( ) ) ;
235
- pending_events. append ( & mut self . events ) ;
236
- pending_events
257
+ fn flush_events < C : Deref > ( & mut self , chain_source : & Option < C > ) -> bool where C :: Target : chain:: Notify {
258
+ let num_events = self . events . len ( ) ;
259
+ match chain_source {
260
+ & None => self . events . clear ( ) ,
261
+ & Some ( ref chain_source) => {
262
+ for event in self . events . drain ( ..) {
263
+ match event {
264
+ WatchEvent :: WatchTransaction { txid, script_pubkey } => {
265
+ chain_source. register_tx ( txid, script_pubkey)
266
+ } ,
267
+ WatchEvent :: WatchOutput { outpoint, script_pubkey } => {
268
+ chain_source. register_output ( outpoint, script_pubkey)
269
+ } ,
270
+ }
271
+ }
272
+ }
273
+ }
274
+ num_events > 0
237
275
}
238
276
}
239
277
240
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , T , F , L >
241
- where T :: Target : BroadcasterInterface ,
278
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
279
+ where C :: Target : chain:: Notify ,
280
+ T :: Target : BroadcasterInterface ,
242
281
F :: Target : FeeEstimator ,
243
282
L :: Target : Logger ,
244
283
{
@@ -247,9 +286,13 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, Ch
247
286
/// [`ChannelMonitor::block_connected`] for details. Any HTLCs that were resolved on chain will
248
287
/// be retuned by [`chain::Watch::release_pending_htlc_updates`].
249
288
///
289
+ /// Calls back to [`chain::Notify`] if any monitor indicated new outputs to watch, returning
290
+ /// `true` if so.
291
+ ///
250
292
/// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
251
293
/// [`chain::Watch::release_pending_htlc_updates`]: ../../chain/trait.Watch.html#tymethod.release_pending_htlc_updates
252
- pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) {
294
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
295
+ pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) -> bool {
253
296
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
254
297
let matched_txn: Vec < _ > = txdata. iter ( ) . filter ( |& & ( _, tx) | watch_events. watched . does_match_tx ( tx) ) . map ( |e| * e) . collect ( ) ;
255
298
{
@@ -264,6 +307,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, Ch
264
307
}
265
308
}
266
309
}
310
+ watch_events. flush_events ( & self . chain_source )
267
311
}
268
312
269
313
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
@@ -279,24 +323,30 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, Ch
279
323
}
280
324
}
281
325
282
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , T , F , L >
283
- where T :: Target : BroadcasterInterface ,
326
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
327
+ where C :: Target : chain:: Notify ,
328
+ T :: Target : BroadcasterInterface ,
284
329
F :: Target : FeeEstimator ,
285
330
L :: Target : Logger ,
286
331
{
287
332
/// Creates a new object which can be used to monitor several channels given the chain
288
333
/// interface with which to register to receive notifications.
289
- pub fn new ( broadcaster : T , logger : L , feeest : F ) -> Self {
334
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> Self {
290
335
Self {
291
336
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
292
- watch_events : Mutex :: new ( WatchEventQueue :: new ( ) ) ,
337
+ watch_events : Mutex :: new ( WatchEventCache :: new ( ) ) ,
338
+ chain_source,
293
339
broadcaster,
294
340
logger,
295
341
fee_estimator : feeest,
296
342
}
297
343
}
298
344
299
345
/// Adds or updates the monitor which monitors the channel referred to by the given outpoint.
346
+ ///
347
+ /// Calls back to [`chain::Notify`] with the funding transaction and outputs to watch.
348
+ ///
349
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
300
350
pub fn add_monitor ( & self , outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
301
351
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
302
352
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
@@ -316,6 +366,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
316
366
}
317
367
}
318
368
entry. insert ( monitor) ;
369
+ watch_events. flush_events ( & self . chain_source ) ;
319
370
Ok ( ( ) )
320
371
}
321
372
@@ -332,8 +383,9 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
332
383
}
333
384
}
334
385
335
- impl < ChanSigner : ChannelKeys , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , T , F , L >
336
- where T :: Target : BroadcasterInterface ,
386
+ impl < ChanSigner : ChannelKeys , C : Deref + Sync + Send , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , C , T , F , L >
387
+ where C :: Target : chain:: Notify ,
388
+ T :: Target : BroadcasterInterface ,
337
389
F :: Target : FeeEstimator ,
338
390
L :: Target : Logger ,
339
391
{
@@ -362,8 +414,9 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
362
414
}
363
415
}
364
416
365
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , T , F , L >
366
- where T :: Target : BroadcasterInterface ,
417
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L >
418
+ where C :: Target : chain:: Notify ,
419
+ T :: Target : BroadcasterInterface ,
367
420
F :: Target : FeeEstimator ,
368
421
L :: Target : Logger ,
369
422
{
@@ -376,16 +429,6 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvid
376
429
}
377
430
}
378
431
379
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > chain:: WatchEventProvider for ChainMonitor < ChanSigner , T , F , L >
380
- where T :: Target : BroadcasterInterface ,
381
- F :: Target : FeeEstimator ,
382
- L :: Target : Logger ,
383
- {
384
- fn release_pending_watch_events ( & self ) -> Vec < chain:: WatchEvent > {
385
- self . watch_events . lock ( ) . unwrap ( ) . dequeue_events ( )
386
- }
387
- }
388
-
389
432
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
390
433
/// instead claiming it in its own individual transaction.
391
434
pub ( crate ) const CLTV_SHARED_CLAIM_BUFFER : u32 = 12 ;
0 commit comments