forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevo_db_tests.cpp
More file actions
313 lines (271 loc) · 11.5 KB
/
Copy pathevo_db_tests.cpp
File metadata and controls
313 lines (271 loc) · 11.5 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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, EvoDbIdentity identity, uint32_t height)
{
auto tx = db.BeginTransaction(identity);
db.Write(PayloadKey(height), PayloadFor(height));
tx->Commit();
}
void WriteMarker(CEvoDB& db, EvoDbIdentity identity, const uint256& hash)
{
auto tx = db.BeginTransaction(identity);
db.WriteBestBlock(identity, hash);
tx->Commit();
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(evo_db_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(own_overlay_tombstone)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_tombstone", .memory = true, .wipe = true}};
const auto key = PayloadKey(1);
WritePayload(db, EvoDbIdentity::NORMAL, 1);
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
{
auto tx = db.BeginTransaction(EvoDbIdentity::NORMAL);
db.Erase(key);
tx->Commit();
}
{
auto tx = db.BeginTransaction(EvoDbIdentity::NORMAL);
Payload value;
BOOST_CHECK(!db.Read(key, value));
}
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
Payload value;
BOOST_REQUIRE(db.Read(key, value));
BOOST_CHECK(value == PayloadFor(1));
}
}
BOOST_AUTO_TEST_CASE(transaction_less_access_uses_default_identity)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_default_identity", .memory = true, .wipe = true}};
const auto key = PayloadKey(7);
// Committed to the SNAPSHOT overlay but not yet flushed to disk.
WritePayload(db, EvoDbIdentity::SNAPSHOT, 7);
// Transaction-less reads default to NORMAL and must not see it.
Payload value;
BOOST_CHECK(!db.Read(key, value));
BOOST_CHECK(!db.Exists(key));
// Once the snapshot chainstate is active, transaction-less consumers
// resolve against the SNAPSHOT overlay.
db.SetDefaultIdentity(EvoDbIdentity::SNAPSHOT);
BOOST_REQUIRE(db.Read(key, value));
BOOST_CHECK(value == PayloadFor(7));
BOOST_CHECK(db.Exists(key));
// Transaction-less writes land in the default identity's overlay and stay
// invisible to the other identity.
const auto key2 = PayloadKey(8);
db.Write(key2, PayloadFor(8));
db.SetDefaultIdentity(EvoDbIdentity::NORMAL);
Payload value2;
BOOST_CHECK(!db.Read(key2, value2));
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
BOOST_REQUIRE(db.Read(key2, value2));
BOOST_CHECK(value2 == PayloadFor(8));
}
}
BOOST_AUTO_TEST_CASE(write_derived_verifies_other_unflushed_overlay)
{
const fs::path path = m_args.GetDataDirBase() / "evodb_derived_overlay";
const auto key = PayloadKey(2);
const auto payload = PayloadFor(2);
Payload mismatch = payload;
mismatch.push_back(0xff);
{
CEvoDB db{util::DbWrapperParams{.path = path, .memory = false, .wipe = true}};
{
auto tx = db.BeginTransaction(EvoDbIdentity::NORMAL);
BOOST_REQUIRE(db.WriteDerived(key, payload));
tx->Commit();
}
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
BOOST_CHECK(!db.WriteDerived(key, mismatch));
BOOST_REQUIRE(db.WriteDerived(key, payload));
tx->Commit();
}
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
// Destroying db drops NORMAL's unflushed context. The reopened value
// therefore proves SNAPSHOT's identical overlap did not suppress its write.
}
CEvoDB reloaded{util::DbWrapperParams{.path = path, .memory = false, .wipe = false}};
Payload value;
BOOST_REQUIRE(reloaded.Read(key, value));
BOOST_CHECK(value == payload);
}
BOOST_AUTO_TEST_CASE(write_derived_rejects_disk_mismatch)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_derived_mismatch", .memory = true, .wipe = true}};
const auto key = PayloadKey(3);
WritePayload(db, EvoDbIdentity::NORMAL, 3);
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
Payload mismatch = PayloadFor(3);
mismatch.push_back(0xff);
BOOST_CHECK(!db.WriteDerived(key, mismatch));
}
BOOST_AUTO_TEST_CASE(marker_flush_independence)
{
const fs::path path = m_args.GetDataDirBase() / "evodb_markers";
{
CEvoDB db{util::DbWrapperParams{.path = path, .memory = false, .wipe = true}};
WriteMarker(db, EvoDbIdentity::NORMAL, BlockHash(10));
WriteMarker(db, EvoDbIdentity::SNAPSHOT, BlockHash(100));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
WriteMarker(db, EvoDbIdentity::NORMAL, BlockHash(11));
WriteMarker(db, EvoDbIdentity::SNAPSHOT, BlockHash(101));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
}
{
CEvoDB db{util::DbWrapperParams{.path = path, .memory = false, .wipe = false}};
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::NORMAL, BlockHash(11)));
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::SNAPSHOT, BlockHash(100)));
WriteMarker(db, EvoDbIdentity::NORMAL, BlockHash(12));
WriteMarker(db, EvoDbIdentity::SNAPSHOT, BlockHash(102));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
}
{
CEvoDB db{util::DbWrapperParams{.path = path, .memory = false, .wipe = false}};
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::NORMAL, BlockHash(11)));
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::SNAPSHOT, BlockHash(102)));
}
}
BOOST_AUTO_TEST_CASE(normal_marker_preserves_legacy_key_bytes)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_legacy_key", .memory = true, .wipe = true}};
WriteMarker(db, EvoDbIdentity::NORMAL, BlockHash(20));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
CDataStream expected{SER_DISK, CLIENT_VERSION};
expected << EVODB_BEST_BLOCK;
std::unique_ptr<CDBIterator> it{db.GetRawDB().NewIterator()};
it->SeekToFirst();
BOOST_REQUIRE(it->Valid());
const CDataStream actual = it->GetKey();
BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
it->Next();
BOOST_CHECK(!it->Valid());
}
BOOST_AUTO_TEST_CASE(open_transaction_does_not_capture_other_threads)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_tx_thread", .memory = true, .wipe = true}};
db.SetDefaultIdentity(EvoDbIdentity::SNAPSHOT);
WritePayload(db, EvoDbIdentity::SNAPSHOT, 30);
// A transaction is one validation execution context, not a process-wide
// mode: while the background chainstate holds a NORMAL transaction open,
// concurrent transaction-less readers must still see the active snapshot's
// overlay.
auto tx = db.BeginTransaction(EvoDbIdentity::NORMAL);
Payload same_thread;
BOOST_CHECK(!db.Read(PayloadKey(30), same_thread));
Payload other_thread;
bool other_thread_read{false};
std::thread reader{[&] { other_thread_read = db.Read(PayloadKey(30), other_thread); }};
reader.join();
tx->Commit();
BOOST_REQUIRE(other_thread_read);
BOOST_CHECK(other_thread == PayloadFor(30));
}
BOOST_AUTO_TEST_CASE(snapshot_markers_can_be_discarded)
{
const fs::path path = m_args.GetDataDirBase() / "evodb_marker_rollback";
{
CEvoDB db{util::DbWrapperParams{.path = path, .memory = false, .wipe = true}};
WriteMarker(db, EvoDbIdentity::SNAPSHOT, BlockHash(40));
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
db.WriteSnapshotBaseMNListHash(BlockHash(4));
db.WriteBackgroundMNListHash(BlockHash(40), BlockHash(4));
db.WriteDualChainstateMarker();
tx->Commit();
}
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
BOOST_REQUIRE(db.HasDualChainstateMarker());
// Abandoning snapshot activation after the markers were committed must
// leave no trace: a stale dual-chainstate marker breaks legacy
// bootstrapping and a stale best-block marker would revive a future
// snapshot directory.
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
db.EraseSnapshotMarkers();
tx->Commit();
}
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
}
CEvoDB reopened{util::DbWrapperParams{.path = path, .memory = false, .wipe = false}};
uint256 hash;
uint256 hash2;
BOOST_CHECK(!reopened.ReadBestBlock(EvoDbIdentity::SNAPSHOT, hash));
BOOST_CHECK(!reopened.ReadSnapshotBaseMNListHash(hash));
BOOST_CHECK(!reopened.ReadBackgroundMNListHash(hash, hash2));
BOOST_CHECK(!reopened.HasDualChainstateMarker());
}
BOOST_AUTO_TEST_CASE(snapshot_marker_promotion_and_discard)
{
CEvoDB db{util::DbWrapperParams{.path = m_args.GetDataDirBase() / "evodb_promotion", .memory = true, .wipe = true}};
const uint256 normal_tip = BlockHash(30);
const uint256 snapshot_tip = BlockHash(300);
const uint256 mn_list_hash = BlockHash(3);
WriteMarker(db, EvoDbIdentity::NORMAL, normal_tip);
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
db.WriteBestBlock(EvoDbIdentity::SNAPSHOT, snapshot_tip);
db.WriteSnapshotBaseMNListHash(mn_list_hash);
db.WriteDualChainstateMarker();
tx->Commit();
}
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::NORMAL));
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
BOOST_REQUIRE(db.PromoteSnapshotMarkers(snapshot_tip));
// Promotion is idempotent across a restart after the synced batch lands.
BOOST_REQUIRE(db.PromoteSnapshotMarkers(snapshot_tip));
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::NORMAL, snapshot_tip));
uint256 value;
BOOST_CHECK(!db.ReadBestBlock(EvoDbIdentity::SNAPSHOT, value));
BOOST_CHECK(!db.ReadSnapshotBaseMNListHash(value));
BOOST_CHECK(!db.HasDualChainstateMarker());
WriteMarker(db, EvoDbIdentity::SNAPSHOT, BlockHash(301));
{
auto tx = db.BeginTransaction(EvoDbIdentity::SNAPSHOT);
db.WriteSnapshotBaseMNListHash(BlockHash(4));
db.WriteDualChainstateMarker();
tx->Commit();
}
BOOST_REQUIRE(db.CommitRootTransaction(EvoDbIdentity::SNAPSHOT));
BOOST_REQUIRE(db.DiscardSnapshotMarkers());
// Discard is likewise safe to retry.
BOOST_REQUIRE(db.DiscardSnapshotMarkers());
BOOST_CHECK(db.VerifyBestBlock(EvoDbIdentity::NORMAL, snapshot_tip));
BOOST_CHECK(!db.ReadBestBlock(EvoDbIdentity::SNAPSHOT, value));
BOOST_CHECK(!db.ReadSnapshotBaseMNListHash(value));
BOOST_CHECK(!db.HasDualChainstateMarker());
}
BOOST_AUTO_TEST_SUITE_END()