Skip to content

Commit 8566486

Browse files
authored
Merge pull request #722 from TheBlueMatt/2020-09-broken-fn
Drop the redundant/broken `ChannelMonitor::get_monitored_outpoints`
2 parents 45a558b + 8e99800 commit 8566486

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

lightning/src/chain/channelmonitor.rs

+13-32
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
631631
/// spending. Thus, in order to claim them via revocation key, we track all the counterparty
632632
/// commitment transactions which we find on-chain, mapping them to the commitment number which
633633
/// can be used to derive the revocation key and claim the transactions.
634-
counterparty_commitment_txn_on_chain: HashMap<Txid, (u64, Vec<Script>)>,
634+
counterparty_commitment_txn_on_chain: HashMap<Txid, u64>,
635635
/// Cache used to make pruning of payment_preimages faster.
636636
/// Maps payment_hash values to commitment numbers for counterparty transactions for non-revoked
637637
/// counterparty transactions (ie should remain pretty small).
@@ -824,13 +824,9 @@ impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
824824
}
825825

826826
writer.write_all(&byte_utils::be64_to_array(self.counterparty_commitment_txn_on_chain.len() as u64))?;
827-
for (ref txid, &(commitment_number, ref txouts)) in self.counterparty_commitment_txn_on_chain.iter() {
827+
for (ref txid, commitment_number) in self.counterparty_commitment_txn_on_chain.iter() {
828828
writer.write_all(&txid[..])?;
829-
writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
830-
(txouts.len() as u64).write(writer)?;
831-
for script in txouts.iter() {
832-
script.write(writer)?;
833-
}
829+
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
834830
}
835831

836832
writer.write_all(&byte_utils::be64_to_array(self.counterparty_hash_commitment_number.len() as u64))?;
@@ -1214,23 +1210,13 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
12141210
///
12151211
/// (C-not exported) because we have no HashMap bindings
12161212
pub fn get_outputs_to_watch(&self) -> &HashMap<Txid, Vec<Script>> {
1217-
&self.outputs_to_watch
1218-
}
1219-
1220-
/// Gets the sets of all outpoints which this ChannelMonitor expects to hear about spends of.
1221-
/// Generally useful when deserializing as during normal operation the return values of
1222-
/// block_connected are sufficient to ensure all relevant outpoints are being monitored (note
1223-
/// that the get_funding_txo outpoint and transaction must also be monitored for!).
1224-
///
1225-
/// (C-not exported) as there is no practical way to track lifetimes of returned values.
1226-
pub fn get_monitored_outpoints(&self) -> Vec<(Txid, u32, &Script)> {
1227-
let mut res = Vec::with_capacity(self.counterparty_commitment_txn_on_chain.len() * 2);
1228-
for (ref txid, &(_, ref outputs)) in self.counterparty_commitment_txn_on_chain.iter() {
1229-
for (idx, output) in outputs.iter().enumerate() {
1230-
res.push(((*txid).clone(), idx as u32, output));
1231-
}
1213+
// If we've detected a counterparty commitment tx on chain, we must include it in the set
1214+
// of outputs to watch for spends of, otherwise we're likely to lose user funds. Because
1215+
// its trivial to do, double-check that here.
1216+
for (txid, _) in self.counterparty_commitment_txn_on_chain.iter() {
1217+
self.outputs_to_watch.get(txid).expect("Counterparty commitment txn which have been broadcast should have outputs registered");
12321218
}
1233-
res
1219+
&self.outputs_to_watch
12341220
}
12351221

12361222
/// Get the list of HTLCs who's status has been updated on chain. This should be called by
@@ -1334,7 +1320,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13341320
// We're definitely a counterparty commitment transaction!
13351321
log_trace!(logger, "Got broadcast of revoked counterparty commitment transaction, going to generate general spend tx with {} inputs", claimable_outpoints.len());
13361322
watch_outputs.append(&mut tx.output.clone());
1337-
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
1323+
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
13381324

13391325
macro_rules! check_htlc_fails {
13401326
($txid: expr, $commitment_tx: expr) => {
@@ -1381,7 +1367,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
13811367
// not being generated by the above conditional. Thus, to be safe, we go ahead and
13821368
// insert it here.
13831369
watch_outputs.append(&mut tx.output.clone());
1384-
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, (commitment_number, tx.output.iter().map(|output| { output.script_pubkey.clone() }).collect()));
1370+
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
13851371

13861372
log_trace!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
13871373

@@ -1719,7 +1705,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17191705
claimable_outpoints.append(&mut new_outpoints);
17201706
}
17211707
} else {
1722-
if let Some(&(commitment_number, _)) = self.counterparty_commitment_txn_on_chain.get(&prevout.txid) {
1708+
if let Some(&commitment_number) = self.counterparty_commitment_txn_on_chain.get(&prevout.txid) {
17231709
let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(&tx, commitment_number, height, &logger);
17241710
claimable_outpoints.append(&mut new_outpoints);
17251711
if let Some(new_outputs) = new_outputs_option {
@@ -2211,12 +2197,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor
22112197
for _ in 0..counterparty_commitment_txn_on_chain_len {
22122198
let txid: Txid = Readable::read(reader)?;
22132199
let commitment_number = <U48 as Readable>::read(reader)?.0;
2214-
let outputs_count = <u64 as Readable>::read(reader)?;
2215-
let mut outputs = Vec::with_capacity(cmp::min(outputs_count as usize, MAX_ALLOC_SIZE / 8));
2216-
for _ in 0..outputs_count {
2217-
outputs.push(Readable::read(reader)?);
2218-
}
2219-
if let Some(_) = counterparty_commitment_txn_on_chain.insert(txid, (commitment_number, outputs)) {
2200+
if let Some(_) = counterparty_commitment_txn_on_chain.insert(txid, commitment_number) {
22202201
return Err(DecodeError::InvalidValue);
22212202
}
22222203
}

lightning/src/ln/channelmanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3730,7 +3730,7 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
37303730
/// This may result in closing some Channels if the ChannelMonitor is newer than the stored
37313731
/// ChannelManager state to ensure no loss of funds. Thus, transactions may be broadcasted.
37323732
/// 3) Register all relevant ChannelMonitor outpoints with your chain watch mechanism using
3733-
/// ChannelMonitor::get_monitored_outpoints and ChannelMonitor::get_funding_txo().
3733+
/// ChannelMonitor::get_outputs_to_watch() and ChannelMonitor::get_funding_txo().
37343734
/// 4) Reconnect blocks on your ChannelMonitors.
37353735
/// 5) Move the ChannelMonitors into your local chain::Watch.
37363736
/// 6) Disconnect/connect blocks on the ChannelManager.

0 commit comments

Comments
 (0)