Skip to content

Commit 29a9f07

Browse files
committed
Merge #12153: Avoid permanent cs_main lock in getblockheader
f12e1d0 rpc: Avoid permanent cs_main lock in getblockheader (João Barbosa) Pull request description: This PR reduces the `cs_main` lock scope in `getblockheader` RPC. Tree-SHA512: bc51f80e15d1b32d3c7886836457f9929706b6aad9841dafce31ffca444281471b21b56192bb50de774184b9377412f815ad8d3d2439049a7e64d2e59c415767
2 parents 1973257 + f12e1d0 commit 29a9f07

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,20 @@ static UniValue getblockheader(const JSONRPCRequest& request)
747747
+ HelpExampleRpc("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
748748
);
749749

750-
LOCK(cs_main);
751-
752750
uint256 hash(ParseHashV(request.params[0], "hash"));
753751

754752
bool fVerbose = true;
755753
if (!request.params[1].isNull())
756754
fVerbose = request.params[1].get_bool();
757755

758-
const CBlockIndex* pblockindex = LookupBlockIndex(hash);
756+
const CBlockIndex* pblockindex;
757+
const CBlockIndex* tip;
758+
{
759+
LOCK(cs_main);
760+
pblockindex = LookupBlockIndex(hash);
761+
tip = chainActive.Tip();
762+
}
763+
759764
if (!pblockindex) {
760765
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
761766
}
@@ -768,7 +773,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
768773
return strHex;
769774
}
770775

771-
return blockheaderToJSON(chainActive.Tip(), pblockindex);
776+
return blockheaderToJSON(tip, pblockindex);
772777
}
773778

774779
static CBlock GetBlockChecked(const CBlockIndex* pblockindex)

0 commit comments

Comments
 (0)