@@ -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 } ;
@@ -222,7 +222,6 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: D
222
222
monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
223
223
chain_monitor : Arc < ChainWatchInterface > ,
224
224
broadcaster : T ,
225
- pending_events : Mutex < Vec < events:: Event > > ,
226
225
logger : Arc < Logger > ,
227
226
fee_estimator : F
228
227
}
@@ -234,16 +233,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
234
233
{
235
234
fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
236
235
let block_hash = header. bitcoin_hash ( ) ;
237
- let mut new_events: Vec < events:: Event > = Vec :: with_capacity ( 0 ) ;
238
236
{
239
237
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
240
238
for monitor in monitors. values_mut ( ) {
241
- let ( txn_outputs, spendable_outputs) = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator ) ;
242
- if spendable_outputs. len ( ) > 0 {
243
- new_events. push ( events:: Event :: SpendableOutputs {
244
- outputs : spendable_outputs,
245
- } ) ;
246
- }
239
+ let txn_outputs = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator ) ;
247
240
248
241
for ( ref txid, ref outputs) in txn_outputs {
249
242
for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
@@ -252,8 +245,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
252
245
}
253
246
}
254
247
}
255
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
256
- pending_events. append ( & mut new_events) ;
257
248
}
258
249
259
250
fn block_disconnected ( & self , header : & BlockHeader , disconnected_height : u32 ) {
@@ -276,7 +267,6 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys, T: De
276
267
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
277
268
chain_monitor,
278
269
broadcaster,
279
- pending_events : Mutex :: new ( Vec :: new ( ) ) ,
280
270
logger,
281
271
fee_estimator : feeest,
282
272
} ;
@@ -362,10 +352,11 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
362
352
F :: Target : FeeEstimator
363
353
{
364
354
fn get_and_clear_pending_events ( & self ) -> Vec < events:: Event > {
365
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
366
- let mut ret = Vec :: new ( ) ;
367
- mem:: swap ( & mut ret, & mut * pending_events) ;
368
- ret
355
+ let mut pending_events = Vec :: new ( ) ;
356
+ for chan in self . monitors . lock ( ) . unwrap ( ) . values_mut ( ) {
357
+ pending_events. append ( & mut chan. get_and_clear_pending_events ( ) ) ;
358
+ }
359
+ pending_events
369
360
}
370
361
}
371
362
@@ -835,6 +826,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
835
826
payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
836
827
837
828
pending_htlcs_updated : Vec < HTLCUpdate > ,
829
+ pending_events : Vec < events:: Event > ,
838
830
839
831
destination_script : Script ,
840
832
// Thanks to data loss protection, we may be able to claim our non-htlc funds
@@ -948,6 +940,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
948
940
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
949
941
self . payment_preimages != other. payment_preimages ||
950
942
self . pending_htlcs_updated != other. pending_htlcs_updated ||
943
+ self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
951
944
self . destination_script != other. destination_script ||
952
945
self . to_remote_rescue != other. to_remote_rescue ||
953
946
self . pending_claim_requests != other. pending_claim_requests ||
@@ -1135,6 +1128,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1135
1128
data. write ( writer) ?;
1136
1129
}
1137
1130
1131
+ writer. write_all ( & byte_utils:: be64_to_array ( self . pending_events . len ( ) as u64 ) ) ?;
1132
+ for event in self . pending_events . iter ( ) {
1133
+ event. write ( writer) ?;
1134
+ }
1135
+
1138
1136
self . last_block_hash . write ( writer) ?;
1139
1137
self . destination_script . write ( writer) ?;
1140
1138
if let Some ( ( ref to_remote_script, ref local_key) ) = self . to_remote_rescue {
@@ -1267,6 +1265,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1267
1265
1268
1266
payment_preimages : HashMap :: new ( ) ,
1269
1267
pending_htlcs_updated : Vec :: new ( ) ,
1268
+ pending_events : Vec :: new ( ) ,
1270
1269
1271
1270
destination_script : destination_script. clone ( ) ,
1272
1271
to_remote_rescue : None ,
@@ -1560,6 +1559,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1560
1559
ret
1561
1560
}
1562
1561
1562
+ /// Gets the list of pending events which were generated by previous actions, clearing the list
1563
+ /// in the process.
1564
+ ///
1565
+ /// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
1566
+ /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
1567
+ /// no internal locking in ChannelMonitors.
1568
+ pub fn get_and_clear_pending_events ( & mut self ) -> Vec < events:: Event > {
1569
+ let mut ret = Vec :: new ( ) ;
1570
+ mem:: swap ( & mut ret, & mut self . pending_events ) ;
1571
+ ret
1572
+ }
1573
+
1563
1574
/// Can only fail if idx is < get_min_seen_secret
1564
1575
pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
1565
1576
self . commitment_secrets . get_secret ( idx)
@@ -2534,7 +2545,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2534
2545
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
2535
2546
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
2536
2547
/// on-chain.
2537
- fn block_connected < B : Deref , F : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : F ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > )
2548
+ fn block_connected < B : Deref , F : Deref > ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : F ) -> Vec < ( Sha256dHash , Vec < TxOut > ) >
2538
2549
where B :: Target : BroadcasterInterface ,
2539
2550
F :: Target : FeeEstimator
2540
2551
{
@@ -2767,7 +2778,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2767
2778
for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2768
2779
self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2769
2780
}
2770
- ( watch_outputs, spendable_outputs)
2781
+
2782
+ if spendable_outputs. len ( ) > 0 {
2783
+ self . pending_events . push ( events:: Event :: SpendableOutputs {
2784
+ outputs : spendable_outputs,
2785
+ } ) ;
2786
+ }
2787
+
2788
+ watch_outputs
2771
2789
}
2772
2790
2773
2791
fn block_disconnected < B : Deref , F : Deref > ( & mut self , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : F )
@@ -3369,6 +3387,14 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3369
3387
pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
3370
3388
}
3371
3389
3390
+ let pending_events_len: u64 = Readable :: read ( reader) ?;
3391
+ let mut pending_events = Vec :: with_capacity ( cmp:: min ( pending_events_len as usize , MAX_ALLOC_SIZE / mem:: size_of :: < events:: Event > ( ) ) ) ;
3392
+ for _ in 0 ..pending_events_len {
3393
+ if let Some ( event) = MaybeReadable :: read ( reader) ? {
3394
+ pending_events. push ( event) ;
3395
+ }
3396
+ }
3397
+
3372
3398
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
3373
3399
let destination_script = Readable :: read ( reader) ?;
3374
3400
let to_remote_rescue = match <u8 as Readable < R > >:: read ( reader) ? {
@@ -3471,6 +3497,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3471
3497
3472
3498
payment_preimages,
3473
3499
pending_htlcs_updated,
3500
+ pending_events,
3474
3501
3475
3502
destination_script,
3476
3503
to_remote_rescue,
0 commit comments