MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / getblock

Function getblock

src/rpc/blockchain.cpp:805–890  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

803}
804
805static RPCMethod getblock()
806{
807 return RPCMethod{
808 "getblock",
809 "If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.\n"
810 "If verbosity is 1, returns an Object with information about block <hash>.\n"
811 "If verbosity is 2, returns an Object with information about block <hash> and information about each transaction.\n"
812 "If verbosity is 3, returns an Object with information about block <hash> and information about each transaction, including prevout information for inputs (only for unpruned blocks in the current best chain).\n",
813 {
814 {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
815 {"verbosity|verbose", RPCArg::Type::NUM, RPCArg::Default{1}, "0 for hex-encoded data, 1 for a JSON object, 2 for JSON object with transaction data, and 3 for JSON object with transaction data including prevout information for inputs",
816 RPCArgOptions{.skip_type_check = true}},
817 },
818 {
819 RPCResult{"for verbosity = 0", RPCResult::Type::STR_HEX, "", "A string that is serialized, hex-encoded data for block 'hash'"},
820 RPCResult{"for verbosity = 1", RPCResult::Type::OBJ, "", "",
821 GetBlockFields({RPCResult::Type::ARR, "tx", "The transaction ids",
822 {{RPCResult::Type::STR_HEX, "", "The transaction id"}}})},
823 RPCResult{"for verbosity = 2", RPCResult::Type::OBJ, "", "",
824 GetBlockFields({RPCResult::Type::ARR, "tx", "",
825 {
826 {RPCResult::Type::OBJ, "", "",
827 TxDoc({.elision_mode = ElisionMode::WithSummary,
828 .elision_summary = "The transactions in the format of the getrawtransaction RPC. Different from verbosity = 1 \"tx\" result",
829 .fee = true, .hex = true,
830 .fee_doc = "The transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available"})},
831 }}, /*elision_msg=*/"Same output as verbosity = 1")},
832 RPCResult{"for verbosity = 3", RPCResult::Type::OBJ, "", "",
833 GetBlockFields({RPCResult::Type::ARR, "tx", "",
834 {
835 {RPCResult::Type::OBJ, "", "",
836 TxDoc({.elision_mode = ElisionMode::Silent,
837 .prevout = true,
838 .prevout_optional = true,
839 .fee = true,
840 .hex = true,
841 .vin_item_doc = "",
842 .prevout_doc = "(Only if undo information is available)",
843 .vin_inner_elision = "The same output as verbosity = 2"})},
844 }}, /*elision_msg=*/"Same output as verbosity = 2")},
845 },
846 RPCExamples{
847 HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
848 + HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
849 },
850 [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
851{
852 uint256 hash(ParseHashV(request.params[0], "blockhash"));
853
854 int verbosity{ParseVerbosity(request.params[1], /*default_verbosity=*/1, /*allow_bool=*/true)};
855
856 const CBlockIndex* pblockindex;
857 const CBlockIndex* tip;
858 ChainstateManager& chainman = EnsureAnyChainman(request.context);
859 {
860 LOCK(cs_main);
861 pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
862 tip = chainman.ActiveChain().Tip();

Callers

nothing calls this directly

Calls 12

GetBlockFieldsFunction · 0.85
TxDocFunction · 0.85
HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
ParseHashVFunction · 0.85
ParseVerbosityFunction · 0.85
JSONRPCErrorFunction · 0.85
GetRawBlockCheckedFunction · 0.85
blockToJSONFunction · 0.85
LookupBlockIndexMethod · 0.80
HexStrFunction · 0.50
TipMethod · 0.45

Tested by

no test coverage detected