3
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
5
5
#include < consensus/validation.h>
6
+ #include < miner.h>
6
7
#include < test/fuzz/FuzzedDataProvider.h>
7
8
#include < test/fuzz/fuzz.h>
8
9
#include < test/fuzz/util.h>
@@ -77,13 +78,44 @@ void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_pr
77
78
ToString (fuzzed_data_provider.ConsumeIntegralInRange <unsigned >(0 , 999 )));
78
79
}
79
80
81
+ void Finish (FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, CChainState& chainstate)
82
+ {
83
+ WITH_LOCK (::cs_main, tx_pool.check (chainstate));
84
+ {
85
+ BlockAssembler::Options options;
86
+ options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange (0U , MAX_BLOCK_WEIGHT);
87
+ options.blockMinFeeRate = CFeeRate{ConsumeMoney (fuzzed_data_provider)};
88
+ auto assembler = BlockAssembler{chainstate, *static_cast <CTxMemPool*>(&tx_pool), ::Params (), options};
89
+ auto block_template = assembler.CreateNewBlock (CScript{} << OP_TRUE);
90
+ Assert (block_template->block .vtx .size () >= 1 );
91
+ }
92
+ const auto info_all = tx_pool.infoAll ();
93
+ if (!info_all.empty ()) {
94
+ const auto & tx_to_remove = *PickValue (fuzzed_data_provider, info_all).tx ;
95
+ WITH_LOCK (tx_pool.cs , tx_pool.removeRecursive (tx_to_remove, /* dummy */ MemPoolRemovalReason::BLOCK));
96
+ std::vector<uint256> all_txids;
97
+ tx_pool.queryHashes (all_txids);
98
+ assert (all_txids.size () < info_all.size ());
99
+ WITH_LOCK (::cs_main, tx_pool.check (chainstate));
100
+ }
101
+ SyncWithValidationInterfaceQueue ();
102
+ }
103
+
104
+ void MockTime (FuzzedDataProvider& fuzzed_data_provider, const CChainState& chainstate)
105
+ {
106
+ const auto time = ConsumeTime (fuzzed_data_provider,
107
+ chainstate.m_chain .Tip ()->GetMedianTimePast () + 1 ,
108
+ std::numeric_limits<decltype (chainstate.m_chain .Tip ()->nTime )>::max ());
109
+ SetMockTime (time);
110
+ }
111
+
80
112
FUZZ_TARGET_INIT (tx_pool_standard, initialize_tx_pool)
81
113
{
82
114
FuzzedDataProvider fuzzed_data_provider (buffer.data (), buffer.size ());
83
115
const auto & node = g_setup->m_node ;
84
116
auto & chainstate = node.chainman ->ActiveChainstate ();
85
117
86
- SetMockTime ( ConsumeTime ( fuzzed_data_provider) );
118
+ MockTime ( fuzzed_data_provider, chainstate );
87
119
SetMempoolConstraints (*node.args , fuzzed_data_provider);
88
120
89
121
// All RBF-spendable outpoints
@@ -163,7 +195,7 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
163
195
}();
164
196
165
197
if (fuzzed_data_provider.ConsumeBool ()) {
166
- SetMockTime ( ConsumeTime ( fuzzed_data_provider) );
198
+ MockTime ( fuzzed_data_provider, chainstate );
167
199
}
168
200
if (fuzzed_data_provider.ConsumeBool ()) {
169
201
SetMempoolConstraints (*node.args , fuzzed_data_provider);
@@ -237,23 +269,17 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
237
269
}
238
270
}
239
271
}
240
- WITH_LOCK (::cs_main, tx_pool.check (chainstate));
241
- const auto info_all = tx_pool.infoAll ();
242
- if (!info_all.empty ()) {
243
- const auto & tx_to_remove = *PickValue (fuzzed_data_provider, info_all).tx ;
244
- WITH_LOCK (tx_pool.cs , tx_pool.removeRecursive (tx_to_remove, /* dummy */ MemPoolRemovalReason::BLOCK));
245
- std::vector<uint256> all_txids;
246
- tx_pool.queryHashes (all_txids);
247
- assert (all_txids.size () < info_all.size ());
248
- WITH_LOCK (::cs_main, tx_pool.check (chainstate));
249
- }
250
- SyncWithValidationInterfaceQueue ();
272
+ Finish (fuzzed_data_provider, tx_pool, chainstate);
251
273
}
252
274
253
275
FUZZ_TARGET_INIT (tx_pool, initialize_tx_pool)
254
276
{
255
277
FuzzedDataProvider fuzzed_data_provider (buffer.data (), buffer.size ());
256
278
const auto & node = g_setup->m_node ;
279
+ auto & chainstate = node.chainman ->ActiveChainstate ();
280
+
281
+ MockTime (fuzzed_data_provider, chainstate);
282
+ SetMempoolConstraints (*node.args , fuzzed_data_provider);
257
283
258
284
std::vector<uint256> txids;
259
285
for (const auto & outpoint : g_outpoints_coinbase_init_mature) {
@@ -265,11 +291,29 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
265
291
txids.push_back (ConsumeUInt256 (fuzzed_data_provider));
266
292
}
267
293
268
- CTxMemPool tx_pool{/* estimator */ nullptr , /* check_ratio */ 1 };
294
+ CTxMemPool tx_pool_{/* estimator */ nullptr , /* check_ratio */ 1 };
295
+ MockedTxPool& tx_pool = *static_cast <MockedTxPool*>(&tx_pool_);
269
296
270
297
while (fuzzed_data_provider.ConsumeBool ()) {
271
298
const auto mut_tx = ConsumeTransaction (fuzzed_data_provider, txids);
272
299
300
+ if (fuzzed_data_provider.ConsumeBool ()) {
301
+ MockTime (fuzzed_data_provider, chainstate);
302
+ }
303
+ if (fuzzed_data_provider.ConsumeBool ()) {
304
+ SetMempoolConstraints (*node.args , fuzzed_data_provider);
305
+ }
306
+ if (fuzzed_data_provider.ConsumeBool ()) {
307
+ tx_pool.RollingFeeUpdate ();
308
+ }
309
+ if (fuzzed_data_provider.ConsumeBool ()) {
310
+ const auto & txid = fuzzed_data_provider.ConsumeBool () ?
311
+ mut_tx.GetHash () :
312
+ PickValue (fuzzed_data_provider, txids);
313
+ const auto delta = fuzzed_data_provider.ConsumeIntegralInRange <CAmount>(-50 * COIN, +50 * COIN);
314
+ tx_pool.PrioritiseTransaction (txid, delta);
315
+ }
316
+
273
317
const auto tx = MakeTransactionRef (mut_tx);
274
318
const bool bypass_limits = fuzzed_data_provider.ConsumeBool ();
275
319
::fRequireStandard = fuzzed_data_provider.ConsumeBool ();
@@ -278,8 +322,7 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
278
322
if (accepted) {
279
323
txids.push_back (tx->GetHash ());
280
324
}
281
-
282
- SyncWithValidationInterfaceQueue ();
283
325
}
326
+ Finish (fuzzed_data_provider, tx_pool, chainstate);
284
327
}
285
328
} // namespace
0 commit comments