@@ -117,9 +117,13 @@ pub struct HTLCUpdate {
117
117
pub trait ManyChannelMonitor < ChanSigner : ChannelKeys > : Send + Sync {
118
118
/// Adds or updates a monitor for the given `funding_txo`.
119
119
///
120
- /// Implementor must also ensure that the funding_txo outpoint is registered with any relevant
121
- /// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with
122
- /// any spends of it.
120
+ /// Implementer must also ensure that the funding_txo txid *and* outpoint are registered with
121
+ /// any relevant ChainWatchInterfaces such that the provided monitor receives block_connected
122
+ /// callbacks with the funding transaction, or any spends of it.
123
+ ///
124
+ /// Further, the implementer must also ensure that each output returned in
125
+ /// monitor.get_watch_outputs() is registered to ensure that the provided monitor learns about
126
+ /// any spends of any of the outputs.
123
127
fn add_update_monitor ( & self , funding_txo : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > ;
124
128
125
129
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
@@ -259,6 +263,11 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys> Simpl
259
263
self . chain_monitor . watch_all_txn ( ) ;
260
264
}
261
265
}
266
+ for ( txid, outputs) in monitor. get_watch_outputs ( ) . iter ( ) {
267
+ for ( idx, script) in outputs. iter ( ) . enumerate ( ) {
268
+ self . chain_monitor . install_watch_outpoint ( ( * txid, idx as u32 ) , script) ;
269
+ }
270
+ }
262
271
monitors. insert ( key, monitor) ;
263
272
Ok ( ( ) )
264
273
}
@@ -666,6 +675,12 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
666
675
// actions when we receive a block with given height. Actions depend on OnchainEvent type.
667
676
onchain_events_waiting_threshold_conf : HashMap < u32 , Vec < OnchainEvent > > ,
668
677
678
+ // If we get serialized out and re-read, we need to make sure that the chain monitoring
679
+ // interface knows about the TXOs that we want to be notified of spends of. We could probably
680
+ // be smart and derive them from the above storage fields, but its much simpler and more
681
+ // Obviously Correct (tm) if we just keep track of them explicitly.
682
+ watch_outputs : HashMap < Sha256dHash , Vec < Script > > ,
683
+
669
684
// We simply modify last_block_hash in Channel's block_connected so that serialization is
670
685
// consistent but hopefully the users' copy handles block_connected in a consistent way.
671
686
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -736,7 +751,8 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
736
751
self . to_remote_rescue != other. to_remote_rescue ||
737
752
self . pending_claim_requests != other. pending_claim_requests ||
738
753
self . claimable_outpoints != other. claimable_outpoints ||
739
- self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf
754
+ self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf ||
755
+ self . watch_outputs != other. watch_outputs
740
756
{
741
757
false
742
758
} else {
@@ -966,6 +982,15 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
966
982
}
967
983
}
968
984
985
+ ( self . watch_outputs . len ( ) as u64 ) . write ( writer) ?;
986
+ for ( txid, output_scripts) in self . watch_outputs . iter ( ) {
987
+ txid. write ( writer) ?;
988
+ ( output_scripts. len ( ) as u64 ) . write ( writer) ?;
989
+ for script in output_scripts. iter ( ) {
990
+ script. write ( writer) ?;
991
+ }
992
+ }
993
+
969
994
Ok ( ( ) )
970
995
}
971
996
@@ -1036,6 +1061,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1036
1061
claimable_outpoints : HashMap :: new ( ) ,
1037
1062
1038
1063
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
1064
+ watch_outputs : HashMap :: new ( ) ,
1039
1065
1040
1066
last_block_hash : Default :: default ( ) ,
1041
1067
secp_ctx : Secp256k1 :: new ( ) ,
@@ -1370,6 +1396,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1370
1396
}
1371
1397
}
1372
1398
1399
+ /// Gets a list of txids, with their output scripts (in the order they appear in the
1400
+ /// transaction), which we must learn about spends of via block_connected().
1401
+ pub fn get_watch_outputs ( & self ) -> & HashMap < Sha256dHash , Vec < Script > > {
1402
+ & self . watch_outputs
1403
+ }
1404
+
1373
1405
/// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
1374
1406
/// Generally useful when deserializing as during normal operation the return values of
1375
1407
/// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
@@ -2589,6 +2621,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2589
2621
}
2590
2622
}
2591
2623
self . last_block_hash = block_hash. clone ( ) ;
2624
+ for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2625
+ self . watch_outputs . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2626
+ }
2592
2627
( watch_outputs, spendable_outputs, htlc_updated)
2593
2628
}
2594
2629
@@ -3242,6 +3277,20 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3242
3277
onchain_events_waiting_threshold_conf. insert ( height_target, events) ;
3243
3278
}
3244
3279
3280
+ let watch_outputs_len: u64 = Readable :: read ( reader) ?;
3281
+ let mut watch_outputs = HashMap :: with_capacity ( cmp:: min ( watch_outputs_len as usize , MAX_ALLOC_SIZE / ( 32 + 3 * 8 ) ) ) ;
3282
+ for _ in 0 ..watch_outputs_len {
3283
+ let txid = Readable :: read ( reader) ?;
3284
+ let outputs_len: u64 = Readable :: read ( reader) ?;
3285
+ let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_len as usize , MAX_ALLOC_SIZE / 3 * 8 ) ) ;
3286
+ for _ in 0 ..outputs_len {
3287
+ outputs. push ( Readable :: read ( reader) ?) ;
3288
+ }
3289
+ if let Some ( _) = watch_outputs. insert ( txid, outputs) {
3290
+ return Err ( DecodeError :: InvalidValue ) ;
3291
+ }
3292
+ }
3293
+
3245
3294
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
3246
3295
commitment_transaction_number_obscure_factor,
3247
3296
@@ -3274,6 +3323,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3274
3323
claimable_outpoints,
3275
3324
3276
3325
onchain_events_waiting_threshold_conf,
3326
+ watch_outputs,
3277
3327
3278
3328
last_block_hash,
3279
3329
secp_ctx,
0 commit comments