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 29, 2021
1 parent fed3307 commit 2994d88
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 @@ -160,8 +160,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 @@ -177,9 +176,12 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
return nullptr;
}

// Add transactions
addPriorityTxs();
addScoreTxs();
{
// Add transactions from mempool
LOCK2(cs_main,mempool.cs);
addPriorityTxs();
addScoreTxs();
}

if (!fProofOfStake) {
// Coinbase can get the fees.
Expand Down Expand Up @@ -211,9 +213,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 2994d88

Please sign in to comment.