LoadExternalBlockFile() : Bulk block proccessing;
authorCryptoManiac <balthazar@yandex.ru>
Sat, 9 Apr 2016 20:50:08 +0000 (23:50 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Sat, 9 Apr 2016 20:50:08 +0000 (23:50 +0300)
GetBlockHash() : Don't check current date;
CTxDB : Check only 192 recent blocks instead of 2500.

src/init.cpp
src/main.cpp
src/main.h
src/protocol.h
src/qt/bitcoinstrings.cpp
src/qt/locale/bitcoin_en.ts
src/qt/locale/bitcoin_ru.ts
src/qt/locale/bitcoin_uk.ts
src/txdb-bdb.cpp
src/txdb-leveldb.cpp

index 349dd32..8b6ff3e 100644 (file)
@@ -299,7 +299,7 @@ std::string HelpMessage()
         "  -rescan                " + _("Rescan the block chain for missing wallet transactions") + "\n" +
         "  -zapwallettxes         " + _("Clear list of wallet transactions (diagnostic tool; implies -rescan)") + "\n" +
         "  -salvagewallet         " + _("Attempt to recover private keys from a corrupt wallet.dat") + "\n" +
-        "  -checkblocks=<n>       " + _("How many blocks to check at startup (default: 2500, 0 = all)") + "\n" +
+        "  -checkblocks=<n>       " + _("How many blocks to check at startup (default: 192, 0 = all)") + "\n" +
         "  -checklevel=<n>        " + _("How thorough the block verification is (0-6, default: 1)") + "\n" +
         "  -par=N                 " + _("Set the number of script verification threads (1-16, 0=auto, default: 0)") + "\n" +
         "  -loadblock=<file>      " + _("Imports blocks from external blk000?.dat file") + "\n" +
@@ -944,7 +944,7 @@ bool AppInit2()
         {
             FILE *file = fopen(strFile.c_str(), "rb");
             if (file)
-                LoadExternalBlockFile(file);
+                LoadExternalBlockFile(file, uiInterface);
         }
         StartShutdown();
     }
@@ -956,7 +956,7 @@ bool AppInit2()
         FILE *file = fopen(pathBootstrap.string().c_str(), "rb");
         if (file) {
             filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
-            LoadExternalBlockFile(file);
+            LoadExternalBlockFile(file, uiInterface);
             RenameOver(pathBootstrap, pathBootstrapOld);
         }
     }
index 21d6125..b5dd38d 100644 (file)
@@ -2943,59 +2943,84 @@ void PrintBlockTree()
     }
 }
 
