2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
2014-11-04 14:34:04 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-09-03 19:05:30 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <dbwrapper.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <random.h>
|
2012-09-03 19:05:30 +02:00
|
|
|
|
|
|
|
#include <leveldb/cache.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <leveldb/env.h>
|
2012-09-03 19:05:30 +02:00
|
|
|
#include <leveldb/filter_policy.h>
|
2013-09-09 04:02:28 +02:00
|
|
|
#include <memenv.h>
|
2015-09-08 00:22:23 +02:00
|
|
|
#include <stdint.h>
|
2017-03-15 10:43:00 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
class CBitcoinLevelDBLogger : public leveldb::Logger {
|
|
|
|
public:
|
|
|
|
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
|
|
|
|
// Please do not do this in normal code
|
2017-06-29 12:57:45 +02:00
|
|
|
void Logv(const char * format, va_list ap) override {
|
2016-12-25 21:19:40 +01:00
|
|
|
if (!LogAcceptCategory(BCLog::LEVELDB)) {
|
2017-03-15 10:43:00 +01:00
|
|
|
return;
|
2016-12-25 21:19:40 +01:00
|
|
|
}
|
2017-03-15 10:43:00 +01:00
|
|
|
char buffer[500];
|
|
|
|
for (int iter = 0; iter < 2; iter++) {
|
|
|
|
char* base;
|
|
|
|
int bufsize;
|
|
|
|
if (iter == 0) {
|
|
|
|
bufsize = sizeof(buffer);
|
|
|
|
base = buffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bufsize = 30000;
|
|
|
|
base = new char[bufsize];
|
|
|
|
}
|
|
|
|
char* p = base;
|
|
|
|
char* limit = base + bufsize;
|
|
|
|
|
|
|
|
// Print the message
|
|
|
|
if (p < limit) {
|
|
|
|
va_list backup_ap;
|
|
|
|
va_copy(backup_ap, ap);
|
|
|
|
// Do not use vsnprintf elsewhere in bitcoin source code, see above.
|
|
|
|
p += vsnprintf(p, limit - p, format, backup_ap);
|
|
|
|
va_end(backup_ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate to available space if necessary
|
|
|
|
if (p >= limit) {
|
|
|
|
if (iter == 0) {
|
|
|
|
continue; // Try again with larger buffer
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p = limit - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add newline if necessary
|
|
|
|
if (p == base || p[-1] != '\n') {
|
|
|
|
*p++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(p <= limit);
|
|
|
|
base[std::min(bufsize - 1, (int)(p - base))] = '\0';
|
2017-11-30 09:27:19 +01:00
|
|
|
LogPrintf("leveldb: %s", base);
|
2017-03-15 10:43:00 +01:00
|
|
|
if (base != buffer) {
|
|
|
|
delete[] base;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-09-03 19:05:30 +02:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
static leveldb::Options GetOptions(size_t nCacheSize)
|
|
|
|
{
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Options options;
|
2012-11-04 17:11:48 +01:00
|
|
|
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
|
|
|
|
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
|
2012-09-03 19:05:30 +02:00
|
|
|
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
|
|
|
|
options.compression = leveldb::kNoCompression;
|
2013-04-24 00:10:23 +02:00
|
|
|
options.max_open_files = 64;
|
2017-03-15 10:43:00 +01:00
|
|
|
options.info_log = new CBitcoinLevelDBLogger();
|
2014-05-12 12:24:22 +02:00
|
|
|
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
|
|
|
|
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
|
|
|
|
// on corruption in later versions.
|
|
|
|
options.paranoid_checks = true;
|
|
|
|
}
|
2012-09-03 19:05:30 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:05:50 +01:00
|
|
|
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
|
2014-09-19 19:21:46 +02:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
penv = nullptr;
|
2012-09-03 19:05:30 +02:00
|
|
|
readoptions.verify_checksums = true;
|
|
|
|
iteroptions.verify_checksums = true;
|
|
|
|
iteroptions.fill_cache = false;
|
|
|
|
syncoptions.sync = true;
|
2012-11-04 17:11:48 +01:00
|
|
|
options = GetOptions(nCacheSize);
|
2012-09-03 19:05:30 +02:00
|
|
|
options.create_if_missing = true;
|
2012-09-04 18:12:00 +02:00
|
|
|
if (fMemory) {
|
|
|
|
penv = leveldb::NewMemEnv(leveldb::Env::Default());
|
|
|
|
options.env = penv;
|
|
|
|
} else {
|
2012-10-21 21:23:13 +02:00
|
|
|
if (fWipe) {
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Wiping LevelDB in %s\n", path.string());
|
2015-08-13 01:32:20 +02:00
|
|
|
leveldb::Status result = leveldb::DestroyDB(path.string(), options);
|
2016-04-20 11:48:57 +02:00
|
|
|
dbwrapper_private::HandleError(result);
|
2012-10-21 21:23:13 +02:00
|
|
|
}
|
2017-02-22 10:10:00 +01:00
|
|
|
TryCreateDirectories(path);
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Opening LevelDB in %s\n", path.string());
|
2012-09-04 18:12:00 +02:00
|
|
|
}
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
|
2016-04-20 11:48:57 +02:00
|
|
|
dbwrapper_private::HandleError(status);
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("Opened LevelDB successfully\n");
|
2015-09-08 00:22:23 +02:00
|
|
|
|
2017-08-01 21:17:40 +02:00
|
|
|
if (gArgs.GetBoolArg("-forcecompactdb", false)) {
|
2017-08-04 01:23:43 +02:00
|
|
|
LogPrintf("Starting database compaction of %s\n", path.string());
|
|
|
|
pdb->CompactRange(nullptr, nullptr);
|
|
|
|
LogPrintf("Finished database compaction of %s\n", path.string());
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:22:23 +02:00
|
|
|
// 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()) {
|
2015-10-23 02:49:02 +02:00
|
|
|
// Initialize non-degenerate obfuscation if it won't upset
|
2015-09-08 00:22:23 +02:00
|
|
|
// existing, non-obfuscated data.
|
|
|
|
std::vector<unsigned char> new_key = CreateObfuscateKey();
|
|
|
|
|
|
|
|
// Write `new_key` so we don't obfuscate the key with itself
|
|
|
|
Write(OBFUSCATE_KEY_KEY, new_key);
|
|
|
|
obfuscate_key = new_key;
|
|
|
|
|
2016-04-20 09:08:45 +02:00
|
|
|
LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), HexStr(obfuscate_key));
|
2015-09-08 00:22:23 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:08:45 +02:00
|
|
|
LogPrintf("Using obfuscation key for %s: %s\n", path.string(), HexStr(obfuscate_key));
|
2012-09-03 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-23 03:02:20 +02:00
|
|
|
CDBWrapper::~CDBWrapper()
|
2014-09-19 19:21:46 +02:00
|
|
|
{
|
2012-09-03 19:05:30 +02:00
|
|
|
delete pdb;
|
2017-08-07 07:36:37 +02:00
|
|
|
pdb = nullptr;
|
2012-09-03 19:05:30 +02:00
|
|
|
delete options.filter_policy;
|
2017-08-07 07:36:37 +02:00
|
|
|
options.filter_policy = nullptr;
|
2017-03-15 10:43:00 +01:00
|
|
|
delete options.info_log;
|
2017-08-07 07:36:37 +02:00
|
|
|
options.info_log = nullptr;
|
2012-09-03 19:05:30 +02:00
|
|
|
delete options.block_cache;
|
2017-08-07 07:36:37 +02:00
|
|
|
options.block_cache = nullptr;
|
2012-09-03 19:05:30 +02:00
|
|
|
delete penv;
|
2017-08-07 07:36:37 +02:00
|
|
|
options.env = nullptr;
|
2012-09-03 19:05:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:05:12 +02:00
|
|
|
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
2014-09-19 19:21:46 +02:00
|
|
|
{
|
2012-09-03 19:05:30 +02:00
|
|
|
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
|
2016-04-20 11:48:57 +02:00
|
|
|
dbwrapper_private::HandleError(status);
|
2012-09-03 19:05:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-08 00:22:23 +02:00
|
|
|
|
|
|
|
// Prefixed with null character to avoid collisions with other keys
|
|
|
|
//
|
|
|
|
// We must use a string constructor which specifies length so that we copy
|
|
|
|
// past the null-terminator.
|
2015-10-23 03:02:20 +02:00
|
|
|
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
|
2015-09-08 00:22:23 +02:00
|
|
|
|
2015-10-23 03:02:20 +02:00
|
|
|
const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
|
2015-09-08 00:22:23 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-23 02:49:02 +02:00
|
|
|
* Returns a string (consisting of 8 random bytes) suitable for use as an
|
|
|
|
* obfuscating XOR key.
|
2015-09-08 00:22:23 +02:00
|
|
|
*/
|
2015-10-23 03:02:20 +02:00
|
|
|
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
|
2015-09-08 00:22:23 +02:00
|
|
|
{
|
|
|
|
unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
|
|
|
|
GetRandBytes(buff, OBFUSCATE_KEY_NUM_BYTES);
|
|
|
|
return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-23 03:02:20 +02:00
|
|
|
bool CDBWrapper::IsEmpty()
|
2015-09-08 00:22:23 +02:00
|
|
|
{
|
2016-08-30 22:41:56 +02:00
|
|
|
std::unique_ptr<CDBIterator> it(NewIterator());
|
2015-09-08 00:22:23 +02:00
|
|
|
it->SeekToFirst();
|
|
|
|
return !(it->Valid());
|
|
|
|
}
|
|
|
|
|
2015-10-23 03:02:20 +02:00
|
|
|
CDBIterator::~CDBIterator() { delete piter; }
|
2017-03-09 13:34:54 +01:00
|
|
|
bool CDBIterator::Valid() const { return piter->Valid(); }
|
2015-10-23 03:02:20 +02:00
|
|
|
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
|
|
|
|
void CDBIterator::Next() { piter->Next(); }
|
2016-04-20 11:46:01 +02:00
|
|
|
|
|
|
|
namespace dbwrapper_private {
|
|
|
|
|
2016-04-20 11:48:57 +02:00
|
|
|
void HandleError(const leveldb::Status& status)
|
|
|
|
{
|
|
|
|
if (status.ok())
|
|
|
|
return;
|
|
|
|
LogPrintf("%s\n", status.ToString());
|
|
|
|
if (status.IsCorruption())
|
|
|
|
throw dbwrapper_error("Database corrupted");
|
|
|
|
if (status.IsIOError())
|
|
|
|
throw dbwrapper_error("Database I/O error");
|
|
|
|
if (status.IsNotFound())
|
|
|
|
throw dbwrapper_error("Database entry missing");
|
|
|
|
throw dbwrapper_error("Unknown database error");
|
|
|
|
}
|
|
|
|
|
2016-04-20 11:46:01 +02:00
|
|
|
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
|
|
|
|
{
|
|
|
|
return w.obfuscate_key;
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:21:25 +02:00
|
|
|
} // namespace dbwrapper_private
|