6#include <chainparams.h>
42const std::vector<std::shared_ptr<CBlock>>* g_chain;
46void sanity_check_snapshot()
48 Assert(g_chain && g_setup ==
nullptr);
52 const auto&
node{tmp_setup->m_node};
53 for (
auto& block: *g_chain) {
59 auto&
cs{
node.chainman->ActiveChainstate()};
60 cs.ForceFlushStateToDisk();
63 Assert(stats.nHeight == cp_au_data.height);
64 Assert(stats.nTransactions + 1 == cp_au_data.m_chain_tx_count);
65 Assert(stats.hashBlock == cp_au_data.blockhash);
69template <
bool INVALID>
70void initialize_chain()
78 sanity_check_snapshot();
80 static const auto setup{
84 .setup_validation_interface =
false,
85 .min_validation_cache =
true,
89 auto& chainman{*setup->m_node.chainman};
90 for (
const auto& block : chain) {
92 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
94 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
98 g_setup = setup.get();
101template <
bool INVALID>
107 auto& setup{*g_setup};
108 bool dirty_chainman{
false};
109 auto& chainman{*setup.m_node.chainman};
113 Assert(!chainman.SnapshotBlockhash());
118 if (fuzzed_data_provider.ConsumeBool()) {
120 outfile << std::span{metadata};
122 auto msg_start = chainman.GetParams().MessageStart();
123 int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<
int>(1, 2 *
COINBASE_MATURITY)};
124 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
125 uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 *
COINBASE_MATURITY)};
130 if (fuzzed_data_provider.ConsumeBool()) {
132 outfile << std::span{file_data};
135 for (
const auto& block : *g_chain) {
136 auto coinbase{block->vtx.at(0)};
137 outfile << coinbase->GetHash();
140 outfile <<
Coin(coinbase->vout[0], height, 1);
148 const auto& coinbase{g_chain->back()->vtx.back()};
149 outfile << coinbase->GetHash();
152 outfile <<
Coin{coinbase->vout[0], 999, 0};
154 assert(outfile.fclose() == 0);
157 const auto ActivateFuzzedSnapshot{[&] {
159 auto msg_start = chainman.GetParams().MessageStart();
163 }
catch (
const std::ios_base::failure&) {
166 return !!chainman.ActivateSnapshot(infile, metadata,
true);
169 if (fuzzed_data_provider.ConsumeBool()) {
172 for (
const auto& block : *g_chain) {
174 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
176 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
179 dirty_chainman =
true;
183 if (ActivateFuzzedSnapshot()) {
185 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
186 Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
187 *chainman.SnapshotBlockhash());
188 const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
189 for (
const auto& block : *g_chain) {
190 Assert(coinscache.HaveCoin(
COutPoint{block->vtx.at(0)->GetHash(), 0}));
191 const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
194 if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
195 auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
196 Assert(params.has_value());
197 Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
199 Assert(index->m_chain_tx_count == 0);
202 Assert(g_chain->size() == coinscache.GetCacheSize());
203 dirty_chainman =
true;
205 Assert(!chainman.SnapshotBlockhash());
206 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
209 Assert(!ActivateFuzzedSnapshot());
215 if (dirty_chainman) {
216 setup.m_node.chainman.reset();
217 setup.m_make_chainman();
218 setup.LoadVerifyActivateChainstate();
231FUZZ_TARGET(utxo_snapshot , .
init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
232FUZZ_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.
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)
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Testing setup that configures a complete environment.
#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
COutPoint ProcessBlock(const NodeContext &node, const std::shared_ptr< CBlock > &block)
Returns the generated coin (or Null if the block was invalid).
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.