Skip to content

Commit fa33592

Browse files
committed
Merge bitcoin/bitcoin#32723: Refactor: Redefine CTransaction equality to include witness data
6efbd1e refactor: CTransaction equality should consider witness data (Cory Fields) cbf9b2d mempool: codify existing assumption about duplicate txids during removal (Cory Fields) e9331cd wallet: IsEquivalentTo should strip witness data in addition to scriptsigs (Cory Fields) Pull request description: I stumbled upon the `CTransaction` comparison operators while refactoring some nearby code. I found it surprising and not at all obvious that two transactions would test equal even if their witness data differed. It seems like an unnecessary potential footgun. Fix that by comparing against wtxid rather than txid. Outside of tests, there were only 3 users of these functions in the code-base: - Its use in the mempool has been replaced with an explicit txid comparison, as that's a tighter constraint and matches the old behavior. glozow suggested also upgrading this to an `Assume()`. - Its use in the wallet was accidentally doing the correct thing by ignoring witness data. I've changed that to an explicit witness removal so that `IsEquivalentTo` continues to work as-intended. - Its use in `getrawtransaction` is indifferent to the change. ACKs for top commit: maflcko: review ACK 6efbd1e 🦋 achow101: ACK 6efbd1e glozow: ACK 6efbd1e Tree-SHA512: 89be424889f49e7e26dd2bdab7fbc8b2def59bf002ae8b94989b349ce97245f007d6c96e409a626cbf0de9df83ae2485b4815b40a70f7aa5b6c720eb34a6c017
2 parents ce000c8 + 6efbd1e commit fa33592

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/primitives/transaction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ class CTransaction
360360

361361
friend bool operator==(const CTransaction& a, const CTransaction& b)
362362
{
363-
return a.hash == b.hash;
363+
return a.GetWitnessHash() == b.GetWitnessHash();
364364
}
365365

366366
friend bool operator!=(const CTransaction& a, const CTransaction& b)
367367
{
368-
return a.hash != b.hash;
368+
return !operator==(a, b);
369369
}
370370

371371
std::string ToString() const;

src/txmempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ void CTxMemPool::removeConflicts(const CTransaction &tx)
652652
auto it = mapNextTx.find(txin.prevout);
653653
if (it != mapNextTx.end()) {
654654
const CTransaction &txConflict = *it->second;
655-
if (txConflict != tx)
655+
if (Assume(txConflict.GetHash() != tx.GetHash()))
656656
{
657657
ClearPrioritisation(txConflict.GetHash());
658658
removeRecursive(txConflict, MemPoolRemovalReason::CONFLICT);

src/wallet/transaction.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
1313
{
1414
CMutableTransaction tx1 {*this->tx};
1515
CMutableTransaction tx2 {*_tx.tx};
16-
for (auto& txin : tx1.vin) txin.scriptSig = CScript();
17-
for (auto& txin : tx2.vin) txin.scriptSig = CScript();
16+
for (auto& txin : tx1.vin) {
17+
txin.scriptSig = CScript();
18+
txin.scriptWitness.SetNull();
19+
}
20+
for (auto& txin : tx2.vin) {
21+
txin.scriptSig = CScript();
22+
txin.scriptWitness.SetNull();
23+
}
1824
return CTransaction(tx1) == CTransaction(tx2);
1925
}
2026

0 commit comments

Comments
 (0)