Skip to content

Commit fab00a5

Browse files
author
MarcoFalke
committed
rpc: Serialize in getblock without cs_main
1 parent fa1c359 commit fab00a5

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/rpc/blockchain.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
9393

9494
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex)
9595
{
96+
// Serialize passed information without accessing chain state of the active chain!
9697
UniValue result(UniValue::VOBJ);
9798
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
9899
const CBlockIndex* pnext;
@@ -119,6 +120,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
119120

120121
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
121122
{
123+
// Serialize passed information without accessing chain state of the active chain!
122124
UniValue result(UniValue::VOBJ);
123125
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
124126
const CBlockIndex* pnext;
@@ -882,8 +884,6 @@ static UniValue getblock(const JSONRPCRequest& request)
882884
throw std::runtime_error(help.ToString());
883885
}
884886

885-
LOCK(cs_main);
886-
887887
uint256 hash(ParseHashV(request.params[0], "blockhash"));
888888

889889
int verbosity = 1;
@@ -894,12 +894,20 @@ static UniValue getblock(const JSONRPCRequest& request)
894894
verbosity = request.params[1].get_bool() ? 1 : 0;
895895
}
896896

897-
const CBlockIndex* pblockindex = LookupBlockIndex(hash);
898-
if (!pblockindex) {
899-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
900-
}
897+
CBlock block;
898+
const CBlockIndex* pblockindex;
899+
const CBlockIndex* tip;
900+
{
901+
LOCK(cs_main);
902+
pblockindex = LookupBlockIndex(hash);
903+
tip = chainActive.Tip();
904+
905+
if (!pblockindex) {
906+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
907+
}
901908

902-
const CBlock block = GetBlockChecked(pblockindex);
909+
block = GetBlockChecked(pblockindex);
910+
}
903911

904912
if (verbosity <= 0)
905913
{
@@ -909,7 +917,7 @@ static UniValue getblock(const JSONRPCRequest& request)
909917
return strHex;
910918
}
911919

912-
return blockToJSON(block, chainActive.Tip(), pblockindex, verbosity >= 2);
920+
return blockToJSON(block, tip, pblockindex, verbosity >= 2);
913921
}
914922

915923
struct CCoinsStats

0 commit comments

Comments
 (0)