mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-27 09:21:43 +02:00
[primitives] Precompute result of CTransaction::HasWitness
This commit is contained in:
parent
d51fb9caa6
commit
af1d2ff883
@ -15,6 +15,7 @@
|
|||||||
#include <util/transaction_identifier.h>
|
#include <util/transaction_identifier.h>
|
||||||
#include <version.h>
|
#include <version.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
@ -71,6 +72,13 @@ Txid CMutableTransaction::GetHash() const
|
|||||||
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
|
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_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((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
|
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
|
||||||
@ -85,8 +93,8 @@ Wtxid CTransaction::ComputeWitnessHash() const
|
|||||||
return Wtxid::FromUint256((CHashWriter{0} << *this).GetHash());
|
return Wtxid::FromUint256((CHashWriter{0} << *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);
|
||||||
@ -365,15 +368,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…
x
Reference in New Issue
Block a user