Skip to content

Commit 494bbe5

Browse files
committed
Track the full list of outpoints a chanmon wants monitoring for.
Upon deserialization/reload we need to be able to register each outpoint which spends the commitment txo which a channelmonitor believes to be on chain. While our other internal tracking is likely sufficient to regenerate these, its much easier to simply track all outpouts we've ever generated, so we do that here.
1 parent f263b37 commit 494bbe5

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ pub struct HTLCUpdate {
117117
pub trait ManyChannelMonitor: Send + Sync {
118118
/// Adds or updates a monitor for the given `funding_txo`.
119119
///
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.
123127
fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
124128

125129
/// 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>
260264
self.chain_monitor.watch_all_txn();
261265
}
262266
}
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+
}
263272
monitors.insert(key, monitor);
264273
Ok(())
265274
}
@@ -643,6 +652,12 @@ pub struct ChannelMonitor {
643652
// actions when we receive a block with given height. Actions depend on OnchainEvent type.
644653
onchain_events_waiting_threshold_conf: HashMap<u32, Vec<OnchainEvent>>,
645654

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+
646661
// We simply modify last_block_hash in Channel's block_connected so that serialization is
647662
// consistent but hopefully the users' copy handles block_connected in a consistent way.
648663
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -713,7 +728,8 @@ impl PartialEq for ChannelMonitor {
713728
self.to_remote_rescue != other.to_remote_rescue ||
714729
self.pending_claim_requests != other.pending_claim_requests ||
715730
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
717733
{
718734
false
719735
} else {
@@ -770,6 +786,7 @@ impl ChannelMonitor {
770786
claimable_outpoints: HashMap::new(),
771787

772788
onchain_events_waiting_threshold_conf: HashMap::new(),
789+
watch_outputs: HashMap::new(),
773790

774791
last_block_hash: Default::default(),
775792
secp_ctx: Secp256k1::new(),
@@ -1104,6 +1121,12 @@ impl ChannelMonitor {
11041121
}
11051122
}
11061123

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+
11071130
/// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
11081131
/// Generally useful when deserializing as during normal operation the return values of
11091132
/// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
@@ -1332,6 +1355,15 @@ impl ChannelMonitor {
13321355
}
13331356
}
13341357

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+
13351367
Ok(())
13361368
}
13371369

@@ -2551,6 +2583,9 @@ impl ChannelMonitor {
25512583
}
25522584
}
25532585
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+
}
25542589
(watch_outputs, spendable_outputs, htlc_updated)
25552590
}
25562591

@@ -3202,6 +3237,20 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
32023237
onchain_events_waiting_threshold_conf.insert(height_target, events);
32033238
}
32043239

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+
32053254
Ok((last_block_hash.clone(), ChannelMonitor {
32063255
commitment_transaction_number_obscure_factor,
32073256

@@ -3234,6 +3283,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
32343283
claimable_outpoints,
32353284

32363285
onchain_events_waiting_threshold_conf,
3286+
watch_outputs,
32373287

32383288
last_block_hash,
32393289
secp_ctx,

0 commit comments

Comments
 (0)