5 #include <chainparams.h>
22 const std::vector<std::shared_ptr<CBlock>>* g_chain;
24 void initialize_chain()
34 std::unique_ptr<const TestingSetup> setup{
35 MakeNoLogFileContext<const TestingSetup>(
39 .setup_validation_interface =
false,
41 const auto&
node = setup->m_node;
42 auto& chainman{*
node.chainman};
46 Assert(!chainman.SnapshotBlockhash());
51 if (fuzzed_data_provider.ConsumeBool()) {
53 outfile <<
Span{metadata};
56 auto msg_start = chainman.GetParams().MessageStart();
57 int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<
int>(1, 2 *
COINBASE_MATURITY)};
58 uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
59 uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 *
COINBASE_MATURITY)};
60 SnapshotMetadata metadata{msg_start, base_blockhash, base_blockheight, m_coins_count};
64 if (fuzzed_data_provider.ConsumeBool()) {
66 outfile <<
Span{file_data};
69 for (
const auto& block : *g_chain) {
70 auto coinbase{block->vtx.at(0)};
71 outfile << coinbase->GetHash();
74 outfile <<
Coin(coinbase->vout[0], height, 1);
80 const auto ActivateFuzzedSnapshot{[&] {
82 auto msg_start = chainman.GetParams().MessageStart();
86 }
catch (
const std::ios_base::failure&) {
89 return !!chainman.ActivateSnapshot(infile, metadata,
true);
92 if (fuzzed_data_provider.ConsumeBool()) {
93 for (
const auto& block : *g_chain) {
95 bool processed{chainman.ProcessNewBlockHeaders({*block},
true, dummy)};
97 const auto* index{
WITH_LOCK(::
cs_main,
return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
102 if (ActivateFuzzedSnapshot()) {
104 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
105 Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
106 *chainman.SnapshotBlockhash());
107 const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
108 for (
const auto& block : *g_chain) {
109 Assert(coinscache.HaveCoin(
COutPoint{block->vtx.at(0)->GetHash(), 0}));
110 const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
113 if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
114 auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
115 Assert(params.has_value());
116 Assert(params.value().nChainTx == index->nChainTx);
118 Assert(index->nChainTx == 0);
121 Assert(g_chain->size() == coinscache.GetCacheSize());
123 Assert(!chainman.SnapshotBlockhash());
124 Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
127 Assert(!ActivateFuzzedSnapshot());
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.
Double ended buffer combining vector and stream-like interfaces.
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.
FILE * fopen(const fs::path &p, const char *mode)
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
#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.