Skip to content

Commit f00808e

Browse files
committed
rpc: reduce LOCK(cs_main) scope in GetBlockChecked and getblock
1 parent 7d253c9 commit f00808e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/rpc/blockchain.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -579,18 +579,20 @@ static RPCHelpMan getblockheader()
579579
};
580580
}
581581

582-
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
582+
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex)
583583
{
584-
AssertLockHeld(::cs_main);
585584
CBlock block;
586-
if (blockman.IsBlockPruned(pblockindex)) {
587-
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
585+
{
586+
LOCK(cs_main);
587+
if (blockman.IsBlockPruned(pblockindex)) {
588+
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
589+
}
588590
}
589591

590592
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) {
591593
// Block not found on disk. This could be because we have the block
592594
// header in our index but not yet have the block or did not accept the
593-
// block.
595+
// block. Or if the block was pruned right after we released the lock above.
594596
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
595597
}
596598

@@ -721,7 +723,6 @@ static RPCHelpMan getblock()
721723
}
722724
}
723725

724-
CBlock block;
725726
const CBlockIndex* pblockindex;
726727
const CBlockIndex* tip;
727728
ChainstateManager& chainman = EnsureAnyChainman(request.context);
@@ -733,10 +734,10 @@ static RPCHelpMan getblock()
733734
if (!pblockindex) {
734735
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
735736
}
736-
737-
block = GetBlockChecked(chainman.m_blockman, pblockindex);
738737
}
739738

739+
const CBlock block{GetBlockChecked(chainman.m_blockman, pblockindex)};
740+
740741
if (verbosity <= 0)
741742
{
742743
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());

0 commit comments

Comments
 (0)