]> Git Repo - VerusCoin.git/commitdiff
Make sure the genesis block is present after reindex
authorPieter Wuille <[email protected]>
Wed, 30 Jan 2013 20:43:36 +0000 (21:43 +0100)
committerPieter Wuille <[email protected]>
Fri, 1 Feb 2013 22:29:59 +0000 (23:29 +0100)
src/init.cpp
src/main.cpp
src/main.h
src/test/test_bitcoin.cpp

index 2f37dad56c4d4bac08911f593fc796791acb9d7d..15a46946fa6653734ae856d66f070df3f537b7e5 100644 (file)
@@ -365,6 +365,8 @@ void ThreadImport(void *data) {
             pblocktree->WriteReindexing(false);
             fReindex = false;
             printf("Reindexing finished\n");
+            // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
+            InitBlockIndex();
         }
     }
 
@@ -802,6 +804,10 @@ bool AppInit2()
     if (!LoadBlockIndex())
         return InitError(_("Error loading block database"));
 
+    // Initialize the block index (no-op if non-empty database was already loaded)
+    if (!InitBlockIndex())
+        return InitError(_("Error initializing block database"));
+
     uiInterface.InitMessage(_("Verifying block database integrity..."));
 
     if (!VerifyDB())
index 847b1ea8a61bcca90344cf17eabfe77a5f1d6389..ee1d23c3373cc7b536d504e36eb130337d799f67 100644 (file)
@@ -2661,18 +2661,22 @@ bool LoadBlockIndex()
     if (!fReindex && !LoadBlockIndexDB())
         return false;
 
-    //
-    // Init with genesis block
-    //
-    if (mapBlockIndex.empty())
-    {
-        fTxIndex = GetBoolArg("-txindex", false);
-        pblocktree->WriteFlag("txindex", fTxIndex);
-        printf("Initializing databases...\n");
+    return true;
+}
 
-        if (fReindex)
-            return true;
 
+bool InitBlockIndex() {
+    // Check whether we're already initialized
+    if (pindexGenesisBlock != NULL)
+        return true;
+
+    // Use the provided setting for -txindex in the new database
+    fTxIndex = GetBoolArg("-txindex", false);
+    pblocktree->WriteFlag("txindex", fTxIndex);
+    printf("Initializing databases...\n");
+
+    // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
+    if (!fReindex) {
         // Genesis Block:
         // CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
         //   CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
@@ -2713,15 +2717,19 @@ bool LoadBlockIndex()
         assert(hash == hashGenesisBlock);
 
         // Start new block file
-        unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
-        CDiskBlockPos blockPos;
-        CValidationState state;
-        if (!FindBlockPos(state, 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(state, blockPos))
-            return error("LoadBlockIndex() : genesis block not accepted");
+        try {
+            unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
+            CDiskBlockPos blockPos;
+            CValidationState state;
+            if (!FindBlockPos(state, 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(state, blockPos))
+                return error("LoadBlockIndex() : genesis block not accepted");
+        } catch(std::runtime_error &e) {
+            return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
+        }
     }
 
     return true;
index 23a4d3ba31870bc9c97117c6584ffd532ba1a697..3c7e4e1005487e4ea6c7e61b435b319bf99a580a 100644 (file)
@@ -133,6 +133,8 @@ FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
 /** Import blocks from an external file */
 bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
+/** Initialize a new block tree database + block data on disk */
+bool InitBlockIndex();
 /** Load the block tree and coins database from disk */
 bool LoadBlockIndex();
 /** Verify consistency of the block and coin databases */
index f75b762f1fc9f3df48fa895acd57da162918eaf0..1116507a34fc1672a3fc73e0762bfbf69547d60d 100644 (file)
@@ -28,7 +28,7 @@ struct TestingSetup {
         pblocktree = new CBlockTreeDB(1 << 20, true);
         pcoinsdbview = new CCoinsViewDB(1 << 23, true);
         pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
-        LoadBlockIndex();
+        InitBlockIndex();
         bool fFirstRun;
         pwalletMain = new CWallet("wallet.dat");
         pwalletMain->LoadWallet(fFirstRun);
This page took 0.0391 seconds and 4 git commands to generate.