Browse Source

dbwrapper: Pass parent CDBWrapper into CDBBatch and CDBIterator

Pass parent wrapper directly instead of obfuscation key. This
makes it possible for other databases which re-use this code
to use other properties from the database.

Add a namespace dbwrapper_private for private functions to be used
only in dbwrapper.h/cpp and dbwrapper_tests.
0.13
Wladimir J. van der Laan 9 years ago
parent
commit
b69836d6ff
  1. 14
      src/dbwrapper.cpp
  2. 46
      src/dbwrapper.h
  3. 8
      src/test/dbwrapper_tests.cpp
  4. 6
      src/txdb.cpp

14
src/dbwrapper.cpp

@ -136,12 +136,16 @@ bool CDBWrapper::IsEmpty()
return !(it->Valid()); return !(it->Valid());
} }
const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
{
return obfuscate_key;
}
CDBIterator::~CDBIterator() { delete piter; } CDBIterator::~CDBIterator() { delete piter; }
bool CDBIterator::Valid() { return piter->Valid(); } bool CDBIterator::Valid() { return piter->Valid(); }
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); } void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CDBIterator::Next() { piter->Next(); } void CDBIterator::Next() { piter->Next(); }
namespace dbwrapper_private {
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
{
return w.obfuscate_key;
}
};

46
src/dbwrapper.h

