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>
|
2022-09-05 07:55:25 +01:00
|
|
|
#include <logging.h>
|
2021-04-07 11:48:02 -04:00
|
|
|
#include <node/context.h>
|
|
|
|
#include <node/utxo_snapshot.h>
|
|
|
|
#include <rpc/blockchain.h>
|
2022-02-08 15:56:16 -05:00
|
|
|
#include <test/util/setup_common.h>
|
2021-04-07 11:48:02 -04:00
|
|
|
#include <validation.h>
|
|
|
|
|
|
|
|
#include <univalue.h>
|
|
|
|
|
2022-06-06 17:22:59 +02:00
|
|
|
const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
|
2021-04-07 11:48:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and activate a UTXO snapshot, optionally providing a function to
|
|
|
|
* malleate the snapshot.
|
2022-02-08 15:56:16 -05:00
|
|
|
*
|
|
|
|
* If `reset_chainstate` is true, reset the original chainstate back to the genesis
|
|
|
|
* block. This allows us to simulate more realistic conditions in which a snapshot is
|
|
|
|
* loaded into an otherwise mostly-uninitialized datadir. It also allows us to test
|
|
|
|
* conditions that would otherwise cause shutdowns based on the IBD chainstate going
|
|
|
|
* past the snapshot it generated.
|
2021-04-07 11:48:02 -04:00
|
|
|
*/
|
|
|
|
template<typename F = decltype(NoMalleation)>
|
|
|
|
static bool
|
2022-02-08 15:56:16 -05:00
|
|
|
CreateAndActivateUTXOSnapshot(TestingSetup* fixture, F malleation = NoMalleation, bool reset_chainstate = false)
|
2021-04-07 11:48:02 -04:00
|
|
|
{
|
2022-02-08 15:56:16 -05:00
|
|
|
node::NodeContext& node = fixture->m_node;
|
|
|
|
fs::path root = fixture->m_path_root;
|
|
|
|
|
2021-04-07 11:48:02 -04:00
|
|
|
// Write out a snapshot to the test's tempdir.
|
|
|
|
//
|
|
|
|
int height;
|
|
|
|
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
|
2022-03-03 14:40:18 -05:00
|
|
|
fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
|
2021-04-07 11:48:02 -04:00
|
|
|
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
|
2022-06-06 17:22:59 +02:00
|
|
|
AutoFile auto_outfile{outfile};
|
2021-04-07 11:48:02 -04:00
|
|
|
|
2019-04-25 11:09:57 -04:00
|
|
|
UniValue result = CreateUTXOSnapshot(
|
|
|
|
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
|
2022-09-05 07:55:25 +01:00
|
|
|
LogPrintf(
|
|
|
|
"Wrote UTXO snapshot to %s: %s", 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")};
|
2022-06-06 17:22:59 +02:00
|
|
|
AutoFile auto_infile{infile};
|
2021-11-12 10:06:00 -05:00
|
|
|
node::SnapshotMetadata metadata;
|
2021-04-07 11:48:02 -04:00
|
|
|
auto_infile >> metadata;
|
|
|
|
|
|
|
|
malleation(auto_infile, metadata);
|
|
|
|
|
2022-02-08 15:56:16 -05:00
|
|
|
if (reset_chainstate) {
|
|
|
|
{
|
|
|
|
// What follows is code to selectively reset chainstate data without
|
|
|
|
// disturbing the existing BlockManager instance, which is needed to
|
|
|
|
// recognize the headers chain previously generated by the chainstate we're
|
|
|
|
// removing. Without those headers, we can't activate the snapshot below.
|
|
|
|
//
|
|
|
|
// This is a stripped-down version of node::LoadChainstate which
|
|
|
|
// preserves the block index.
|
|
|
|
LOCK(::cs_main);
|
|
|
|
uint256 gen_hash = node.chainman->ActiveChainstate().m_chain[0]->GetBlockHash();
|
|
|
|
node.chainman->ResetChainstates();
|
|
|
|
node.chainman->InitializeChainstate(node.mempool.get());
|
|
|
|
Chainstate& chain = node.chainman->ActiveChainstate();
|
|
|
|
Assert(chain.LoadGenesisBlock());
|
|
|
|
// These cache values will be corrected shortly in `MaybeRebalanceCaches`.
|
|
|
|
chain.InitCoinsDB(1 << 20, true, false, "");
|
|
|
|
chain.InitCoinsCache(1 << 20);
|
|
|
|
chain.CoinsTip().SetBestBlock(gen_hash);
|
|
|
|
chain.setBlockIndexCandidates.insert(node.chainman->m_blockman.LookupBlockIndex(gen_hash));
|
|
|
|
chain.LoadChainTip();
|
|
|
|
node.chainman->MaybeRebalanceCaches();
|
|
|
|
}
|
|
|
|
BlockValidationState state;
|
|
|
|
if (!node.chainman->ActiveChainstate().ActivateBestChain(state)) {
|
|
|
|
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
|
|
|
|
}
|
|
|
|
Assert(
|
|
|
|
0 == WITH_LOCK(node.chainman->GetMutex(), return node.chainman->ActiveHeight()));
|
|
|
|
}
|
|
|
|
|
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
|