Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#28766: Improve peformance of CTransaction::HasW…
Browse files Browse the repository at this point in the history
…itness (28107 follow-up)

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

Pull request description:

  This addresses bitcoin/bitcoin#28107 (comment) from #28107.

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

Tree-SHA512: a77654ae429d0d7ce12daa309770e75beec4f8984734f80ed203156199425af43b50ad3d8aab85a89371a71356464ebd4503a0248fd0103579adfc74a55aaf51
achow101 committed Nov 28, 2023

Unverified

This user has not yet uploaded their public signing key.
2 parents c252a0f + af1d2ff commit 26b7bcf
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
@@ -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
{
13 changes: 4 additions & 9 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
@@ -310,12 +310,15 @@ class CTransaction

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 @@ class CTransaction

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. */

0 comments on commit 26b7bcf

Please sign in to comment.