@@ -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 } ;
@@ -160,28 +161,51 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
160
161
/// independently to monitor channels remotely.
161
162
///
162
163
/// [`chain::Watch`]: ../../chain/trait.Watch.html
163
- /// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html
164
- pub struct ChainMonitor < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref >
165
- where T :: Target : BroadcasterInterface ,
164
+ pub struct ChainMonitor < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref >
165
+ where C :: Target : chain :: Notify ,
166
+ T :: Target : BroadcasterInterface ,
166
167
F :: Target : FeeEstimator ,
167
168
L :: Target : Logger ,
168
169
{
169
170
#[ cfg( test) ] // Used in ChannelManager tests to manipulate channels directly
170
171
pub monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
171
172
#[ cfg( not( test) ) ]
172
173
monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
173
- watch_events : Mutex < WatchEventQueue > ,
174
+ watch_events : Mutex < WatchEventCache > ,
175
+ chain_source : Option < C > ,
174
176
broadcaster : T ,
175
177
logger : L ,
176
178
fee_estimator : F
177
179
}
178
180
179
- struct WatchEventQueue {
181
+ struct WatchEventCache {
180
182
watched : ChainWatchedUtil ,
181
- events : Vec < chain :: WatchEvent > ,
183
+ events : Vec < WatchEvent > ,
182
184
}
183
185
184
- impl WatchEventQueue {
186
+ /// An event indicating on-chain activity to watch for pertaining to a channel.
187
+ enum WatchEvent {
188
+ /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
189
+ /// condition.
190
+ WatchTransaction {
191
+ /// Identifier of the transaction.
192
+ txid : Txid ,
193
+
194
+ /// Spending condition for an output of the transaction.
195
+ script_pubkey : Script ,
196
+ } ,
197
+ /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
198
+ /// the spending condition.
199
+ WatchOutput {
200
+ /// Identifier for the output.
201
+ outpoint : OutPoint ,
202
+
203
+ /// Spending condition for the output.
204
+ script_pubkey : Script ,
205
+ }
206
+ }
207
+
208
+ impl WatchEventCache {
185
209
fn new ( ) -> Self {
186
210
Self {
187
211
watched : ChainWatchedUtil :: new ( ) ,
@@ -191,7 +215,7 @@ impl WatchEventQueue {
191
215
192
216
fn watch_tx ( & mut self , txid : & Txid , script_pubkey : & Script ) {
193
217
if self . watched . register_tx ( txid, script_pubkey) {
194
- self . events . push ( chain :: WatchEvent :: WatchTransaction {
218
+ self . events . push ( WatchEvent :: WatchTransaction {
195
219
txid : * txid,
196
220
script_pubkey : script_pubkey. clone ( )
197
221
} ) ;
@@ -201,7 +225,7 @@ impl WatchEventQueue {
201
225
fn watch_output ( & mut self , outpoint : ( & Txid , usize ) , script_pubkey : & Script ) {
202
226
let ( txid, index) = outpoint;
203
227
if self . watched . register_outpoint ( ( * txid, index as u32 ) , script_pubkey) {
204
- self . events . push ( chain :: WatchEvent :: WatchOutput {
228
+ self . events . push ( WatchEvent :: WatchOutput {
205
229
outpoint : OutPoint {
206
230
txid : * txid,
207
231
index : index as u16 ,
@@ -211,24 +235,43 @@ impl WatchEventQueue {
211
235
}
212
236
}
213
237
214
- fn dequeue_events ( & mut self ) -> Vec < chain:: WatchEvent > {
215
- let mut pending_events = Vec :: with_capacity ( self . events . len ( ) ) ;
216
- pending_events. append ( & mut self . events ) ;
217
- pending_events
238
+ fn flush_events < C : Deref > ( & mut self , chain_source : & Option < C > ) -> bool where C :: Target : chain:: Notify {
239
+ let num_events = self . events . len ( ) ;
240
+ match chain_source {
241
+ & None => self . events . clear ( ) ,
242
+ & Some ( ref chain_source) => {
243
+ for event in self . events . drain ( ..) {
244
+ match event {
245
+ WatchEvent :: WatchTransaction { txid, script_pubkey } => {
246
+ chain_source. register_tx ( txid, script_pubkey)
247
+ } ,
248
+ WatchEvent :: WatchOutput { outpoint, script_pubkey } => {
249
+ chain_source. register_output ( outpoint, script_pubkey)
250
+ } ,
251
+ }
252
+ }
253
+ }
254
+ }
255
+ num_events > 0
218
256
}
219
257
}
220
258
221
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , T , F , L >
222
- where T :: Target : BroadcasterInterface ,
259
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
260
+ where C :: Target : chain:: Notify ,
261
+ T :: Target : BroadcasterInterface ,
223
262
F :: Target : FeeEstimator ,
224
263
L :: Target : Logger ,
225
264
{
226
265
/// Delegates to [`ChannelMonitor::block_connected`] for each watched channel. Any HTLCs that
227
266
/// were resolved on chain will be retuned by [`chain::Watch::release_pending_htlc_updates`].
228
267
///
268
+ /// Calls back to [`chain::Notify`] if any monitor indicated new outputs to watch, returning
269
+ /// `true` if so.
270
+ ///
229
271
/// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
230
272
/// [`chain::Watch::release_pending_htlc_updates`]: ../../chain/trait.Watch.html#tymethod.release_pending_htlc_updates
231
- pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) {
273
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
274
+ pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) -> bool {
232
275
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
233
276
let matched_txn: Vec < _ > = txdata. iter ( ) . filter ( |& & ( _, tx) | watch_events. watched . does_match_tx ( tx) ) . map ( |e| * e) . collect ( ) ;
234
277
{
@@ -243,6 +286,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, Ch
243
286
}
244
287
}
245
288
}
289
+ watch_events. flush_events ( & self . chain_source )
246
290
}
247
291
248
292
/// Delegates to [`ChannelMonitor::block_disconnected`] for each watched channel.
@@ -256,24 +300,30 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<Key, Ch
256
300
}
257
301
}
258
302
259
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , T , F , L >
260
- where T :: Target : BroadcasterInterface ,
303
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
304
+ where C :: Target : chain:: Notify ,
305
+ T :: Target : BroadcasterInterface ,
261
306
F :: Target : FeeEstimator ,
262
307
L :: Target : Logger ,
263
308
{
264
309
/// Creates a new object which can be used to monitor several channels given the chain
265
310
/// interface with which to register to receive notifications.
266
- pub fn new ( broadcaster : T , logger : L , feeest : F ) -> Self {
311
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> Self {
267
312
Self {
268
313
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
269
- watch_events : Mutex :: new ( WatchEventQueue :: new ( ) ) ,
314
+ watch_events : Mutex :: new ( WatchEventCache :: new ( ) ) ,
315
+ chain_source,
270
316
broadcaster,
271
317
logger,
272
318
fee_estimator : feeest,
273
319
}
274
320
}
275
321
276
322
/// Adds or updates the monitor which monitors the channel referred to by the given outpoint.
323
+ ///
324
+ /// Calls back to [`chain::Notify`] with the funding transaction and outputs to watch.
325
+ ///
326
+ /// [`chain::Notify`]: ../../chain/trait.Notify.html
277
327
pub fn add_monitor ( & self , outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
278
328
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
279
329
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
@@ -293,6 +343,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
293
343
}
294
344
}
295
345
entry. insert ( monitor) ;
346
+ watch_events. flush_events ( & self . chain_source ) ;
296
347
Ok ( ( ) )
297
348
}
298
349
@@ -309,8 +360,9 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
309
360
}
310
361
}
311
362
312
- impl < ChanSigner : ChannelKeys , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < ChanSigner , T , F , L >
313
- where T :: Target : BroadcasterInterface ,
363
+ 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 >
364
+ where C :: Target : chain:: Notify ,
365
+ T :: Target : BroadcasterInterface ,
314
366
F :: Target : FeeEstimator ,
315
367
L :: Target : Logger ,
316
368
{
@@ -339,8 +391,9 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
339
391
}
340
392
}
341
393
342
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , T , F , L >
343
- where T :: Target : BroadcasterInterface ,
394
+ impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < ChanSigner , C , T , F , L >
395
+ where C :: Target : chain:: Notify ,
396
+ T :: Target : BroadcasterInterface ,
344
397
F :: Target : FeeEstimator ,
345
398
L :: Target : Logger ,
346
399
{
@@ -353,16 +406,6 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvid
353
406
}
354
407
}
355
408
356
- impl < ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > chain:: WatchEventProvider for ChainMonitor < ChanSigner , T , F , L >
357
- where T :: Target : BroadcasterInterface ,
358
- F :: Target : FeeEstimator ,
359
- L :: Target : Logger ,
360
- {
361
- fn release_pending_watch_events ( & self ) -> Vec < chain:: WatchEvent > {
362
- self . watch_events . lock ( ) . unwrap ( ) . dequeue_events ( )
363
- }
364
- }
365
-
366
409
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
367
410
/// instead claiming it in its own individual transaction.
368
411
pub ( crate ) const CLTV_SHARED_CLAIM_BUFFER : u32 = 12 ;
0 commit comments