Skip to content

Commit 33c36d0

Browse files
committed
Avoid removing stale preimages when hashes collide in fuzzing
1 parent 6ddf69c commit 33c36d0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,8 +2041,12 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
20412041
// Prune HTLCs from the previous counterparty commitment tx so we don't generate failure/fulfill
20422042
// events for now-revoked/fulfilled HTLCs.
20432043
if let Some(txid) = self.prev_counterparty_commitment_txid.take() {
2044-
for &mut (_, ref mut source) in self.counterparty_claimable_outpoints.get_mut(&txid).unwrap() {
2045-
*source = None;
2044+
if self.current_counterparty_commitment_txid.unwrap() != txid {
2045+
for &mut (_, ref mut source_opt) in self.counterparty_claimable_outpoints.get_mut(&txid).unwrap() {
2046+
*source_opt = None;
2047+
}
2048+
} else {
2049+
assert!(cfg!(fuzzing), "Commitment txids are unique outside of fuzzing, where hashes can collide");
20462050
}
20472051
}
20482052

@@ -4168,10 +4172,10 @@ mod tests {
41684172

41694173
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..10])).unwrap();
41704174
let dummy_txid = dummy_tx.txid();
4171-
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
4172-
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
4173-
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
4174-
monitor.provide_latest_counterparty_commitment_tx(dummy_txid, preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
4175+
monitor.provide_latest_counterparty_commitment_tx(Txid::from_inner(Sha256::hash(b"1").into_inner()),
4176+
preimages_slice_to_htlc_outputs!(preimages[5..15]), 281474976710655, dummy_key, &logger);
4177+
monitor.provide_latest_counterparty_commitment_tx(Txid::from_inner(Sha256::hash(b"2").into_inner()),
4178+
preimages_slice_to_htlc_outputs!(preimages[15..20]), 281474976710654, dummy_key, &logger);
41754179
for &(ref preimage, ref hash) in preimages.iter() {
41764180
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&fee_estimator);
41774181
monitor.provide_payment_preimage(hash, preimage, &broadcaster, &bounded_fee_estimator, &logger);
@@ -4185,13 +4189,19 @@ mod tests {
41854189
test_preimages_exist!(&preimages[0..10], monitor);
41864190
test_preimages_exist!(&preimages[15..20], monitor);
41874191

4192+
monitor.provide_latest_counterparty_commitment_tx(Txid::from_inner(Sha256::hash(b"3").into_inner()),
4193+
preimages_slice_to_htlc_outputs!(preimages[17..20]), 281474976710653, dummy_key, &logger);
4194+
41884195
// Now provide a further secret, pruning preimages 15-17
41894196
secret[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
41904197
monitor.provide_secret(281474976710654, secret.clone()).unwrap();
41914198
assert_eq!(monitor.inner.lock().unwrap().payment_preimages.len(), 13);
41924199
test_preimages_exist!(&preimages[0..10], monitor);
41934200
test_preimages_exist!(&preimages[17..20], monitor);
41944201

4202+
monitor.provide_latest_counterparty_commitment_tx(Txid::from_inner(Sha256::hash(b"4").into_inner()),
4203+
preimages_slice_to_htlc_outputs!(preimages[18..20]), 281474976710652, dummy_key, &logger);
4204+
41954205
// Now update holder commitment tx info, pruning only element 18 as we still care about the
41964206
// previous commitment tx's preimages too
41974207
monitor.provide_latest_holder_commitment_tx(HolderCommitmentTransaction::dummy(), preimages_to_holder_htlcs!(preimages[0..5])).unwrap();

0 commit comments

Comments
 (0)