@@ -37,7 +37,7 @@ use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInter
3737use chain:: transaction:: OutPoint ;
3838use chain:: keysinterface:: { SpendableOutputDescriptor , ChannelKeys } ;
3939use util:: logger:: Logger ;
40- use util:: ser:: { ReadableArgs , Readable , Writer , Writeable , U48 } ;
40+ use util:: ser:: { ReadableArgs , Readable , MaybeReadable , Writer , Writeable , U48 } ;
4141use util:: { byte_utils, events} ;
4242
4343use std:: collections:: { HashMap , hash_map, HashSet } ;
@@ -219,7 +219,6 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref> wher
219219 monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
220220 chain_monitor : Arc < ChainWatchInterface > ,
221221 broadcaster : T ,
222- pending_events : Mutex < Vec < events:: Event > > ,
223222 logger : Arc < Logger > ,
224223 fee_estimator : Arc < FeeEstimator >
225224}
@@ -229,16 +228,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
229228{
230229 fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
231230 let block_hash = header. bitcoin_hash ( ) ;
232- let mut new_events: Vec < events:: Event > = Vec :: with_capacity ( 0 ) ;
233231 {
234232 let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
235233 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 ) ;
242235
243236 for ( ref txid, ref outputs) in txn_outputs {
244237 for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
@@ -247,8 +240,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
247240 }
248241 }
249242 }
250- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
251- pending_events. append ( & mut new_events) ;
252243 }
253244
254245 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
270261 monitors : Mutex :: new ( HashMap :: new ( ) ) ,
271262 chain_monitor,
272263 broadcaster,
273- pending_events : Mutex :: new ( Vec :: new ( ) ) ,
274264 logger,
275265 fee_estimator : feeest,
276266 } ;
@@ -354,10 +344,11 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref> event
354344 where T :: Target : BroadcasterInterface
355345{
356346 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
361352 }
362353}
363354
@@ -827,6 +818,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
827818 payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
828819
829820 pending_htlcs_updated : Vec < HTLCUpdate > ,
821+ pending_events : Vec < events:: Event > ,
830822
831823 destination_script : Script ,
832824 // 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> {
940932 self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
941933 self . payment_preimages != other. payment_preimages ||
942934 self . pending_htlcs_updated != other. pending_htlcs_updated ||
935+ self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
943936 self . destination_script != other. destination_script ||
944937 self . to_remote_rescue != other. to_remote_rescue ||
945938 self . pending_claim_requests != other. pending_claim_requests ||
@@ -1127,6 +1120,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
11271120 data. write ( writer) ?;
11281121 }
11291122
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+
11301128 self . last_block_hash . write ( writer) ?;
11311129 self . destination_script . write ( writer) ?;
11321130 if let Some ( ( ref to_remote_script, ref local_key) ) = self . to_remote_rescue {
@@ -1259,6 +1257,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12591257
12601258 payment_preimages : HashMap :: new ( ) ,
12611259 pending_htlcs_updated : Vec :: new ( ) ,
1260+ pending_events : Vec :: new ( ) ,
12621261
12631262 destination_script : destination_script. clone ( ) ,
12641263 to_remote_rescue : None ,
@@ -1552,6 +1551,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15521551 ret
15531552 }
15541553
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+
15551566 /// Can only fail if idx is < get_min_seen_secret
15561567 pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
15571568 self . commitment_secrets . get_secret ( idx)
@@ -2522,7 +2533,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
25222533 /// Eventually this should be pub and, roughly, implement ChainListener, however this requires
25232534 /// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
25242535 /// 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 > ) >
25262537 where B :: Target : BroadcasterInterface
25272538 {
25282539 for tx in txn_matched {
@@ -2754,7 +2765,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
27542765 for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
27552766 self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
27562767 }
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
27582776 }
27592777
27602778 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,
33533371 pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
33543372 }
33553373
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+
33563382 let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
33573383 let destination_script = Readable :: read ( reader) ?;
33583384 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,
34553481
34563482 payment_preimages,
34573483 pending_htlcs_updated,
3484+ pending_events,
34583485
34593486 destination_script,
34603487 to_remote_rescue,
0 commit comments