@@ -37,7 +37,7 @@ use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInter
37
37
use chain:: transaction:: OutPoint ;
38
38
use chain:: keysinterface:: { SpendableOutputDescriptor , ChannelKeys } ;
39
39
use util:: logger:: Logger ;
40
- use util:: ser:: { ReadableArgs , Readable , Writer , Writeable , U48 } ;
40
+ use util:: ser:: { ReadableArgs , Readable , MaybeReadable , Writer , Writeable , U48 } ;
41
41
use util:: { byte_utils, events} ;
42
42
43
43
use std:: collections:: { HashMap , hash_map, HashSet } ;
@@ -219,7 +219,6 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref> wher
219
219
monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
220
220
chain_monitor : Arc < ChainWatchInterface > ,
221
221
broadcaster : T ,
222
- pending_events : Mutex < Vec < events:: Event > > ,
223
222
logger : Arc < Logger > ,
224
223
fee_estimator : Arc < FeeEstimator >
225
224
}
@@ -229,16 +228,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
229
228
{
230
229
fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
231
230
let block_hash = header. bitcoin_hash ( ) ;
232
- let mut new_events: Vec < events:: Event > = Vec :: with_capacity ( 0 ) ;
233
231
{
234
232
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
235
233
for monitor in monitors. values_mut ( ) {
236
- let ( txn_outputs, spendable_outputs) = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator ) ;
237
- if spendable_outputs. len ( ) > 0 {
238
- new_events. push ( events:: Event :: SpendableOutputs {
239
- outputs : spendable_outputs,
240
- } ) ;
241
- }
234
+ let txn_outputs = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator ) ;
242
235
243
236
for ( ref txid, ref outputs) in txn_outputs {
244
237
for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
@@ -247,8 +240,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
247
240
}
248
241
}
249
242
}
250
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
251
- pending_events. append ( & mut new_events) ;
252
243
}
253
244
254
245
fn block_disconnected ( & self , header : & BlockHeader , disconnected_height : u32 ) {
@@ -270,7 +261,6 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
270
261
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
271
262
chain_monitor,
272
263
broadcaster,
273
- pending_events : Mutex :: new ( Vec :: new ( ) ) ,
274
264
logger,
275
265
fee_estimator : feeest,
276
266
} ;
@@ -354,10 +344,11 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref> event
354
344
where T :: Target : BroadcasterInterface
355
345
{
356
346
fn get_and_clear_pending_events ( & self ) -> Vec < events:: Event > {
357
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
358
- let mut ret = Vec :: new ( ) ;
359
- mem:: swap ( & mut ret, & mut * pending_events) ;
360
- ret
347
+ let mut pending_events = Vec :: new ( ) ;
348
+ for chan in self . monitors . lock ( ) . unwrap ( ) . values_mut ( ) {
349
+ pending_events. append ( & mut chan. get_and_clear_pending_events ( ) ) ;
350
+ }
351
+ pending_events
361
352
}
362
353
}
363
354
@@ -827,6 +818,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
827
818
payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
828
819
829
820
pending_htlcs_updated : Vec < HTLCUpdate > ,
821
+ pending_events : Vec < events:: Event > ,
830
822
831
823
destination_script : Script ,
832
824
// Thanks to data loss protection, we may be able to claim our non-htlc funds
@@ -940,6 +932,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
940
932
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
941
933
self . payment_preimages != other. payment_preimages ||
942
934
self . pending_htlcs_updated != other. pending_htlcs_updated ||
935
+ self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
943
936
self . destination_script != other. destination_script ||
944
937
self . to_remote_rescue != other. to_remote_rescue ||
945
938
self . pending_claim_requests != other. pending_claim_requests ||
@@ -1127,6 +1120,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1127
1120
data. write ( writer) ?;
1128
1121
}
1129
1122
1123
+ writer. write_all ( & byte_utils:: be64_to_array ( self . pending_events . len ( ) as u64 ) ) ?;
1124
+ for event in self . pending_events . iter ( ) {
1125
+ event. write ( writer) ?;
1126
+ }
1127
+
1130
1128
self . last_block_hash . write ( writer) ?;
1131
1129
self . destination_script . write ( writer) ?;
1132
1130
if let Some ( ( ref to_remote_script, ref local_key) ) = self . to_remote_rescue {
@@ -1259,6 +1257,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1259
1257
1260
1258
payment_preimages : HashMap :: new ( ) ,
1261
1259
pending_htlcs_updated : Vec :: new ( ) ,
1260
+ pending_events : Vec :: new ( ) ,
1262
1261
1263
1262
destination_script : destination_script. clone ( ) ,
1264
1263
to_remote_rescue : None ,
@@ -1552,6 +1551,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1552
1551
ret
1553
1552
}
1554
1553
1554
+ /// Gets the list of pending events which were generated by previous actions, clearing the list
1555
+ /// in the process.
1556
+ ///
1557
+ /// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
1558
+ /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
1559
+ /// no internal locking in ChannelMonitors.
1560
+ pub fn get_and_clear_pending_events ( & mut self ) -> Vec < events:: Event > {
1561
+ let mut ret = Vec :: new ( ) ;
1562
+ mem:: swap ( & mut ret, & mut self . pending_events ) ;
1563
+ ret
1564
+ }
1565
+
1555
1566
/// Can only fail if idx is < get_min_seen_secret
1556
1567
pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
1557
1568
self . commitment_secrets . get_secret ( idx)
@@ -2522,7 +2533,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2522
2533
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
2523
2534
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
2524
2535
/// on-chain.
2525
- fn block_connected < B : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : & FeeEstimator ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > )
2536
+ fn block_connected < B : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : & FeeEstimator ) -> Vec < ( Sha256dHash , Vec < TxOut > ) >
2526
2537
where B :: Target : BroadcasterInterface
2527
2538
{
2528
2539
for tx in txn_matched {
@@ -2754,7 +2765,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2754
2765
for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2755
2766
self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2756
2767
}
2757
- ( watch_outputs, spendable_outputs)
2768
+
2769
+ if spendable_outputs. len ( ) > 0 {
2770
+ self . pending_events . push ( events:: Event :: SpendableOutputs {
2771
+ outputs : spendable_outputs,
2772
+ } ) ;
2773
+ }
2774
+
2775
+ watch_outputs
2758
2776
}
2759
2777
2760
2778
fn block_disconnected < B : Deref > ( & mut self , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : & FeeEstimator )
@@ -3353,6 +3371,14 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3353
3371
pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
3354
3372
}
3355
3373
3374
+ let pending_events_len: u64 = Readable :: read ( reader) ?;
3375
+ let mut pending_events = Vec :: with_capacity ( cmp:: min ( pending_events_len as usize , MAX_ALLOC_SIZE / mem:: size_of :: < events:: Event > ( ) ) ) ;
3376
+ for _ in 0 ..pending_events_len {
3377
+ if let Some ( event) = MaybeReadable :: read ( reader) ? {
3378
+ pending_events. push ( event) ;
3379
+ }
3380
+ }
3381
+
3356
3382
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
3357
3383
let destination_script = Readable :: read ( reader) ?;
3358
3384
let to_remote_rescue = match <u8 as Readable < R > >:: read ( reader) ? {
@@ -3455,6 +3481,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3455
3481
3456
3482
payment_preimages,
3457
3483
pending_htlcs_updated,
3484
+ pending_events,
3458
3485
3459
3486
destination_script,
3460
3487
to_remote_rescue,
0 commit comments