Repository navigation
Expand file tree
/
Copy pathevo_db_tests.cpp
More file actions
124 lines (102 loc) · 3.98 KB
/
Copy pathevo_db_tests.cpp
File metadata and controls
124 lines (102 loc) · 3.98 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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 <evo/evodb.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <boost/test/unit_test.hpp>
#include <cstdint>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <vector>
namespace {
using Payload = std::vector<uint8_t>;
uint256 BlockHash(uint32_t height)
{
return uint256S(strprintf("%064x", height));
}
auto PayloadKey(uint32_t height)
{
return std::make_pair(std::string{"test_evo_payload"}, BlockHash(height));
}
Payload PayloadFor(uint32_t height)
{
return {static_cast<uint8_t>(height), static_cast<uint8_t>(height >> 8)};
}
void WritePayload(CEvoDB& db, uint32_t height) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
auto tx = db.BeginTransaction();
db.Write(PayloadKey(height), PayloadFor(height));
tx->Commit();
}
void WriteBestBlock(CEvoDB& db, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
auto tx = db.BeginTransaction();
db.WriteBestBlock(hash);
tx->Commit();
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(evo_db_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(write_derived_rejects_disk_mismatch)
{
LOCK(::cs_main);
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_derived_mismatch", .memory = true, .wipe = true}};
const auto key = PayloadKey(3);
WritePayload(db, 3);
BOOST_REQUIRE(db.CommitRootTransaction());
auto tx = db.BeginTransaction();
BOOST_CHECK(db.WriteDerived(key, PayloadFor(3)));
Payload mismatch = PayloadFor(3);
mismatch.push_back(0xff);
BOOST_CHECK(!db.WriteDerived(key, mismatch));
}
BOOST_AUTO_TEST_CASE(open_transaction_belongs_to_its_thread)
{
LOCK(::cs_main);
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_tx_thread", .memory = true, .wipe = true}};
auto tx = db.BeginTransaction();
BOOST_CHECK(db.HasActiveTransaction());
bool other_thread_has_transaction{true};
std::thread reader{[&] { other_thread_has_transaction = db.HasActiveTransaction(); }};
reader.join();
BOOST_CHECK(!other_thread_has_transaction);
tx->Commit();
BOOST_CHECK(!db.HasActiveTransaction());
}
BOOST_AUTO_TEST_CASE(merge_snapshot_and_finish_promotion)
{
LOCK(::cs_main);
CEvoDB background{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_background", .memory = true, .wipe = true}};
CEvoDB snapshot{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_snapshot", .memory = true, .wipe = true}};
// The background chainstate validated up to the base at height 10, the
// snapshot chainstate connected blocks 11 and 12 on top of it.
WritePayload(background, 9);
WritePayload(background, 10);
WriteBestBlock(background, BlockHash(10));
BOOST_REQUIRE(background.CommitRootTransaction());
WritePayload(snapshot, 10);
WritePayload(snapshot, 11);
WritePayload(snapshot, 12);
WriteBestBlock(snapshot, BlockHash(12));
BOOST_REQUIRE(snapshot.CommitRootTransaction());
BOOST_CHECK(!background.ReadPendingPromotion());
// A merge interrupted before the coins directories were swapped runs again.
for (int i = 0; i < 2; ++i) {
BOOST_REQUIRE(background.MergeSnapshot(snapshot, BlockHash(12)));
BOOST_CHECK(background.ReadPendingPromotion() == BlockHash(12));
// Until the promotion finishes, the best block is still the base.
BOOST_CHECK(background.VerifyBestBlock(BlockHash(10)));
}
for (const uint32_t height : {9, 10, 11, 12}) {
Payload value;
BOOST_REQUIRE(background.Read(PayloadKey(height), value));
BOOST_CHECK(value == PayloadFor(height));
}
BOOST_REQUIRE(background.FinishPromotion(BlockHash(12)));
BOOST_CHECK(!background.ReadPendingPromotion());
BOOST_CHECK(background.VerifyBestBlock(BlockHash(12)));
}
BOOST_AUTO_TEST_SUITE_END()