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);
193 for (
const bool obfuscate : {
false,
true}) {
194 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_iterator_obfuscate_true" :
"dbwrapper_iterator_obfuscate_false");
195 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
203 dbw.Write(key2, in2);
205 std::unique_ptr<CDBIterator> it(dbw.NewIterator());
211 uint16_t key_too_large{0};
217 BOOST_REQUIRE(it->GetKey(key_res));
218 BOOST_REQUIRE(it->GetValue(val_res));
224 BOOST_REQUIRE(it->GetKey(key_res));
225 BOOST_REQUIRE(it->GetValue(val_res));
238 fs::path ph = m_args.GetDataDirBase() /
"existing_data_no_obfuscate";
239 fs::create_directories(ph);
242 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
255 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
true});
270 odbw.Write(key, in2);
279 fs::path ph = m_args.GetDataDirBase() /
"existing_data_reindex";
280 fs::create_directories(ph);
283 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
296 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
true, .obfuscate =
true});
307 odbw.Write(key, in2);
314 fs::path ph = m_args.GetDataDirBase() /
"iterator_ordering";
315 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
316 for (
int x=0x00; x<256; ++x) {
318 uint32_t value = x*x;
319 if (!(x & 1)) dbw.
Write(key, value);
323 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
325 for (
unsigned int x=0x00; x<256; ++x) {
327 uint32_t value = x*x;
328 if (x & 1) dbw.Write(key, value);
331 for (
const int seek_start : {0x00, 0x80}) {
332 it->Seek((uint8_t)seek_start);
333 for (
unsigned int x=seek_start; x<255; ++x) {
360 template<
typename Stream>
363 for (
size_t i = 0; i <
str.size(); i++) {
364 s << uint8_t(
str[i]);
368 template<
typename Stream>
382 fs::path ph = m_args.GetDataDirBase() /
"iterator_string_ordering";
383 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
384 for (
int x = 0; x < 10; ++x) {
385 for (
int y = 0; y < 10; ++y) {
387 for (
int z = 0; z < y; ++z)
389 uint32_t value = x*x;
394 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
395 for (
const int seek_start : {0, 5}) {
397 for (
unsigned int x = seek_start; x < 10; ++x) {
398 for (
int y = 0; y < 10; ++y) {
400 for (
int z = 0; z < y; ++z)
424 fs::path ph = m_args.GetDataDirBase() /
"test_runner_₿_🏃_20191128_104644";
425 CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB});
427 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)