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/strencodings.h>
|
||||||
#include <util/transaction_identifier.h>
|
#include <util/transaction_identifier.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
@ -70,6 +71,13 @@ Txid CMutableTransaction::GetHash() const
|
|||||||
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
|
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
|
Txid CTransaction::ComputeHash() const
|
||||||
{
|
{
|
||||||
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
|
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());
|
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(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), 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
|
CAmount CTransaction::GetValueOut() const
|
||||||
{
|
{
|
||||||
|
@ -310,12 +310,15 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
/** Memory only. */
|
/** Memory only. */
|
||||||
|
const bool m_has_witness;
|
||||||
const Txid hash;
|
const Txid hash;
|
||||||
const Wtxid m_witness_hash;
|
const Wtxid m_witness_hash;
|
||||||
|
|
||||||
Txid ComputeHash() const;
|
Txid ComputeHash() const;
|
||||||
Wtxid ComputeWitnessHash() const;
|
Wtxid ComputeWitnessHash() const;
|
||||||
|
|
||||||
|
bool ComputeHasWitness() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Convert a CMutableTransaction into a CTransaction. */
|
/** Convert a CMutableTransaction into a CTransaction. */
|
||||||
explicit CTransaction(const CMutableTransaction& tx);
|
explicit CTransaction(const CMutableTransaction& tx);
|
||||||
@ -367,15 +370,7 @@ public:
|
|||||||
|
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
|
|
||||||
bool HasWitness() const
|
bool HasWitness() const { return m_has_witness; }
|
||||||
{
|
|
||||||
for (size_t i = 0; i < vin.size(); i++) {
|
|
||||||
if (!vin[i].scriptWitness.IsNull()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A mutable version of CTransaction. */
|
/** A mutable version of CTransaction. */
|
||||||
|
Loading…
Reference in New Issue
Block a user