@@ -35,6 +35,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, Loca
35
35
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
36
36
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
37
37
use chain;
38
+ use chain:: Notify ;
38
39
use chain:: chaininterface:: { ChainWatchedUtil , BroadcasterInterface , FeeEstimator } ;
39
40
use chain:: transaction:: OutPoint ;
40
41
use chain:: keysinterface:: { SpendableOutputDescriptor , ChannelKeys } ;
@@ -167,27 +168,51 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
167
168
/// `OutPoint` as the key, which will give you a [`chain::Watch`] implementation.
168
169
///
169
170
/// [`chain::Watch`]: ../../chain/trait.Watch.html
170
- pub struct ChainMonitor < Key , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref >
171
- where T :: Target : BroadcasterInterface ,
171
+ pub struct ChainMonitor < Key , ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref >
172
+ where C :: Target : chain:: Notify ,
173
+ T :: Target : BroadcasterInterface ,
172
174
F :: Target : FeeEstimator ,
173
175
L :: Target : Logger ,
174
176
{
175
177
#[ cfg( test) ] // Used in ChannelManager tests to manipulate channels directly
176
178
pub monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
177
179
#[ cfg( not( test) ) ]
178
180
monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
179
- watch_events : Mutex < WatchEventQueue > ,
181
+ watch_events : Mutex < WatchEventCache > ,
182
+ chain_source : Option < C > ,
180
183
broadcaster : T ,
181
184
logger : L ,
182
185
fee_estimator : F
183
186
}
184
187
185
- struct WatchEventQueue {
188
+ struct WatchEventCache {
186
189
watched : ChainWatchedUtil ,
187
- events : Vec < chain :: WatchEvent > ,
190
+ events : Vec < WatchEvent > ,
188
191
}
189
192
190
- impl WatchEventQueue {
193
+ /// An event indicating on-chain activity to watch for pertaining to a channel.
194
+ enum WatchEvent {
195
+ /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
196
+ /// condition.
197
+ WatchTransaction {
198
+ /// Identifier of the transaction.
199
+ txid : Txid ,
200
+
201
+ /// Spending condition for an output of the transaction.
202
+ script_pubkey : Script ,
203
+ } ,
204
+ /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
205
+ /// the spending condition.
206
+ WatchOutput {
207
+ /// Identifier for the output.
208
+ outpoint : OutPoint ,
209
+
210
+ /// Spending condition for the output.
211
+ script_pubkey : Script ,
212
+ }
213
+ }
214
+
215
+ impl WatchEventCache {
191
216
fn new ( ) -> Self {
192
217
Self {
193
218
watched : ChainWatchedUtil :: new ( ) ,
@@ -197,7 +222,7 @@ impl WatchEventQueue {
197
222
198
223
fn watch_tx ( & mut self , txid : & Txid , script_pubkey : & Script ) {
199
224
if self . watched . register_tx ( txid, script_pubkey) {
200
- self . events . push ( chain :: WatchEvent :: WatchTransaction {
225
+ self . events . push ( WatchEvent :: WatchTransaction {
201
226
txid : * txid,
202
227
script_pubkey : script_pubkey. clone ( )
203
228
} ) ;
@@ -207,7 +232,7 @@ impl WatchEventQueue {
207
232
fn watch_output ( & mut self , outpoint : ( & Txid , usize ) , script_pubkey : & Script ) {
208
233
let ( txid, index) = outpoint;
209
234
if self . watched . register_outpoint ( ( * txid, index as u32 ) , script_pubkey) {
210
- self . events . push ( chain :: WatchEvent :: WatchOutput {
235
+ self . events . push ( WatchEvent :: WatchOutput {
211
236
outpoint : OutPoint {
212
237
txid : * txid,
213
238
index : index as u16 ,
@@ -217,24 +242,43 @@ impl WatchEventQueue {
217
242
}
218
243
}
219
244
220
- fn dequeue_events ( & mut self ) -> Vec < chain:: WatchEvent > {
221
- let mut pending_events = Vec :: with_capacity ( self . events . len ( ) ) ;
222
- pending_events. append ( & mut self . events ) ;
223
- pending_events
245
+ fn flush_events < C : Deref > ( & mut self , chain_source : & Option < C > ) -> bool where C :: Target : chain:: Notify {
246
+ let num_events = self . events . len ( ) ;
247
+ match chain_source {
248
+ & None => self . events . clear ( ) ,
249
+ & Some ( ref chain_source) => {
250
+ for event in self . events . drain ( ..) {
251
+ match event {
252
+ WatchEvent :: WatchTransaction { txid, script_pubkey } => {
253
+ chain_source. register_tx ( txid, script_pubkey)
254
+ } ,
255
+ WatchEvent :: WatchOutput { outpoint, script_pubkey } => {
256
+ chain_source. register_output ( outpoint, script_pubkey)
257
+ } ,
258
+ }
259
+ }
260
+ }
261
+ }
262
+ num_events > 0
224
263
}
225
264
}
226
265
227
- impl < Key : Send + cmp:: Eq + hash:: Hash + ' static , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , T , F , L >
228
- where T :: Target : BroadcasterInterface ,
266
+ impl < Key : Send + cmp:: Eq + hash:: Hash + ' static , ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , C , T , F , L >
267
+ where C :: Target : chain:: Notify ,
268
+ T :: Target : BroadcasterInterface ,
229
269
F :: Target : FeeEstimator ,
230
270
L :: Target : Logger ,
231
271
{
232
272
/// Delegates to [`ChannelMonitor::block_connected`] for each watched channel. Any HTLCs that
233
273
/// were resolved on chain will be retuned by [`chain::Watch::release_pending_htlc_updates`].
234
274
///
275
+ /// Calls back to [`chain::Notify`] if any monitor indicated new outputs to watch, returning
276
+ /// `true` if so.
277
+ ///
235
278
/// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
236
279
/// [`chain::Watch::release_pending_htlc_updates`]: ../../chain/trait.Watch.html#tymethod.release_pending_htlc_updates
237
- pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) {
280
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
281
+ pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) -> bool {
238
282
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
239
283
let matched_txn: Vec < _ > = txdata. iter ( ) . filter ( |& & ( _, tx) | watch_events. watched . does_match_tx ( tx) ) . map ( |e| * e) . collect ( ) ;
240
284
{
@@ -249,6 +293,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
249
293
}
250
294
}
251
295
}
296
+ watch_events. flush_events ( & self . chain_source )
252
297
}
253
298
254
299
/// Delegates to [`ChannelMonitor::block_disconnected`] for each watched channel.
@@ -262,24 +307,30 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
262
307
}
263
308
}
264
309
265
- impl < Key : Send + cmp:: Eq + hash:: Hash + ' static , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , T , F , L >
266
- where T :: Target : BroadcasterInterface ,
310
+ impl < Key : Send + cmp:: Eq + hash:: Hash + ' static , ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , C , T , F , L >
311
+ where C :: Target : chain:: Notify ,
312
+ T :: Target : BroadcasterInterface ,
267
313
F :: Target : FeeEstimator ,
268
314
L :: Target : Logger ,
269
315
{
270
316
/// Creates a new object which can be used to monitor several channels given the chain
271
317
/// interface with which to register to receive notifications.
272
- pub fn new ( broadcaster : T , logger : L , feeest : F ) -> ChainMonitor < Key , ChanSigner , T , F , L > {
318
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> ChainMonitor < Key , ChanSigner , C , T , F , L > {
273
319
Self {
274
320
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
275
- watch_events : Mutex :: new ( WatchEventQueue :: new ( ) ) ,
321
+ watch_events : Mutex :: new ( WatchEventCache :: new ( ) ) ,
322
+ chain_source,
276
323
broadcaster,
277
324
logger,
278
325
fee_estimator : feeest,
279
326
}
280
327
}
281
328
282
329
/// Adds or updates the monitor which monitors the channel referred to by the given key.
330
+ ///
331
+ /// Calls back to [`chain::Notify`] with the funding transaction and outputs to watch.
332
+ ///
333
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
283
334
pub fn add_monitor_by_key ( & self , key : Key , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
284
335
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
285
336
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
@@ -299,6 +350,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
299
350
}
300
351
}
301
352
entry. insert ( monitor) ;
353
+ watch_events. flush_events ( & self . chain_source ) ;
302
354
Ok ( ( ) )
303
355
}
304
356
@@ -315,8 +367,9 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
315
367
}
316
368
}
317
369
318
- impl < ChanSigner : ChannelKeys , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < OutPoint , ChanSigner , T , F , L >
319
- where T :: Target : BroadcasterInterface ,
370
+ impl < ChanSigner : ChannelKeys , C : Deref + Sync + Send , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < OutPoint , ChanSigner , C , T , F , L >
371
+ where C :: Target : chain:: Notify ,
372
+ T :: Target : BroadcasterInterface ,
320
373
F :: Target : FeeEstimator ,
321
374
L :: Target : Logger ,
322
375
{
@@ -345,8 +398,9 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
345
398
}
346
399
}
347
400
348
- impl < Key : Send + cmp:: Eq + hash:: Hash , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < Key , ChanSigner , T , F , L >
349
- where T :: Target : BroadcasterInterface ,
401
+ impl < Key : Send + cmp:: Eq + hash:: Hash , ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < Key , ChanSigner , C , T , F , L >
402
+ where C :: Target : chain:: Notify ,
403
+ T :: Target : BroadcasterInterface ,
350
404
F :: Target : FeeEstimator ,
351
405
L :: Target : Logger ,
352
406
{
@@ -359,16 +413,6 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
359
413
}
360
414
}
361
415
362
- impl < Key : Send + cmp:: Eq + hash:: Hash , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > chain:: WatchEventProvider for ChainMonitor < Key , ChanSigner , T , F , L >
363
- where T :: Target : BroadcasterInterface ,
364
- F :: Target : FeeEstimator ,
365
- L :: Target : Logger ,
366
- {
367
- fn release_pending_watch_events ( & self ) -> Vec < chain:: WatchEvent > {
368
- self . watch_events . lock ( ) . unwrap ( ) . dequeue_events ( )
369
- }
370
- }
371
-
372
416
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
373
417
/// instead claiming it in its own individual transaction.
374
418
pub ( crate ) const CLTV_SHARED_CLAIM_BUFFER : u32 = 12 ;
0 commit comments