Bitcoin Core  29.1.0
P2P Digital Currency
utxo_snapshot.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-present 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 <chain.h>
6 #include <chainparams.h>
7 #include <coins.h>
8 #include <consensus/consensus.h>
9 #include <consensus/validation.h>
10 #include <node/blockstorage.h>
11 #include <node/utxo_snapshot.h>
12 #include <primitives/block.h>
13 #include <primitives/transaction.h>
14 #include <serialize.h>
15 #include <span.h>
16 #include <streams.h>
17 #include <sync.h>
19 #include <test/fuzz/fuzz.h>
20 #include <test/fuzz/util.h>
21 #include <test/util/mining.h>
22 #include <test/util/setup_common.h>
23 #include <uint256.h>
24 #include <util/check.h>
25 #include <util/fs.h>
26 #include <util/result.h>
27 #include <util/time.h>
28 #include <validation.h>
29 
30 #include <cstdint>
31 #include <functional>
32 #include <ios>
33 #include <memory>
34 #include <optional>
35 #include <vector>
36 
38 
39 namespace {
40 
41 const std::vector<std::shared_ptr<CBlock>>* g_chain;
42 TestingSetup* g_setup;
43 
44 template <bool INVALID>
45 void initialize_chain()
46 {
47  const auto params{CreateChainParams(ArgsManager{}, ChainType::REGTEST)};
48  static const auto chain{CreateBlockChain(2 * COINBASE_MATURITY, *params)};
49  g_chain = &chain;
50  static const auto setup{
51  MakeNoLogFileContext<TestingSetup>(ChainType::REGTEST,
52  TestOpts{
53  .setup_net = false,
54  .setup_validation_interface = false,
55  .min_validation_cache = true,
56  }),
57  };
58  if constexpr (INVALID) {
59  auto& chainman{*setup->m_node.chainman};
60  for (const auto& block : chain) {
62  bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, 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  g_setup = setup.get();
69 }
70 
71 template <bool INVALID>
72 void utxo_snapshot_fuzz(FuzzBufferType buffer)
73 {
75  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
76  SetMockTime(ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)); // regtest genesis block timestamp
77  auto& setup{*g_setup};
78  bool dirty_chainman{false}; // Re-use the global chainman, but reset it when it is dirty
79  auto& chainman{*setup.m_node.chainman};
80 
81  const auto snapshot_path = gArgs.GetDataDirNet() / "fuzzed_snapshot.dat";
82 
83  Assert(!chainman.SnapshotBlockhash());
84 
85  {
86  AutoFile outfile{fsbridge::fopen(snapshot_path, "wb")};
87  // Metadata
88  if (fuzzed_data_provider.ConsumeBool()) {
89  std::vector<uint8_t> metadata{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
90  outfile << Span{metadata};
91  } else {
92  auto msg_start = chainman.GetParams().MessageStart();
93  int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 2 * COINBASE_MATURITY)};
94  uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
95  uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 * COINBASE_MATURITY)};
96  SnapshotMetadata metadata{msg_start, base_blockhash, m_coins_count};
97  outfile << metadata;
98  }
99  // Coins
100  if (fuzzed_data_provider.ConsumeBool()) {
101  std::vector<uint8_t> file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
102  outfile << Span{file_data};
103  } else {
104  int height{0};
105  for (const auto& block : *g_chain) {
106  auto coinbase{block->vtx.at(0)};
107  outfile << coinbase->GetHash();
108  WriteCompactSize(outfile, 1); // number of coins for the hash
109  WriteCompactSize(outfile, 0); // index of coin
110  outfile << Coin(coinbase->vout[0], height, /*fCoinBaseIn=*/1);
111  height++;
112  }
113  }
114  if constexpr (INVALID) {
115  // Append an invalid coin to ensure invalidity. This error will be
116  // detected late in PopulateAndValidateSnapshot, and allows the
117  // INVALID fuzz target to reach more potential code coverage.
118  const auto& coinbase{g_chain->back()->vtx.back()};
119  outfile << coinbase->GetHash();
120  WriteCompactSize(outfile, 1); // number of coins for the hash
121  WriteCompactSize(outfile, 999); // index of coin
122  outfile << Coin{coinbase->vout[0], /*nHeightIn=*/999, /*fCoinBaseIn=*/0};
123  }
124  }
125 
126  const auto ActivateFuzzedSnapshot{[&] {
127  AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
128  auto msg_start = chainman.GetParams().MessageStart();
129  SnapshotMetadata metadata{msg_start};
130  try {
131  infile >> metadata;
132  } catch (const std::ios_base::failure&) {
133  return false;
134  }
135  return !!chainman.ActivateSnapshot(infile, metadata, /*in_memory=*/true);
136  }};
137 
138  if (fuzzed_data_provider.ConsumeBool()) {
139  // Consume the bool, but skip the code for the INVALID fuzz target
140  if constexpr (!INVALID) {
141  for (const auto& block : *g_chain) {
142  BlockValidationState dummy;
143  bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, true, dummy)};
144  Assert(processed);
145  const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
146  Assert(index);
147  }
148  dirty_chainman = true;
149  }
150  }
151 
152  if (ActivateFuzzedSnapshot()) {
153  LOCK(::cs_main);
154  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
155  Assert(*chainman.ActiveChainstate().m_from_snapshot_blockhash ==
156  *chainman.SnapshotBlockhash());
157  const auto& coinscache{chainman.ActiveChainstate().CoinsTip()};
158  for (const auto& block : *g_chain) {
159  Assert(coinscache.HaveCoin(COutPoint{block->vtx.at(0)->GetHash(), 0}));
160  const auto* index{chainman.m_blockman.LookupBlockIndex(block->GetHash())};
161  Assert(index);
162  Assert(index->nTx == 0);
163  if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
164  auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
165  Assert(params.has_value());
166  Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
167  } else {
168  Assert(index->m_chain_tx_count == 0);
169  }
170  }
171  Assert(g_chain->size() == coinscache.GetCacheSize());
172  dirty_chainman = true;
173  } else {
174  Assert(!chainman.SnapshotBlockhash());
175  Assert(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
176  }
177  // Snapshot should refuse to load a second time regardless of validity
178  Assert(!ActivateFuzzedSnapshot());
179  if constexpr (INVALID) {
180  // Activating the snapshot, or any other action that makes the chainman
181  // "dirty" can and must not happen for the INVALID fuzz target
182  Assert(!dirty_chainman);
183  }
184  if (dirty_chainman) {
185  setup.m_node.chainman.reset();
186  setup.m_make_chainman();
187  setup.LoadVerifyActivateChainstate();
188  }
189 }
190 
191 // There are two fuzz targets:
192 //
193 // The target 'utxo_snapshot', which allows valid snapshots, but is slow,
194 // because it has to reset the chainstate manager on almost all fuzz inputs.
195 // Otherwise, a dirty header tree or dirty chainstate could leak from one fuzz
196 // input execution into the next, which makes execution non-deterministic.
197 //
198 // The target 'utxo_snapshot_invalid', which is fast and does not require any
199 // expensive state to be reset.
200 FUZZ_TARGET(utxo_snapshot /*valid*/, .init = initialize_chain<false>) { utxo_snapshot_fuzz<false>(buffer); }
201 FUZZ_TARGET(utxo_snapshot_invalid, .init = initialize_chain<true>) { utxo_snapshot_fuzz<true>(buffer); }
202 
203 } // namespace
bool setup_net
Definition: setup_common.h:56
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
A UTXO entry.
Definition: coins.h:32
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
Definition: consensus.h:19
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:391
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
std::span< const uint8_t > FuzzBufferType
Definition: fuzz.h:25
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
#define LOCK(cs)
Definition: sync.h:257
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1097
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
ArgsManager gArgs
Definition: args.cpp:42
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
256-bit opaque blob.
Definition: uint256.h:201
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
#define FUZZ_TARGET(...)
Definition: fuzz.h:35
Seed with a compile time constant of zeros.
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams &params)
Create a blockchain, starting from genesis.
Definition: mining.cpp:33
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
node::NodeContext m_node
Definition: setup_common.h:66
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
Testing setup that configures a complete environment.
Definition: setup_common.h:121
#define Assert(val)
Identity function.
Definition: check.h:85
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:33