@@ -36,7 +36,7 @@ use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
36
36
use ln:: onchaintx:: { OnchainTxHandler , InputDescriptors } ;
37
37
use chain;
38
38
use chain:: Notify ;
39
- use chain:: chaininterface:: { ChainWatchedUtil , BroadcasterInterface , FeeEstimator } ;
39
+ use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
40
40
use chain:: transaction:: OutPoint ;
41
41
use chain:: keysinterface:: { SpendableOutputDescriptor , ChannelKeys } ;
42
42
use util:: logger:: Logger ;
@@ -171,91 +171,12 @@ pub struct ChainMonitor<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L
171
171
pub monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
172
172
#[ cfg( not( test) ) ]
173
173
monitors : Mutex < HashMap < OutPoint , ChannelMonitor < ChanSigner > > > ,
174
- watch_events : Mutex < WatchEventCache > ,
175
174
chain_source : Option < C > ,
176
175
broadcaster : T ,
177
176
logger : L ,
178
177
fee_estimator : F
179
178
}
180
179
181
- struct WatchEventCache {
182
- watched : ChainWatchedUtil ,
183
- events : Vec < WatchEvent > ,
184
- }
185
-
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 {
209
- fn new ( ) -> Self {
210
- Self {
211
- watched : ChainWatchedUtil :: new ( ) ,
212
- events : Vec :: new ( ) ,
213
- }
214
- }
215
-
216
- fn watch_tx ( & mut self , txid : & Txid , script_pubkey : & Script ) {
217
- if self . watched . register_tx ( txid, script_pubkey) {
218
- self . events . push ( WatchEvent :: WatchTransaction {
219
- txid : * txid,
220
- script_pubkey : script_pubkey. clone ( )
221
- } ) ;
222
- }
223
- }
224
-
225
- fn watch_output ( & mut self , outpoint : ( & Txid , usize ) , script_pubkey : & Script ) {
226
- let ( txid, index) = outpoint;
227
- if self . watched . register_outpoint ( ( * txid, index as u32 ) , script_pubkey) {
228
- self . events . push ( WatchEvent :: WatchOutput {
229
- outpoint : OutPoint {
230
- txid : * txid,
231
- index : index as u16 ,
232
- } ,
233
- script_pubkey : script_pubkey. clone ( ) ,
234
- } ) ;
235
- }
236
- }
237
-
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
256
- }
257
- }
258
-
259
180
impl < ChanSigner : ChannelKeys , C : Deref , T : Deref , F : Deref , L : Deref > ChainMonitor < ChanSigner , C , T , F , L >
260
181
where C :: Target : chain:: Notify ,
261
182
T :: Target : BroadcasterInterface ,
@@ -272,21 +193,23 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
272
193
/// [`chain::Watch::release_pending_htlc_updates`]: ../../chain/trait.Watch.html#tymethod.release_pending_htlc_updates
273
194
/// [`chain::Notify`]: ../../chain/trait.Notify.html
274
195
pub fn block_connected ( & self , header : & BlockHeader , txdata : & [ ( usize , & Transaction ) ] , height : u32 ) -> bool {
275
- let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
276
- let matched_txn: Vec < _ > = txdata. iter ( ) . filter ( |& & ( _, tx) | watch_events. watched . does_match_tx ( tx) ) . map ( |e| * e) . collect ( ) ;
196
+ let mut new_outputs = false ;
277
197
{
278
198
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
279
199
for monitor in monitors. values_mut ( ) {
280
- let txn_outputs = monitor. block_connected ( header, & matched_txn, height, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
200
+ let mut txn_outputs = monitor. block_connected ( header, txdata, height, & * self . broadcaster , & * self . fee_estimator , & * self . logger ) ;
201
+ new_outputs |= !txn_outputs. is_empty ( ) ;
281
202
282
- for ( ref txid, ref outputs) in txn_outputs {
283
- for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
284
- watch_events. watch_output ( ( txid, idx) , & output. script_pubkey ) ;
203
+ if let Some ( ref chain_source) = self . chain_source {
204
+ for ( txid, outputs) in txn_outputs. drain ( ..) {
205
+ for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
206
+ chain_source. register_output ( & OutPoint { txid, index : idx as u16 } , & output. script_pubkey ) ;
207
+ }
285
208
}
286
- }
209
+ } ;
287
210
}
288
211
}
289
- watch_events . flush_events ( & self . chain_source )
212
+ new_outputs
290
213
}
291
214
292
215
/// Delegates to [`ChannelMonitor::block_disconnected`] for each watched channel.
@@ -311,7 +234,6 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
311
234
pub fn new ( chain_source : Option < C > , broadcaster : T , logger : L , feeest : F ) -> Self {
312
235
Self {
313
236
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
314
- watch_events : Mutex :: new ( WatchEventCache :: new ( ) ) ,
315
237
chain_source,
316
238
broadcaster,
317
239
logger,
@@ -325,7 +247,6 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
325
247
///
326
248
/// [`chain::Notify`]: ../../chain/trait.Notify.html
327
249
pub fn add_monitor ( & self , outpoint : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , MonitorUpdateError > {
328
- let mut watch_events = self . watch_events . lock ( ) . unwrap ( ) ;
329
250
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
330
251
let entry = match monitors. entry ( outpoint) {
331
252
hash_map:: Entry :: Occupied ( _) => return Err ( MonitorUpdateError ( "Channel monitor for given outpoint is already present" ) ) ,
@@ -334,16 +255,17 @@ impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonit
334
255
{
335
256
let funding_txo = monitor. get_funding_txo ( ) ;
336
257
log_trace ! ( self . logger, "Got new Channel Monitor for channel {}" , log_bytes!( funding_txo. 0 . to_channel_id( ) [ ..] ) ) ;
337
- watch_events. watch_tx ( & funding_txo. 0 . txid , & funding_txo. 1 ) ;
338
- watch_events. watch_output ( ( & funding_txo. 0 . txid , funding_txo. 0 . index as usize ) , & funding_txo. 1 ) ;
339
- for ( txid, outputs) in monitor. get_outputs_to_watch ( ) . iter ( ) {
340
- for ( idx, script) in outputs. iter ( ) . enumerate ( ) {
341
- watch_events. watch_output ( ( txid, idx) , script) ;
258
+
259
+ if let Some ( ref chain_source) = self . chain_source {
260
+ chain_source. register_tx ( & funding_txo. 0 . txid , & funding_txo. 1 ) ;
261
+ for ( txid, outputs) in monitor. get_outputs_to_watch ( ) . iter ( ) {
262
+ for ( idx, script_pubkey) in outputs. iter ( ) . enumerate ( ) {
263
+ chain_source. register_output ( & OutPoint { txid : * txid, index : idx as u16 } , & script_pubkey) ;
264
+ }
342
265
}
343
- }
266
+ } ;
344
267
}
345
268
entry. insert ( monitor) ;
346
- watch_events. flush_events ( & self . chain_source ) ;
347
269
Ok ( ( ) )
348
270
}
349
271
@@ -2000,12 +1922,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2000
1922
}
2001
1923
}
2002
1924
self . onchain_tx_handler . block_connected ( txn_matched, claimable_outpoints, height, & * broadcaster, & * fee_estimator, & * logger) ;
2003
-
2004
1925
self . last_block_hash = block_hash;
2005
- for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2006
- self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2007
- }
2008
1926
1927
+ watch_outputs. retain ( |& ( ref txid, ref txouts) | {
1928
+ let output_scripts = txouts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ;
1929
+ self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts) . is_none ( )
1930
+ } ) ;
2009
1931
watch_outputs
2010
1932
}
2011
1933
0 commit comments