Add pblock to connectTrace at the end of ConnectTip, not start

This makes ConnectTip responsible for the ConnectTrace instead
of splitting the logic between ActivateBestChainStep and ConnectTip
This commit is contained in:
Matt Corallo 2017-03-06 17:11:33 -05:00
parent f5e9a019a4
commit 822000cf82

View file

@ -2219,24 +2219,23 @@ struct ConnectTrace {
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
* corresponding to pindexNew, to bypass loading it again from disk. * corresponding to pindexNew, to bypass loading it again from disk.
* *
* The block is always added to connectTrace (either after loading from disk or by copying * The block is added to connectTrace if connection succeeds.
* pblock) - if that is not intended, care must be taken to remove the last entry in
* blocksConnected in case of failure.
*/ */
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace) bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
{ {
assert(pindexNew->pprev == chainActive.Tip()); assert(pindexNew->pprev == chainActive.Tip());
// Read block from disk. // Read block from disk.
int64_t nTime1 = GetTimeMicros(); int64_t nTime1 = GetTimeMicros();
std::shared_ptr<const CBlock> pthisBlock;
if (!pblock) { if (!pblock) {
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>(); std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
connectTrace.blocksConnected.emplace_back(pindexNew, pblockNew);
if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus())) if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
return AbortNode(state, "Failed to read block"); return AbortNode(state, "Failed to read block");
pthisBlock = pblockNew;
} else { } else {
connectTrace.blocksConnected.emplace_back(pindexNew, pblock); pthisBlock = pblock;
} }
const CBlock& blockConnecting = *connectTrace.blocksConnected.back().second; const CBlock& blockConnecting = *pthisBlock;
// Apply the block atomically to the chain state. // Apply the block atomically to the chain state.
int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1; int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
int64_t nTime3; int64_t nTime3;
@ -2270,6 +2269,8 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001); LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001); LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
connectTrace.blocksConnected.emplace_back(pindexNew, std::move(pthisBlock));
return true; return true;
} }
@ -2388,8 +2389,6 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
state = CValidationState(); state = CValidationState();
fInvalidFound = true; fInvalidFound = true;
fContinue = false; fContinue = false;
// If we didn't actually connect the block, don't notify listeners about it
connectTrace.blocksConnected.pop_back();
break; break;
} else { } else {
// A system error occurred (disk space, database error, ...). // A system error occurred (disk space, database error, ...).