@@ -2165,7 +2165,7 @@ struct ConnectTrace {
2165
2165
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2166
2166
* corresponding to pindexNew, to bypass loading it again from disk.
2167
2167
*/
2168
- bool static ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, ConnectTrace& connectTrace)
2168
+ bool static ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr< const CBlock>& pblock, ConnectTrace& connectTrace)
2169
2169
{
2170
2170
assert (pindexNew->pprev == chainActive.Tip ());
2171
2171
// Read block from disk.
@@ -2175,19 +2175,18 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
2175
2175
connectTrace.blocksConnected .emplace_back (pindexNew, pblockNew);
2176
2176
if (!ReadBlockFromDisk (*pblockNew, pindexNew, chainparams.GetConsensus ()))
2177
2177
return AbortNode (state, " Failed to read block" );
2178
- pblock = pblockNew.get ();
2179
2178
} else {
2180
- // TODO: This copy is a major performance regression, but ProcessNewBlock callers need updated to fix this
2181
- connectTrace.blocksConnected .emplace_back (pindexNew, std::make_shared<const CBlock>(*pblock));
2179
+ connectTrace.blocksConnected .emplace_back (pindexNew, pblock);
2182
2180
}
2181
+ const CBlock& blockConnecting = *connectTrace.blocksConnected .back ().second ;
2183
2182
// Apply the block atomically to the chain state.
2184
2183
int64_t nTime2 = GetTimeMicros (); nTimeReadFromDisk += nTime2 - nTime1;
2185
2184
int64_t nTime3;
2186
2185
LogPrint (" bench" , " - Load block from disk: %.2fms [%.2fs]\n " , (nTime2 - nTime1) * 0.001 , nTimeReadFromDisk * 0.000001 );
2187
2186
{
2188
2187
CCoinsViewCache view (pcoinsTip);
2189
- bool rv = ConnectBlock (*pblock , state, pindexNew, view, chainparams);
2190
- GetMainSignals ().BlockChecked (*pblock , state);
2188
+ bool rv = ConnectBlock (blockConnecting , state, pindexNew, view, chainparams);
2189
+ GetMainSignals ().BlockChecked (blockConnecting , state);
2191
2190
if (!rv) {
2192
2191
if (state.IsInvalid ())
2193
2192
InvalidBlockFound (pindexNew, state);
@@ -2205,7 +2204,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
2205
2204
int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
2206
2205
LogPrint (" bench" , " - Writing chainstate: %.2fms [%.2fs]\n " , (nTime5 - nTime4) * 0.001 , nTimeChainState * 0.000001 );
2207
2206
// Remove conflicting transactions from the mempool.;
2208
- mempool.removeForBlock (pblock-> vtx , pindexNew->nHeight , &connectTrace.txConflicted , !IsInitialBlockDownload ());
2207
+ mempool.removeForBlock (blockConnecting. vtx , pindexNew->nHeight , &connectTrace.txConflicted , !IsInitialBlockDownload ());
2209
2208
// Update chainActive & related variables.
2210
2209
UpdateTip (pindexNew, chainparams);
2211
2210
@@ -2322,7 +2321,8 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
2322
2321
2323
2322
// Connect new blocks.
2324
2323
BOOST_REVERSE_FOREACH (CBlockIndex *pindexConnect, vpindexToConnect) {
2325
- if (!ConnectTip (state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL , connectTrace)) {
2324
+ // TODO: The pblock copy is a major performance regression, but callers need updated to fix this
2325
+ if (!ConnectTip (state, chainparams, pindexConnect, (pblock && pindexConnect == pindexMostWork) ? std::make_shared<const CBlock>(*pblock) : std::shared_ptr<const CBlock>(), connectTrace)) {
2326
2326
if (state.IsInvalid ()) {
2327
2327
// The block violates a consensus rule.
2328
2328
if (!state.CorruptionPossible ())
0 commit comments