Skip to content

Commit 324edf1

Browse files
author
Antoine Riard
committed
Assert on correct registeration of outputs index
1 parent 613ac6e commit 324edf1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,20 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17721772
let idx_and_scripts = txouts.iter().map(|o| (o.0, o.1.script_pubkey.clone())).collect();
17731773
self.outputs_to_watch.insert(txid.clone(), idx_and_scripts).is_none()
17741774
});
1775+
#[cfg(test)]
1776+
{
1777+
// If we see a transaction for which we registered outputs previously,
1778+
// make sure the registered scriptpubkey at the expected index match
1779+
// the actual transaction output one. We failed this case before #653.
1780+
for tx in &txn_matched {
1781+
if let Some(outputs) = self.get_outputs_to_watch().get(&tx.txid()) {
1782+
for idx_and_script in outputs.iter() {
1783+
assert!((idx_and_script.0 as usize) < tx.output.len());
1784+
assert_eq!(tx.output[idx_and_script.0 as usize].script_pubkey, idx_and_script.1);
1785+
}
1786+
}
1787+
}
1788+
}
17751789
watch_outputs
17761790
}
17771791

@@ -1821,6 +1835,21 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18211835
if let Some(outputs) = self.get_outputs_to_watch().get(&input.previous_output.txid) {
18221836
for (idx, _script_pubkey) in outputs.iter() {
18231837
if *idx == input.previous_output.vout {
1838+
#[cfg(test)]
1839+
{
1840+
// If the witness is empty this transaction is a dummy one expressely
1841+
// passed to test parsing code robustness. Return true to test downstream
1842+
// monitoring code.
1843+
if input.witness.last().is_none() { return true; }
1844+
// If the expected script is a known type, check that the witness
1845+
// appears to be spending the correct type (ie that the match would
1846+
// actually succeed in BIP 158/159-style filters).
1847+
if _script_pubkey.is_v0_p2wsh() {
1848+
assert_eq!(&bitcoin::Address::p2wsh(&Script::from(input.witness.last().unwrap().clone()), bitcoin::Network::Bitcoin).script_pubkey(), _script_pubkey);
1849+
} else if _script_pubkey.is_v0_p2wpkh() {
1850+
assert_eq!(&bitcoin::Address::p2wpkh(&bitcoin::PublicKey::from_slice(&input.witness.last().unwrap()).unwrap(), bitcoin::Network::Bitcoin).unwrap().script_pubkey(), _script_pubkey);
1851+
} else { panic!(); }
1852+
}
18241853
return true;
18251854
}
18261855
}

0 commit comments

Comments
 (0)