@@ -93,6 +93,9 @@ 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!
97
+ AssertLockNotHeld (cs_main); // For performance reasons
98
+
96
99
UniValue result (UniValue::VOBJ);
97
100
result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
98
101
const CBlockIndex* pnext;
@@ -119,6 +122,9 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
119
122
120
123
UniValue blockToJSON (const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
121
124
{
125
+ // Serialize passed information without accessing chain state of the active chain!
126
+ AssertLockNotHeld (cs_main); // For performance reasons
127
+
122
128
UniValue result (UniValue::VOBJ);
123
129
result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
124
130
const CBlockIndex* pnext;
@@ -824,9 +830,7 @@ static CBlock GetBlockChecked(const CBlockIndex* pblockindex)
824
830
825
831
static UniValue getblock (const JSONRPCRequest& request)
826
832
{
827
- if (request.fHelp || request.params .size () < 1 || request.params .size () > 2 )
828
- throw std::runtime_error (
829
- RPCHelpMan{" getblock" ,
833
+ const RPCHelpMan help{" getblock" ,
830
834
" \n If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n "
831
835
" If verbosity is 1, returns an Object with information about block <hash>.\n "
832
836
" If verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n " ,
@@ -878,9 +882,11 @@ static UniValue getblock(const JSONRPCRequest& request)
878
882
HelpExampleCli (" getblock" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" " )
879
883
+ HelpExampleRpc (" getblock" , " \" 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\" " )
880
884
},
881
- }. ToString ()) ;
885
+ } ;
882
886
883
- LOCK (cs_main);
887
+ if (request.fHelp || !help.IsValidNumArgs (request.params .size ())) {
888
+ throw std::runtime_error (help.ToString ());
889
+ }
884
890
885
891
uint256 hash (ParseHashV (request.params [0 ], " blockhash" ));
886
892
@@ -892,12 +898,20 @@ static UniValue getblock(const JSONRPCRequest& request)
892
898
verbosity = request.params [1 ].get_bool () ? 1 : 0 ;
893
899
}
894
900
895
- const CBlockIndex* pblockindex = LookupBlockIndex (hash);
896
- if (!pblockindex) {
897
- throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Block not found" );
898
- }
901
+ CBlock block;
902
+ const CBlockIndex* pblockindex;
903
+ const CBlockIndex* tip;
904
+ {
905
+ LOCK (cs_main);
906
+ pblockindex = LookupBlockIndex (hash);
907
+ tip = chainActive.Tip ();
908
+
909
+ if (!pblockindex) {
910
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Block not found" );
911
+ }
899
912
900
- const CBlock block = GetBlockChecked (pblockindex);
913
+ block = GetBlockChecked (pblockindex);
914
+ }
901
915
902
916
if (verbosity <= 0 )
903
917
{
@@ -907,7 +921,7 @@ static UniValue getblock(const JSONRPCRequest& request)
907
921
return strHex;
908
922
}
909
923
910
- return blockToJSON (block, chainActive. Tip () , pblockindex, verbosity >= 2 );
924
+ return blockToJSON (block, tip , pblockindex, verbosity >= 2 );
911
925
}
912
926
913
927
struct CCoinsStats
0 commit comments