Expand file tree
/
Copy pathindex_migration_tests.cpp
More file actions
74 lines (63 loc) · 2.87 KB
/
Copy pathindex_migration_tests.cpp
File metadata and controls
74 lines (63 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <coins.h>
#include <common/system.h>
#include <index/timestampindex.h>
#include <interfaces/chain.h>
#include <node/blockstorage.h>
#include <test/util/setup_common.h>
#include <txdb.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
using kernel::BlockTreeDB;
namespace {
struct IndexMigrationSetup : public TestChainSetup {
IndexMigrationSetup() :
TestChainSetup{100, ChainType::REGTEST, {"-timestampindex=1"}}
{
}
//! Give MigrateOldIndexData() the on-disk chainstate best block it reads on a real node.
void WriteChainstateBestBlock()
{
const uint256 tip{WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->GetBlockHash())};
CCoinsViewDB coins_db{DBParams{.path = gArgs.GetDataDirNet() / "chainstate", .cache_bytes = 1 << 20},
CoinsViewOptions{}};
CoinsCachePair sentinel{};
sentinel.second.SelfRef(sentinel);
CCoinsMapMemoryResource resource;
CCoinsMap no_coins{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
size_t usage{0};
auto cursor{CoinsViewCacheCursor(usage, sentinel, no_coins, /*will_erase=*/true)};
BOOST_REQUIRE(coins_db.BatchWrite(cursor, tip));
}
//! Height the timestamp index resumes from, read before its sync thread can advance it.
int LoadedTimestampIndexHeight()
{
TimestampIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1 << 20};
BOOST_REQUIRE(index.Init());
const int height{index.GetSummary().best_block_height};
index.Interrupt();
index.Stop();
return height;
}
};
} // namespace
BOOST_AUTO_TEST_SUITE(index_migration_tests)
// A datadir upgraded from v23 keeps its legacy "timestampindex" flag. If the index's own
// database is then missing (deleted to rebuild it), there is nothing left to migrate, and
// the empty database must not be stamped with the chain tip as if it were synced.
BOOST_FIXTURE_TEST_CASE(empty_index_is_not_adopted_as_migrated, IndexMigrationSetup)
{
WriteChainstateBestBlock();
BlockTreeDB& block_tree_db{*WITH_LOCK(cs_main, return m_node.chainman->m_blockman.m_block_tree_db.get())};
BOOST_REQUIRE(block_tree_db.WriteFlag("timestampindex", true));
BOOST_REQUIRE(
WITH_LOCK(cs_main, return block_tree_db.MigrateOldIndexData(gArgs.GetDataDirNet(), {.timestamp_index = true})));
BOOST_CHECK_EQUAL(LoadedTimestampIndexHeight(), 0);
// The legacy entries are gone, so older versions must not trust their synchronous index either.
bool legacy_current{true};
BOOST_REQUIRE(block_tree_db.ReadFlag("timestampindex", legacy_current));
BOOST_CHECK(!legacy_current);
}
BOOST_AUTO_TEST_SUITE_END()