Branch data Line data Source code
# 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 : :
# 19 : 4 : const auto NoMalleation = [](CAutoFile& file, node::SnapshotMetadata& meta){};
# 20 : :
# 21 : : /**
# 22 : : * Create and activate a UTXO snapshot, optionally providing a function to
# 23 : : * malleate the snapshot.
# 24 : : */
# 25 : : template<typename F = decltype(NoMalleation)>
# 26 : : static bool
# 27 : : CreateAndActivateUTXOSnapshot(node::NodeContext& node, const fs::path root, F malleation = NoMalleation)
# 28 : 9 : {
# 29 : : // Write out a snapshot to the test's tempdir.
# 30 : : //
# 31 : 9 : int height;
# 32 : 9 : WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
# 33 : 9 : fs::path snapshot_path = root / tfm::format("test_snapshot.%d.dat", height);
# 34 : 9 : FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
# 35 : 9 : CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
# 36 : :
# 37 : 9 : UniValue result = CreateUTXOSnapshot(
# 38 : 9 : node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
# 39 : 9 : BOOST_TEST_MESSAGE(
# 40 : 9 : "Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write());
# 41 : :
# 42 : : // Read the written snapshot in and then activate it.
# 43 : : //
# 44 : 9 : FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
# 45 : 9 : CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
# 46 : 9 : node::SnapshotMetadata metadata;
# 47 : 9 : auto_infile >> metadata;
# 48 : :
# 49 : 9 : malleation(auto_infile, metadata);
# 50 : :
# 51 : 9 : return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory=*/true);
# 52 : 9 : }
# 53 : :
# 54 : :
# 55 : : #endif // BITCOIN_TEST_UTIL_CHAINSTATE_H
|