2021-04-07 11:48:02 -04:00
|
|
|
// Copyright (c) 2021 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
//
|
|
|
|
#ifndef BITCOIN_TEST_UTIL_CHAINSTATE_H
|
|
|
|
#define BITCOIN_TEST_UTIL_CHAINSTATE_H
|
|
|
|
|
|
|
|
#include <clientversion.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <node/context.h>
|
|
|
|
#include <node/utxo_snapshot.h>
|
|
|
|
#include <rpc/blockchain.h>
|
|
|
|
#include <validation.h>
|
|
|
|
|
|
|
|
#include <univalue.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2021-09-15 15:39:40 -04:00
|
|
|
const auto NoMalleation = [](CAutoFile& file, SnapshotMetadata& meta){};
|
2021-04-07 11:48:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and activate a UTXO snapshot, optionally providing a function to
|
|
|
|
* malleate the snapshot.
|
|
|
|
*/
|
|
|
|
template<typename F = decltype(NoMalleation)>
|
|
|
|
static bool
|
|
|
|
CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleation = NoMalleation)
|
|
|
|
{
|
|
|
|
// Write out a snapshot to the test's tempdir.
|
|
|
|
//
|
|
|
|
int height;
|
|
|
|
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
|
|
|
|
fs::path snapshot_path = root / tfm::format("test_snapshot.%d.dat", height);
|
|
|
|
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
|
|
|
|
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
|
|
|
|
|
2019-04-25 11:09:57 -04:00
|
|
|
UniValue result = CreateUTXOSnapshot(
|
|
|
|
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
|
2021-04-07 11:48:02 -04:00
|
|
|
BOOST_TEST_MESSAGE(
|
2021-09-10 00:17:20 -04:00
|
|
|
"Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write());
|
2021-04-07 11:48:02 -04:00
|
|
|
|
|
|
|
// Read the written snapshot in and then activate it.
|
|
|
|
//
|
|
|
|
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
|
|
|
|
CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
|
|
|
|
SnapshotMetadata metadata;
|
|
|
|
auto_infile >> metadata;
|
|
|
|
|
|
|
|
malleation(auto_infile, metadata);
|
|
|
|
|
2021-09-14 16:56:34 +02:00
|
|
|
return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory=*/true);
|
2021-04-07 11:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // BITCOIN_TEST_UTIL_CHAINSTATE_H
|