mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
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: ACKaf1d2ff883
achow101: ACKaf1d2ff883
stickies-v: ACKaf1d2ff883
TheCharlatan: ACKaf1d2ff883
Tree-SHA512: a77654ae429d0d7ce12daa309770e75beec4f8984734f80ed203156199425af43b50ad3d8aab85a89371a71356464ebd4503a0248fd0103579adfc74a55aaf51
This commit is contained in:
commit
26b7bcf10e
@ -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
|
||||
{
|
||||
|
@ -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. */
|
||||
|
Loading…
Reference in New Issue
Block a user