From 0fdf8c80ee322ab747321d61faf9c72af4a51445 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Thu, 8 Oct 2015 00:44:10 -0700 Subject: [PATCH] Handle obfuscation in CLevelDBIterator --- src/leveldbwrapper.cpp | 2 +- src/leveldbwrapper.h | 15 +++++++++++---- src/txdb.cpp | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/leveldbwrapper.cpp b/src/leveldbwrapper.cpp index a94cfd8a30..32c9345be5 100644 --- a/src/leveldbwrapper.cpp +++ b/src/leveldbwrapper.cpp @@ -145,7 +145,7 @@ std::string CLevelDBWrapper::GetObfuscateKeyHex() const { return HexStr(obfuscate_key); } - + CLevelDBIterator::~CLevelDBIterator() { delete piter; } bool CLevelDBIterator::Valid() { return piter->Valid(); } void CLevelDBIterator::SeekToFirst() { piter->SeekToFirst(); } diff --git a/src/leveldbwrapper.h b/src/leveldbwrapper.h index ece4e49694..891381c5f2 100644 --- a/src/leveldbwrapper.h +++ b/src/leveldbwrapper.h @@ -73,9 +73,16 @@ class CLevelDBIterator { private: leveldb::Iterator *piter; + const std::vector obfuscate_key; public: - CLevelDBIterator(leveldb::Iterator *piterIn) : piter(piterIn) {} + + /** + * @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& obfuscate_key) : + piter(piterIn), obfuscate_key(obfuscate_key) { }; ~CLevelDBIterator(); bool Valid(); @@ -113,7 +120,7 @@ public: leveldb::Slice slValue = piter->value(); try { CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); - ssValue.Xor(db.GetObfuscateKey()); + ssValue.Xor(obfuscate_key); ssValue >> value; } catch(std::exception &e) { return false; @@ -251,8 +258,8 @@ public: CLevelDBIterator *NewIterator() { - return new CLevelDBIterator(pdb->NewIterator(iteroptions)); - { + return new CLevelDBIterator(pdb->NewIterator(iteroptions), obfuscate_key); + } /** * Return true if the database managed by this class contains no entries. diff --git a/src/txdb.cpp b/src/txdb.cpp index 3d20508912..4d7ec27aae 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -98,7 +98,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const { /* It seems that there are no "const iterators" for LevelDB. Since we only need read operations on it, use a const-cast to get around that restriction. */ - boost::scoped_ptr pcursor(const_cast(&db)->NewIterator()); + boost::scoped_ptr pcursor(const_cast(&db)->NewIterator()); pcursor->Seek('c'); CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); @@ -177,9 +177,9 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) { bool CBlockTreeDB::LoadBlockIndexGuts() { - boost::scoped_ptr pcursor(NewIterator()); + boost::scoped_ptr pcursor(NewIterator()); - pcursor->Seek(make_pair('b', uint256(0))); + pcursor->Seek(make_pair('b', uint256())); // Load mapBlockIndex while (pcursor->Valid()) {