Bitcoin Core  24.99.0
P2P Digital Currency
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
4 //
8 #include <clientversion.h>
9 #include <fs.h>
10 #include <logging.h>
11 #include <node/context.h>
12 #include <node/utxo_snapshot.h>
13 #include <rpc/blockchain.h>
14 #include <test/util/setup_common.h>
15 #include <validation.h>
17 #include <univalue.h>
19 const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
31 template<typename F = decltype(NoMalleation)>
32 static bool
34  TestingSetup* fixture,
35  F malleation = NoMalleation,
36  bool reset_chainstate = false,
37  bool in_memory_chainstate = false)
38 {
39  node::NodeContext& node = fixture->m_node;
40  fs::path root = fixture->m_path_root;
42  // Write out a snapshot to the test's tempdir.
43  //
44  int height;
45  WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
46  fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
47  FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
48  AutoFile auto_outfile{outfile};
51  node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
52  LogPrintf(
53  "Wrote UTXO snapshot to %s: %s", fs::PathToString(snapshot_path.make_preferred()), result.write());
55  // Read the written snapshot in and then activate it.
56  //
57  FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
58  AutoFile auto_infile{infile};
59  node::SnapshotMetadata metadata;
60  auto_infile >> metadata;
62  malleation(auto_infile, metadata);
64  if (reset_chainstate) {
65  {
66  // What follows is code to selectively reset chainstate data without
67  // disturbing the existing BlockManager instance, which is needed to
68  // recognize the headers chain previously generated by the chainstate we're
69  // removing. Without those headers, we can't activate the snapshot below.
70  //
71  // This is a stripped-down version of node::LoadChainstate which
72  // preserves the block index.
73  LOCK(::cs_main);
74  uint256 gen_hash = node.chainman->ActiveChainstate().m_chain[0]->GetBlockHash();
75  node.chainman->ResetChainstates();
76  node.chainman->InitializeChainstate(node.mempool.get());
77  Chainstate& chain = node.chainman->ActiveChainstate();
78  Assert(chain.LoadGenesisBlock());
79  // These cache values will be corrected shortly in `MaybeRebalanceCaches`.
80  chain.InitCoinsDB(1 << 20, true, false, "");
81  chain.InitCoinsCache(1 << 20);
82  chain.CoinsTip().SetBestBlock(gen_hash);
83  chain.setBlockIndexCandidates.insert(node.chainman->m_blockman.LookupBlockIndex(gen_hash));
84  chain.LoadChainTip();
85  node.chainman->MaybeRebalanceCaches();
86  }
88  if (!node.chainman->ActiveChainstate().ActivateBestChain(state)) {
89  throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
90  }
91  Assert(
92  0 == WITH_LOCK(node.chainman->GetMutex(), return node.chainman->ActiveHeight()));
93  }
95  return node.chainman->ActivateSnapshot(auto_infile, metadata, in_memory_chainstate);
96 }
UniValue CreateUTXOSnapshot(NodeContext &node, Chainstate &chainstate, AutoFile &afile, const fs::path &path, const fs::path &temppath)
Helper to create UTXO snapshots given a chainstate and a file handle.
#define Assert(val)
Identity function.
Definition: check.h:73
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:496
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:175
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:441
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
Definition: validation.h:537
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
Definition: validation.h:540
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string ToString() const
Definition: validation.h:127
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
path & make_preferred()
Definition: fs.h:66
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:25
256-bit opaque blob.
Definition: uint256.h:119
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
#define LogPrintf(...)
Definition: logging.h:236
static path u8path(const std::string &utf8_str)
Definition: fs.h:70
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
Definition: init.h:25
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1063
const fs::path m_path_root
Definition: setup_common.h:91
node::NodeContext m_node
Definition: setup_common.h:86
Testing setup that configures a complete environment.
Definition: setup_common.h:108
NodeContext struct containing references to chain state and connection state.
Definition: context.h:43
#define LOCK(cs)
Definition: sync.h:258
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:302
static bool CreateAndActivateUTXOSnapshot(TestingSetup *fixture, F malleation=NoMalleation, bool reset_chainstate=false, bool in_memory_chainstate=false)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot.
Definition: chainstate.h:33
const auto NoMalleation
Definition: chainstate.h:19
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165