Skip to content

Commit 822000c

Browse files
committed
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
1 parent f5e9a01 commit 822000c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/validation.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,24 +2219,23 @@ struct ConnectTrace {
22192219
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
22202220
* corresponding to pindexNew, to bypass loading it again from disk.
22212221
*
2222-
* The block is always added to connectTrace (either after loading from disk or by copying
2223-
* pblock) - if that is not intended, care must be taken to remove the last entry in
2224-
* blocksConnected in case of failure.
2222+
* The block is added to connectTrace if connection succeeds.
22252223
*/
22262224
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
22272225
{
22282226
assert(pindexNew->pprev == chainActive.Tip());
22292227
// Read block from disk.
22302228
int64_t nTime1 = GetTimeMicros();
2229+
std::shared_ptr<const CBlock> pthisBlock;
22312230
if (!pblock) {
22322231
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2233-
connectTrace.blocksConnected.emplace_back(pindexNew, pblockNew);
22342232
if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
22352233
return AbortNode(state, "Failed to read block");
2234+
pthisBlock = pblockNew;
22362235
} else {
2237-
connectTrace.blocksConnected.emplace_back(pindexNew, pblock);
2236+
pthisBlock = pblock;
22382237
}
2239-
const CBlock& blockConnecting = *connectTrace.blocksConnected.back().second;
2238+
const CBlock& blockConnecting = *pthisBlock;
22402239
// Apply the block atomically to the chain state.
22412240
int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
22422241
int64_t nTime3;
@@ -2270,6 +2269,8 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
22702269
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
22712270
LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
22722271
LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
2272+
2273+
connectTrace.blocksConnected.emplace_back(pindexNew, std::move(pthisBlock));
22732274
return true;
22742275
}
22752276

@@ -2388,8 +2389,6 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
23882389
state = CValidationState();
23892390
fInvalidFound = true;
23902391
fContinue = false;
2391-
// If we didn't actually connect the block, don't notify listeners about it
2392-
connectTrace.blocksConnected.pop_back();
23932392
break;
23942393
} else {
23952394
// A system error occurred (disk space, database error, ...).

0 commit comments

Comments
 (0)