Skip to content

Commit 26b7bcf

Browse files
committed
Merge bitcoin#28766: Improve peformance of CTransaction::HasWitness (28107 follow-up)
af1d2ff [primitives] Precompute result of CTransaction::HasWitness (dergoegge) Pull request description: This addresses bitcoin#28107 (comment) from bitcoin#28107. ACKs for top commit: theStack: ACK af1d2ff achow101: ACK af1d2ff stickies-v: ACK af1d2ff TheCharlatan: ACK af1d2ff Tree-SHA512: a77654ae429d0d7ce12daa309770e75beec4f8984734f80ed203156199425af43b50ad3d8aab85a89371a71356464ebd4503a0248fd0103579adfc74a55aaf51
2 parents c252a0f + af1d2ff commit 26b7bcf

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/primitives/transaction.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <util/strencodings.h>
1515
#include <util/transaction_identifier.h>
1616

17+
#include <algorithm>
1718
#include <cassert>
1819
#include <stdexcept>
1920

@@ -70,6 +71,13 @@ Txid CMutableTransaction::GetHash() const
7071
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
7172
}
7273

74+
bool CTransaction::ComputeHasWitness() const
75+
{
76+
return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
77+
return !input.scriptWitness.IsNull();
78+
});
79+
}
80+
7381
Txid CTransaction::ComputeHash() const
7482
{
7583
return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
@@ -84,8 +92,8 @@ Wtxid CTransaction::ComputeWitnessHash() const
8492
return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
8593
}
8694

87-
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
88-
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()} {}
95+
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()} {}
96+
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()} {}
8997

9098
CAmount CTransaction::GetValueOut() const
9199
{

src/primitives/transaction.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,15 @@ class CTransaction
310310

311311
private:
312312
/** Memory only. */
313+
const bool m_has_witness;
313314
const Txid hash;
314315
const Wtxid m_witness_hash;
315316

316317
Txid ComputeHash() const;
317318
Wtxid ComputeWitnessHash() const;
318319

320+
bool ComputeHasWitness() const;
321+
319322
public:
320323
/** Convert a CMutableTransaction into a CTransaction. */
321324
explicit CTransaction(const CMutableTransaction& tx);
@@ -367,15 +370,7 @@ class CTransaction
367370

368371
std::string ToString() const;
369372

370-
bool HasWitness() const
371-
{
372-
for (size_t i = 0; i < vin.size(); i++) {
373-
if (!vin[i].scriptWitness.IsNull()) {
374-
return true;
375-
}
376-
}
377-
return false;
378-
}
373+
bool HasWitness() const { return m_has_witness; }
379374
};
380375

381376
/** A mutable version of CTransaction. */

0 commit comments

Comments
 (0)