| 607 | } |
| 608 | |
| 609 | static RPCMethod getblockheader() |
| 610 | { |
| 611 | return RPCMethod{ |
| 612 | "getblockheader", |
| 613 | "If verbose is false, returns a string that is serialized, hex-encoded data for blockheader 'hash'.\n" |
| 614 | "If verbose is true, returns an Object with information about blockheader <hash>.\n", |
| 615 | { |
| 616 | {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"}, |
| 617 | {"verbose", RPCArg::Type::BOOL, RPCArg::Default{true}, "true for a json object, false for the hex-encoded data"}, |
| 618 | }, |
| 619 | { |
| 620 | RPCResult{"for verbose = true", |
| 621 | RPCResult::Type::OBJ, "", "", |
| 622 | { |
| 623 | {RPCResult::Type::STR_HEX, "hash", "the block hash (same as provided)"}, |
| 624 | {RPCResult::Type::NUM, "confirmations", "The number of confirmations, or -1 if the block is not on the main chain"}, |
| 625 | {RPCResult::Type::NUM, "height", "The block height or index"}, |
| 626 | {RPCResult::Type::NUM, "version", "The block version"}, |
| 627 | {RPCResult::Type::STR_HEX, "versionHex", "The block version formatted in hexadecimal"}, |
| 628 | {RPCResult::Type::STR_HEX, "merkleroot", "The merkle root"}, |
| 629 | {RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME}, |
| 630 | {RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME}, |
| 631 | {RPCResult::Type::NUM, "nonce", "The nonce"}, |
| 632 | {RPCResult::Type::STR_HEX, "bits", "nBits: compact representation of the block difficulty target"}, |
| 633 | {RPCResult::Type::STR_HEX, "target", "The difficulty target"}, |
| 634 | {RPCResult::Type::NUM, "difficulty", "The difficulty"}, |
| 635 | {RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the current chain"}, |
| 636 | {RPCResult::Type::NUM, "nTx", "The number of transactions in the block"}, |
| 637 | {RPCResult::Type::STR_HEX, "previousblockhash", /*optional=*/true, "The hash of the previous block (if available)"}, |
| 638 | {RPCResult::Type::STR_HEX, "nextblockhash", /*optional=*/true, "The hash of the next block (if available)"}, |
| 639 | }}, |
| 640 | RPCResult{"for verbose=false", |
| 641 | RPCResult::Type::STR_HEX, "", "A string that is serialized, hex-encoded data for block 'hash'"}, |
| 642 | }, |
| 643 | RPCExamples{ |
| 644 | HelpExampleCli("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") |
| 645 | + HelpExampleRpc("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") |
| 646 | }, |
| 647 | [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue |
| 648 | { |
| 649 | uint256 hash(ParseHashV(request.params[0], "hash")); |
| 650 | |
| 651 | bool fVerbose = true; |
| 652 | if (!request.params[1].isNull()) |
| 653 | fVerbose = request.params[1].get_bool(); |
| 654 | |
| 655 | const CBlockIndex* pblockindex; |
| 656 | const CBlockIndex* tip; |
| 657 | ChainstateManager& chainman = EnsureAnyChainman(request.context); |
| 658 | { |
| 659 | LOCK(cs_main); |
| 660 | pblockindex = chainman.m_blockman.LookupBlockIndex(hash); |
| 661 | tip = chainman.ActiveChain().Tip(); |
| 662 | } |
| 663 | |
| 664 | if (!pblockindex) { |
| 665 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); |
| 666 | } |
nothing calls this directly
no test coverage detected