Skip to content

Commit

Permalink
Merge pull request #6873
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Oct 23, 2015
2 parents c719cef + 3795e81 commit 46f7437
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ BITCOIN_CORE_H = \
init.h \
key.h \
keystore.h \
leveldbwrapper.h \
dbwrapper.h \
limitedmap.h \
main.h \
memusage.h \
Expand Down Expand Up @@ -188,7 +188,7 @@ libbitcoin_server_a_SOURCES = \
httprpc.cpp \
httpserver.cpp \
init.cpp \
leveldbwrapper.cpp \
dbwrapper.cpp \
main.cpp \
merkleblock.cpp \
miner.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BITCOIN_TESTS =\
test/hash_tests.cpp \
test/key_tests.cpp \
test/limitedmap_tests.cpp \
test/leveldbwrapper_tests.cpp \
test/dbwrapper_tests.cpp \
test/main_tests.cpp \
test/mempool_tests.cpp \
test/miner_tests.cpp \
Expand Down
56 changes: 27 additions & 29 deletions src/leveldbwrapper.cpp → src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "leveldbwrapper.h"
#include "dbwrapper.h"

#include "util.h"
#include "random.h"
Expand All @@ -15,18 +15,18 @@
#include <memenv.h>
#include <stdint.h>

void HandleError(const leveldb::Status& status) throw(leveldb_error)
void HandleError(const leveldb::Status& status) throw(dbwrapper_error)
{
if (status.ok())
return;
LogPrintf("%s\n", status.ToString());
if (status.IsCorruption())
throw leveldb_error("Database corrupted");
throw dbwrapper_error("Database corrupted");
if (status.IsIOError())
throw leveldb_error("Database I/O error");
throw dbwrapper_error("Database I/O error");
if (status.IsNotFound())
throw leveldb_error("Database entry missing");
throw leveldb_error("Unknown database error");
throw dbwrapper_error("Database entry missing");
throw dbwrapper_error("Unknown database error");
}

static leveldb::Options GetOptions(size_t nCacheSize)
Expand All @@ -45,7 +45,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options;
}

CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
{
penv = NULL;
readoptions.verify_checksums = true;
Expand Down Expand Up @@ -76,7 +76,7 @@ CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCa
bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);

if (!key_exists && obfuscate && IsEmpty()) {
// Initialize non-degenerate obfuscation if it won't upset
// Initialize non-degenerate obfuscation if it won't upset
// existing, non-obfuscated data.
std::vector<unsigned char> new_key = CreateObfuscateKey();

Expand All @@ -90,7 +90,7 @@ CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCa
LogPrintf("Using obfuscation key for %s: %s\n", path.string(), GetObfuscateKeyHex());
}

CLevelDBWrapper::~CLevelDBWrapper()
CDBWrapper::~CDBWrapper()
{
delete pdb;
pdb = NULL;
Expand All @@ -102,7 +102,7 @@ CLevelDBWrapper::~CLevelDBWrapper()
options.env = NULL;
}

bool CLevelDBWrapper::WriteBatch(CLevelDBBatch& batch, bool fSync) throw(leveldb_error)
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) throw(dbwrapper_error)
{
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
HandleError(status);
Expand All @@ -113,42 +113,40 @@ bool CLevelDBWrapper::WriteBatch(CLevelDBBatch& batch, bool fSync) throw(leveldb
//
// We must use a string constructor which specifies length so that we copy
// past the null-terminator.
const std::string CLevelDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);

const unsigned int CLevelDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;

/**
* Returns a string (consisting of 8 random bytes) suitable for use as an
* obfuscating XOR key.
* Returns a string (consisting of 8 random bytes) suitable for use as an
* obfuscating XOR key.
*/
std::vector<unsigned char> CLevelDBWrapper::CreateObfuscateKey() const
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
{
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]);

}

bool CLevelDBWrapper::IsEmpty()
bool CDBWrapper::IsEmpty()
{
boost::scoped_ptr<CLevelDBIterator> it(NewIterator());
boost::scoped_ptr<CDBIterator> it(NewIterator());
it->SeekToFirst();
return !(it->Valid());
}

