6#include <chainparams.h>
44const std::vector<std::shared_ptr<CBlock>>* g_chain;
48void sanity_check_snapshot()
54 const auto&
node{tmp_setup->m_node};
55 for (
auto& block: *g_chain) {
61 auto&
cs{
node.chainman->ActiveChainstate()};
62 cs.ForceFlushStateToDisk(
false);
65 Assert(stats.nHeight == cp_au_data.height);
66 Assert(stats.nTransactions + 1 == cp_au_data.m_chain_tx_count);
67 Assert(stats.hashBlock == cp_au_data.blockhash);
71template <
bool INVALID>
72void initialize_chain()
80 sanity_check_snapshot();
82 static const auto setup{
86 .setup_validation_interface =
false,
87 .min_validation_cache =
true,
91 auto& chainman{*
setup->m_node.chainman};
92 for (
const auto& block : chain) {
94 bool processed{chainman.ProcessNewBlockHeaders({{*block}},
true, dummy)};
96 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
103template <
bool INVALID>
110 bool dirty_chainman{
false};
111 auto& chainman{*
setup.m_node.chainman};
115 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
122 outfile << std::span{metadata};
124 auto msg_start = chainman.GetParams().MessageStart();
126 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
134 outfile << std::span{file_data};
137 for (
const auto& block : *g_chain) {
138 auto coinbase{block->vtx.at(0)};
139 outfile << coinbase->GetHash();
142 outfile <<
Coin(coinbase->vout[0], height,
true);
150 const auto& coinbase{g_chain->back()->vtx.back()};
151 outfile << coinbase->GetHash();
154 outfile <<
Coin{coinbase->vout[0], 999,
false};
156 assert(outfile.fclose() == 0);
159 const auto ActivateFuzzedSnapshot{[&] {
161 auto msg_start = chainman.GetParams().MessageStart();
165 }
catch (
const std::ios_base::failure&) {
168 return !!chainman.ActivateSnapshot(infile, metadata,
true);
174 for (
const auto& block : *g_chain) {
176 bool processed{chainman.ProcessNewBlockHeaders({{*block}},
true, dummy)};
178 const auto* index{
WITH_LOCK(
::cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
181 dirty_chainman =
true;
185 if (ActivateFuzzedSnapshot()) {
187 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
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.ActiveChainstate().SnapshotBase()->nHeight) {
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.ActiveChainstate().m_from_snapshot_blockhash);
208 Assert(!ActivateFuzzedSnapshot());
214 if (dirty_chainman) {
215 setup.m_node.block_template_manager.reset();
216 setup.m_node.chainman.reset();
217 setup.m_make_chainman();
218 setup.LoadVerifyActivateChainstate();
219 setup.CreateBlockTemplateManager();
232FUZZ_TARGET(utxo_snapshot , .
init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
233FUZZ_TARGET(utxo_snapshot_invalid, .
init = initialize_chain<true>) { utxo_snapshot_fuzz<true>(buffer); }
const TestingSetup * g_setup
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 EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
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.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
T ConsumeIntegralInRange(T min, T max)
constexpr 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
FILE * fopen(const fs::path &p, const char *mode)
static std::optional< CCoinsStats > ComputeUTXOStats(T hash_obj, const CCoinsViewDB &view, node::BlockManager &blockman, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
const auto INVALID
A stack representing the lack of any (dis)satisfactions.
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.
SeedRandomStateForTest(SeedRand::ZEROS)
NodeSeconds 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.
@ ZEROS
Seed with a compile time constant of zeros.
FuzzedDataProvider & fuzzed_data_provider