From 493ba0f688311280056491986068cd4a9ad2be66 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Wed, 27 Aug 2025 21:13:36 -0400 Subject: [PATCH] threading: reduce the scope of lock in getblocktemplate --- src/rpc/mining.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index b710c605bcb..750ca72bb43 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -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