16#include <boost/test/unit_test.hpp>
25 for (
const bool obfuscate : {
false,
true}) {
26 constexpr size_t CACHE_SIZE{1_MiB};
27 const fs::path path{m_args.GetDataDirBase() /
"dbwrapper"};
30 std::vector<std::pair<uint8_t, uint256>> key_values{};
34 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data =
true, .obfuscate = obfuscate}};
41 for (uint8_t
k{0};
k < 10; ++
k) {
44 dbw.Write(key, value);
45 key_values.emplace_back(key, value);
51 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate =
false}};
57 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
64 for (
const auto& [key, expected_value] : key_values) {
76 for (
bool obfuscate : {
false,
true}) {
77 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_1_obfuscate_true" :
"dbwrapper_1_obfuscate_false");
78 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
false, .wipe_data =
true, .obfuscate = obfuscate});
88 std::string key_block =
"b" + m_rng.rand256().ToString();
90 uint256 in_block = m_rng.rand256();
91 dbw.Write(key_block, in_block);
96 std::string key_file =
strprintf(
"f%04x", m_rng.rand32());
98 uint256 in_file_info = m_rng.rand256();
99 dbw.Write(key_file, in_file_info);
104 std::string key_transaction =
"t" + m_rng.rand256().ToString();
106 uint256 in_transaction = m_rng.rand256();
107 dbw.Write(key_transaction, in_transaction);
112 std::string key_utxo =
"c" + m_rng.rand256().ToString();
114 uint256 in_utxo = m_rng.rand256();
115 dbw.Write(key_utxo, in_utxo);
120 uint8_t key_last_blockfile_number{
'l'};
121 uint32_t lastblockfilenumber = m_rng.rand32();
122 dbw.Write(key_last_blockfile_number, lastblockfilenumber);
123 BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
127 uint8_t key_IsReindexing{
'R'};
128 bool isInReindexing = m_rng.randbool();
129 dbw.Write(key_IsReindexing, isInReindexing);
134 uint8_t key_lastblockhash_uxto{
'B'};
135 uint256 lastblock_hash = m_rng.rand256();
136 dbw.Write(key_lastblockhash_uxto, lastblock_hash);
137 BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
141 std::string file_option_tag =
"F";
142 uint8_t filename_length = m_rng.randbits(8);
143 std::string filename =
"randomfilename";
144 std::string key_file_option =
strprintf(
"%s%01x%s", file_option_tag, filename_length, filename);
146 bool in_file_bool = m_rng.randbool();
147 dbw.Write(key_file_option, in_file_bool);
157 for (
const bool obfuscate : {
false,
true}) {
158 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_batch_obfuscate_true" :
"dbwrapper_batch_obfuscate_false");
159 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
171 batch.
Write(key, in);
172 batch.
Write(key2, in2);
173 batch.
Write(key3, in3);
178 dbw.WriteBatch(batch);
189 batch.
Write(key3, in3);
190 dbw.WriteBatch(batch);
200 for (
const bool obfuscate : {
false,
true}) {
201 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_iterator_obfuscate_true" :
"dbwrapper_iterator_obfuscate_false");
202 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
210 dbw.Write(key2, in2);
212 std::unique_ptr<CDBIterator> it(dbw.NewIterator());
218 uint16_t key_too_large{0};
223 BOOST_REQUIRE(it->GetKey(key_res));
226 std::pair<uint256, uint8_t> value_too_large;
230 BOOST_REQUIRE(it->GetValue(val_res));
235 BOOST_REQUIRE(it->GetKey(key_res));
237 BOOST_REQUIRE(it->GetValue(val_res));
242 BOOST_REQUIRE(it->GetKey(key_res));
244 BOOST_REQUIRE(it->GetValue(val_res));
249 BOOST_REQUIRE(it->GetKey(key_res));
251 BOOST_REQUIRE(it->GetValue(val_res));
263 fs::path ph = m_args.GetDataDirBase() /
"existing_data_no_obfuscate";
264 fs::create_directories(ph);
267 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
280 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
true});
295 odbw.Write(key, in2);
304 fs::path ph = m_args.GetDataDirBase() /
"existing_data_reindex";
305 fs::create_directories(ph);
308 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
321 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
true, .obfuscate =
true});
332 odbw.Write(key, in2);
339 fs::path ph = m_args.GetDataDirBase() /
"iterator_ordering";
340 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
341 for (
int x=0x00; x<256; ++x) {
343 uint32_t value = x*x;
344 if (!(x & 1)) dbw.
Write(key, value);
348 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
350 for (
unsigned int x=0x00; x<256; ++x) {
352 uint32_t value = x*x;
353 if (x & 1) dbw.Write(key, value);
356 for (
const int seek_start : {0x00, 0x80}) {
357 it->Seek((uint8_t)seek_start);
358 for (
unsigned int x=seek_start; x<255; ++x) {
385 template<
typename Stream>
388 for (
size_t i = 0; i <
str.size(); i++) {
389 s << uint8_t(
str[i]);
393 template<
typename Stream>
407 fs::path ph = m_args.GetDataDirBase() /
"iterator_string_ordering";
408 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
409 for (
int x = 0; x < 10; ++x) {
410 for (
int y = 0; y < 10; ++y) {
412 for (
int z = 0; z < y; ++z)
414 uint32_t value = x*x;
419 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
420 for (
const int seek_start : {0, 5}) {
422 for (
unsigned int x = seek_start; x < 10; ++x) {
423 for (
int y = 0; y < 10; ++y) {
425 for (
int z = 0; z < y; ++z)
449 fs::path ph = m_args.GetDataDirBase() /
"test_runner_₿_🏃_20191128_104644";
450 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB});
452 fs::path lockPath = ph /
"LOCK";
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
void Write(const K &key, const V &value, bool fSync=false)
std::string ToString() const
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(dbwrapper)
static bool exists(const path &p)
const Obfuscation & GetObfuscation(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
StringContentsSerializer()=default
void Unserialize(Stream &s)
void Serialize(Stream &s) const
StringContentsSerializer(const std::string &inp)