Skip to content

Commit 4d54bb2

Browse files
committed
Merge bitcoin/bitcoin#33264: threading: reduce the scope of lock in getblocktemplate
493ba0f threading: reduce the scope of lock in getblocktemplate (kevkevinpal) Pull request description: This change was motivated by bitcoin/bitcoin#32592 (comment) 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 493ba0f maflcko: lgtm ACK 493ba0f Tree-SHA512: aa3a21ef3da6be6c0af78aa2dda61ee21c3f6d4d9c897413dba9e7d7d2a91e9e069bbc6b6684b45aadaa28d8603dd310f2c2d2e58c31bb4d864204e468fefaf1
2 parents 9ae2395 + 493ba0f commit 4d54bb2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/rpc/mining.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ static RPCHelpMan getblocktemplate()
704704
NodeContext& node = EnsureAnyNodeContext(request.context);
705705
ChainstateManager& chainman = EnsureChainman(node);
706706
Mining& miner = EnsureMining(node);
707-
WAIT_LOCK(cs_main, csmain_lock);
708-
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
709707

710708
std::string strMode = "template";
711709
UniValue lpval = NullUniValue;
@@ -735,6 +733,7 @@ static RPCHelpMan getblocktemplate()
735733
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
736734

737735
uint256 hash = block.GetHash();
736+
LOCK(cs_main);
738737
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
739738
if (pindex) {
740739
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
@@ -773,6 +772,9 @@ static RPCHelpMan getblocktemplate()
773772
static unsigned int nTransactionsUpdatedLast;
774773
const CTxMemPool& mempool = EnsureMemPool(node);
775774

775+
WAIT_LOCK(cs_main, cs_main_lock);
776+
uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
777+
776778
// Long Polling (BIP22)
777779
if (!lpval.isNull()) {
778780
/**
@@ -811,7 +813,7 @@ static RPCHelpMan getblocktemplate()
811813

812814
// Release lock while waiting
813815
{
814-
REVERSE_LOCK(csmain_lock, cs_main);
816+
REVERSE_LOCK(cs_main_lock, cs_main);
815817
MillisecondsDouble checktxtime{std::chrono::minutes(1)};
816818
while (IsRPCRunning()) {
817819
// If hashWatchedChain is not a real block hash, this will

0 commit comments

Comments
 (0)