Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
mini_miner.cpp
Go to the documentation of this file.
2#include <test/fuzz/fuzz.h>
3#include <test/fuzz/util.h>
5#include <test/util/script.h>
8#include <test/util/mining.h>
9
10#include <node/miner.h>
11#include <node/mini_miner.h>
13#include <random.h>
14#include <txmempool.h>
15#include <util/check.h>
16#include <util/translation.h>
17
18#include <deque>
19#include <vector>
20
21namespace {
22
23const TestingSetup* g_setup;
24std::deque<COutPoint> g_available_coins;
25void initialize_miner()
26{
27 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
28 g_setup = testing_setup.get();
29 for (uint32_t i = 0; i < uint32_t{100}; ++i) {
30 g_available_coins.emplace_back(Txid::FromUint256(uint256::ZERO), i);
31 }
32}
33
34// Test that the MiniMiner can run with various outpoints and feerates.
35FUZZ_TARGET(mini_miner, .init = initialize_miner)
36{
37 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
38 bilingual_str error;
39 CTxMemPool pool{CTxMemPool::Options{}, error};
40 Assert(error.empty());
41 std::vector<COutPoint> outpoints;
42 std::deque<COutPoint> available_coins = g_available_coins;
43 LOCK2(::cs_main, pool.cs);
44 // Cluster size cannot exceed 500
45 LIMITED_WHILE(!available_coins.empty(), 500)
46 {
48 const size_t num_inputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, available_coins.size());
49 const size_t num_outputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
50 for (size_t n{0}; n < num_inputs; ++n) {
51 auto prevout = available_coins.front();
52 mtx.vin.emplace_back(prevout, CScript());
53 available_coins.pop_front();
54 }
55 for (uint32_t n{0}; n < num_outputs; ++n) {
56 mtx.vout.emplace_back(100, P2WSH_OP_TRUE);
57 }
60 const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
61 assert(MoneyRange(fee));
62 pool.addUnchecked(entry.Fee(fee).FromTx(tx));
63
64 // All outputs are available to spend
65 for (uint32_t n{0}; n < num_outputs; ++n) {
66 if (fuzzed_data_provider.ConsumeBool()) {
67 available_coins.emplace_back(tx->GetHash(), n);
68 }
69 }
70
71 if (fuzzed_data_provider.ConsumeBool() && !tx->vout.empty()) {
72 // Add outpoint from this tx (may or not be spent by a later tx)
73 outpoints.emplace_back(tx->GetHash(),
74 (uint32_t)fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, tx->vout.size()));
75 } else {
76 // Add some random outpoint (will be interpreted as confirmed or not yet submitted
77 // to mempool).
78 auto outpoint = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
79 if (outpoint.has_value() && std::find(outpoints.begin(), outpoints.end(), *outpoint) == outpoints.end()) {
80 outpoints.push_back(*outpoint);
81 }
82 }
83
84 }
85
86 const CFeeRate target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/1000)}};
87 std::optional<CAmount> total_bumpfee;
88 CAmount sum_fees = 0;
89 {
90 node::MiniMiner mini_miner{pool, outpoints};
91 assert(mini_miner.IsReadyToCalculate());
92 const auto bump_fees = mini_miner.CalculateBumpFees(target_feerate);
93 for (const auto& outpoint : outpoints) {
94 auto it = bump_fees.find(outpoint);
95 assert(it != bump_fees.end());
96 assert(it->second >= 0);
97 sum_fees += it->second;
98 }
99 assert(!mini_miner.IsReadyToCalculate());
100 }
101 {
102 node::MiniMiner mini_miner{pool, outpoints};
103 assert(mini_miner.IsReadyToCalculate());
104 total_bumpfee = mini_miner.CalculateTotalBumpFees(target_feerate);
105 assert(total_bumpfee.has_value());
106 assert(!mini_miner.IsReadyToCalculate());
107 }
108 // Overlapping ancestry across multiple outpoints can only reduce the total bump fee.
109 assert (sum_fees >= *total_bumpfee);
110}
111
112// Test that MiniMiner and BlockAssembler build the same block given the same transactions and constraints.
113FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
114{
115 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
116 bilingual_str error;
117 CTxMemPool pool{CTxMemPool::Options{}, error};
118 Assert(error.empty());
119 // Make a copy to preserve determinism.
120 std::deque<COutPoint> available_coins = g_available_coins;
121 std::vector<CTransactionRef> transactions;
122
123 LOCK2(::cs_main, pool.cs);
124 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
125 {
127 assert(!available_coins.empty());
128 const size_t num_inputs = std::min(size_t{2}, available_coins.size());
129 const size_t num_outputs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(2, 5);
130 for (size_t n{0}; n < num_inputs; ++n) {
131 auto prevout = available_coins.at(0);
132 mtx.vin.emplace_back(prevout, CScript());
133 available_coins.pop_front();
134 }
135 for (uint32_t n{0}; n < num_outputs; ++n) {
136 mtx.vout.emplace_back(100, P2WSH_OP_TRUE);
137 }
139
140 // First 2 outputs are available to spend. The rest are added to outpoints to calculate bumpfees.
141 // There is no overlap between spendable coins and outpoints passed to MiniMiner because the
142 // MiniMiner interprets spent coins as to-be-replaced and excludes them.
143 for (uint32_t n{0}; n < num_outputs - 1; ++n) {
144 if (fuzzed_data_provider.ConsumeBool()) {
145 available_coins.emplace_front(tx->GetHash(), n);
146 } else {
147 available_coins.emplace_back(tx->GetHash(), n);
148 }
149 }
150
151 // Stop if pool reaches DEFAULT_BLOCK_MAX_WEIGHT because BlockAssembler will stop when the
152 // block template reaches that, but the MiniMiner will keep going.
153 if (pool.GetTotalTxSize() + GetVirtualTransactionSize(*tx) >= DEFAULT_BLOCK_MAX_WEIGHT) break;
155 const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
156 assert(MoneyRange(fee));
157 pool.addUnchecked(entry.Fee(fee).FromTx(tx));
158 transactions.push_back(tx);
159 }
160 std::vector<COutPoint> outpoints;
161 for (const auto& coin : g_available_coins) {
162 if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
163 }
164 for (const auto& tx : transactions) {
165 assert(pool.exists(GenTxid::Txid(tx->GetHash())));
166 for (uint32_t n{0}; n < tx->vout.size(); ++n) {
167 COutPoint coin{tx->GetHash(), n};
168 if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
169 }
170 }
171 const CFeeRate target_feerate{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY/100000)};
172
173 node::BlockAssembler::Options miner_options;
174 miner_options.blockMinFeeRate = target_feerate;
176 miner_options.test_block_validity = false;
177
178 node::BlockAssembler miner{g_setup->m_node.chainman->ActiveChainstate(), &pool, miner_options};
179 node::MiniMiner mini_miner{pool, outpoints};
180 assert(mini_miner.IsReadyToCalculate());
181
182 CScript spk_placeholder = CScript() << OP_0;
183 // Use BlockAssembler as oracle. BlockAssembler and MiniMiner should select the same
184 // transactions, stopping once packages do not meet target_feerate.
185 const auto blocktemplate{miner.CreateNewBlock(spk_placeholder)};
186 mini_miner.BuildMockTemplate(target_feerate);
187 assert(!mini_miner.IsReadyToCalculate());
188 auto mock_template_txids = mini_miner.GetMockTemplateTxids();
189 // MiniMiner doesn't add a coinbase tx.
190 assert(mock_template_txids.count(blocktemplate->block.vtx[0]->GetHash()) == 0);
191 auto [iter, new_entry] = mock_template_txids.emplace(blocktemplate->block.vtx[0]->GetHash());
192 assert(new_entry);
193
194 assert(mock_template_txids.size() == blocktemplate->block.vtx.size());
195 for (const auto& tx : blocktemplate->block.vtx) {
196 assert(mock_template_txids.count(tx->GetHash()));
197 }
198}
199} // 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:77
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:414
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)
Generate a new block, without valid proof-of-work.
Definition miner.h:140
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:185
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
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:300
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)
std::shared_ptr< const CTransaction > CTransactionRef
@ OP_0
Definition script.h:75
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
node::NodeContext m_node
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
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.
Bilingual messages:
Definition translation.h:18
bool empty() const
Definition translation.h:29
Options struct containing options for constructing a CTxMemPool.
std::unique_ptr< ChainstateManager > chainman
Definition context.h:69
#define LOCK2(cs1, cs2)
Definition sync.h:258
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition util.cpp:29
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:103
static const CScript P2WSH_OP_TRUE
Definition script.h:12
assert(!tx.IsCoinBase())