@@ -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
@@ -792,6 +783,11 @@ impl<R: ::std::io::Read> Readable<R> for ChannelMonitorUpdateStep {
792
783
///
793
784
/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
794
785
/// 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.
795
791
pub struct ChannelMonitor < ChanSigner : ChannelKeys > {
796
792
latest_update_id : u64 ,
797
793
commitment_transaction_number_obscure_factor : u64 ,
@@ -835,6 +831,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
835
831
payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
836
832
837
833
pending_htlcs_updated : Vec < HTLCUpdate > ,
834
+ pending_events : Vec < events:: Event > ,
838
835
839
836
destination_script : Script ,
840
837
// 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> {
948
945
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
949
946
self . payment_preimages != other. payment_preimages ||
950
947
self . pending_htlcs_updated != other. pending_htlcs_updated ||
948
+ self . pending_events . len ( ) != other. pending_events . len ( ) || // We trust events to round-trip properly
951
949
self . destination_script != other. destination_script ||
952
950
self . to_remote_rescue != other. to_remote_rescue ||
953
951
self . pending_claim_requests != other. pending_claim_requests ||
@@ -1135,6 +1133,11 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
1135
1133
data. write ( writer) ?;
1136
1134
}
1137
1135
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
+
1138
1141
self . last_block_hash . write ( writer) ?;
1139
1142
self . destination_script . write ( writer) ?;
1140
1143
if let Some ( ( ref to_remote_script, ref local_key) ) = self . to_remote_rescue {
@@ -1267,6 +1270,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1267
1270
1268
1271
payment_preimages : HashMap :: new ( ) ,
1269
1272
pending_htlcs_updated : Vec :: new ( ) ,
1273
+ pending_events : Vec :: new ( ) ,
1270
1274
1271
1275
destination_script : destination_script. clone ( ) ,
1272
1276
to_remote_rescue : None ,
@@ -1560,6 +1564,18 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1560
1564
ret
1561
1565
}
1562
1566
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
+
1563
1579
/// Can only fail if idx is < get_min_seen_secret
1564
1580
pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
1565
1581
self . commitment_secrets . get_secret ( idx)
@@ -2534,7 +2550,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2534
2550
/// Eventually this should be pub and, roughly, implement ChainListener, however this requires
2535
2551
/// &mut self, as well as returns new spendable outputs and outpoints to watch for spending of
2536
2552
/// 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 > ) >
2538
2554
where B :: Target : BroadcasterInterface ,
2539
2555
F :: Target : FeeEstimator
2540
2556
{
@@ -2767,7 +2783,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2767
2783
for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2768
2784
self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2769
2785
}
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
2771
2794
}
2772
2795
2773
2796
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,
3369
3392
pending_htlcs_updated. push ( Readable :: read ( reader) ?) ;
3370
3393
}
3371
3394
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
+
3372
3403
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
3373
3404
let destination_script = Readable :: read ( reader) ?;
3374
3405
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,
3471
3502
3472
3503
payment_preimages,
3473
3504
pending_htlcs_updated,
3505
+ pending_events,
3474
3506
3475
3507
destination_script,
3476
3508
to_remote_rescue,
0 commit comments