@ -25,20 +25,34 @@ public:
void HandleError(const leveldb::Status& status); void HandleError(const leveldb::Status& status);
class CDBWrapper;
/** These should be considered an implementation detail of the specific database.
*/
namespace dbwrapper_private {
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
* Database obfuscation should be considered an implementation detail of the
* specific database.
*/
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
};
/** Batch of changes queued to be written to a CDBWrapper */ /** Batch of changes queued to be written to a CDBWrapper */
class CDBBatch class CDBBatch
{ {
friend class CDBWrapper; friend class CDBWrapper;
private: private:
const CDBWrapper &parent;
leveldb::WriteBatch batch; leveldb::WriteBatch batch;
const std::vector<unsigned char> *obfuscate_key;
public: public:
/** /**
* @param[in] obfuscate_key If passed, XOR data with this key. * @param[in] parent CDBWrapper that this batch is to be submitted to
*/ */
CDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { }; CDBBatch(const CDBWrapper &parent) : parent(parent) { };
template <typename K, typename V> template <typename K, typename V>
void Write(const K& key, const V& value) void Write(const K& key, const V& value)
@ -51,7 +65,7 @@ public:
CDataStream ssValue(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(ssValue.GetSerializeSize(value)); ssValue.reserve(ssValue.GetSerializeSize(value));
ssValue << value; ssValue << value;
ssValue.Xor(*obfuscate_key); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(&ssValue[0], ssValue.size()); leveldb::Slice slValue(&ssValue[0], ssValue.size());
batch.Put(slKey, slValue); batch.Put(slKey, slValue);
@ -72,17 +86,17 @@ public:
class CDBIterator class CDBIterator
{ {
private: private:
const CDBWrapper &parent;
leveldb::Iterator *piter; leveldb::Iterator *piter;
const std::vector<unsigned char> *obfuscate_key;
public: public:
/** /**
* @param[in] parent Parent CDBWrapper instance.
* @param[in] piterIn The original leveldb iterator. * @param[in] piterIn The original leveldb iterator.
* @param[in] obfuscate_key If passed, XOR data with this key.
*/ */
CDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) : CDBIterator(const CDBWrapper &parent, leveldb::Iterator *piterIn) :
piter(piterIn), obfuscate_key(obfuscate_key) { }; parent(parent), piter(piterIn) { };
~CDBIterator(); ~CDBIterator();
bool Valid(); bool Valid();
@ -118,7 +132,7 @@ public:
leveldb::Slice slValue = piter->value(); leveldb::Slice slValue = piter->value();
try { try {
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
ssValue.Xor(*obfuscate_key); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
ssValue >> value; ssValue >> value;
} catch (const std::exception&) { } catch (const std::exception&) {
return false; return false;
@ -134,6 +148,7 @@ public:
class CDBWrapper class CDBWrapper
{ {
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
private: private:
//! custom environment this database is using (may be NULL in case of default environment) //! custom environment this database is using (may be NULL in case of default environment)
leveldb::Env* penv; leveldb::Env* penv;
@ -208,7 +223,7 @@ public:
template <typename K, typename V> template <typename K, typename V>
bool Write(const K& key, const V& value, bool fSync = false) bool Write(const K& key, const V& value, bool fSync = false)
{ {
CDBBatch batch(&obfuscate_key); CDBBatch batch(*this);
batch.Write(key, value); batch.Write(key, value);
return WriteBatch(batch, fSync); return WriteBatch(batch, fSync);
} }
@ -235,7 +250,7 @@ public:
template <typename K> template <typename K>
bool Erase(const K& key, bool fSync = false) bool Erase(const K& key, bool fSync = false)
{ {
CDBBatch batch(&obfuscate_key); CDBBatch batch(*this);
batch.Erase(key); batch.Erase(key);
return WriteBatch(batch, fSync); return WriteBatch(batch, fSync);
} }
@ -250,24 +265,19 @@ public:
bool Sync() bool Sync()
{ {
CDBBatch batch(&obfuscate_key); CDBBatch batch(*this);
return WriteBatch(batch, true); return WriteBatch(batch, true);
} }
CDBIterator *NewIterator() CDBIterator *NewIterator()
{ {
return new CDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key); return new CDBIterator(*this, pdb->NewIterator(iteroptions));
} }
/** /**
* Return true if the database managed by this class contains no entries. * Return true if the database managed by this class contains no entries.
*/ */
bool IsEmpty(); bool IsEmpty();
/**
* Accessor for obfuscate_key.
*/
const std::vector<unsigned char>& GetObfuscateKey() const;
}; };
#endif // BITCOIN_DBWRAPPER_H #endif // BITCOIN_DBWRAPPER_H

8
src/test/dbwrapper_tests.cpp

@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
uint256 res; uint256 res;
// Ensure that we're doing real obfuscation when obfuscate=true // Ensure that we're doing real obfuscation when obfuscate=true
BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey())); BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
BOOST_CHECK(dbw.Write(key, in)); BOOST_CHECK(dbw.Write(key, in));
BOOST_CHECK(dbw.Read(key, res)); BOOST_CHECK(dbw.Read(key, res));
@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
uint256 in3 = GetRandHash(); uint256 in3 = GetRandHash();
uint256 res; uint256 res;
CDBBatch batch(&dbw.GetObfuscateKey()); CDBBatch batch(dbw);
batch.Write(key, in); batch.Write(key, in);
batch.Write(key2, in2); batch.Write(key2, in2);
@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString()); BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
BOOST_CHECK(is_null_key(odbw.GetObfuscateKey())); // The key should be an empty string BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
uint256 in2 = GetRandHash(); uint256 in2 = GetRandHash();
uint256 res3; uint256 res3;
@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
// Check that the key/val we wrote with unobfuscated wrapper doesn't exist // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
uint256 res2; uint256 res2;
BOOST_CHECK(!odbw.Read(key, res2)); BOOST_CHECK(!odbw.Read(key, res2));
BOOST_CHECK(!is_null_key(odbw.GetObfuscateKey())); BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
uint256 in2 = GetRandHash(); uint256 in2 = GetRandHash();
uint256 res3; uint256 res3;

6
src/txdb.cpp

@ -49,7 +49,7 @@ uint256 CCoinsViewDB::GetBestBlock() const {
} }
bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
CDBBatch batch(&db.GetObfuscateKey()); CDBBatch batch(db);
size_t count = 0; size_t count = 0;
size_t changed = 0; size_t changed = 0;
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) { for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
@ -139,7 +139,7 @@ void CCoinsViewDBCursor::Next()
} }
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) { bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
CDBBatch batch(&GetObfuscateKey()); CDBBatch batch(*this);
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) { for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
batch.Write(make_pair(DB_BLOCK_FILES, it->first), *it->second); batch.Write(make_pair(DB_BLOCK_FILES, it->first), *it->second);
} }
@ -155,7 +155,7 @@ bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
} }
bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) { bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
CDBBatch batch(&GetObfuscateKey()); CDBBatch batch(*this);
for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++) for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
batch.Write(make_pair(DB_TXINDEX, it->first), it->second); batch.Write(make_pair(DB_TXINDEX, it->first), it->second);
return WriteBatch(batch); return WriteBatch(batch);

Loading…
Cancel
Save