Skip to content

Commit 06ec480

Browse files
committed
threading: reduce the scope of lock in getblocktemplate
1 parent 6ca6f3b commit 06ec480

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;
@@ -734,6 +732,7 @@ static RPCHelpMan getblocktemplate()
734732
if (!DecodeHexBlk(block, dataval.get_str()))
735733
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
736734

735+
LOCK(cs_main);
737736
uint256 hash = block.GetHash();
738737
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
739738
if (pindex) {
@@ -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)