2015-12-13 08:58:29 -08:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-10-30 17:43:19 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-09-03 10:05:30 -07:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-11-05 17:58:43 -08:00
|
|
|
|
2015-10-22 18:33:06 -07:00
|
|
|
#ifndef BITCOIN_DBWRAPPER_H
|
|
|
|
#define BITCOIN_DBWRAPPER_H
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2014-10-28 18:33:23 -07:00
|
|
|
#include "clientversion.h"
|
2012-09-03 10:05:30 -07:00
|
|
|
#include "serialize.h"
|
2014-10-22 12:08:30 -07:00
|
|
|
#include "streams.h"
|
2013-09-18 03:38:08 -07:00
|
|
|
#include "util.h"
|
2015-09-07 15:22:23 -07:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "version.h"
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-09-14 03:43:56 -07:00
|
|
|
|
2012-09-03 10:05:30 -07:00
|
|
|
#include <leveldb/db.h>
|
|
|
|
#include <leveldb/write_batch.h>
|
|
|
|
|
2015-10-22 18:02:20 -07:00
|
|
|
class dbwrapper_error : public std::runtime_error
|
2013-01-28 16:44:19 -08:00
|
|
|
{
|
|
|
|
public:
|
2015-10-22 18:02:20 -07:00
|
|
|
dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
|
2013-01-28 16:44:19 -08:00
|
|
|
};
|
|
|
|
|
2016-04-20 00:05:12 -07:00
|
|
|
void HandleError(const leveldb::Status& status);
|
2013-01-28 16:44:19 -08:00
|
|
|
|
2015-10-22 18:02:20 -07:00
|
|
|
/** Batch of changes queued to be written to a CDBWrapper */
|
|
|
|
class CDBBatch
|
2012-09-03 10:05:30 -07:00
|
|
|
{
|
2015-10-22 18:02:20 -07:00
|
|
|
friend class CDBWrapper;
|
2012-09-03 10:05:30 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
leveldb::WriteBatch batch;
|
2015-10-09 10:55:27 -07:00
|
|
|
const std::vector<unsigned char> *obfuscate_key;
|
2012-09-03 10:05:30 -07:00
|
|
|
|
|
|
|
public:
|
2015-09-07 15:22:23 -07:00
|
|
|
/**
|
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key.
|
|
|
|
*/
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { };
|
2015-09-07 15:22:23 -07:00
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K, typename V>
|
|
|
|
void Write(const K& key, const V& value)
|
|
|
|
{
|
2012-09-03 10:05:30 -07:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key));
|
|
|
|
ssKey << key;
|
|
|
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssValue.reserve(ssValue.GetSerializeSize(value));
|
|
|
|
ssValue << value;
|
2015-10-09 10:55:27 -07:00
|
|
|
ssValue.Xor(*obfuscate_key);
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::Slice slValue(&ssValue[0], ssValue.size());
|
|
|
|
|
|
|
|
batch.Put(slKey, slValue);
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K>
|
|
|
|
void Erase(const K& key)
|
|
|
|
{
|
2012-09-03 10:05:30 -07:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key));
|
|
|
|
ssKey << key;
|
|
|
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
batch.Delete(slKey);
|
|
|
|
}
|
|
|
|
};
|
2015-10-22 17:49:02 -07:00
|
|
|
|
2015-10-22 18:02:20 -07:00
|
|
|
class CDBIterator
|
2015-10-07 17:12:24 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
leveldb::Iterator *piter;
|
2015-10-09 10:55:27 -07:00
|
|
|
const std::vector<unsigned char> *obfuscate_key;
|
2015-10-07 17:12:24 -07:00
|
|
|
|
|
|
|
public:
|
2015-10-08 00:44:10 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param[in] piterIn The original leveldb iterator.
|
|
|
|
* @param[in] obfuscate_key If passed, XOR data with this key.
|
|
|
|
*/
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) :
|
2015-10-08 00:44:10 -07:00
|
|
|
piter(piterIn), obfuscate_key(obfuscate_key) { };
|
2015-10-22 18:02:20 -07:00
|
|
|
~CDBIterator();
|
2015-10-07 17:12:24 -07:00
|
|
|
|
|
|
|
bool Valid();
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2015-10-07 17:12:24 -07:00
|
|
|
void SeekToFirst();
|
|
|
|
|
|
|
|
template<typename K> void Seek(const K& key) {
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key));
|
|
|
|
ssKey << key;
|
|
|
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
|
|
|
piter->Seek(slKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Next();
|
|
|
|
|
|
|
|
template<typename K> bool GetKey(K& key) {
|
|
|
|
leveldb::Slice slKey = piter->key();
|
|
|
|
try {
|
|
|
|
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey >> key;
|
2015-10-27 09:39:42 -07:00
|
|
|
} catch (const std::exception&) {
|
2015-10-07 17:12:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetKeySize() {
|
|
|
|
return piter->key().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename V> bool GetValue(V& value) {
|
|
|
|
leveldb::Slice slValue = piter->value();
|
|
|
|
try {
|
|
|
|
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
|
2015-10-09 10:55:27 -07:00
|
|
|
ssValue.Xor(*obfuscate_key);
|
2015-10-07 17:12:24 -07:00
|
|
|
ssValue >> value;
|
2015-10-27 09:39:42 -07:00
|
|
|
} catch (const std::exception&) {
|
2015-10-07 17:12:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetValueSize() {
|
|
|
|
return piter->value().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2015-10-22 17:49:02 -07:00
|
|
|
|
2015-10-22 18:02:20 -07:00
|
|
|
class CDBWrapper
|
2012-09-03 10:05:30 -07:00
|
|
|
{
|
|
|
|
private:
|
2014-10-30 17:43:19 -07:00
|
|
|
//! custom environment this database is using (may be NULL in case of default environment)
|
2014-09-19 10:21:46 -07:00
|
|
|
leveldb::Env* penv;
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! database options used
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::Options options;
|
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! options used when reading from the database
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::ReadOptions readoptions;
|
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! options used when iterating over values of the database
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::ReadOptions iteroptions;
|
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! options used when writing to the database
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::WriteOptions writeoptions;
|
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! options used when sync writing to the database
|
2012-09-03 10:05:30 -07:00
|
|
|
leveldb::WriteOptions syncoptions;
|
|
|
|
|
2014-10-30 17:43:19 -07:00
|
|
|
//! the database itself
|
2014-09-19 10:21:46 -07:00
|
|
|
leveldb::DB* pdb;
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2015-09-07 15:22:23 -07:00
|
|
|
//! a key used for optional XOR-obfuscation of the database
|
|
|
|
std::vector<unsigned char> obfuscate_key;
|
|
|
|
|
|
|
|
//! the key under which the obfuscation key is stored
|
|
|
|
static const std::string OBFUSCATE_KEY_KEY;
|
2015-10-22 17:49:02 -07:00
|
|
|
|
2015-09-07 15:22:23 -07:00
|
|
|
//! the length of the obfuscate key in number of bytes
|
|
|
|
static const unsigned int OBFUSCATE_KEY_NUM_BYTES;
|
2015-10-22 17:49:02 -07:00
|
|
|
|
2015-09-07 15:22:23 -07:00
|
|
|
std::vector<unsigned char> CreateObfuscateKey() const;
|
|
|
|
|
2012-09-03 10:05:30 -07:00
|
|
|
public:
|
2015-09-07 15:22:23 -07:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
|
|
|
|
~CDBWrapper();
|
2012-09-03 10:05:30 -07:00
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K, typename V>
|
2016-04-20 00:05:12 -07:00
|
|
|
bool Read(const K& key, V& value) const
|
2014-09-19 10:21:46 -07:00
|
|
|
{
|
2012-09-03 10:05:30 -07:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key));
|
|
|
|
ssKey << key;
|
|
|
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
std::string strValue;
|
|
|
|
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
|
|
|
if (!status.ok()) {
|
|
|
|
if (status.IsNotFound())
|
|
|
|
return false;
|
2014-05-21 03:50:46 -07:00
|
|
|
LogPrintf("LevelDB read failure: %s\n", status.ToString());
|
2013-01-28 16:44:19 -08:00
|
|
|
HandleError(status);
|
2012-09-03 10:05:30 -07:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
|
2015-09-07 15:22:23 -07:00
|
|
|
ssValue.Xor(obfuscate_key);
|
2012-09-03 10:05:30 -07:00
|
|
|
ssValue >> value;
|
2014-09-19 10:21:46 -07:00
|
|
|
} catch (const std::exception&) {
|
2012-09-03 10:05:30 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K, typename V>
|
2016-04-20 00:05:12 -07:00
|
|
|
bool Write(const K& key, const V& value, bool fSync = false)
|
2014-09-19 10:21:46 -07:00
|
|
|
{
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBBatch batch(&obfuscate_key);
|
2012-09-03 10:05:30 -07:00
|
|
|
batch.Write(key, value);
|
|
|
|
return WriteBatch(batch, fSync);
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K>
|
2016-04-20 00:05:12 -07:00
|
|
|
bool Exists(const K& key) const
|
2014-09-19 10:21:46 -07:00
|
|
|
{
|
2012-09-03 10:05:30 -07:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssKey.reserve(ssKey.GetSerializeSize(key));
|
|
|
|
ssKey << key;
|
|
|
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
std::string strValue;
|
|
|
|
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
|
|
|
if (!status.ok()) {
|
|
|
|
if (status.IsNotFound())
|
|
|
|
return false;
|
2014-05-21 03:50:46 -07:00
|
|
|
LogPrintf("LevelDB read failure: %s\n", status.ToString());
|
2013-01-28 16:44:19 -08:00
|
|
|
HandleError(status);
|
2012-09-03 10:05:30 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:21:46 -07:00
|
|
|
template <typename K>
|
2016-04-20 00:05:12 -07:00
|
|
|
bool Erase(const K& key, bool fSync = false)
|
2014-09-19 10:21:46 -07:00
|
|
|
{
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBBatch batch(&obfuscate_key);
|
2012-09-03 10:05:30 -07:00
|
|
|
batch.Erase(key);
|
|
|
|
return WriteBatch(batch, fSync);
|
|
|
|
}
|
|
|
|
|
2016-04-20 00:05:12 -07:00
|
|
|
bool WriteBatch(CDBBatch& batch, bool fSync = false);
|
2012-09-03 10:05:30 -07:00
|
|
|
|
|
|
|
// not available for LevelDB; provide for compatibility with BDB
|
2014-09-19 10:21:46 -07:00
|
|
|
bool Flush()
|
|
|
|
{
|
2012-09-03 10:05:30 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-20 00:05:12 -07:00
|
|
|
bool Sync()
|
2014-09-19 10:21:46 -07:00
|
|
|
{
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBBatch batch(&obfuscate_key);
|
2012-09-03 10:05:30 -07:00
|
|
|
return WriteBatch(batch, true);
|
|
|
|
}
|
|
|
|
|
2015-10-22 18:02:20 -07:00
|
|
|
CDBIterator *NewIterator()
|
2015-10-07 17:12:24 -07:00
|
|
|
{
|
2015-10-22 18:02:20 -07:00
|
|
|
return new CDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key);
|
2015-10-08 00:44:10 -07:00
|
|
|
}
|
2015-09-07 15:22:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the database managed by this class contains no entries.
|
|
|
|
*/
|
|
|
|
bool IsEmpty();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor for obfuscate_key.
|
|
|
|
*/
|
|
|
|
const std::vector<unsigned char>& GetObfuscateKey() const;
|
2012-09-03 10:05:30 -07:00
|
|
|
};
|
|
|
|
|
2015-10-22 18:33:06 -07:00
|
|
|
#endif // BITCOIN_DBWRAPPER_H
|
2015-09-07 15:22:23 -07:00
|
|
|
|