From 87c63ba66bf0360b8e76e784e0a485203ecbdb2a Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 22 Jan 2014 20:29:20 +0400 Subject: [PATCH] Reorganize at startup to best known block Trying to detect the presence of a better chain in chain.dat and then reorganize to this chain using existent blk files. --- src/init.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index ef0067f..565eec7 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -846,6 +846,40 @@ bool AppInit2() // ********************************************************* Step 9: import blocks + // scan for better chains in the block chain database, that are not yet connected in the active best chain + CBlockIndex *pindexFoundBest = pindexBest; + for (std::map::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) { + CBlockIndex *pindex = it->second; + if (pindexFoundBest==NULL || pindex->nChainTrust > pindexFoundBest->nChainTrust) + pindexFoundBest = pindex; + } + if (pindexFoundBest != pindexBest) { + uiInterface.InitMessage(_("Importing blocks from block database...")); + uint64 nTxs = 0; + uint64 nBlocks = 0; + std::vector vAttach; + vAttach.reserve(pindexFoundBest->nHeight - (pindexBest==NULL ? 0 : pindexBest->nHeight)); + while (pindexFoundBest && pindexFoundBest->nChainTrust > (pindexBest==NULL ? 0 : pindexBest->nChainTrust)) { + vAttach.push_back(pindexFoundBest); + pindexFoundBest = pindexFoundBest->pprev; + } + for (std::vector::reverse_iterator it = vAttach.rbegin(); it != vAttach.rend(); it++) { + CBlockIndex *pindex = *it; + CBlock block; + if (!block.ReadFromDisk(pindex)) + break; + nTxs += block.vtx.size(); + nBlocks++; + if (pindex->nHeight == 0 || nTxs + nBlocks*3 > 500) { + nTxs=0; + nBlocks=0; + block.SetBestChain(pindex); + } + if (fRequestShutdown) + break; + } + } + if (mapArgs.count("-loadblock")) { uiInterface.InitMessage(_("Importing blockchain data file.")); -- 1.7.1