Skip to content

Commit

Permalink
CDBWrapper: Add constructor using Options struct
Browse files Browse the repository at this point in the history
The non-Options constructor is kept (it now calls the Options version of
the constructor) so that we can fix callsites one-by-one without
breaking builds, it will be removed later in this patchset.
  • Loading branch information
dongcarl committed Jul 20, 2022
1 parent 5560682 commit df3463e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
30 changes: 15 additions & 15 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,48 +127,48 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options;
}

CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
: m_name{fs::PathToString(path.stem())}
CDBWrapper::CDBWrapper(const Options& opts)
: m_name{fs::PathToString(opts.db_path.stem())}
{
penv = nullptr;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
options = GetOptions(nCacheSize);
options = GetOptions(opts.cache_size);
options.create_if_missing = true;
if (fMemory) {
if (opts.in_memory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());
options.env = penv;
} else {
if (fWipe) {
LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(path));
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(path), options);
if (opts.wipe_existing) {
LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(opts.db_path));
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(opts.db_path), options);
dbwrapper_private::HandleError(result);
}
TryCreateDirectories(path);
LogPrintf("Opening LevelDB in %s\n", fs::PathToString(path));
TryCreateDirectories(opts.db_path);
LogPrintf("Opening LevelDB in %s\n", fs::PathToString(opts.db_path));
}
// PathToString() return value is safe to pass to leveldb open function,
// because on POSIX leveldb passes the byte string directly to ::open(), and
// on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
// (see env_posix.cc and env_windows.cc).
leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(path), &pdb);
leveldb::Status status = leveldb::DB::Open(options, fs::PathToString(opts.db_path), &pdb);
dbwrapper_private::HandleError(status);
LogPrintf("Opened LevelDB successfully\n");

if (gArgs.GetBoolArg("-forcecompactdb", false)) {
LogPrintf("Starting database compaction of %s\n", fs::PathToString(path));
LogPrintf("Starting database compaction of %s\n", fs::PathToString(opts.db_path));
pdb->CompactRange(nullptr, nullptr);
LogPrintf("Finished database compaction of %s\n", fs::PathToString(path));
LogPrintf("Finished database compaction of %s\n", fs::PathToString(opts.db_path));
}

// The base-case obfuscation key, which is a noop.
obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');

bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);

if (!key_exists && obfuscate && IsEmpty()) {
if (!key_exists && opts.obfuscate_data && IsEmpty()) {
// Initialize non-degenerate obfuscation if it won't upset
// existing, non-obfuscated data.
std::vector<unsigned char> new_key = CreateObfuscateKey();
Expand All @@ -177,10 +177,10 @@ CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bo
Write(OBFUSCATE_KEY_KEY, new_key);
obfuscate_key = new_key;

LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(path), HexStr(obfuscate_key));
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(opts.db_path), HexStr(obfuscate_key));
}

LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(path), HexStr(obfuscate_key));
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(opts.db_path), HexStr(obfuscate_key));
}

CDBWrapper::~CDBWrapper()
Expand Down
27 changes: 18 additions & 9 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,24 @@ class CDBWrapper
std::vector<unsigned char> CreateObfuscateKey() const;

public:
/**
* @param[in] path Location in the filesystem where leveldb data will be stored.
* @param[in] nCacheSize Configures various leveldb cache settings.
* @param[in] fMemory If true, use leveldb's memory environment.
* @param[in] fWipe If true, remove all existing data.
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
* with a zero'd byte array.
*/
CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);

struct Options {
fs::path db_path;
size_t cache_size;
bool in_memory = false;
bool wipe_existing = false;
bool obfuscate_data = false;
};

CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false)
: CDBWrapper{{
.db_path = path,
.cache_size = nCacheSize,
.in_memory = fMemory,
.wipe_existing = fWipe,
.obfuscate_data = obfuscate,
}} {}
CDBWrapper(const Options& opts);
~CDBWrapper();

CDBWrapper(const CDBWrapper&) = delete;
Expand Down

0 comments on commit df3463e

Please sign in to comment.