@@ -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 } ;
@@ -222,7 +222,6 @@ pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys, T: Deref, F: D
222222 monitors : Mutex < HashMap < Key , ChannelMonitor < ChanSigner > > > ,
223223 chain_monitor : Arc < ChainWatchInterface > ,
224224 broadcaster : T ,
225- pending_events : Mutex < Vec < events:: Event > > ,
226225 logger : Arc < Logger > ,
227226 fee_estimator : F
228227}
@@ -234,16 +233,10 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
234233{
235234 fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , _indexes_of_txn_matched : & [ u32 ] ) {
236235 let block_hash = header. bitcoin_hash ( ) ;
237- let mut new_events: Vec < events:: Event > = Vec :: with_capacity ( 0 ) ;
238236 {
239237 let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
240238 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 ) ;
247240
248241 for ( ref txid, ref outputs) in txn_outputs {
249242 for ( idx, output) in outputs. iter ( ) . enumerate ( ) {
@@ -252,8 +245,6 @@ impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref +
252245 }
253246 }
254247 }
255- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
256- pending_events. append ( & mut new_events) ;
257248 }
258249
259250 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
276267 monitors : Mutex :: new ( HashMap :: new ( ) ) ,
277268 chain_monitor,
278269 broadcaster,
279- pending_events : Mutex :: new ( Vec :: new ( ) ) ,
280270 logger,
281271 fee_estimator : feeest,
282272 } ;
@@ -362,10 +352,11 @@ impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys, T: Deref, F: De
362352 F :: Target : FeeEstimator
363353{
364354 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
369360 }
370361}
371362
@@ -792,6 +783,11 @@ impl<R: ::std::io::Read> Readable<R> for ChannelMonitorUpdateStep {
792783///
793784/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
794785/// information and are actively monitoring the chain.
786+ ///
787+ /// Pending Events or updated HTLCs which have not yet been read out by
788+ /// get_and_clear_pending_htlcs_updated or get_and_clear_pending_events are serialized to disk and
789+ /// reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
790+ /// gotten are fully handled before re-serializing the new state.
795791pub struct ChannelMonitor < ChanSigner : ChannelKeys > {
796792 latest_update_id : u64 ,
797793 commitment_transaction_number_obscure_factor : u64 ,
@@ -835,6 +831,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
835831 payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
836832
837833 pending_htlcs_updated : Vec < HTLCUpdate > ,
834+ pending_events : Vec < events:: Event > ,
838835
839836 destination_script : Script ,
840837 // Thanks to data loss protection, we may be able to claim our non-htlc funds
@@ -948,6 +945,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
948945 self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
949946 self . payment_preimages != other. payment_preimages ||
950947 self . pending_htlcs_updated != other. pending_htlcs_updated ||
948+ self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
951949 self . destination_script != other. destination_script ||
952950 self . to_remote_rescue != other. to_remote_rescue ||
953951 self . pending_claim_requests != other. pending_claim_requests ||
@@ -1135,6 +1133,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
11351133 data. write ( writer) ?;
11361134 }
11371135
1136+ writer. write_all ( & byte_utils:: be64_to_array ( self . pending_events . len ( ) as u64 ) ) ?;
1137+ for event in self . pending_events . iter ( ) {
1138+ event. write ( writer) ?;
1139+ }
1140+
11381141 self . last_block_hash . write ( writer) ?;
11391142 self . destination_script . write ( writer) ?;
11401143 if let Some ( ( ref to_remote_script, ref local_key) ) = self . to_remote_rescue {
@@ -1267,6 +1270,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12671270
12681271 payment_preimages : HashMap :: new ( ) ,
12691272 pending_htlcs_updated : Vec :: new ( ) ,
1273+ pending_events : Vec :: new ( ) ,
12701274
12711275 destination_script : destination_script. clone ( ) ,
12721276 to_remote_rescue : None ,
@@ -1560,6 +1564,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15601564 ret
15611565 }
15621566
1567+ /// Gets the list of pending events which were generated by previous actions, clearing the list
1568+ /// in the process.
1569+ ///
1570+ /// This is called by ManyChannelMonitor::get_and_clear_pending_events() and is equivalent to
1571+ /// EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
1572+ /// no internal locking in ChannelMonitors.
1573+ pub fn get_and_clear_pending_events ( & mut self ) -> Vec < events:: Event > {
1574+ let mut ret = Vec :: new ( ) ;
1575+ mem:: swap ( & mut ret, & mut self . pending_events ) ;
1576+ ret
1577+ }
1578+
15631579 /// Can only fail if idx is < get_min_seen_secret
15641580 pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
15651581 self . commitment_secrets . get_secret ( idx)
@@ -2534,7 +2550,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
25342550 /// Eventually this should be pub and, roughly, implement ChainListener, however this requires
25352551 /// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
25362552 /// 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 > )
2553+ 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 > ) >
25382554 where B :: Target : BroadcasterInterface ,
25392555 F :: Target : FeeEstimator
25402556 {
@@ -2767,7 +2783,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
27672783 for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
27682784 self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
27692785 }
2770- ( watch_outputs, spendable_outputs)
2786+
2787+ if spendable_outputs. len ( ) > 0 {
2788+ self . pending_events . push ( events:: Event :: SpendableOutputs {
2789+ outputs : spendable_outputs,
2790+ } ) ;
2791+ }
2792+
2793+ watch_outputs
27712794 }
27722795
27732796 fn block_disconnected < B : Deref , F : Deref > ( & mut self , height : u32 , block_hash : & Sha256dHash , broadcaster : B , fee_estimator : F )
@@ -3369,6 +3392,14 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
33693392 pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
33703393 }
33713394
3395+ let pending_events_len: u64 = Readable :: read ( reader) ?;
3396+ let mut pending_events = Vec :: with_capacity ( cmp:: min ( pending_events_len as usize , MAX_ALLOC_SIZE / mem:: size_of :: < events:: Event > ( ) ) ) ;
3397+ for _ in 0 ..pending_events_len {
3398+ if let Some ( event) = MaybeReadable :: read ( reader) ? {
3399+ pending_events. push ( event) ;
3400+ }
3401+ }
3402+
33723403 let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
33733404 let destination_script = Readable :: read ( reader) ?;
33743405 let to_remote_rescue = match <u8 as Readable < R > >:: read ( reader) ? {
@@ -3471,6 +3502,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
34713502
34723503 payment_preimages,
34733504 pending_htlcs_updated,
3505+ pending_events,
34743506
34753507 destination_script,
34763508 to_remote_rescue,
0 commit comments