Merge bitcoin/bitcoin#28766: Improve peformance of CTransaction::HasWitness (28107 follow-up)

af1d2ff883 [primitives] Precompute result of CTransaction::HasWitness (dergoegge)

Pull request description:

  This addresses https://github.com/bitcoin/bitcoin/pull/28107#discussion_r1364961590 from #28107.

ACKs for top commit:
  theStack:
    ACK af1d2ff883
  achow101:
    ACK af1d2ff883
  stickies-v:
    ACK af1d2ff883
  TheCharlatan:
    ACK af1d2ff883

Tree-SHA512: a77654ae429d0d7ce12daa309770e75beec4f8984734f80ed203156199425af43b50ad3d8aab85a89371a71356464ebd4503a0248fd0103579adfc74a55aaf51
This commit is contained in:
Andrew Chow 2023-11-28 08:31:26 -05:00
commit 26b7bcf10e
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41
2 changed files with 14 additions and 11 deletions

View File

@ -14,6 +14,7 @@
#include <util/strencodings.h>
#include <util/transaction_identifier.h>
#include <algorithm>
#include <cassert>
#include <stdexcept>
@ -70,6 +71,13 @@ Txid CMutableTransaction::GetHash() const
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
}
bool CTransaction::ComputeHasWitness() const
{
return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
return !input.scriptWitness.IsNull();
});
}
Txid CTransaction::ComputeHash() const
{
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
@ -84,8 +92,8 @@ Wtxid CTransaction::ComputeWitnessHash() const
return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
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()} {}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CAmount CTransaction::GetValueOut() const
{

View File

@ -310,12 +310,15 @@ public:
private:
/** Memory only. */
const bool m_has_witness;
const Txid hash;
const Wtxid m_witness_hash;
Txid ComputeHash() const;
Wtxid ComputeWitnessHash() const;
bool ComputeHasWitness() const;
public:
/** Convert a CMutableTransaction into a CTransaction. */
explicit CTransaction(const CMutableTransaction& tx);
@ -367,15 +370,7 @@ public:
std::string ToString() const;
bool HasWitness() const
{
for (size_t i = 0; i < vin.size(); i++) {
if (!vin[i].scriptWitness.IsNull()) {
return true;
}
}
return false;
}
bool HasWitness() const { return m_has_witness; }
};
/** A mutable version of CTransaction. */