@@ -93,6 +93,7 @@ static int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* b
93
93
94
94
UniValue blockheaderToJSON (const CBlockIndex* tip, const CBlockIndex* blockindex)
95
95
{
96
+ // Serialize passed information without accessing chain state of the active chain!
96
97
UniValue result (UniValue::VOBJ);
97
98
result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
98
99
const CBlockIndex* pnext;
@@ -119,6 +120,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
119
120
120
121
UniValue blockToJSON (const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
121
122
{
123
+ // Serialize passed information without accessing chain state of the active chain!
122
124
UniValue result (UniValue::VOBJ);
123
125
result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
124
126
const CBlockIndex* pnext;
@@ -882,8 +884,6 @@ static UniValue getblock(const JSONRPCRequest& request)
882
884
throw std::runtime_error (help.ToString ());
883
885
}
884
886
885
- LOCK (cs_main);
886
-
887
887
uint256 hash (ParseHashV (request.params [0 ], " blockhash" ));
888
888
889
889
int verbosity = 1 ;
@@ -894,12 +894,20 @@ static UniValue getblock(const JSONRPCRequest& request)
894
894
verbosity = request.params [1 ].get_bool () ? 1 : 0 ;
895
895
}
896
896
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
+ }
901
908
902
- const CBlock block = GetBlockChecked (pblockindex);
909
+ block = GetBlockChecked (pblockindex);
910
+ }
903
911
904
912
if (verbosity <= 0 )
905
913
{
@@ -909,7 +917,7 @@ static UniValue getblock(const JSONRPCRequest& request)
909
917
return strHex;
910
918
}
911
919
912
- return blockToJSON (block, chainActive. Tip () , pblockindex, verbosity >= 2 );
920
+ return blockToJSON (block, tip , pblockindex, verbosity >= 2 );
913
921
}
914
922
915
923
struct CCoinsStats
0 commit comments