@@ -2219,24 +2219,23 @@ struct ConnectTrace {
2219
2219
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2220
2220
* corresponding to pindexNew, to bypass loading it again from disk.
2221
2221
*
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.
2225
2223
*/
2226
2224
bool static ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
2227
2225
{
2228
2226
assert (pindexNew->pprev == chainActive.Tip ());
2229
2227
// Read block from disk.
2230
2228
int64_t nTime1 = GetTimeMicros ();
2229
+ std::shared_ptr<const CBlock> pthisBlock;
2231
2230
if (!pblock) {
2232
2231
std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2233
- connectTrace.blocksConnected .emplace_back (pindexNew, pblockNew);
2234
2232
if (!ReadBlockFromDisk (*pblockNew, pindexNew, chainparams.GetConsensus ()))
2235
2233
return AbortNode (state, " Failed to read block" );
2234
+ pthisBlock = pblockNew;
2236
2235
} else {
2237
- connectTrace. blocksConnected . emplace_back (pindexNew, pblock) ;
2236
+ pthisBlock = pblock;
2238
2237
}
2239
- const CBlock& blockConnecting = *connectTrace. blocksConnected . back (). second ;
2238
+ const CBlock& blockConnecting = *pthisBlock ;
2240
2239
// Apply the block atomically to the chain state.
2241
2240
int64_t nTime2 = GetTimeMicros (); nTimeReadFromDisk += nTime2 - nTime1;
2242
2241
int64_t nTime3;
@@ -2270,6 +2269,8 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
2270
2269
int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2271
2270
LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs]\n " , (nTime6 - nTime5) * 0.001 , nTimePostConnect * 0.000001 );
2272
2271
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));
2273
2274
return true ;
2274
2275
}
2275
2276
@@ -2388,8 +2389,6 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
2388
2389
state = CValidationState ();
2389
2390
fInvalidFound = true ;
2390
2391
fContinue = false ;
2391
- // If we didn't actually connect the block, don't notify listeners about it
2392
- connectTrace.blocksConnected .pop_back ();
2393
2392
break ;
2394
2393
} else {
2395
2394
// A system error occurred (disk space, database error, ...).
0 commit comments