diff --git a/src/main.cpp b/src/main.cpp index 85914e54e..df2dfd537 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3212,15 +3212,8 @@ uint64_t nNotifiedSequence = 0; */ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock) { - assert(pindexNew->pprev == chainActive.Tip()); - // Read block from disk. + assert(pblock && pindexNew->pprev == chainActive.Tip()); int64_t nTime1 = GetTimeMicros(); - CBlock block; - if (!pblock) { - if (!ReadBlockFromDisk(block, pindexNew, chainparams.GetConsensus())) - return AbortNode(state, "Failed to read block"); - pblock = █ - } // Apply the block atomically to the chain state. int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1; int64_t nTime3; @@ -3431,7 +3424,18 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c // Connect new blocks. BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) { - if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) { + const CBlock* pconnectBlock; + CBlock block; + if (pblock && pindexConnect == pindexMostWork) { + pconnectBlock = pblock; + } else { + // read the block to be connected from disk + if (!ReadBlockFromDisk(block, pindexConnect, chainparams.GetConsensus())) + return AbortNode(state, "Failed to read block"); + pconnectBlock = █ + } + + if (!ConnectTip(state, chainparams, pindexConnect, pconnectBlock)) { if (state.IsInvalid()) { // The block violates a consensus rule. if (!state.CorruptionPossible())