Changeset View
Changeset View
Standalone View
Standalone View
src/dbwrapper.cpp
Show First 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | void Logv(const char *format, va_list ap) override { | ||||
// Add newline if necessary | // Add newline if necessary | ||||
if (p == base || p[-1] != '\n') { | if (p == base || p[-1] != '\n') { | ||||
*p++ = '\n'; | *p++ = '\n'; | ||||
} | } | ||||
assert(p <= limit); | assert(p <= limit); | ||||
base[std::min(bufsize - 1, (int)(p - base))] = '\0'; | base[std::min(bufsize - 1, (int)(p - base))] = '\0'; | ||||
LogPrintf("leveldb: %s", base); | LogPrint(BCLog::LEVELDB, "leveldb: %s", base); | ||||
if (base != buffer) { | if (base != buffer) { | ||||
delete[] base; | delete[] base; | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
Show All 24 Lines | CDBWrapper::CDBWrapper(const fs::path &path, size_t nCacheSize, bool fMemory, | ||||
syncoptions.sync = true; | syncoptions.sync = true; | ||||
options = GetOptions(nCacheSize); | options = GetOptions(nCacheSize); | ||||
options.create_if_missing = true; | options.create_if_missing = true; | ||||
if (fMemory) { | if (fMemory) { | ||||
penv = leveldb::NewMemEnv(leveldb::Env::Default()); | penv = leveldb::NewMemEnv(leveldb::Env::Default()); | ||||
options.env = penv; | options.env = penv; | ||||
} else { | } else { | ||||
if (fWipe) { | if (fWipe) { | ||||
LogPrintf("Wiping LevelDB in %s\n", path.string()); | LogPrint(BCLog::LEVELDB, "Wiping LevelDB in %s\n", path.string()); | ||||
leveldb::Status result = leveldb::DestroyDB(path.string(), options); | leveldb::Status result = leveldb::DestroyDB(path.string(), options); | ||||
dbwrapper_private::HandleError(result); | dbwrapper_private::HandleError(result); | ||||
} | } | ||||
TryCreateDirectories(path); | TryCreateDirectories(path); | ||||
LogPrintf("Opening LevelDB in %s\n", path.string()); | LogPrint(BCLog::LEVELDB, "Opening LevelDB in %s\n", path.string()); | ||||
} | } | ||||
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); | leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); | ||||
dbwrapper_private::HandleError(status); | dbwrapper_private::HandleError(status); | ||||
LogPrintf("Opened LevelDB successfully\n"); | LogPrint(BCLog::LEVELDB, "Opened LevelDB successfully\n"); | ||||
if (gArgs.GetBoolArg("-forcecompactdb", false)) { | if (gArgs.GetBoolArg("-forcecompactdb", false)) { | ||||
LogPrintf("Starting database compaction of %s\n", path.string()); | LogPrint(BCLog::LEVELDB, "Starting database compaction of %s\n", | ||||
path.string()); | |||||
pdb->CompactRange(nullptr, nullptr); | pdb->CompactRange(nullptr, nullptr); | ||||
LogPrintf("Finished database compaction of %s\n", path.string()); | LogPrint(BCLog::LEVELDB, "Finished database compaction of %s\n", | ||||
path.string()); | |||||
} | } | ||||
// The base-case obfuscation key, which is a noop. | // The base-case obfuscation key, which is a noop. | ||||
obfuscate_key = std::vector<uint8_t>(OBFUSCATE_KEY_NUM_BYTES, '\000'); | obfuscate_key = std::vector<uint8_t>(OBFUSCATE_KEY_NUM_BYTES, '\000'); | ||||
bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key); | bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key); | ||||
if (!key_exists && obfuscate && IsEmpty()) { | if (!key_exists && obfuscate && IsEmpty()) { | ||||
// Initialize non-degenerate obfuscation if it won't upset existing, | // Initialize non-degenerate obfuscation if it won't upset existing, | ||||
// non-obfuscated data. | // non-obfuscated data. | ||||
std::vector<uint8_t> new_key = CreateObfuscateKey(); | std::vector<uint8_t> new_key = CreateObfuscateKey(); | ||||
// Write `new_key` so we don't obfuscate the key with itself | // Write `new_key` so we don't obfuscate the key with itself | ||||
Write(OBFUSCATE_KEY_KEY, new_key); | Write(OBFUSCATE_KEY_KEY, new_key); | ||||
obfuscate_key = new_key; | obfuscate_key = new_key; | ||||
LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), | LogPrint(BCLog::LEVELDB, "Wrote new obfuscate key for %s: %s\n", | ||||
HexStr(obfuscate_key)); | path.string(), HexStr(obfuscate_key)); | ||||
} | } | ||||
LogPrintf("Using obfuscation key for %s: %s\n", path.string(), | LogPrint(BCLog::LEVELDB, "Using obfuscation key for %s: %s\n", | ||||
HexStr(obfuscate_key)); | path.string(), HexStr(obfuscate_key)); | ||||
} | } | ||||
CDBWrapper::~CDBWrapper() { | CDBWrapper::~CDBWrapper() { | ||||
delete pdb; | delete pdb; | ||||
pdb = nullptr; | pdb = nullptr; | ||||
delete options.filter_policy; | delete options.filter_policy; | ||||
options.filter_policy = nullptr; | options.filter_policy = nullptr; | ||||
delete options.info_log; | delete options.info_log; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
namespace dbwrapper_private { | namespace dbwrapper_private { | ||||
void HandleError(const leveldb::Status &status) { | void HandleError(const leveldb::Status &status) { | ||||
if (status.ok()) { | if (status.ok()) { | ||||
return; | return; | ||||
} | } | ||||
LogPrintf("%s\n", status.ToString()); | LogPrint(BCLog::LEVELDB, "%s\n", status.ToString()); | ||||
if (status.IsCorruption()) { | if (status.IsCorruption()) { | ||||
throw dbwrapper_error("Database corrupted"); | throw dbwrapper_error("Database corrupted"); | ||||
} | } | ||||
if (status.IsIOError()) { | if (status.IsIOError()) { | ||||
throw dbwrapper_error("Database I/O error"); | throw dbwrapper_error("Database I/O error"); | ||||
} | } | ||||
if (status.IsNotFound()) { | if (status.IsNotFound()) { | ||||
throw dbwrapper_error("Database entry missing"); | throw dbwrapper_error("Database entry missing"); | ||||
} | } | ||||
throw dbwrapper_error("Unknown database error"); | throw dbwrapper_error("Unknown database error"); | ||||
} | } | ||||
const std::vector<uint8_t> &GetObfuscateKey(const CDBWrapper &w) { | const std::vector<uint8_t> &GetObfuscateKey(const CDBWrapper &w) { | ||||
return w.obfuscate_key; | return w.obfuscate_key; | ||||
} | } | ||||
}; // namespace dbwrapper_private | }; // namespace dbwrapper_private |