Skip to content

Commit fac1223

Browse files
author
MarcoFalke
committed
Cache witness hash in CTransaction
1 parent faab55f commit fac1223

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/primitives/transaction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ uint256 CTransaction::ComputeHash() const
6767
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
6868
}
6969

70-
uint256 CTransaction::GetWitnessHash() const
70+
uint256 CTransaction::ComputeWitnessHash() const
7171
{
7272
if (!HasWitness()) {
73-
return GetHash();
73+
return hash;
7474
}
7575
return SerializeHash(*this, SER_GETHASH, 0);
7676
}
7777

7878
/* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
79-
CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash() {}
80-
CTransaction::CTransaction(const CMutableTransaction &tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
81-
CTransaction::CTransaction(CMutableTransaction &&tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
79+
CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
80+
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
81+
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
8282

8383
CAmount CTransaction::GetValueOut() const
8484
{

src/primitives/transaction.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ class CTransaction
286286
private:
287287
/** Memory only. */
288288
const uint256 hash;
289+
const uint256 m_witness_hash;
289290

290291
uint256 ComputeHash() const;
292+
uint256 ComputeWitnessHash() const;
291293

292294
public:
293295
/** Construct a CTransaction that qualifies as IsNull() */
@@ -311,12 +313,8 @@ class CTransaction
311313
return vin.empty() && vout.empty();
312314
}
313315

314-
const uint256& GetHash() const {
315-
return hash;
316-
}
317-
318-
// Compute a hash that includes both transaction and witness data
319-
uint256 GetWitnessHash() const;
316+
const uint256& GetHash() const { return hash; }
317+
const uint256& GetWitnessHash() const { return m_witness_hash; };
320318

321319
// Return sum of txouts.
322320
CAmount GetValueOut() const;

0 commit comments

Comments
 (0)