mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-02 17:56:16 +00:00
`ComputeMerkleRoot` duplicates the last hash when the input size is odd. If the caller provides a `std::vector` whose capacity equals its size, that extra `push_back` forces a reallocation, doubling its capacity (allocating 3x the necessary memory). This affects roughly half of the created blocks (those with odd transaction counts), causing unnecessary memory fragmentation during every block validation. Fix this by pre-reserving the vector capacity to account for the odd-count duplication. The expression `(size + 1) & ~1ULL` adds 1 to the size and clears the last bit, effectively rounding up to the next even number. This syntax produces optimal assembly across x86/ARM and 32/64-bit platforms for gcc/clang, see https://godbolt.org/z/xzscoq7nv. Also switch from `resize` to `reserve` + `push_back` to eliminate the default construction of `uint256` objects that are immediately overwritten. > ./build/bin/bench_bitcoin -filter='MerkleRoot.*' -min-time=1000 | ns/leaf | leaf/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 43.73 | 22,867,350.51 | 0.0% | 1.10 | `MerkleRoot` | 44.17 | 22,640,349.14 | 0.0% | 1.10 | `MerkleRootWithMutation` Massif memory measurements after show 0.8 MB peak memory usage KB 801.4^ # | # | # | # | # | # | # | # :::::@:::::@: | #:::@@@::@:::::::::::::::@::@:@:::@@:::::::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: | #:::@ @: @:::::::::::::::@::@:@:::@ :::: ::::@::::::@:::::@::::@:::::@: 0 +----------------------------------------------------------------------->s 0 227.5 and the stacks don't show reallocs anymore: 96.37% (790,809B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. ->35.10% (288,064B) 0x2234AF: allocate (new_allocator.h:151) | ->35.10% (288,064B) 0x2234AF: allocate (allocator.h:203) | ->35.10% (288,064B) 0x2234AF: allocate (alloc_traits.h:614) | ->35.10% (288,064B) 0x2234AF: _M_allocate (stl_vector.h:387) | ->35.10% (288,064B) 0x2234AF: reserve (vector.tcc:79) | ->35.10% (288,064B) 0x2234AF: ToMerkleLeaves<std::vector<uint256>, MerkleRoot(ankerl::nanobench::Bench&)::<lambda()>::<lambda(bool, const auto:46&)> > (merkle.h:19) | ->35.10% (288,064B) 0x2234AF: operator() (merkle_root.cpp:25) | ->35.10% (288,064B) 0x2234AF: ankerl::nanobench::Bench& ankerl::nanobench::Bench::run<MerkleRoot(ankerl::nanobench::Bench&)::{lambda() Co-authored-by: optout21 <13562139+optout21@users.noreply.github.com> Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
153 lines
5.7 KiB
C++
153 lines
5.7 KiB
C++
// Copyright (c) 2019-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <signet.h>
|
|
|
|
#include <common/system.h>
|
|
#include <consensus/merkle.h>
|
|
#include <consensus/params.h>
|
|
#include <consensus/validation.h>
|
|
#include <core_io.h>
|
|
#include <hash.h>
|
|
#include <logging.h>
|
|
#include <primitives/block.h>
|
|
#include <primitives/transaction.h>
|
|
#include <script/interpreter.h>
|
|
#include <span.h>
|
|
#include <streams.h>
|
|
#include <uint256.h>
|
|
#include <util/strencodings.h>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
|
|
|
|
static constexpr script_verify_flags BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;
|
|
|
|
static bool FetchAndClearCommitmentSection(const std::span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)
|
|
{
|
|
CScript replacement;
|
|
bool found_header = false;
|
|
result.clear();
|
|
|
|
opcodetype opcode;
|
|
CScript::const_iterator pc = witness_commitment.begin();
|
|
std::vector<uint8_t> pushdata;
|
|
while (witness_commitment.GetOp(pc, opcode, pushdata)) {
|
|
if (pushdata.size() > 0) {
|
|
if (!found_header && pushdata.size() > header.size() && std::ranges::equal(std::span{pushdata}.first(header.size()), header)) {
|
|
// pushdata only counts if it has the header _and_ some data
|
|
result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
|
|
pushdata.erase(pushdata.begin() + header.size(), pushdata.end());
|
|
found_header = true;
|
|
}
|
|
replacement << pushdata;
|
|
} else {
|
|
replacement << opcode;
|
|
}
|
|
}
|
|
|
|
if (found_header) witness_commitment = replacement;
|
|
return found_header;
|
|
}
|
|
|
|
static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block)
|
|
{
|
|
std::vector<uint256> leaves;
|
|
leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even
|
|
leaves.push_back(cb.GetHash().ToUint256());
|
|
for (size_t s = 1; s < block.vtx.size(); ++s) {
|
|
leaves.push_back(block.vtx[s]->GetHash().ToUint256());
|
|
}
|
|
return ComputeMerkleRoot(std::move(leaves));
|
|
}
|
|
|
|
std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
|
|
{
|
|
CMutableTransaction tx_to_spend;
|
|
tx_to_spend.version = 0;
|
|
tx_to_spend.nLockTime = 0;
|
|
tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);
|
|
tx_to_spend.vout.emplace_back(0, challenge);
|
|
|
|
CMutableTransaction tx_spending;
|
|
tx_spending.version = 0;
|
|
tx_spending.nLockTime = 0;
|
|
tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);
|
|
tx_spending.vout.emplace_back(0, CScript(OP_RETURN));
|
|
|
|
// can't fill any other fields before extracting signet
|
|
// responses from block coinbase tx
|
|
|
|
// find and delete signet signature
|
|
if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid
|
|
CMutableTransaction modified_cb(*block.vtx.at(0));
|
|
|
|
const int cidx = GetWitnessCommitmentIndex(block);
|
|
if (cidx == NO_WITNESS_COMMITMENT) {
|
|
return std::nullopt; // require a witness commitment
|
|
}
|
|
|
|
CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
|
|
|
|
std::vector<uint8_t> signet_solution;
|
|
if (!FetchAndClearCommitmentSection(SIGNET_HEADER, witness_commitment, signet_solution)) {
|
|
// no signet solution -- allow this to support OP_TRUE as trivial block challenge
|
|
} else {
|
|
try {
|
|
SpanReader v{signet_solution};
|
|
v >> tx_spending.vin[0].scriptSig;
|
|
v >> tx_spending.vin[0].scriptWitness.stack;
|
|
if (!v.empty()) return std::nullopt; // extraneous data encountered
|
|
} catch (const std::exception&) {
|
|
return std::nullopt; // parsing error
|
|
}
|
|
}
|
|
uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
|
|
|
|
std::vector<uint8_t> block_data;
|
|
VectorWriter writer{block_data, 0};
|
|
writer << block.nVersion;
|
|
writer << block.hashPrevBlock;
|
|
writer << signet_merkle;
|
|
writer << block.nTime;
|
|
tx_to_spend.vin[0].scriptSig << block_data;
|
|
tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
|
|
|
|
return SignetTxs{tx_to_spend, tx_spending};
|
|
}
|
|
|
|
// Signet block solution checker
|
|
bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)
|
|
{
|
|
if (block.GetHash() == consensusParams.hashGenesisBlock) {
|
|
// genesis block solution is always valid
|
|
return true;
|
|
}
|
|
|
|
const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
|
|
const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
|
|
|
|
if (!signet_txs) {
|
|
LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
|
|
return false;
|
|
}
|
|
|
|
const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
|
|
const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
|
|
|
|
PrecomputedTransactionData txdata;
|
|
txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});
|
|
TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);
|
|
|
|
if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
|
|
LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|