Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
merkle_root.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <consensus/merkle.h>
7#include <random.h>
8#include <uint256.h>
9#include <util/check.h>
10
11#include <utility>
12#include <vector>
13
14static void MerkleRoot(benchmark::Bench& bench)
15{
16 FastRandomContext rng{/*fDeterministic=*/true};
17
18 std::vector<uint256> hashes{};
19 hashes.resize(9001);
20 for (auto& item : hashes) {
21 item = rng.rand256();
22 }
23
24 constexpr uint256 expected_root{"d8d4dfd014a533bc3941b8663fa6e7f3a8707af124f713164d75b0c3179ecb08"};
25 for (bool mutate : {false, true}) {
26 bench.name(mutate ? "MerkleRootWithMutation" : "MerkleRoot").batch(hashes.size()).unit("leaf").run([&] {
27 std::vector<uint256> leaves;
28 leaves.reserve((hashes.size() + 1) & ~1ULL); // capacity rounded up to even
29 for (const auto& hash : hashes) {
30 leaves.push_back(hash);
31 }
32
33 bool mutated{false};
34 const uint256 root{ComputeMerkleRoot(std::move(leaves), mutate ? &mutated : nullptr)};
35 assert(root == expected_root);
36 });
37 }
38}
39
#define BENCHMARK(n)
Definition bench.h:69
Fast randomness source.
Definition random.h:386
uint256 rand256() noexcept
generate a random uint256.
Definition random.h:317
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition nanobench.h:1308
ANKERL_NANOBENCH(NODISCARD) std Bench & name(char const *benchmarkName)
Gets the title of the benchmark.
Bench & batch(T b) noexcept
Sets the batch size.
Definition nanobench.h:1332
Bench & unit(char const *unit)
Sets the operation unit.
256-bit opaque blob.
Definition uint256.h:196
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Compute a Merkle root from the provided leaf hashes.
Definition merkle.cpp:51
static void MerkleRoot(benchmark::Bench &bench)
FastRandomContext rng
assert(!tx.IsCoinBase())