Merge bitcoin/bitcoin#33264: threading: reduce the scope of lock in getblocktemplate

493ba0f688 threading: reduce the scope of lock in getblocktemplate (kevkevinpal)

Pull request description:

  This change was motivated by https://github.com/bitcoin/bitcoin/pull/32592#discussion_r2294770722

  It does exactly what is said in the comment. Reducing the scope of the lock by a bit before it is needed

ACKs for top commit:
  stickies-v:
    re-ACK 493ba0f688
  maflcko:
    lgtm ACK 493ba0f688

Tree-SHA512: aa3a21ef3da6be6c0af78aa2dda61ee21c3f6d4d9c897413dba9e7d7d2a91e9e069bbc6b6684b45aadaa28d8603dd310f2c2d2e58c31bb4d864204e468fefaf1
This commit is contained in:
merge-script
2025-08-28 19:35:48 +01:00

View File

@@ -704,8 +704,6 @@ static RPCHelpMan getblocktemplate()
NodeContext& node = EnsureAnyNodeContext(request.context);
ChainstateManager& chainman = EnsureChainman(node);
Mining& miner = EnsureMining(node);
WAIT_LOCK(cs_main, csmain_lock);
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
std::string strMode = "template";
UniValue lpval = NullUniValue;
@@ -735,6 +733,7 @@ static RPCHelpMan getblocktemplate()
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
uint256 hash = block.GetHash();
LOCK(cs_main);
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
@@ -773,6 +772,9 @@ static RPCHelpMan getblocktemplate()
static unsigned int nTransactionsUpdatedLast;
const CTxMemPool& mempool = EnsureMemPool(node);
WAIT_LOCK(cs_main, cs_main_lock);
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
// Long Polling (BIP22)
if (!lpval.isNull()) {
/**
@@ -811,7 +813,7 @@ static RPCHelpMan getblocktemplate()
// Release lock while waiting
{
REVERSE_LOCK(csmain_lock, cs_main);
REVERSE_LOCK(cs_main_lock, cs_main);
MillisecondsDouble checktxtime{std::chrono::minutes(1)};
while (IsRPCRunning()) {
// If hashWatchedChain is not a real block hash, this will