Bitcoin Core  27.99.0
P2P Digital Currency
utxo_snapshot.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <chainparams.h>
6 #include <consensus/validation.h>
7 #include <node/utxo_snapshot.h>
9 #include <test/fuzz/fuzz.h>
10 #include <test/fuzz/util.h>
11 #include <test/util/mining.h>
12 #include <test/util/setup_common.h>
13 #include <util/chaintype.h>
14 #include <util/fs.h>
15 #include <validation.h>
16 #include <validationinterface.h>
17 
19 
20 namespace {
21 
22 const std::vector<std::shared_ptr<CBlock>>* g_chain;
23 
24 void initialize_chain()
25 {
26  const auto params{CreateChainParams(ArgsManager{}, ChainType::REGTEST)};
27  static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
28  g_chain = &chain;
29 }
30 
31 FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
32 {
33  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
34  std::unique_ptr<const TestingSetup> setup{MakeNoLogFileContext<const TestingSetup>()};
35  const auto& node = setup->m_node;
36  auto& chainman{*node.chainman};
37 
38  const auto snapshot_path = gArgs.GetDataDirNet() / "fuzzed_snapshot.dat";
39 
40  Assert(!chainman.SnapshotBlockhash());
41 
42  {
43  AutoFile outfile{fsbridge::fopen(snapshot_path, "wb")};
44  const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
45  outfile << Span{file_data};
46  }
47 
48  const auto ActivateFuzzedSnapshot{[&] {
49  AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
50  SnapshotMetadata metadata;
51  try {
52  infile >> metadata;
53  } catch (const std::ios_base::failure&) {
54  return false;
55  }
56  return chainman.ActivateSnapshot(infile, metadata, /*in_memory=*/true);
57  }};
58 
59  if (fuzzed_data_provider.ConsumeBool()) {
60  for (const auto& block : *g_chain) {
62  bool processed{chainman.ProcessNewBlockHeaders({*block}, true, dummy)};
63  Assert(processed);
64  const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
65  Assert(index);
66  }
67  }
68 
69  if (ActivateFuzzedSnapshot()) {
70  LOCK(::cs_main);
71  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
72  Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
73  *chainman.SnapshotBlockhash());
74  const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
75  int64_t chain_tx{};
76  for (const auto& block : *g_chain) {
77  Assert(coinscache.HaveCoin(COutPoint{block->vtx.at(0)->GetHash(), 0}));
78  const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
79  const auto num_tx{Assert(index)->nTx};
80  Assert(num_tx == 1);
81  chain_tx += num_tx;
82  }
83  Assert(g_chain->size() == coinscache.GetCacheSize());
84  Assert(chain_tx == chainman.ActiveTip()->nChainTx);
85  } else {
86  Assert(!chainman.SnapshotBlockhash());
87  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
88  }
89  // Snapshot should refuse to load a second time regardless of validity
90  Assert(!ActivateFuzzedSnapshot());
91 }
92 } // namespace
ArgsManager gArgs
Definition: args.cpp:41
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.
Definition: check.h:77
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:389
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:25
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
#define FUZZ_TARGET(...)
Definition: fuzz.h:36
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
Definition: init.h:25
#define LOCK(cs)
Definition: sync.h:257
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams &params)
Create a blockchain, starting from genesis.
Definition: mining.cpp:32