Skip to content

Commit ae4db44

Browse files
committed
Create a shared_ptr for the block we're connecting in ActivateBCS
1 parent fd9d890 commit ae4db44

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/validation.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ struct ConnectTrace {
21652165
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
21662166
* corresponding to pindexNew, to bypass loading it again from disk.
21672167
*/
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)
21692169
{
21702170
assert(pindexNew->pprev == chainActive.Tip());
21712171
// Read block from disk.
@@ -2175,19 +2175,18 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
21752175
connectTrace.blocksConnected.emplace_back(pindexNew, pblockNew);
21762176
if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
21772177
return AbortNode(state, "Failed to read block");
2178-
pblock = pblockNew.get();
21792178
} 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);
21822180
}
2181+
const CBlock& blockConnecting = *connectTrace.blocksConnected.back().second;
21832182
// Apply the block atomically to the chain state.
21842183
int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
21852184
int64_t nTime3;
21862185
LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
21872186
{
21882187
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);
21912190
if (!rv) {
21922191
if (state.IsInvalid())
21932192
InvalidBlockFound(pindexNew, state);
@@ -2205,7 +2204,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
22052204
int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
22062205
LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
22072206
// 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());
22092208
// Update chainActive & related variables.
22102209
UpdateTip(pindexNew, chainparams);
22112210

@@ -2322,7 +2321,8 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
23222321

23232322
// Connect new blocks.
23242323
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)) {
23262326
if (state.IsInvalid()) {
23272327
// The block violates a consensus rule.
23282328
if (!state.CorruptionPossible())

0 commit comments

Comments
 (0)