Merge pull request #7259

fa3cb49 [init] Fix typo (MarcoFalke)
fa24941 [dbwrapper] Detect obfuscation (MarcoFalke)
This commit is contained in:
Wladimir J. van der Laan
2016-01-09 16:34:10 +01:00
6 changed files with 29 additions and 10 deletions

View File

@@ -1127,8 +1127,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
delete pcoinscatcher; delete pcoinscatcher;
delete pblocktree; delete pblocktree;
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex); // Detect database obfuscation by future versions of the DBWrapper
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex); bool chainstateScrambled;
bool blockDbScrambled;
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, blockDbScrambled, false, fReindex);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, chainstateScrambled, false, fReindex);
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview); pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
pcoinsTip = new CCoinsViewCache(pcoinscatcher); pcoinsTip = new CCoinsViewCache(pcoinscatcher);
@@ -1139,6 +1143,11 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
CleanupBlockRevFiles(); CleanupBlockRevFiles();
} }
if (chainstateScrambled || blockDbScrambled) {
strLoadError = _("Reindex required as the chainstate or block database is obfuscated");
break;
}
if (!LoadBlockIndex()) { if (!LoadBlockIndex()) {
strLoadError = _("Error loading block database"); strLoadError = _("Error loading block database");
break; break;

View File

@@ -43,7 +43,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
return options; return options;
} }
CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe) CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool &isObfuscated, bool fMemory, bool fWipe)
{ {
penv = NULL; penv = NULL;
readoptions.verify_checksums = true; readoptions.verify_checksums = true;
@@ -67,6 +67,9 @@ CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path& path, size_t nCa
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
HandleError(status); HandleError(status);
LogPrintf("Opened LevelDB successfully\n"); LogPrintf("Opened LevelDB successfully\n");
std::vector<unsigned char> obfuscate_key;
isObfuscated = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
} }
CLevelDBWrapper::~CLevelDBWrapper() CLevelDBWrapper::~CLevelDBWrapper()
@@ -87,3 +90,6 @@ bool CLevelDBWrapper::WriteBatch(CLevelDBBatch& batch, bool fSync) throw(leveldb
HandleError(status); HandleError(status);
return true; return true;
} }
// Taken from future release of DBWrapper
const std::string CLevelDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);

View File

@@ -85,8 +85,11 @@ private:
//! the database itself //! the database itself
leveldb::DB* pdb; leveldb::DB* pdb;
//! the key under which a obfuscation key may be stored by a future version of DBWrapper
static const std::string OBFUSCATE_KEY_KEY;
public: public:
CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool &isObfuscated, bool fMemory = false, bool fWipe = false);
~CLevelDBWrapper(); ~CLevelDBWrapper();
template <typename K, typename V> template <typename K, typename V>

View File

@@ -49,8 +49,9 @@ TestingSetup::TestingSetup()
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000))); pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
boost::filesystem::create_directories(pathTemp); boost::filesystem::create_directories(pathTemp);
mapArgs["-datadir"] = pathTemp.string(); mapArgs["-datadir"] = pathTemp.string();
pblocktree = new CBlockTreeDB(1 << 20, true); bool isObfuscated;
pcoinsdbview = new CCoinsViewDB(1 << 23, true); pblocktree = new CBlockTreeDB(1 << 20, isObfuscated, true);
pcoinsdbview = new CCoinsViewDB(1 << 23, isObfuscated, true);
pcoinsTip = new CCoinsViewCache(pcoinsdbview); pcoinsTip = new CCoinsViewCache(pcoinsdbview);
InitBlockIndex(); InitBlockIndex();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET

View File

@@ -39,7 +39,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
batch.Write(DB_BEST_BLOCK, hash); batch.Write(DB_BEST_BLOCK, hash);
} }
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe) { CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool &isObfuscated, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, isObfuscated, fMemory, fWipe) {
} }
bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const { bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
@@ -77,7 +77,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
return db.WriteBatch(batch); return db.WriteBatch(batch);
} }
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) { CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool &isObfuscated, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, isObfuscated, fMemory, fWipe) {
} }
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) { bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {

View File

@@ -32,7 +32,7 @@ class CCoinsViewDB : public CCoinsView
protected: protected:
CLevelDBWrapper db; CLevelDBWrapper db;
public: public:
CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false); CCoinsViewDB(size_t nCacheSize, bool &isObfuscated, bool fMemory = false, bool fWipe = false);
bool GetCoins(const uint256 &txid, CCoins &coins) const; bool GetCoins(const uint256 &txid, CCoins &coins) const;
bool HaveCoins(const uint256 &txid) const; bool HaveCoins(const uint256 &txid) const;
@@ -45,7 +45,7 @@ public:
class CBlockTreeDB : public CLevelDBWrapper class CBlockTreeDB : public CLevelDBWrapper
{ {
public: public:
CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false); CBlockTreeDB(size_t nCacheSize, bool &isObfuscated, bool fMemory = false, bool fWipe = false);
private: private:
CBlockTreeDB(const CBlockTreeDB&); CBlockTreeDB(const CBlockTreeDB&);
void operator=(const CBlockTreeDB&); void operator=(const CBlockTreeDB&);