Skip to content

Commit 2736c44

Browse files
committed
Make the optional pblock in ActivateBestChain a shared_ptr
1 parent ae4db44 commit 2736c44

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ UniValue invalidateblock(const JSONRPCRequest& request)
13191319
}
13201320

13211321
if (state.IsValid()) {
1322-
ActivateBestChain(state, Params(), NULL);
1322+
ActivateBestChain(state, Params());
13231323
}
13241324

13251325
if (!state.IsValid()) {
@@ -1357,7 +1357,7 @@ UniValue reconsiderblock(const JSONRPCRequest& request)
13571357
}
13581358

13591359
CValidationState state;
1360-
ActivateBestChain(state, Params(), NULL);
1360+
ActivateBestChain(state, Params());
13611361

13621362
if (!state.IsValid()) {
13631363
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason());

src/validation.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ static void PruneBlockIndexCandidates() {
22882288
* Try to make some progress towards making pindexMostWork the active block.
22892289
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
22902290
*/
2291-
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
2291+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
22922292
{
22932293
AssertLockHeld(cs_main);
22942294
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2321,8 +2321,7 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
23212321

23222322
// Connect new blocks.
23232323
BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
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)) {
2324+
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace)) {
23262325
if (state.IsInvalid()) {
23272326
// The block violates a consensus rule.
23282327
if (!state.CorruptionPossible())
@@ -2389,7 +2388,7 @@ static void NotifyHeaderTip() {
23892388
* or an activated best chain. pblock is either NULL or a pointer to a block
23902389
* that is already loaded (to avoid loading it again from disk).
23912390
*/
2392-
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
2391+
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
23932392
CBlockIndex *pindexMostWork = NULL;
23942393
CBlockIndex *pindexNewTip = NULL;
23952394
do {
@@ -2412,7 +2411,8 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
24122411
return true;
24132412

24142413
bool fInvalidFound = false;
2415-
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound, connectTrace))
2414+
std::shared_ptr<const CBlock> nullBlockPtr;
2415+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
24162416
return false;
24172417

24182418
if (fInvalidFound) {
@@ -3142,8 +3142,13 @@ bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool
31423142

31433143
NotifyHeaderTip();
31443144

3145+
//TODO: This copy is a major performance regression, but callers need updated to fix this
3146+
std::shared_ptr<const CBlock> block_ptr;
3147+
if (pblock)
3148+
block_ptr.reset(new CBlock(*pblock));
3149+
31453150
CValidationState state; // Only used to report errors, not invalidity - ignore it
3146-
if (!ActivateBestChain(state, chainparams, pblock))
3151+
if (!ActivateBestChain(state, chainparams, block_ptr))
31473152
return error("%s: ActivateBestChain failed", __func__);
31483153

31493154
return true;

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ std::string GetWarnings(const std::string& strFor);
278278
/** Retrieve a transaction (from memory pool, or from disk, if possible) */
279279
bool GetTransaction(const uint256 &hash, CTransaction &tx, const Consensus::Params& params, uint256 &hashBlock, bool fAllowSlow = false);
280280
/** Find the best known block, and make it the tip of the block chain */
281-
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, const CBlock* pblock = NULL);
281+
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());
282282
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
283283

284284
/**

0 commit comments

Comments
 (0)