Bitcoin ABC 0.32.7
P2P Digital Currency
merkle_root.cpp
Go to the documentation of this file.
1// Copyright (c) 2016 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
7#include <consensus/merkle.h>
8#include <random.h>
9#include <uint256.h>
10
11#include <cassert>
12#include <vector>
13
14static void MerkleRoot(benchmark::Bench &bench) {
15 FastRandomContext rng{/*fDeterministic=*/true};
16
17 std::vector<uint256> hashes{};
18 hashes.resize(9001);
19 for (auto &item : hashes) {
20 item = rng.rand256();
21 }
22
23 const uint256 expected_root{uint256S(
24 "d8d4dfd014a533bc3941b8663fa6e7f3a8707af124f713164d75b0c3179ecb08")};
25 for (bool mutate : {false, true}) {
26 bench.name(mutate ? "MerkleRootWithMutation" : "MerkleRoot")
27 .batch(hashes.size())
28 .unit("leaf")
29 .run([&] {
30 std::vector<uint256> leaves;
31 leaves.reserve((hashes.size() + 1) &
32 ~1ULL); // capacity rounded up to even
33 for (size_t s = 0; s < hashes.size(); s++) {
34 leaves.push_back(hashes[s]);
35 }
36
37 bool mutated{false};
38 const uint256 root{ComputeMerkleRoot(
39 std::move(leaves), mutate ? &mutated : nullptr)};
40 assert(root == expected_root);
41 });
42 }
43}
44
Fast randomness source.
Definition: random.h:411
uint256 rand256() noexcept
generate a random uint256.
Definition: random.h:328
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
ANKERL_NANOBENCH(NODISCARD) std Bench & name(char const *benchmarkName)
Name of the benchmark, will be shown in the table row.
Bench & unit(char const *unit)
Sets the operation unit.
256-bit opaque blob.
Definition: uint256.h:129
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Definition: merkle.cpp:44
static void MerkleRoot(benchmark::Bench &bench)
Definition: merkle_root.cpp:14
BENCHMARK(MerkleRoot)
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
for(const CTxIn &txin :tx.vin)
Definition: validation.cpp:256
assert(!tx.IsCoinBase())