@@ -117,9 +117,16 @@ 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_outputs_to_watch() is registered to ensure that the provided monitor learns about
126
+ /// any spends of any of the outputs.
127
+ ///
128
+ /// Any spends of outputs which should have been registered which aren't passed to
129
+ /// ChannelMonitors via block_connected may result in funds loss.
123
130
fn add_update_monitor ( & self , funding_txo : OutPoint , monitor : ChannelMonitor < ChanSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > ;
124
131
125
132
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
@@ -259,6 +266,11 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys> Simpl
259
266
self . chain_monitor . watch_all_txn ( ) ;
260
267
}
261
268
}
269
+ for ( txid, outputs) in monitor. get_outputs_to_watch ( ) . iter ( ) {
270
+ for ( idx, script) in outputs. iter ( ) . enumerate ( ) {
271
+ self . chain_monitor . install_watch_outpoint ( ( * txid, idx as u32 ) , script) ;
272
+ }
273
+ }
262
274
monitors. insert ( key, monitor) ;
263
275
Ok ( ( ) )
264
276
}
@@ -666,6 +678,12 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
666
678
// actions when we receive a block with given height. Actions depend on OnchainEvent type.
667
679
onchain_events_waiting_threshold_conf : HashMap < u32 , Vec < OnchainEvent > > ,
668
680
681
+ // If we get serialized out and re-read, we need to make sure that the chain monitoring
682
+ // interface knows about the TXOs that we want to be notified of spends of. We could probably
683
+ // be smart and derive them from the above storage fields, but its much simpler and more
684
+ // Obviously Correct (tm) if we just keep track of them explicitly.
685
+ outputs_to_watch : HashMap < Sha256dHash , Vec < Script > > ,
686
+
669
687
// We simply modify last_block_hash in Channel's block_connected so that serialization is
670
688
// consistent but hopefully the users' copy handles block_connected in a consistent way.
671
689
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -736,7 +754,8 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
736
754
self . to_remote_rescue != other. to_remote_rescue ||
737
755
self . pending_claim_requests != other. pending_claim_requests ||
738
756
self . claimable_outpoints != other. claimable_outpoints ||
739
- self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf
757
+ self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf ||
758
+ self . outputs_to_watch != other. outputs_to_watch
740
759
{
741
760
false
742
761
} else {
@@ -966,6 +985,15 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
966
985
}
967
986
}
968
987
988
+ ( self . outputs_to_watch . len ( ) as u64 ) . write ( writer) ?;
989
+ for ( txid, output_scripts) in self . outputs_to_watch . iter ( ) {
990
+ txid. write ( writer) ?;
991
+ ( output_scripts. len ( ) as u64 ) . write ( writer) ?;
992
+ for script in output_scripts. iter ( ) {
993
+ script. write ( writer) ?;
994
+ }
995
+ }
996
+
969
997
Ok ( ( ) )
970
998
}
971
999
@@ -1036,6 +1064,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1036
1064
claimable_outpoints : HashMap :: new ( ) ,
1037
1065
1038
1066
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
1067
+ outputs_to_watch : HashMap :: new ( ) ,
1039
1068
1040
1069
last_block_hash : Default :: default ( ) ,
1041
1070
secp_ctx : Secp256k1 :: new ( ) ,
@@ -1370,6 +1399,12 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
1370
1399
}
1371
1400
}
1372
1401
1402
+ /// Gets a list of txids, with their output scripts (in the order they appear in the
1403
+ /// transaction), which we must learn about spends of via block_connected().
1404
+ pub fn get_outputs_to_watch ( & self ) -> & HashMap < Sha256dHash , Vec < Script > > {
1405
+ & self . outputs_to_watch
1406
+ }
1407
+
1373
1408
/// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
1374
1409
/// Generally useful when deserializing as during normal operation the return values of
1375
1410
/// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
@@ -2589,6 +2624,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
2589
2624
}
2590
2625
}
2591
2626
self . last_block_hash = block_hash. clone ( ) ;
2627
+ for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2628
+ self . outputs_to_watch . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2629
+ }
2592
2630
( watch_outputs, spendable_outputs, htlc_updated)
2593
2631
}
2594
2632
@@ -3241,6 +3279,20 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3241
3279
onchain_events_waiting_threshold_conf. insert ( height_target, events) ;
3242
3280
}
3243
3281
3282
+ let outputs_to_watch_len: u64 = Readable :: read ( reader) ?;
3283
+ let mut outputs_to_watch = HashMap :: with_capacity ( cmp:: min ( outputs_to_watch_len as usize , MAX_ALLOC_SIZE / ( mem:: size_of :: < Sha256dHash > ( ) + mem:: size_of :: < Vec < Script > > ( ) ) ) ) ;
3284
+ for _ in 0 ..outputs_to_watch_len {
3285
+ let txid = Readable :: read ( reader) ?;
3286
+ let outputs_len: u64 = Readable :: read ( reader) ?;
3287
+ let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_len as usize , MAX_ALLOC_SIZE / mem:: size_of :: < Script > ( ) ) ) ;
3288
+ for _ in 0 ..outputs_len {
3289
+ outputs. push ( Readable :: read ( reader) ?) ;
3290
+ }
3291
+ if let Some ( _) = outputs_to_watch. insert ( txid, outputs) {
3292
+ return Err ( DecodeError :: InvalidValue ) ;
3293
+ }
3294
+ }
3295
+
3244
3296
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
3245
3297
commitment_transaction_number_obscure_factor,
3246
3298
@@ -3273,6 +3325,7 @@ impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3273
3325
claimable_outpoints,
3274
3326
3275
3327
onchain_events_waiting_threshold_conf,
3328
+ outputs_to_watch,
3276
3329
3277
3330
last_block_hash,
3278
3331
secp_ctx,
0 commit comments