@@ -117,9 +117,13 @@ pub struct HTLCUpdate {
117
117
pub trait ManyChannelMonitor : 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 ) -> Result < ( ) , ChannelMonitorUpdateErr > ;
124
128
125
129
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
@@ -260,6 +264,11 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
260
264
self . chain_monitor . watch_all_txn ( ) ;
261
265
}
262
266
}
267
+ for ( txid, outputs) in monitor. get_watch_outputs ( ) . iter ( ) {
268
+ for ( idx, script) in outputs. iter ( ) . enumerate ( ) {
269
+ self . chain_monitor . install_watch_outpoint ( ( * txid, idx as u32 ) , script) ;
270
+ }
271
+ }
263
272
monitors. insert ( key, monitor) ;
264
273
Ok ( ( ) )
265
274
}
@@ -643,6 +652,12 @@ pub struct ChannelMonitor {
643
652
// actions when we receive a block with given height. Actions depend on OnchainEvent type.
644
653
onchain_events_waiting_threshold_conf : HashMap < u32 , Vec < OnchainEvent > > ,
645
654
655
+ // If we get serialized out and re-read, we need to make sure that the chain monitoring
656
+ // interface knows about the TXOs that we want to be notified of spends of. We could probably
657
+ // be smart and derive them from the above storage fields, but its much simpler and more
658
+ // Obviously Correct (tm) if we just keep track of them explicitly.
659
+ watch_outputs : HashMap < Sha256dHash , Vec < Script > > ,
660
+
646
661
// We simply modify last_block_hash in Channel's block_connected so that serialization is
647
662
// consistent but hopefully the users' copy handles block_connected in a consistent way.
648
663
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -713,7 +728,8 @@ impl PartialEq for ChannelMonitor {
713
728
self . to_remote_rescue != other. to_remote_rescue ||
714
729
self . pending_claim_requests != other. pending_claim_requests ||
715
730
self . claimable_outpoints != other. claimable_outpoints ||
716
- self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf
731
+ self . onchain_events_waiting_threshold_conf != other. onchain_events_waiting_threshold_conf ||
732
+ self . watch_outputs != other. watch_outputs
717
733
{
718
734
false
719
735
} else {
@@ -770,6 +786,7 @@ impl ChannelMonitor {
770
786
claimable_outpoints : HashMap :: new ( ) ,
771
787
772
788
onchain_events_waiting_threshold_conf : HashMap :: new ( ) ,
789
+ watch_outputs : HashMap :: new ( ) ,
773
790
774
791
last_block_hash : Default :: default ( ) ,
775
792
secp_ctx : Secp256k1 :: new ( ) ,
@@ -1104,6 +1121,12 @@ impl ChannelMonitor {
1104
1121
}
1105
1122
}
1106
1123
1124
+ /// Gets a list of txids, with their output scripts (in the order they appear in the
1125
+ /// transaction), which we must learn about spends of via block_connected().
1126
+ pub fn get_watch_outputs ( & self ) -> & HashMap < Sha256dHash , Vec < Script > > {
1127
+ & self . watch_outputs
1128
+ }
1129
+
1107
1130
/// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
1108
1131
/// Generally useful when deserializing as during normal operation the return values of
1109
1132
/// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
@@ -1332,6 +1355,15 @@ impl ChannelMonitor {
1332
1355
}
1333
1356
}
1334
1357
1358
+ ( self . watch_outputs . len ( ) as u64 ) . write ( writer) ?;
1359
+ for ( txid, output_scripts) in self . watch_outputs . iter ( ) {
1360
+ txid. write ( writer) ?;
1361
+ ( output_scripts. len ( ) as u64 ) . write ( writer) ?;
1362
+ for script in output_scripts. iter ( ) {
1363
+ script. write ( writer) ?;
1364
+ }
1365
+ }
1366
+
1335
1367
Ok ( ( ) )
1336
1368
}
1337
1369
@@ -2551,6 +2583,9 @@ impl ChannelMonitor {
2551
2583
}
2552
2584
}
2553
2585
self . last_block_hash = block_hash. clone ( ) ;
2586
+ for & ( ref txid, ref output_scripts) in watch_outputs. iter ( ) {
2587
+ self . watch_outputs . insert ( txid. clone ( ) , output_scripts. iter ( ) . map ( |o| o. script_pubkey . clone ( ) ) . collect ( ) ) ;
2588
+ }
2554
2589
( watch_outputs, spendable_outputs, htlc_updated)
2555
2590
}
2556
2591
@@ -3202,6 +3237,20 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
3202
3237
onchain_events_waiting_threshold_conf. insert ( height_target, events) ;
3203
3238
}
3204
3239
3240
+ let watch_outputs_len: u64 = Readable :: read ( reader) ?;
3241
+ let mut watch_outputs = HashMap :: with_capacity ( cmp:: min ( watch_outputs_len as usize , MAX_ALLOC_SIZE / ( 32 + 3 * 8 ) ) ) ;
3242
+ for _ in 0 ..watch_outputs_len {
3243
+ let txid = Readable :: read ( reader) ?;
3244
+ let outputs_len: u64 = Readable :: read ( reader) ?;
3245
+ let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_len as usize , MAX_ALLOC_SIZE / 3 * 8 ) ) ;
3246
+ for _ in 0 ..outputs_len {
3247
+ outputs. push ( Readable :: read ( reader) ?) ;
3248
+ }
3249
+ if let Some ( _) = watch_outputs. insert ( txid, outputs) {
3250
+ return Err ( DecodeError :: InvalidValue ) ;
3251
+ }
3252
+ }
3253
+
3205
3254
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
3206
3255
commitment_transaction_number_obscure_factor,
3207
3256
@@ -3234,6 +3283,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
3234
3283
claimable_outpoints,
3235
3284
3236
3285
onchain_events_waiting_threshold_conf,
3286
+ watch_outputs,
3237
3287
3238
3288
last_block_hash,
3239
3289
secp_ctx,
0 commit comments