Bitcoin Core  24.1.0
P2P Digital Currency
chainstate.h
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 #ifndef BITCOIN_TEST_UTIL_CHAINSTATE_H
6 #define BITCOIN_TEST_UTIL_CHAINSTATE_H
7 
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 <validation.h>
15 
16 #include <univalue.h>
17 
18 const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
19 
24 template<typename F = decltype(NoMalleation)>
25 static bool
27 {
28  // Write out a snapshot to the test's tempdir.
29  //
30  int height;
31  WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
32  fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
33  FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
34  AutoFile auto_outfile{outfile};
35 
37  node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
38  LogPrintf(
39  "Wrote UTXO snapshot to %s: %s", fs::PathToString(snapshot_path.make_preferred()), result.write());
40 
41  // Read the written snapshot in and then activate it.
42  //
43  FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
44  AutoFile auto_infile{infile};
45  node::SnapshotMetadata metadata;
46  auto_infile >> metadata;
47 
48  malleation(auto_infile, metadata);
49 
50  return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory=*/true);
51 }
52 
53 
54 #endif // BITCOIN_TEST_UTIL_CHAINSTATE_H
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
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.
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:474
NodeContext struct containing references to chain state and connection state.
Definition: context.h:43
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
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:1062
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:305
Definition: init.h:25
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
const auto NoMalleation
Definition: chainstate.h:18
static path u8path(const std::string &utf8_str)
Definition: fs.h:70
#define LogPrintf(...)
Definition: logging.h:234
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
static bool CreateAndActivateUTXOSnapshot(node::NodeContext &node, const fs::path root, F malleation=NoMalleation)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot...
Definition: chainstate.h:26
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:15