@@ -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,20 +242,35 @@ 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
///
233
- pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) {
273
+ pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) -> bool {
234
274
let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
235
275
let matched_txn: Vec < _ > = txdata. iter ( ) . filter ( |& & ( _, tx) | watch_events. watched . does_match_tx ( tx) ) . map ( |e| * e) . collect ( ) ;
236
276
{
@@ -245,6 +285,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
245
285
}
246
286
}
247
287
}
288
+ watch_events. flush_events ( & self . chain_source )
248
289
}
249
290
250
291
///
@@ -256,17 +297,19 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
256
297
}
257
298
}
258
299
259
- impl < Key : Send + cmp:: Eq + hash:: Hash + ' static , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > ChainMonitor < Key , ChanSigner , T , F , L >
260
- where T :: Target : BroadcasterInterface ,
300
+ 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 >
301
+ where C :: Target : chain:: Notify ,
302
+ T :: Target : BroadcasterInterface ,
261
303
F :: Target : FeeEstimator ,
262
304
L :: Target : Logger ,
263
305
{
264
306
/// Creates a new object which can be used to monitor several channels given the chain
265
307
/// interface with which to register to receive notifications.
266
- pub fn new ( broadcaster : T , logger : L , feeest : F ) -> ChainMonitor < Key , ChanSigner , T , F , L > {
308
+ pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> ChainMonitor < Key , ChanSigner , C , T , F , L > {
267
309
Self {
268
310
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
269
- watch_events : Mutex :: new ( WatchEventQueue :: new ( ) ) ,
311
+ watch_events : Mutex :: new ( WatchEventCache :: new ( ) ) ,
312
+ chain_source,
270
313
broadcaster,
271
314
logger,
272
315
fee_estimator : feeest,
@@ -293,6 +336,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
293
336
}
294
337
}
295
338
entry. insert ( monitor) ;
339
+ watch_events. flush_events ( & self . chain_source ) ;
296
340
Ok ( ( ) )
297
341
}
298
342
@@ -309,8 +353,9 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
309
353
}
310
354
}
311
355
312
- impl < ChanSigner : ChannelKeys , T : Deref + Sync + Send , F : Deref + Sync + Send , L : Deref + Sync + Send > chain:: Watch for ChainMonitor < OutPoint , ChanSigner , T , F , L >
313
- where T :: Target : BroadcasterInterface ,
356
+ 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 >
357
+ where C :: Target : chain:: Notify ,
358
+ T :: Target : BroadcasterInterface ,
314
359
F :: Target : FeeEstimator ,
315
360
L :: Target : Logger ,
316
361
{
@@ -339,8 +384,9 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
339
384
}
340
385
}
341
386
342
- impl < Key : Send + cmp:: Eq + hash:: Hash , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > events:: EventsProvider for ChainMonitor < Key , ChanSigner , T , F , L >
343
- where T :: Target : BroadcasterInterface ,
387
+ 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 >
388
+ where C :: Target : chain:: Notify ,
389
+ T :: Target : BroadcasterInterface ,
344
390
F :: Target : FeeEstimator ,
345
391
L :: Target : Logger ,
346
392
{
@@ -353,16 +399,6 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
353
399
}
354
400
}
355
401
356
- impl < Key : Send + cmp:: Eq + hash:: Hash , ChanSigner : ChannelKeys , T : Deref , F : Deref , L : Deref > chain:: WatchEventProvider for ChainMonitor < Key , 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
402
/// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
367
403
/// instead claiming it in its own individual transaction.
368
404
pub ( crate ) const CLTV_SHARED_CLAIM_BUFFER : u32 = 12 ;
0 commit comments