Bitcoin Core 22.99.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 <node/context.h>
11#include <node/utxo_snapshot.h>
12#include <rpc/blockchain.h>
13#include <validation.h>
14
15#include <univalue.h>
16
17#include <boost/test/unit_test.hpp>
18
19const auto NoMalleation = [](CAutoFile& file, SnapshotMetadata& meta){};
20
25template<typename F = decltype(NoMalleation)>
26static bool
28{
29 // Write out a snapshot to the test's tempdir.
30 //
31 int height;
32 WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
33 fs::path snapshot_path = root / tfm::format("test_snapshot.%d.dat", height);
34 FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
35 CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
36
37 UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
38 BOOST_TEST_MESSAGE(
39 "Wrote UTXO snapshot to " << 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 CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
45 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
UniValue CreateUTXOSnapshot(NodeContext &node, CChainState &chainstate, CAutoFile &afile)
Helper to create UTXO snapshots given a chainstate and a file handle.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
static bool CreateAndActivateUTXOSnapshot(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:27
const auto NoMalleation
Definition: chainstate.h:19
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:565
Metadata describing a serialized version of a UTXO set from which an assumeutxo CChainState can be co...
Definition: utxo_snapshot.h:15
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp: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:1062
@ SER_DISK
Definition: serialize.h:139
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270