6#include <chainparams.h>
41const std::vector<std::shared_ptr<CBlock>>* g_chain;
44template <
bool INVALID>
45void initialize_chain()
50 static const auto setup{
54 .setup_validation_interface =
false,
55 .min_validation_cache =
true,
59 auto& chainman{*setup->m_node.chainman};
60 for (
const auto& block : chain) {
62 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
64 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
68 g_setup = setup.get();
71template <
bool INVALID>
77 auto& setup{*g_setup};
78 bool dirty_chainman{
false};
83 Assert(!chainman.SnapshotBlockhash());
88 if (fuzzed_data_provider.ConsumeBool()) {
90 outfile <<
Span{metadata};
92 auto msg_start = chainman.GetParams().MessageStart();
93 int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<
int>(1, 2 *
COINBASE_MATURITY)};
94 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
95 uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 *
COINBASE_MATURITY)};
100 if (fuzzed_data_provider.ConsumeBool()) {
102 outfile <<
Span{file_data};
105 for (
const auto& block : *g_chain) {
106 auto coinbase{block->vtx.at(0)};
107 outfile << coinbase->GetHash();
110 outfile <<
Coin(coinbase->vout[0], height, 1);
118 const auto& coinbase{g_chain->back()->vtx.back()};
119 outfile << coinbase->GetHash();
122 outfile <<
Coin{coinbase->vout[0], 999, 0};
126 const auto ActivateFuzzedSnapshot{[&] {
128 auto msg_start = chainman.GetParams().MessageStart();
132 }
catch (
const std::ios_base::failure&) {
135 return !!chainman.ActivateSnapshot(infile, metadata,
true);
138 if (fuzzed_data_provider.ConsumeBool()) {
141 for (
const auto& block : *g_chain) {
143 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
145 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
148 dirty_chainman =
true;
152 if (ActivateFuzzedSnapshot()) {
154 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
155 Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
156 *chainman.SnapshotBlockhash());
157 const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
158 for (
const auto& block : *g_chain) {
159 Assert(coinscache.HaveCoin(
COutPoint{block->vtx.at(0)->GetHash(), 0}));
160 const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
163 if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
164 auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
165 Assert(params.has_value());
166 Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
168 Assert(index->m_chain_tx_count == 0);
171 Assert(g_chain->size() == coinscache.GetCacheSize());
172 dirty_chainman =
true;
174 Assert(!chainman.SnapshotBlockhash());
175 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
178 Assert(!ActivateFuzzedSnapshot());
184 if (dirty_chainman) {
185 setup.m_node.chainman.reset();
186 setup.m_make_chainman();
187 setup.LoadVerifyActivateChainstate();
200FUZZ_TARGET(utxo_snapshot , .
init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
201FUZZ_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.
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
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.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.