const std::vector<unsigned char>& CLevelDBWrapper::GetObfuscateKey() const
{
return obfuscate_key;
const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
{
return obfuscate_key;
}

std::string CLevelDBWrapper::GetObfuscateKeyHex() const
{
return HexStr(obfuscate_key);
std::string CDBWrapper::GetObfuscateKeyHex() const
{
return HexStr(obfuscate_key);
}

CLevelDBIterator::~CLevelDBIterator() { delete piter; }
bool CLevelDBIterator::Valid() { return piter->Valid(); }
void CLevelDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CLevelDBIterator::SeekToLast() { piter->SeekToLast(); }
void CLevelDBIterator::Next() { piter->Next(); }
void CLevelDBIterator::Prev() { piter->Prev(); }
CDBIterator::~CDBIterator() { delete piter; }
bool CDBIterator::Valid() { return piter->Valid(); }
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CDBIterator::Next() { piter->Next(); }
64 changes: 31 additions & 33 deletions src/leveldbwrapper.h → src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_LEVELDBWRAPPER_H
#define BITCOIN_LEVELDBWRAPPER_H
#ifndef BITCOIN_DBWRAPPER_H
#define BITCOIN_DBWRAPPER_H

#include "clientversion.h"
#include "serialize.h"
Expand All @@ -17,18 +17,18 @@
#include <leveldb/db.h>
#include <leveldb/write_batch.h>

class leveldb_error : public std::runtime_error
class dbwrapper_error : public std::runtime_error
{
public:
leveldb_error(const std::string& msg) : std::runtime_error(msg) {}
dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
};

void HandleError(const leveldb::Status& status) throw(leveldb_error);
void HandleError(const leveldb::Status& status) throw(dbwrapper_error);

/** Batch of changes queued to be written to a CLevelDBWrapper */
class CLevelDBBatch
/** Batch of changes queued to be written to a CDBWrapper */
class CDBBatch
{
friend class CLevelDBWrapper;
friend class CDBWrapper;

private:
leveldb::WriteBatch batch;
Expand All @@ -38,7 +38,7 @@ class CLevelDBBatch
/**
* @param[in] obfuscate_key If passed, XOR data with this key.
*/
CLevelDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { };
CDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { };

template <typename K, typename V>
void Write(const K& key, const V& value)
Expand Down Expand Up @@ -68,8 +68,8 @@ class CLevelDBBatch
batch.Delete(slKey);
}
};
class CLevelDBIterator

class CDBIterator
{
private:
leveldb::Iterator *piter;
Expand All @@ -81,14 +81,13 @@ class CLevelDBIterator
* @param[in] piterIn The original leveldb iterator.
* @param[in] obfuscate_key If passed, XOR data with this key.
*/
CLevelDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) :
CDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) :
piter(piterIn), obfuscate_key(obfuscate_key) { };
~CLevelDBIterator();
~CDBIterator();

bool Valid();

void SeekToFirst();
void SeekToLast();

template<typename K> void Seek(const K& key) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
Expand All @@ -99,7 +98,6 @@ class CLevelDBIterator
}

void Next();
void Prev();

template<typename K> bool GetKey(K& key) {
leveldb::Slice slKey = piter->key();
Expand Down Expand Up @@ -133,8 +131,8 @@ class CLevelDBIterator
}

};
class CLevelDBWrapper

class CDBWrapper
{
private:
//! custom environment this database is using (may be NULL in case of default environment)
Expand Down Expand Up @@ -163,10 +161,10 @@ class CLevelDBWrapper

//! the key under which the obfuscation key is stored
static const std::string OBFUSCATE_KEY_KEY;

//! the length of the obfuscate key in number of bytes
static const unsigned int OBFUSCATE_KEY_NUM_BYTES;

std::vector<unsigned char> CreateObfuscateKey() const;

public:
Expand All @@ -178,11 +176,11 @@ class CLevelDBWrapper
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
* with a zero'd byte array.
*/
CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
~CLevelDBWrapper();
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
~CDBWrapper();

template <typename K, typename V>
bool Read(const K& key, V& value) const throw(leveldb_error)
bool Read(const K& key, V& value) const throw(dbwrapper_error)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
Expand All @@ -208,15 +206,15 @@ class CLevelDBWrapper
}

template <typename K, typename V>
bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error)
bool Write(const K& key, const V& value, bool fSync = false) throw(dbwrapper_error)
{
CLevelDBBatch batch(&obfuscate_key);
CDBBatch batch(&obfuscate_key);
batch.Write(key, value);
return WriteBatch(batch, fSync);
}

template <typename K>
bool Exists(const K& key) const throw(leveldb_error)
bool Exists(const K& key) const throw(dbwrapper_error)
{
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key));
Expand All @@ -235,30 +233,30 @@ class CLevelDBWrapper
}

template <typename K>
bool Erase(const K& key, bool fSync = false) throw(leveldb_error)
bool Erase(const K& key, bool fSync = false) throw(dbwrapper_error)
{
CLevelDBBatch batch(&obfuscate_key);
CDBBatch batch(&obfuscate_key);
batch.Erase(key);
return WriteBatch(batch, fSync);
}

bool WriteBatch(CLevelDBBatch& batch, bool fSync = false) throw(leveldb_error);
bool WriteBatch(CDBBatch& batch, bool fSync = false) throw(dbwrapper_error);

// not available for LevelDB; provide for compatibility with BDB
bool Flush()
{
return true;
}

bool Sync() throw(leveldb_error)
bool Sync() throw(dbwrapper_error)
{
CLevelDBBatch batch(&obfuscate_key);
CDBBatch batch(&obfuscate_key);
return WriteBatch(batch, true);
}

CLevelDBIterator *NewIterator()
CDBIterator *NewIterator()
{
return new CLevelDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key);
return new CDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key);
}

/**
Expand All @@ -278,5 +276,5 @@ class CLevelDBWrapper

};

#endif // BITCOIN_LEVELDBWRAPPER_H
#endif // BITCOIN_DBWRAPPER_H

Loading

0 comments on commit 46f7437

Please sign in to comment.