Browse Source

Refer to obfuscate_key via pointer in peripheral CLevelDB classes

cc @sipa
0.13
James O'Beirne 9 years ago
parent
commit
dcd8e27c65
  1. 20
      src/leveldbwrapper.h
  2. 2
      src/test/leveldbwrapper_tests.cpp
  3. 6
      src/txdb.cpp

20
src/leveldbwrapper.h

@ -32,13 +32,13 @@ class CLevelDBBatch
private: private:
leveldb::WriteBatch batch; leveldb::WriteBatch batch;
const std::vector<unsigned char> obfuscate_key; const std::vector<unsigned char> *obfuscate_key;
public: public:
/** /**
* @param[in] obfuscate_key If passed, XOR data with this key. * @param[in] obfuscate_key If passed, XOR data with this key.
*/ */
CLevelDBBatch(const std::vector<unsigned char>& obfuscate_key) : obfuscate_key(obfuscate_key) { }; CLevelDBBatch(const std::vector<unsigned char> *obfuscate_key) : obfuscate_key(obfuscate_key) { };
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 +51,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(*obfuscate_key);
leveldb::Slice slValue(&ssValue[0], ssValue.size()); leveldb::Slice slValue(&ssValue[0], ssValue.size());
batch.Put(slKey, slValue); batch.Put(slKey, slValue);
@ -73,7 +73,7 @@ class CLevelDBIterator
{ {
private: private:
leveldb::Iterator *piter; leveldb::Iterator *piter;
const std::vector<unsigned char> obfuscate_key; const std::vector<unsigned char> *obfuscate_key;
public: public:
@ -81,7 +81,7 @@ public:
* @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. * @param[in] obfuscate_key If passed, XOR data with this key.
*/ */
CLevelDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>& obfuscate_key) : CLevelDBIterator(leveldb::Iterator *piterIn, const std::vector<unsigned char>* obfuscate_key) :
piter(piterIn), obfuscate_key(obfuscate_key) { }; piter(piterIn), obfuscate_key(obfuscate_key) { };
~CLevelDBIterator(); ~CLevelDBIterator();
@ -120,7 +120,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(*obfuscate_key);
ssValue >> value; ssValue >> value;
} catch(std::exception &e) { } catch(std::exception &e) {
return false; return false;
@ -210,7 +210,7 @@ public:
template <typename K, typename V> 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(leveldb_error)
{ {
CLevelDBBatch batch(obfuscate_key); CLevelDBBatch batch(&obfuscate_key);
batch.Write(key, value); batch.Write(key, value);
return WriteBatch(batch, fSync); return WriteBatch(batch, fSync);
} }
@ -237,7 +237,7 @@ public:
template <typename K> template <typename K>
bool Erase(const K& key, bool fSync = false) throw(leveldb_error) bool Erase(const K& key, bool fSync = false) throw(leveldb_error)
{ {
CLevelDBBatch batch(obfuscate_key); CLevelDBBatch batch(&obfuscate_key);
batch.Erase(key); batch.Erase(key);
return WriteBatch(batch, fSync); return WriteBatch(batch, fSync);
} }
@ -252,13 +252,13 @@ public:
bool Sync() throw(leveldb_error) bool Sync() throw(leveldb_error)
{ {
CLevelDBBatch batch(obfuscate_key); CLevelDBBatch batch(&obfuscate_key);
return WriteBatch(batch, true); return WriteBatch(batch, true);
} }
CLevelDBIterator *NewIterator() CLevelDBIterator *NewIterator()
{ {
return new CLevelDBIterator(pdb->NewIterator(iteroptions), obfuscate_key); return new CLevelDBIterator(pdb->NewIterator(iteroptions), &obfuscate_key);
} }
/** /**

2
src/test/leveldbwrapper_tests.cpp

@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(leveldbwrapper_batch)
uint256 in3 = GetRandHash(); uint256 in3 = GetRandHash();
uint256 res; uint256 res;
CLevelDBBatch batch(dbw.GetObfuscateKey()); CLevelDBBatch batch(&dbw.GetObfuscateKey());
batch.Write(key, in); batch.Write(key, in);
batch.Write(key2, in2); batch.Write(key2, in2);

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) {
CLevelDBBatch batch(db.GetObfuscateKey()); CLevelDBBatch batch(&db.GetObfuscateKey());
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();) {
@ -141,7 +141,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
} }
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) {
CLevelDBBatch batch(GetObfuscateKey()); CLevelDBBatch batch(&GetObfuscateKey());
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);
} }
@ -157,7 +157,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) {
CLevelDBBatch batch(GetObfuscateKey()); CLevelDBBatch batch(&GetObfuscateKey());
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