Skip to content

Commit 0712009

Browse files
committed
move-only: unittest: add test/util/chainstate.h
and move `CreateAndActivateUTXOSnapshot()` into it for reuse in future test modules.
1 parent 8f5710f commit 0712009

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

src/Makefile.test_util.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ EXTRA_LIBRARIES += \
99

1010
TEST_UTIL_H = \
1111
test/util/blockfilter.h \
12+
test/util/chainstate.h \
1213
test/util/logging.h \
1314
test/util/mining.h \
1415
test/util/net.h \

src/test/util/chainstate.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
auto NoMalleation = [](CAutoFile& file, 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(NodeContext& node, const fs::path root, F malleation = NoMalleation)
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 " << snapshot_path.make_preferred().string() << ": " << 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

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#include <rpc/blockchain.h>
1010
#include <sync.h>
1111
#include <test/util/setup_common.h>
12+
#include <test/util/chainstate.h>
1213
#include <uint256.h>
1314
#include <validation.h>
1415
#include <validationinterface.h>
1516

1617
#include <tinyformat.h>
17-
#include <univalue.h>
1818

1919
#include <vector>
2020

@@ -154,36 +154,6 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
154154
BOOST_CHECK_CLOSE(c2.m_coinsdb_cache_size_bytes, max_cache * 0.95, 1);
155155
}
156156

157-
auto NoMalleation = [](CAutoFile& file, SnapshotMetadata& meta){};
158-
159-
template<typename F = decltype(NoMalleation)>
160-
static bool
161-
CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleation = NoMalleation)
162-
{
163-
// Write out a snapshot to the test's tempdir.
164-
//
165-
int height;
166-
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
167-
fs::path snapshot_path = root / tfm::format("test_snapshot.%d.dat", height);
168-
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
169-
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
170-
171-
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile);
172-
BOOST_TEST_MESSAGE(
173-
"Wrote UTXO snapshot to " << snapshot_path.make_preferred().string() << ": " << result.write());
174-
175-
// Read the written snapshot in and then activate it.
176-
//
177-
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
178-
CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
179-
SnapshotMetadata metadata;
180-
auto_infile >> metadata;
181-
182-
malleation(auto_infile, metadata);
183-
184-
return node.chainman->ActivateSnapshot(auto_infile, metadata, /*in_memory*/ true);
185-
}
186-
187157
//! Test basic snapshot activation.
188158
BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
189159
{

0 commit comments

Comments
 (0)