Skip to content

Commit

Permalink
blockassembler: do not lock cs_main and mempool cs for the entire blo…
Browse files Browse the repository at this point in the history
…ck, only where are needed.
  • Loading branch information
furszy committed Jan 27, 2021
1 parent f8d0705 commit 4932fc5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/blockassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblocktemplate->vTxFees.push_back(-1); // updated at end
pblocktemplate->vTxSigOps.push_back(-1); // updated at end

LOCK2(cs_main, mempool.cs);
CBlockIndex* pindexPrev = chainActive.Tip();
CBlockIndex* pindexPrev = WITH_LOCK(cs_main, return chainActive.Tip());
nHeight = pindexPrev->nHeight + 1;

pblock->nVersion = ComputeBlockVersion(chainparams.GetConsensus(), nHeight);
Expand All @@ -178,9 +177,12 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
return nullptr;
}

// Add transactions
addPriorityTxs();
addScoreTxs();
{
// Add transactions from mempool
LOCK(mempool.cs);
addPriorityTxs();
addScoreTxs();
}

if (!fProofOfStake) {
// Coinbase can get the fees.
Expand Down Expand Up @@ -213,9 +215,15 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}
}

CValidationState state;
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false)) {
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
{
LOCK(cs_main);
if (chainActive.Tip() != pindexPrev) return nullptr; // new block came in, move on

CValidationState state;
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false)) {
throw std::runtime_error(
strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
}
}

return std::move(pblocktemplate);
Expand Down

0 comments on commit 4932fc5

Please sign in to comment.