Bitcoin Core 28.99.0
P2P Digital Currency
mini_miner.cpp
Go to the documentation of this file.
1// Copyright (c) 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
6#include <test/fuzz/fuzz.h>
7#include <test/fuzz/util.h>
9#include <test/util/script.h>
11#include <test/util/txmempool.h>
12#include <test/util/mining.h>
13
14#include <node/miner.h>
15#include <node/mini_miner.h>
17#include <random.h>
18#include <txmempool.h>
19#include <util/check.h>
20#include <util/time.h>
21#include <util/translation.h>
22
23#include <deque>
24#include <vector>
25
26namespace {
27
28const TestingSetup* g_setup;
29std::deque<COutPoint> g_available_coins;
30void initialize_miner()
31{
32 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
33 g_setup = testing_setup.get();
34 for (uint32_t i = 0; i < uint32_t{100}; ++i) {
35 g_available_coins.emplace_back(Txid::FromUint256(uint256::ZERO), i);
36 }
37}
38
39// Test that the MiniMiner can run with various outpoints and feerates.
40FUZZ_TARGET(mini_miner, .init = initialize_miner)
41{
43 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
44 SetMockTime(ConsumeTime(fuzzed_data_provider));
45 bilingual_str error;
46 CTxMemPool pool{CTxMemPool::Options{}, error};
47 Assert(error.empty());
48 std::vector<COutPoint> outpoints;
49 std::deque<COutPoint> available_coins = g_available_coins;
50 LOCK2(::cs_main, pool.cs);
51 // Cluster size cannot exceed 500
52 LIMITED_WHILE(!available_coins.empty(), 500)
53 {
55 const size_t num_inputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, available_coins.size());
56 const size_t num_outputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
57 for (size_t n{0}; n < num_inputs; ++n) {
58 auto prevout = available_coins.front();
59 mtx.vin.emplace_back(prevout, CScript());
60 available_coins.pop_front();
61 }
62 for (uint32_t n{0}; n < num_outputs; ++n) {
63 mtx.vout.emplace_back(100, P2WSH_OP_TRUE);
64 }
67 const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
69 AddToMempool(pool, entry.Fee(fee).FromTx(tx));
70
71 // All outputs are available to spend
72 for (uint32_t n{0}; n < num_outputs; ++n) {
73 if (fuzzed_data_provider.ConsumeBool()) {
74 available_coins.emplace_back(tx->GetHash(), n);
75 }
76 }
77
78 if (fuzzed_data_provider.ConsumeBool() && !tx->vout.empty()) {
79 // Add outpoint from this tx (may or not be spent by a later tx)
80 outpoints.emplace_back(tx->GetHash(),
81 (uint32_t)fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, tx->vout.size()));
82 } else {
83 // Add some random outpoint (will be interpreted as confirmed or not yet submitted
84 // to mempool).
85 auto outpoint = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
86 if (outpoint.has_value() && std::find(outpoints.begin(), outpoints.end(), *outpoint) == outpoints.end()) {
87 outpoints.push_back(*outpoint);
88 }
89 }
90
91 }
92
93 const CFeeRate target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/1000)}};
94 std::optional<CAmount> total_bumpfee;
95 CAmount sum_fees = 0;
96 {
97 node::MiniMiner mini_miner{pool, outpoints};
98 assert(mini_miner.IsReadyToCalculate());
99 const auto bump_fees = mini_miner.CalculateBumpFees(target_feerate);
100 for (const auto& outpoint : outpoints) {
101 auto it = bump_fees.find(outpoint);
102 assert(it != bump_fees.end());
103 assert(it->second >= 0);
104 sum_fees += it->second;
105 }
106 assert(!mini_miner.IsReadyToCalculate());
107 }
108 {
109 node::MiniMiner mini_miner{pool, outpoints};
110 assert(mini_miner.IsReadyToCalculate());
111 total_bumpfee = mini_miner.CalculateTotalBumpFees(target_feerate);
112 assert(total_bumpfee.has_value());
113 assert(!mini_miner.IsReadyToCalculate());
114 }
115 // Overlapping ancestry across multiple outpoints can only reduce the total bump fee.
116 assert (sum_fees >= *total_bumpfee);
117}
118
119// Test that MiniMiner and BlockAssembler build the same block given the same transactions and constraints.
120FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
121{
123 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
124 SetMockTime(ConsumeTime(fuzzed_data_provider));
125 bilingual_str error;
126 CTxMemPool pool{CTxMemPool::Options{}, error};
127 Assert(error.empty());
128 // Make a copy to preserve determinism.
129 std::deque<COutPoint> available_coins = g_available_coins;
130 std::vector<CTransactionRef> transactions;
131
132 LOCK2(::cs_main, pool.cs);
133 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
134 {
136 assert(!available_coins.empty());
137 const size_t num_inputs = std::min(size_t{2}, available_coins.size());
138 const size_t num_outputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(2, 5);
139 for (size_t n{0}; n < num_inputs; ++n) {
140 auto prevout = available_coins.at(0);
141 mtx.vin.emplace_back(prevout, CScript());
142 available_coins.pop_front();
143 }
144 for (uint32_t n{0}; n < num_outputs; ++n) {
145 mtx.vout.emplace_back(100, P2WSH_OP_TRUE);
146 }
148
149 // First 2 outputs are available to spend. The rest are added to outpoints to calculate bumpfees.
150 // There is no overlap between spendable coins and outpoints passed to MiniMiner because the
151 // MiniMiner interprets spent coins as to-be-replaced and excludes them.
152 for (uint32_t n{0}; n < num_outputs - 1; ++n) {
153 if (fuzzed_data_provider.ConsumeBool()) {
154 available_coins.emplace_front(tx->GetHash(), n);
155 } else {
156 available_coins.emplace_back(tx->GetHash(), n);
157 }
158 }
159
160 // Stop if pool reaches DEFAULT_BLOCK_MAX_WEIGHT because BlockAssembler will stop when the
161 // block template reaches that, but the MiniMiner will keep going.
162 if (pool.GetTotalTxSize() + GetVirtualTransactionSize(*tx) >= DEFAULT_BLOCK_MAX_WEIGHT) break;
164 const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
166 AddToMempool(pool, entry.Fee(fee).FromTx(tx));
167 transactions.push_back(tx);
168 }
169 std::vector<COutPoint> outpoints;
170 for (const auto& coin : g_available_coins) {
171 if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
172 }
173 for (const auto& tx : transactions) {
174 assert(pool.exists(GenTxid::Txid(tx->GetHash())));
175 for (uint32_t n{0}; n < tx->vout.size(); ++n) {
176 COutPoint coin{tx->GetHash(), n};
177 if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
178 }
179 }
180 const CFeeRate target_feerate{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
181
182 node::BlockAssembler::Options miner_options;
183 miner_options.blockMinFeeRate = target_feerate;
185 miner_options.test_block_validity = false;
186 miner_options.coinbase_output_script = CScript() << OP_0;
187
188 node::BlockAssembler miner{g_setup->m_node.chainman->ActiveChainstate(), &pool, miner_options};
189 node::MiniMiner mini_miner{pool, outpoints};
190 assert(mini_miner.IsReadyToCalculate());
191
192 // Use BlockAssembler as oracle. BlockAssembler and MiniMiner should select the same
193 // transactions, stopping once packages do not meet target_feerate.
194 const auto blocktemplate{miner.CreateNewBlock()};
195 mini_miner.BuildMockTemplate(target_feerate);
196 assert(!mini_miner.IsReadyToCalculate());
197 auto mock_template_txids = mini_miner.GetMockTemplateTxids();
198 // MiniMiner doesn't add a coinbase tx.
199 assert(mock_template_txids.count(blocktemplate->block.vtx[0]->GetHash()) == 0);
200 auto [iter, new_entry] = mock_template_txids.emplace(blocktemplate->block.vtx[0]->GetHash());
201 assert(new_entry);
202
203 assert(mock_template_txids.size() == blocktemplate->block.vtx.size());
204 for (const auto& tx : blocktemplate->block.vtx) {
205 assert(mock_template_txids.count(tx->GetHash()));
206 }
207}
208} // namespace
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define Assert(val)
Identity function.
Definition: check.h:85
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:304
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
Generate a new block, without valid proof-of-work.
Definition: miner.h:144
A minimal version of BlockAssembler, using the same ancestor set scoring algorithm.
Definition: mini_miner.h:79
static transaction_identifier FromUint256(const uint256 &id)
static const uint256 ZERO
Definition: uint256.h:209
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
#define FUZZ_TARGET(...)
Definition: fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
AddToMempool(pool, CTxMemPoolEntry(tx, fee, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp))
uint64_t fee
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:312
static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT
Default for -blockmaxweight, which controls the range of block weights the mining code will create.
Definition: policy.h:23
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_0
Definition: script.h:76
node::NodeContext m_node
Definition: setup_common.h:66
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
Definition: txmempool.h:19
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:33
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: txmempool.h:33
Testing setup that configures a complete environment.
Definition: setup_common.h:121
Bilingual messages:
Definition: translation.h:24
bool empty() const
Definition: translation.h:35
Options struct containing options for constructing a CTxMemPool.
CScript coinbase_output_script
Script to put in the coinbase transaction.
Definition: types.h:62
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define LOCK2(cs1, cs2)
Definition: sync.h:258
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:29
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
static const CScript P2WSH_OP_TRUE
Definition: script.h:12
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:39
assert(!tx.IsCoinBase())