diff options
author | Andrew Chow <github@achow101.com> | 2023-11-28 08:31:26 -0500 |
---|---|---|
committer | Andrew Chow <github@achow101.com> | 2023-11-28 08:44:41 -0500 |
commit | 26b7bcf10ebcc055cf6226629498a09986ce0e6c (patch) | |
tree | d682cdf1238bd65e02b1f33b67bf062700799b32 /src/primitives | |
parent | c252a0fc0f4dc7d262b971a5e7ff01508159193b (diff) | |
parent | af1d2ff88344e1545ac8d9ad09f8e37e264da712 (diff) | |
download | bitcoin-26b7bcf10ebcc055cf6226629498a09986ce0e6c.tar.xz |
Merge bitcoin/bitcoin#28766: Improve peformance of CTransaction::HasWitness (28107 follow-up)
af1d2ff88344e1545ac8d9ad09f8e37e264da712 [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 af1d2ff88344e1545ac8d9ad09f8e37e264da712
achow101:
ACK af1d2ff88344e1545ac8d9ad09f8e37e264da712
stickies-v:
ACK af1d2ff88344e1545ac8d9ad09f8e37e264da712
TheCharlatan:
ACK af1d2ff88344e1545ac8d9ad09f8e37e264da712
Tree-SHA512: a77654ae429d0d7ce12daa309770e75beec4f8984734f80ed203156199425af43b50ad3d8aab85a89371a71356464ebd4503a0248fd0103579adfc74a55aaf51
Diffstat (limited to 'src/primitives')
-rw-r--r-- | src/primitives/transaction.cpp | 12 | ||||
-rw-r--r-- | src/primitives/transaction.h | 13 |
2 files changed, 14 insertions, 11 deletions
diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 002c922c0d..b4a860dd9e 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -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 { diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index e0c0c860c6..ccbeb3ec49 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -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. */ |