rpc: reduce LOCK(cs_main) scope in GetBlockChecked and getblock

This commit is contained in:
Andrew Toth 2022-10-17 09:10:34 -04:00
parent 7d253c943f
commit f00808e932

View File

@ -579,18 +579,20 @@ static RPCHelpMan getblockheader()
};
}
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex)
{
AssertLockHeld(::cs_main);
CBlock block;
if (blockman.IsBlockPruned(pblockindex)) {
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
{
LOCK(cs_main);
if (blockman.IsBlockPruned(pblockindex)) {
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
}
}
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) {
// Block not found on disk. This could be because we have the block
// header in our index but not yet have the block or did not accept the
// block.
// block. Or if the block was pruned right after we released the lock above.
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
}
@ -721,7 +723,6 @@ static RPCHelpMan getblock()
}
}
CBlock block;
const CBlockIndex* pblockindex;
const CBlockIndex* tip;
ChainstateManager& chainman = EnsureAnyChainman(request.context);
@ -733,10 +734,10 @@ static RPCHelpMan getblock()
if (!pblockindex) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
block = GetBlockChecked(chainman.m_blockman, pblockindex);
}
const CBlock block{GetBlockChecked(chainman.m_blockman, pblockindex)};
if (verbosity <= 0)
{
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());