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