Replace CChainDB instantiations with a global CBlockTreeDB instance
authoralex <alex@alex-VirtualBox.(none)>
Wed, 22 Jan 2014 22:35:06 +0000 (02:35 +0400)
committeralex <alex@alex-VirtualBox.(none)>
Wed, 22 Jan 2014 22:35:06 +0000 (02:35 +0400)
src/checkpoints.cpp
src/db.cpp
src/db.h
src/init.cpp
src/main.cpp
src/main.h

index e9567a2..6502ba2 100644 (file)
@@ -135,14 +135,13 @@ namespace Checkpoints
 
     bool WriteSyncCheckpoint(const uint256& hashCheckpoint)
     {
-        CChainDB chaindb;
-        chaindb.TxnBegin();
-        if (!chaindb.WriteSyncCheckpoint(hashCheckpoint))
+        pblocktree->TxnBegin();
+        if (!pblocktree->WriteSyncCheckpoint(hashCheckpoint))
         {
-            chaindb.TxnAbort();
+            pblocktree->TxnAbort();
             return error("WriteSyncCheckpoint(): failed to write to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
         }
-        if (!chaindb.TxnCommit())
+        if (!pblocktree->TxnCommit())
             return error("WriteSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str());
 
         Checkpoints::hashSyncCheckpoint = hashCheckpoint;
@@ -161,7 +160,6 @@ namespace Checkpoints
                 return false;
             }
 
-            CChainDB chaindb;
             CBlockIndex* pindexCheckpoint = mapBlockIndex[hashPendingCheckpoint];
             if (!pindexCheckpoint->IsInMainChain())
             {
@@ -252,7 +250,6 @@ namespace Checkpoints
         {
             // checkpoint block accepted but not yet in main chain
             printf("ResetSyncCheckpoint: SetBestChain to hardened checkpoint %s\n", hash.ToString().c_str());
-            CChainDB chaindb;
             CBlock block;
             if (!block.ReadFromDisk(mapBlockIndex[hash]))
                 return error("ResetSyncCheckpoint: ReadFromDisk failed for hardened checkpoint %s", hash.ToString().c_str());
@@ -411,7 +408,6 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom)
     if (!Checkpoints::ValidateSyncCheckpoint(hashCheckpoint))
         return false;
 
-    CChainDB chaindb;
     CBlockIndex* pindexCheckpoint = mapBlockIndex[hashCheckpoint];
     if (!pindexCheckpoint->IsInMainChain())
     {
index 0f01dd6..831cb9e 100644 (file)
@@ -279,7 +279,7 @@ CDB::CDB(const char *pszFile, const char* pszMode) :
 
 static bool IsChainFile(std::string strFile)
 {
-    if (strFile == "coins.dat" || strFile == "chain.dat")
+    if (strFile == "coins.dat" || strFile == "blktree.dat")
         return true;
 
     return false;
@@ -489,7 +489,7 @@ void CDBEnv::Flush(bool fShutdown)
 
 
 //
-// CChainDB and CCoinsDB
+// CBlockTreeDB and CCoinsDB
 //
 
 bool CCoinsDB::HaveCoins(uint256 hash) {
@@ -510,7 +510,7 @@ bool CCoinsDB::WriteCoins(uint256 hash, const CCoins &coins) {
         return Write(make_pair('c', hash), coins);
 }
 
-bool CChainDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
+bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
 {
     return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
 }
@@ -525,50 +525,50 @@ bool CCoinsDB::WriteHashBestChain(uint256 hashBestChain)
     return Write('B', hashBestChain);
 }
 
-bool CChainDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
+bool CBlockTreeDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
 {
     return Read('I', bnBestInvalidTrust);
 }
 
-bool CChainDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
+bool CBlockTreeDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
 {
     return Write('I', bnBestInvalidTrust);
 }
 
-bool CChainDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo &info) {
+bool CBlockTreeDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo &info) {
     return Write(make_pair('f', nFile), info);
 }
 
-bool CChainDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
+bool CBlockTreeDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
 {
     return Read('H', hashCheckpoint);
 }
 
-bool CChainDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
+bool CBlockTreeDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
 {
     return Write('H', hashCheckpoint);
 }
 
-bool CChainDB::ReadCheckpointPubKey(string& strPubKey)
+bool CBlockTreeDB::ReadCheckpointPubKey(string& strPubKey)
 {
     return Read('K', strPubKey);
 }
 
-bool CChainDB::WriteCheckpointPubKey(const string& strPubKey)
+bool CBlockTreeDB::WriteCheckpointPubKey(const string& strPubKey)
 {
     return Write('K', strPubKey);
 }
 
 
-bool CChainDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
+bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
     return Read(make_pair('f', nFile), info);
 }
 
-bool CChainDB::WriteLastBlockFile(int nFile) {
+bool CBlockTreeDB::WriteLastBlockFile(int nFile) {
     return Write('l', nFile);
 }
 
-bool CChainDB::ReadLastBlockFile(int &nFile) {
+bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
     return Read('l', nFile);
 }
 
@@ -628,9 +628,9 @@ CBlockIndex static * InsertBlockIndex(uint256 hash)
     return pindexNew;
 }
 
-bool LoadBlockIndex(CChainDB &chaindb)
+bool LoadBlockIndexDB()
 {
-    if (!chaindb.LoadBlockIndexGuts())
+    if (!pblocktree->LoadBlockIndexGuts())
         return false;
 
     if (fRequestShutdown)
@@ -660,9 +660,9 @@ bool LoadBlockIndex(CChainDB &chaindb)
     }
 
     // Load block file info
-    chaindb.ReadLastBlockFile(nLastBlockFile);
+    pblocktree->ReadLastBlockFile(nLastBlockFile);
     printf("LoadBlockIndex(): last block file = %i\n", nLastBlockFile);
-    if (chaindb.ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+    if (pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile))
         printf("LoadBlockIndex(): last block file: %s\n", infoLastBlockFile.ToString().c_str());
  
     // Load hashBestChain pointer to end of best chain
@@ -689,13 +689,13 @@ bool LoadBlockIndex(CChainDB &chaindb)
         DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
 
     // Load sync-checkpoint
-    if (!chaindb.ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
+    if (!pblocktree->ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
         return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
     printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
 
     // Load bnBestInvalidTrust, OK if it doesn't exist
     CBigNum bnBestInvalidTrust;
-    chaindb.ReadBestInvalidTrust(bnBestInvalidTrust);
+    pblocktree->ReadBestInvalidTrust(bnBestInvalidTrust);
     nBestInvalidTrust = bnBestInvalidTrust.getuint256();
 
     // Verify blocks in the best chain
@@ -733,7 +733,7 @@ bool LoadBlockIndex(CChainDB &chaindb)
 
 
 
-bool CChainDB::LoadBlockIndexGuts()
+bool CBlockTreeDB::LoadBlockIndexGuts()
 {
     // Get database cursor
     Dbc* pcursor = GetCursor();
index bc6a5a5..5b805df 100644 (file)
--- a/src/db.h
+++ b/src/db.h
@@ -349,13 +349,13 @@ public:
 
 
 /** Access to the block database (chain.dat) */
-class CChainDB : public CDB
+class CBlockTreeDB : public CDB
 {
 public:
-    CChainDB(const char* pszMode="r+") : CDB("chain.dat", pszMode) { }
+    CBlockTreeDB(const char* pszMode="r+") : CDB("blktree.dat", pszMode) { }
 private:
-    CChainDB(const CChainDB&);
-    void operator=(const CChainDB&);
+    CBlockTreeDB(const CBlockTreeDB&);
+    void operator=(const CBlockTreeDB&);
 public:
     bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
     bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
@@ -372,7 +372,7 @@ public:
 };
 
 
-bool LoadBlockIndex(CChainDB &chaindb);
+bool LoadBlockIndexDB();
 
 
 /** Access to the (IP) address database (peers.dat) */
index 7281e01..d9ee01a 100644 (file)
@@ -89,6 +89,7 @@ void Shutdown(void* parg)
         {
             LOCK(cs_main);
             pcoinsTip->Flush();
+            pblocktree->Flush();
             delete pcoinsTip;
             delete pcoinsdbview;
         }
@@ -706,6 +707,7 @@ bool AppInit2()
     uiInterface.InitMessage(_("Loading block index..."));
     printf("Loading block index...\n");
     nStart = GetTimeMillis();
+    pblocktree = new CBlockTreeDB("cr+");
     pcoinsdbview = new CCoinsViewDB();
     pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
 
index 1c5b0da..46977b2 100644 (file)
@@ -315,6 +315,7 @@ bool CCoinsViewMemPool::HaveCoins(uint256 txid) {
 }
 
 CCoinsViewCache *pcoinsTip = NULL;
+CBlockTreeDB *pblocktree = NULL;
 
 //////////////////////////////////////////////////////////////////////////////
 //
@@ -1303,7 +1304,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
     if (pindexNew->nChainTrust > nBestInvalidTrust)
     {
         nBestInvalidTrust = pindexNew->nChainTrust;
-        CChainDB().WriteBestInvalidTrust(CBigNum(nBestInvalidTrust));
+        pblocktree->WriteBestInvalidTrust(CBigNum(nBestInvalidTrust));
         uiInterface.NotifyBlocksChanged();
     }
 
@@ -1323,7 +1324,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
 
 void static InvalidBlockFound(CBlockIndex *pindex) {
     pindex->nStatus |= BLOCK_FAILED_VALID;
-    CChainDB().WriteBlockIndex(CDiskBlockIndex(pindex));
+    pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
     setBlockIndexValid.erase(pindex);
     InvalidChainFound(pindex);
     if (pindex->pnext)
@@ -1350,12 +1351,11 @@ bool ConnectBestBlock() {
         do {
             if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
                 // mark descendants failed
-                CChainDB chaindb;
                 CBlockIndex *pindexFailed = pindexNewBest;
                 while (pindexTest != pindexFailed) {
                     pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
                     setBlockIndexValid.erase(pindexFailed);
-                    chaindb.WriteBlockIndex(CDiskBlockIndex(pindexFailed));
+                    pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexFailed));
                     pindexFailed = pindexFailed->pprev;
                 }
                 InvalidChainFound(pindexNewBest);
@@ -1680,7 +1680,7 @@ bool CBlock::DisconnectBlock(CBlockIndex *pindex, CCoinsViewCache &view)
     return true;
 }
 
-bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
+bool FindUndoPos(int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
 
 bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJustCheck)
 {
@@ -1779,11 +1779,9 @@ bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJust
     // Write undo information to disk
     if (pindex->GetUndoPos().IsNull() || (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS)
     {
-        CChainDB chaindb;
-
         if (pindex->GetUndoPos().IsNull()) {
             CDiskBlockPos pos;
-            if (!FindUndoPos(chaindb, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 8))
+            if (!FindUndoPos(pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 8))
                 return error("ConnectBlock() : FindUndoPos failed");
             if (!blockundo.WriteToDisk(pos))
                 return error("ConnectBlock() : CBlockUndo::WriteToDisk failed");
@@ -1796,7 +1794,7 @@ bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJust
         pindex->nStatus = (pindex->nStatus & ~BLOCK_VALID_MASK) | BLOCK_VALID_SCRIPTS;
 
         CDiskBlockIndex blockindex(pindex);
-        if (!chaindb.WriteBlockIndex(blockindex))
+        if (!pblocktree->WriteBlockIndex(blockindex))
             return error("ConnectBlock() : WriteBlockIndex failed");
     }
 
@@ -2094,12 +2092,7 @@ bool CBlock::AddToBlockIndex(const CDiskBlockPos &pos)
 
     setBlockIndexValid.insert(pindexNew);
 
-    CChainDB chaindb;
-    if (!chaindb.TxnBegin())
-        return false;
-    chaindb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
-    if (!chaindb.TxnCommit())
-        return false;
+    pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew));
 
     // New best?
     if (!ConnectBestBlock())
@@ -2113,11 +2106,13 @@ bool CBlock::AddToBlockIndex(const CDiskBlockPos &pos)
         hashPrevBestCoinBase = GetTxHash(0);
     }
 
+    pblocktree->Flush();
+
     uiInterface.NotifyBlocksChanged();
     return true;
 }
 
-bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
+bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
 {
     bool fUpdatedLast = false;
 
@@ -2133,7 +2128,7 @@ bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize,
         fclose(file);
         nLastBlockFile++;
         infoLastBlockFile.SetNull();
-        chaindb.ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
+        pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
         fUpdatedLast = true;
     }
 
@@ -2153,15 +2148,15 @@ bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize,
         fclose(file);
     }
 
-    if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+    if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
         return error("FindBlockPos() : cannot write updated block info");
     if (fUpdatedLast)
-        chaindb.WriteLastBlockFile(nLastBlockFile);
+        pblocktree->WriteLastBlockFile(nLastBlockFile);
 
     return true;
 }
 
-bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
+bool FindUndoPos(int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
 {
     pos.nFile = nFile;
 
@@ -2171,15 +2166,15 @@ bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int
     if (nFile == nLastBlockFile) {
         pos.nPos = infoLastBlockFile.nUndoSize;
         nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
-        if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+        if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
             return error("FindUndoPos() : cannot write updated block info");
     } else {
         CBlockFileInfo info;
-        if (!chaindb.ReadBlockFileInfo(nFile, info))
+        if (!pblocktree->ReadBlockFileInfo(nFile, info))
             return error("FindUndoPos() : cannot read block info");
         pos.nPos = info.nUndoSize;
         nNewSize = (info.nUndoSize += nAddSize);
-        if (!chaindb.WriteBlockFileInfo(nFile, info))
+        if (!pblocktree->WriteBlockFileInfo(nFile, info))
             return error("FindUndoPos() : cannot write updated block info");
     }
 
@@ -2357,8 +2352,7 @@ bool CBlock::AcceptBlock()
         return error("AcceptBlock() : out of disk space");
     CDiskBlockPos blockPos;
     {
-        CChainDB chaindb;
-        if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, nHeight, nTime))
+        if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, nTime))
             return error("AcceptBlock() : FindBlockPos failed");
     }
     if (!WriteToDisk(blockPos))
@@ -2825,12 +2819,10 @@ bool LoadBlockIndex(bool fAllowNew)
     ZCParams = new libzerocoin::Params(bnTrustedModulus);
 
     //
-    // Load block index
+    // Load block index from databases
     //
-    CChainDB chaindb("cr");
-    if (!LoadBlockIndex(chaindb))
+    if (!LoadBlockIndexDB())
         return false;
-    chaindb.Close();
 
     //
     // Init with genesis block
@@ -2885,11 +2877,8 @@ bool LoadBlockIndex(bool fAllowNew)
         // Start new block file
         unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
         CDiskBlockPos blockPos;
-        {
-            CChainDB chaindb;
-            if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, 0, block.nTime))
-                return error("AcceptBlock() : FindBlockPos failed");
-        }
+        if (!FindBlockPos(blockPos, nBlockSize+8, 0, block.nTime))
+            return error("AcceptBlock() : FindBlockPos failed");
         if (!block.WriteToDisk(blockPos))
             return error("LoadBlockIndex() : writing genesis block to disk failed");
         if (!block.AddToBlockIndex(blockPos))
