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()
77 sanity_check_snapshot();
79 static const auto setup{
83 .setup_validation_interface =
false,
84 .min_validation_cache =
true,
88 auto& chainman{*setup->m_node.chainman};
89 for (
const auto& block : chain) {
91 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
93 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
97 g_setup = setup.get();
100template <
bool INVALID>
106 auto& setup{*g_setup};
107 bool dirty_chainman{
false};
108 auto& chainman{*setup.m_node.chainman};
112 Assert(!chainman.SnapshotBlockhash());
117 if (fuzzed_data_provider.ConsumeBool()) {
119 outfile << std::span{metadata};
121 auto msg_start = chainman.GetParams().MessageStart();
122 int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<
int>(1, 2 *
COINBASE_MATURITY)};
123 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
124 uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 *
COINBASE_MATURITY)};
129 if (fuzzed_data_provider.ConsumeBool()) {
131 outfile << std::span{file_data};
134 for (
const auto& block : *g_chain) {
135 auto coinbase{block->vtx.at(0)};
136 outfile << coinbase->GetHash();
139 outfile <<
Coin(coinbase->vout[0], height, 1);
147 const auto& coinbase{g_chain->back()->vtx.back()};
148 outfile << coinbase->GetHash();
151 outfile <<
Coin{coinbase->vout[0], 999, 0};
155 const auto ActivateFuzzedSnapshot{[&] {
157 auto msg_start = chainman.GetParams().MessageStart();
161 }
catch (
const std::ios_base::failure&) {
164 return !!chainman.ActivateSnapshot(infile, metadata,
true);
167 if (fuzzed_data_provider.ConsumeBool()) {
170 for (
const auto& block : *g_chain) {
172 bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}},
true, dummy)};
174 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
177 dirty_chainman =
true;
181 if (ActivateFuzzedSnapshot()) {
183 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
184 Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
185 *chainman.SnapshotBlockhash());
186 const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
187 for (
const auto& block : *g_chain) {
188 Assert(coinscache.HaveCoin(
COutPoint{block->vtx.at(0)->GetHash(), 0}));
189 const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
192 if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
193 auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
194 Assert(params.has_value());
195 Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
197 Assert(index->m_chain_tx_count == 0);
200 Assert(g_chain->size() == coinscache.GetCacheSize());
201 dirty_chainman =
true;
203 Assert(!chainman.SnapshotBlockhash());
204 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
207 Assert(!ActivateFuzzedSnapshot());
213 if (dirty_chainman) {
214 setup.m_node.chainman.reset();
215 setup.m_make_chainman();
216 setup.LoadVerifyActivateChainstate();
229FUZZ_TARGET(utxo_snapshot , .
init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
230FUZZ_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.