-bool LoadExternalBlockFile(FILE* fileIn)
+bool LoadExternalBlockFile(FILE* fileIn, CClientUIInterface& uiInterface)
 {
     auto nStart = GetTimeMillis();
-
+    vector<uint8_t> pchData(10 * (8+MAX_BLOCK_SIZE));
     int nLoaded = 0;
     {
         LOCK(cs_main);
         try {
             CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION);
-            unsigned int nPos = 0;
+            uint32_t nPos = 0;
             while (nPos != std::numeric_limits<uint32_t>::max() && blkdat.good() && !fRequestShutdown)
             {
-                unsigned char pchData[65536];
-                do {
+                do
+                {
                     fseek(blkdat, nPos, SEEK_SET);
-                    size_t nRead = fread(pchData, 1, sizeof(pchData), blkdat);
+                    auto nRead = fread(&pchData[0], 1, pchData.size(), blkdat);
                     if (nRead <= 8)
                     {
-                        nPos = std::numeric_limits<uint32_t>::max();
+                        nPos = numeric_limits<uint32_t>::max();
                         break;
                     }
-                    void* nFind = memchr(pchData, pchMessageStart[0], nRead+1-sizeof(pchMessageStart));
-                    if (nFind)
+                    auto it = pchData.begin();
+                    while(it != pchData.end() && !fRequestShutdown)
                     {
-                        if (memcmp(nFind, pchMessageStart, sizeof(pchMessageStart))==0)
+                        auto nBlockLength = *reinterpret_cast<const uint32_t*>(&(*(it+4)));
+                        auto SeekToNext = [&pchData, &it, &nPos, &nBlockLength]() {
+                            auto previt = it;
+                            it = search(it+8, pchData.end(), BEGIN(pchMessageStart), END(pchMessageStart));
+                            if (it != pchData.end())
+                                nPos += (it - previt);
+                        };
+                        if (nBlockLength > 0)
                         {
-                            nPos += ((unsigned char*)nFind - pchData) + sizeof(pchMessageStart);
-                            break;
+                            if (nBlockLength > pchData.end() - it)
+                            {
+                                SeekToNext();
+                                break; // We've reached the end of buffer
+                            }
+                            else
+                            {
+                                CBlock block;
+                                try
+                                {
+                                    vector<unsigned char> vchBlockBytes(it+8, it+8+nBlockLength);
+                                    CDataStream blockData(vchBlockBytes, SER_NETWORK, PROTOCOL_VERSION);
+                                    blockData >> block;
+                                }
+                                catch (const std::exception&)
+                                {
+                                    printf("LoadExternalBlockFile() : Deserialize error caught at the position %" PRId64 ", this block may be truncated.", nPos);
+                                    SeekToNext();
+                                    break;
+                                }
+                                if (ProcessBlock(NULL, &block))
+                                    nLoaded++;
+                                it += (8 + nBlockLength);
+                                nPos += (8 + nBlockLength);
+                                {
+                                    static int64_t nLastUpdate = 0;
+                                    if (GetTimeMillis() - nLastUpdate > 1000)
+                                    {
+                                        uiInterface.InitMessage(strprintf(_("%" PRId64 " blocks were read."), nLoaded));
+                                        nLastUpdate = GetTimeMillis();
+                                    }
+                                }
+                            }
+                        }
+                        else
+                        {
+                            SeekToNext();
                         }
-                        nPos += ((unsigned char*)nFind - pchData) + 1;
-                    }
-                    else
-                        nPos += sizeof(pchData) - sizeof(pchMessageStart) + 1;
-                } while(!fRequestShutdown);
-                if (nPos == std::numeric_limits<uint32_t>::max())
-                    break;
-                fseek(blkdat, nPos, SEEK_SET);
-                unsigned int nSize;
-                blkdat >> nSize;
-                if (nSize > 0 && nSize <= MAX_BLOCK_SIZE)
-                {
-                    CBlock block;
-                    blkdat >> block;
-                    if (ProcessBlock(NULL,&block))
-                    {
-                        nLoaded++;
-                        nPos += 4 + nSize;
                     }
                 }
+                while(!fRequestShutdown);
             }
         }
         catch (const std::exception&) {
-            printf("%s() : Deserialize or I/O error caught during load\n",
+            printf("%s() : I/O error caught during load\n",
                    BOOST_CURRENT_FUNCTION);
         }
     }
@@ -3109,7 +3134,7 @@ bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
 // The message start string is designed to be unlikely to occur in normal data.
 // The characters are rarely used upper ASCII, not valid as UTF-8, and produce
 // a large 4-byte int at any alignment.
-unsigned char pchMessageStart[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
+uint8_t pchMessageStart[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
 
 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
 {
index 2d6cd87..17c52c7 100644 (file)
@@ -13,6 +13,7 @@
 #include "net.h"
 #include "script.h"
 #include "scrypt.h"
+#include "ui_interface.h"
 
 #include <limits>
 #include <list>
@@ -84,7 +85,7 @@ extern const string strMessageMagic;
 extern int64_t nTimeBestReceived;
 extern CCriticalSection cs_setpwalletRegistered;
 extern set<CWallet*> setpwalletRegistered;
-extern unsigned char pchMessageStart[4];
+extern uint8_t pchMessageStart[4];
 extern map<uint256, CBlock*> mapOrphanBlocks;
 
 // Settings
@@ -116,7 +117,7 @@ void PrintBlockTree();
 CBlockIndex* FindBlockByHeight(int nHeight);
 bool ProcessMessages(CNode* pfrom);
 bool SendMessages(CNode* pto);
-bool LoadExternalBlockFile(FILE* fileIn);
+bool LoadExternalBlockFile(FILE* fileIn, CClientUIInterface& uiInterface);
 
 // Run an instance of the script checking thread
 void ThreadScriptCheck(void* parg);
@@ -1450,7 +1451,7 @@ public:
 
     uint256 GetBlockHash() const
     {
-        if (fUseFastIndex && (nTime < GetAdjustedTime() - nOneDay) && blockHash != 0)
+        if (fUseFastIndex && blockHash != 0)
             return blockHash;
 
         CBlock block;
index fcc8c9e..1fc86fc 100644 (file)
@@ -14,6 +14,7 @@
 #include "netbase.h"
 #include <string>
 #include <limits>
+#include <vector>
 #include "uint256.h"
 
 extern bool fTestNet;
@@ -22,8 +23,7 @@ inline unsigned short GetDefaultPort()
     return static_cast<unsigned short>(fTestNet ? 17777 : 7777);
 }
 
-
-extern unsigned char pchMessageStart[4];
+extern uint8_t pchMessageStart[4];
 
 /** Message header.
  * (4) message start.
index ac1b52c..4fa5bcb 100644 (file)
@@ -102,7 +102,7 @@ QT_TRANSLATE_NOOP("bitcoin-core", "Fee per KB to add to transactions you send"),
 QT_TRANSLATE_NOOP("bitcoin-core", "Find peers using DNS lookup (default: 0)"),
 QT_TRANSLATE_NOOP("bitcoin-core", "Find peers using internet relay chat (default: 1)"),
 QT_TRANSLATE_NOOP("bitcoin-core", "Get help for a command"),
-QT_TRANSLATE_NOOP("bitcoin-core", "How many blocks to check at startup (default: 2500, 0 = all)"),
+QT_TRANSLATE_NOOP("bitcoin-core", "How many blocks to check at startup (default: 192, 0 = all)"),
 QT_TRANSLATE_NOOP("bitcoin-core", "How thorough the block verification is (0-6, default: 1)"),
 QT_TRANSLATE_NOOP("bitcoin-core", "Importing blockchain data file."),
 QT_TRANSLATE_NOOP("bitcoin-core", "Importing bootstrap blockchain data file."),
index 6128789..80c87e3 100644 (file)
@@ -4079,8 +4079,8 @@ If the file does not exist, create it with owner-readable-only file permissions.
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="105"/>
-        <source>How many blocks to check at startup (default: 2500, 0 = all)</source>
-        <translation>How many blocks to check at startup (default: 2500, 0 = all)</translation>
+        <source>How many blocks to check at startup (default: 192, 0 = all)</source>
+        <translation>How many blocks to check at startup (default: 192, 0 = all)</translation>
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="106"/>
index a1cf05c..1905d9e 100644 (file)
@@ -4083,8 +4083,8 @@ If the file does not exist, create it with owner-readable-only file permissions.
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="105"/>
-        <source>How many blocks to check at startup (default: 2500, 0 = all)</source>
-        <translation>Сколько блоков проверять при запуске (по умолчанию: 2500, 0 = все)</translation>
+        <source>How many blocks to check at startup (default: 192, 0 = all)</source>
+        <translation>Сколько блоков проверять при запуске (по умолчанию: 192, 0 = все)</translation>
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="106"/>
index f48acac..f8966f9 100644 (file)
@@ -4091,8 +4091,8 @@ If the file does not exist, create it with owner-readable-only file permissions.
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="105"/>
-        <source>How many blocks to check at startup (default: 2500, 0 = all)</source>
-        <translation>Скільки блоків перевіряти під час запуску (типово: 2500, 0 = всі)</translation>
+        <source>How many blocks to check at startup (default: 192, 0 = all)</source>
+        <translation>Скільки блоків перевіряти під час запуску (типово: 192, 0 = всі)</translation>
     </message>
     <message>
         <location filename="../bitcoinstrings.cpp" line="106"/>
index e252f69..7732648 100644 (file)
@@ -224,7 +224,7 @@ bool CTxDB::LoadBlockIndex()
 
     // Verify blocks in the best chain
     int nCheckLevel = GetArgInt("-checklevel", 1);
-    int nCheckDepth = GetArgInt( "-checkblocks", 2500);
+    int nCheckDepth = GetArgInt( "-checkblocks", 192);
     if (nCheckDepth == 0)
         nCheckDepth = 1000000000; // suffices until the year 19000
     if (nCheckDepth > nBestHeight)
index ddc149b..e975e0a 100644 (file)
@@ -455,7 +455,7 @@ bool CTxDB::LoadBlockIndex()
 
     // Verify blocks in the best chain
     int nCheckLevel = GetArgInt("-checklevel", 1);
-    int nCheckDepth = GetArgInt( "-checkblocks", 2500);
+    int nCheckDepth = GetArgInt( "-checkblocks", 192);
     if (nCheckDepth == 0)
         nCheckDepth = 1000000000; // suffices until the year 19000
     if (nCheckDepth > nBestHeight)