@@ -2901,20 +2890,17 @@ bool LoadBlockIndex(bool fAllowNew)
     }
 
     string strPubKey = "";
+    // if checkpoint master key changed must reset sync-checkpoint
+    if (!pblocktree->ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey)
     {
-        CChainDB chaindb;
-        // if checkpoint master key changed must reset sync-checkpoint
-        if (!chaindb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey)
-        {
-            // write checkpoint master key to db
-            chaindb.TxnBegin();
-            if (!chaindb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey))
-                return error("LoadBlockIndex() : failed to write new checkpoint master key to db");
-            if (!chaindb.TxnCommit())
-                return error("LoadBlockIndex() : failed to commit new checkpoint master key to db");
-            if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint())
-                return error("LoadBlockIndex() : failed to reset sync-checkpoint");
-        }
+        // write checkpoint master key to db
+        pblocktree->TxnBegin();
+        if (!pblocktree->WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey))
+            return error("LoadBlockIndex() : failed to write new checkpoint master key to db");
+        if (!pblocktree->TxnCommit())
+            return error("LoadBlockIndex() : failed to commit new checkpoint master key to db");
+        if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint())
+            return error("LoadBlockIndex() : failed to reset sync-checkpoint");
     }
 
     return true;
index 195d62e..12ca01e 100644 (file)
@@ -106,7 +106,7 @@ static const uint64 nMinDiskSpace = 52428800;
 
 class CReserveKey;
 class CCoinsDB;
-class CChainDB;
+class CBlockTreeDB;
 class CDiskBlockPos;
 class CCoins;
 class CTxUndo;
@@ -2135,6 +2135,10 @@ public:
     bool HaveCoins(uint256 txid);
 };
 
+/** Global variable that points to the active CCoinsView (protected by cs_main) */
 extern CCoinsViewCache *pcoinsTip;
 
+/** Global variable that points to the active block tree (protected by cs_main) */
+extern CBlockTreeDB *pblocktree;
+
 #endif