Bitcoin Core  22.99.0
P2P Digital Currency
utxo_snapshot.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <test/util/mining.h>
11 #include <test/util/setup_common.h>
12 #include <validation.h>
13 #include <validationinterface.h>
14 
15 namespace {
16 
17 const std::vector<std::shared_ptr<CBlock>>* g_chain;
18 
19 void initialize_chain()
20 {
22  static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
23  g_chain = &chain;
24 }
25 
26 FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain)
27 {
28  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
29  std::unique_ptr<const TestingSetup> setup{MakeNoLogFileContext<const TestingSetup>()};
30  const auto& node = setup->m_node;
31  auto& chainman{*node.chainman};
32 
33  const auto snapshot_path = gArgs.GetDataDirNet() / "fuzzed_snapshot.dat";
34 
35  Assert(!chainman.SnapshotBlockhash());
36 
37  {
38  CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION};
39  const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
40  outfile << Span<const uint8_t>{file_data};
41  }
42 
43  const auto ActivateFuzzedSnapshot{[&] {
44  CAutoFile infile{fsbridge::fopen(snapshot_path, "rb"), SER_DISK, CLIENT_VERSION};
45  SnapshotMetadata metadata;
46  try {
47  infile >> metadata;
48  } catch (const std::ios_base::failure&) {
49  return false;
50  }
51  return chainman.ActivateSnapshot(infile, metadata, /* in_memory */ true);
52  }};
53 
54  if (fuzzed_data_provider.ConsumeBool()) {
55  for (const auto& block : *g_chain) {
57  bool processed{chainman.ProcessNewBlockHeaders({*block}, dummy, ::Params())};
58  Assert(processed);
59  const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
60  Assert(index);
61  }
62  }
63 
64  if (ActivateFuzzedSnapshot()) {
65  LOCK(::cs_main);
66  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
67  Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
68  *chainman.SnapshotBlockhash());
69  const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
70  int64_t chain_tx{};
71  for (const auto& block : *g_chain) {
72  Assert(coinscache.HaveCoin(COutPoint{block->vtx.at(0)->GetHash(), 0}));
73  const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
74  const auto num_tx{Assert(index)->nTx};
75  Assert(num_tx == 1);
76  chain_tx += num_tx;
77  }
78  Assert(g_chain->size() == coinscache.GetCacheSize());
79  Assert(chain_tx == chainman.ActiveTip()->nChainTx);
80  } else {
81  Assert(!chainman.SnapshotBlockhash());
82  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
83  }
84  // Snapshot should refuse to load a second time regardless of validity
85  Assert(!ActivateFuzzedSnapshot());
86 }
87 } // namespace
SER_DISK
@ SER_DISK
Definition: serialize.h:139
COINBASE_MATURITY
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
BlockValidationState
Definition: validation.h:140
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:24
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:282
setup_common.h
validationinterface.h
validation.h
util.h
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
chainparams.h
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:564
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
FuzzedDataProvider.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
gArgs
ArgsManager gArgs
Definition: system.cpp:84
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
ArgsManager
Definition: system.h:158
fuzz.h
LOCK
#define LOCK(cs)
Definition: sync.h:226
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:536
node
Definition: interfaces.cpp:68
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
FUZZ_TARGET_INIT
#define FUZZ_TARGET_INIT(name, init_fun)
Definition: fuzz.h:34
CreateChainParams
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
Definition: chainparams.cpp:541
SnapshotMetadata
Metadata describing a serialized version of a UTXO set from which an assumeutxo CChainState can be co...
Definition: utxo_snapshot.h:14
mining.h
CreateBlockChain
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams &params)
Create a blockchain, starting from genesis.
Definition: mining.cpp:28