6#include <chainparams.h>
40const std::vector<std::shared_ptr<CBlock>>* g_chain;
43template <
bool INVALID>
44void initialize_chain()
49 static const auto setup{
53 .setup_validation_interface =
false,
54 .min_validation_cache =
true,
58 auto& chainman{*setup->m_node.chainman};
59 for (
const auto& block : chain) {
61 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
63 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
67 g_setup = setup.get();
70template <
bool INVALID>
75 auto& setup{*g_setup};
76 bool dirty_chainman{
false};
81 Assert(!chainman.SnapshotBlockhash());
86 if (fuzzed_data_provider.ConsumeBool()) {
88 outfile <<
Span{metadata};
90 auto msg_start = chainman.GetParams().MessageStart();
91 int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<
int>(1, 2 *
COINBASE_MATURITY)};
92 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
93 uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 *
COINBASE_MATURITY)};
98 if (fuzzed_data_provider.ConsumeBool()) {
100 outfile <<
Span{file_data};
103 for (
const auto& block : *g_chain) {
104 auto coinbase{block->vtx.at(0)};
105 outfile << coinbase->GetHash();
108 outfile <<
Coin(coinbase->vout[0], height, 1);
116 const auto& coinbase{g_chain->back()->vtx.back()};
117 outfile << coinbase->GetHash();
120 outfile <<
Coin{coinbase->vout[0], 999, 0};
124 const auto ActivateFuzzedSnapshot{[&] {
126 auto msg_start = chainman.GetParams().MessageStart();
130 }
catch (
const std::ios_base::failure&) {
133 return !!chainman.ActivateSnapshot(infile, metadata,
true);
136 if (fuzzed_data_provider.ConsumeBool()) {
139 for (
const auto& block : *g_chain) {
141 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
143 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
146 dirty_chainman =
true;
150 if (ActivateFuzzedSnapshot()) {
152 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
153 Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
154 *chainman.SnapshotBlockhash());
155 const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
156 for (
const auto& block : *g_chain) {
157 Assert(coinscache.HaveCoin(
COutPoint{block->vtx.at(0)->GetHash(), 0}));
158 const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
161 if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
162 auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
163 Assert(params.has_value());
164 Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
166 Assert(index->m_chain_tx_count == 0);
169 Assert(g_chain->size() == coinscache.GetCacheSize());
170 dirty_chainman =
true;
172 Assert(!chainman.SnapshotBlockhash());
173 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
176 Assert(!ActivateFuzzedSnapshot());
182 if (dirty_chainman) {
183 setup.m_node.chainman.reset();
184 setup.m_make_chainman();
185 setup.LoadVerifyActivateChainstate();
198FUZZ_TARGET(utxo_snapshot , .
init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
199FUZZ_TARGET(utxo_snapshot_invalid, .
init = initialize_chain<true>) { utxo_snapshot_fuzz<true>(buffer); }
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
#define Assert(val)
Identity function.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Non-refcounted RAII wrapper for FILE*.
An outpoint - a combination of a transaction hash and an index n into its vout.
A Span is an object that can refer to a contiguous sequence of objects.
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
std::span< const uint8_t > FuzzBufferType
@ INVALID
Failed decoding.
FILE * fopen(const fs::path &p, const char *mode)
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Testing setup that configures a complete environment.
std::unique_ptr< ChainstateManager > chainman
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams ¶ms)
Create a blockchain, starting from genesis.
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.