Bitcoin Core
32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
bench
block_assemble.cpp
Go to the documentation of this file.
1
// Copyright (c) 2011-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/consensus.h
>
7
#include <
node/mining_types.h
>
8
#include <
primitives/transaction.h
>
9
#include <random.h>
10
#include <
script/script.h
>
11
#include <
sync.h
>
12
#include <
test/util/mining.h
>
13
#include <
test/util/script.h
>
14
#include <
test/util/setup_common.h
>
15
#include <
util/check.h
>
16
#include <validation.h>
17
18
#include <array>
19
#include <cstddef>
20
#include <memory>
21
#include <vector>
22
23
using
node::BlockCreateOptions
;
24
25
static
void
AssembleBlock
(
benchmark::Bench
&
bench
)
26
{
27
const
auto
test_setup
=
MakeNoLogFileContext<const TestingSetup>
();
28
29
CScriptWitness
witness;
30
witness.
stack
.push_back(
WITNESS_STACK_ELEM_OP_TRUE
);
31
BlockCreateOptions
options{
32
.
coinbase_output_script
=
P2WSH_OP_TRUE
,
33
};
34
35
// Collect some loose transactions that spend the coinbases of our mined blocks
36
constexpr
size_t
NUM_BLOCKS
{200};
37
std::array<
CTransactionRef
,
NUM_BLOCKS
-
COINBASE_MATURITY
+ 1>
txs
;
38
for
(
size_t
b{0}; b <
NUM_BLOCKS
; ++b) {
39
CMutableTransaction
tx;
40
tx.
vin
.emplace_back(
MineBlock
(
test_setup
->m_node, options));
41
tx.
vin
.back().scriptWitness = witness;
42
tx.
vout
.emplace_back(1337,
P2WSH_OP_TRUE
);
43
if
(
NUM_BLOCKS
- b >=
COINBASE_MATURITY
)
44
txs
.at(b) =
MakeTransactionRef
(tx);
45
}
46
{
47
LOCK
(
::cs_main
);
48
49
for
(
const
auto
&
txr
:
txs
) {
50
const
MempoolAcceptResult
res
=
test_setup
->m_node.chainman->ProcessTransaction(
txr
);
51
assert
(
res
.m_result_type ==
MempoolAcceptResult::ResultType::VALID
);
52
}
53
}
54
55
bench
.run([&] {
56
PrepareBlock
(
test_setup
->m_node, options);
57
});
58
}
59
static
void
BlockAssemblerAddPackageTxns
(
benchmark::Bench
&
bench
)
60
{
61
FastRandomContext
det_rand
{
true
};
62
auto
testing_setup{
MakeNoLogFileContext<TestChain100Setup>
()};
63
testing_setup->PopulateMempool(
det_rand
,
/*num_transactions=*/
1000,
/*submit=*/
true
);
64
65
bench
.run([&] {
66
PrepareBlock
(testing_setup->m_node, {
67
.coinbase_output_script = P2WSH_OP_TRUE,
68
.test_block_validity = false
69
});
70
});
71
}
72
73
BENCHMARK
(
AssembleBlock
);
74
BENCHMARK
(
BlockAssemblerAddPackageTxns
);
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:69
AssembleBlock
static void AssembleBlock(benchmark::Bench &bench)
Definition
block_assemble.cpp:25
BlockAssemblerAddPackageTxns
static void BlockAssemblerAddPackageTxns(benchmark::Bench &bench)
Definition
block_assemble.cpp:59
check.h
FastRandomContext
Fast randomness source.
Definition
random.h:386
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:649
consensus.h
COINBASE_MATURITY
constexpr int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition
consensus.h:19
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition
cs_main.cpp:8
mining_types.h
is used externally by mining IPC clients, so it should only declare simple data definitions.
transaction.h
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition
transaction.h:418
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition
transaction.h:417
script.h
setup_common.h
CMutableTransaction
A mutable version of CTransaction.
Definition
transaction.h:372
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition
transaction.h:374
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition
transaction.h:373
CScriptWitness
Definition
script.h:578
CScriptWitness::stack
std::vector< std::vector< unsigned char > > stack
Definition
script.h:581
MempoolAcceptResult
Validation result for a transaction evaluated by MemPoolAccept (single or package).
Definition
validation.h:134
MempoolAcceptResult::ResultType::VALID
@ VALID
node::BlockCreateOptions
Block template creation options.
Definition
mining_types.h:33
node::BlockCreateOptions::coinbase_output_script
CScript coinbase_output_script
Script to put in the coinbase transaction.
Definition
mining_types.h:86
sync.h
LOCK
#define LOCK(cs)
Definition
sync.h:268
PrepareBlock
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Prepare a block to be mined.
Definition
mining.cpp:158
MineBlock
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
Definition
mining.cpp:121
mining.h
script.h
WITNESS_STACK_ELEM_OP_TRUE
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
Definition
script.h:12
P2WSH_OP_TRUE
static const CScript P2WSH_OP_TRUE
Definition
script.h:13
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:82
assert
assert(!tx.IsCoinBase())
Generated on Thu Oct 22 2026 20:00:28 for Bitcoin Core by
1.9.8