Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show First 20 Lines • Show All 754 Lines • ▼ Show 20 Lines | UniValue getblockheader(const Config &config, const JSONRPCRequest &request) { | ||||
return blockheaderToJSON(pblockindex); | return blockheaderToJSON(pblockindex); | ||||
} | } | ||||
UniValue getblock(const Config &config, const JSONRPCRequest &request) { | UniValue getblock(const Config &config, const JSONRPCRequest &request) { | ||||
if (request.fHelp || request.params.size() < 1 || | if (request.fHelp || request.params.size() < 1 || | ||||
request.params.size() > 2) { | request.params.size() > 2) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"getblock \"blockhash\" ( verbose )\n" | "getblock \"blockhash\" ( verbosity )\n" | ||||
"\nIf verbose is false, returns a string that is serialized, " | "\nIf verbosity is 0 or false, returns a string that is " | ||||
"hex-encoded data for block 'hash'.\n" | "serialized, hex-encoded data for block 'hash'.\n" | ||||
"If verbose is true, returns an Object with information about " | "If verbosity is 1 or true, returns an Object with information " | ||||
"block <hash>.\n" | "about block <hash>.\n" | ||||
"If verbosity is 2, returns an Object with information about block " | |||||
"<hash> and information about each transaction.\n" | |||||
"\nArguments:\n" | "\nArguments:\n" | ||||
"1. \"blockhash\" (string, required) The block hash\n" | "1. \"blockhash\" (string, required) The block hash\n" | ||||
"2. verbose (boolean, optional, default=true) true " | "2. verbosity (numeric, optional, default=1) 0 for " | ||||
"for a json object, false for the hex encoded data\n" | "hex-encoded data, 1 for a json object, and 2 for json object with " | ||||
"\nResult (for verbose = true):\n" | "transaction data\n" | ||||
"\nResult (for verbosity = 0):\n" | |||||
"\"data\" (string) A string that is serialized, " | |||||
"hex-encoded data for block 'hash'.\n" | |||||
"\nResult (for verbosity = 1):\n" | |||||
"{\n" | "{\n" | ||||
" \"hash\" : \"hash\", (string) the block hash (same as " | " \"hash\" : \"hash\", (string) The block hash (same as " | ||||
"provided)\n" | "provided)\n" | ||||
" \"confirmations\" : n, (numeric) The number of confirmations, " | " \"confirmations\" : n, (numeric) The number of confirmations, " | ||||
"or -1 if the block is not on the main chain\n" | "or -1 if the block is not on the main chain\n" | ||||
" \"size\" : n, (numeric) The block size\n" | " \"size\" : n, (numeric) The block size\n" | ||||
" \"height\" : n, (numeric) The block height or index\n" | " \"height\" : n, (numeric) The block height or index\n" | ||||
" \"version\" : n, (numeric) The block version\n" | " \"version\" : n, (numeric) The block version\n" | ||||
" \"versionHex\" : \"00000000\", (string) The block version " | " \"versionHex\" : \"00000000\", (string) The block version " | ||||
"formatted in hexadecimal\n" | "formatted in hexadecimal\n" | ||||
" \"merkleroot\" : \"xxxx\", (string) The merkle root\n" | " \"merkleroot\" : \"xxxx\", (string) The merkle root\n" | ||||
" \"tx\" : [ (array of string) The transaction ids\n" | " \"tx\" : [ (array of string) The transaction ids\n" | ||||
" \"transactionid\" (string) The transaction id\n" | " \"transactionid\" (string) The transaction id\n" | ||||
" ,...\n" | " ,...\n" | ||||
" ],\n" | " ],\n" | ||||
" \"time\" : ttt, (numeric) The block time in seconds " | " \"time\" : ttt, (numeric) The block time in seconds " | ||||
"since epoch (Jan 1 1970 GMT)\n" | "since epoch (Jan 1 1970 GMT)\n" | ||||
" \"mediantime\" : ttt, (numeric) The median block time in " | " \"mediantime\" : ttt, (numeric) The median block time in " | ||||
"seconds since epoch (Jan 1 1970 GMT)\n" | "seconds since epoch (Jan 1 1970 GMT)\n" | ||||
" \"nonce\" : n, (numeric) The nonce\n" | " \"nonce\" : n, (numeric) The nonce\n" | ||||
" \"bits\" : \"1d00ffff\", (string) The bits\n" | " \"bits\" : \"1d00ffff\", (string) The bits\n" | ||||
" \"difficulty\" : x.xxx, (numeric) The difficulty\n" | " \"difficulty\" : x.xxx, (numeric) The difficulty\n" | ||||
" \"chainwork\" : \"xxxx\", (string) Expected number of hashes " | " \"chainwork\" : \"xxxx\", (string) Expected number of hashes " | ||||
"required to produce the chain up to this block (in hex)\n" | "required to produce the chain up to this block (in hex)\n" | ||||
" \"previousblockhash\" : \"hash\", (string) The hash of the " | " \"previousblockhash\" : \"hash\", (string) The hash of the " | ||||
"previous block\n" | "previous block\n" | ||||
" \"nextblockhash\" : \"hash\" (string) The hash of the " | " \"nextblockhash\" : \"hash\" (string) The hash of the " | ||||
"next block\n" | "next block\n" | ||||
"}\n" | "}\n" | ||||
"\nResult (for verbose=false):\n" | "\nResult (for verbosity = 2):\n" | ||||
"\"data\" (string) A string that is serialized, " | "{\n" | ||||
"hex-encoded data for block 'hash'.\n" | " ..., Same output as verbosity = 1\n" | ||||
" \"tx\" : [ (array of Objects) The transactions in " | |||||
"the format of the getrawtransaction RPC; different from verbosity " | |||||
"= 1 \"tx\" result\n" | |||||
" ...\n" | |||||
" ],\n" | |||||
" ... Same output as verbosity = 1\n" | |||||
"}\n" | |||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d" | HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d" | ||||
"214adbda81d7e2a3dd146f6ed09\"") + | "214adbda81d7e2a3dd146f6ed09\"") + | ||||
HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d" | HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d" | ||||
"214adbda81d7e2a3dd146f6ed09\"")); | "214adbda81d7e2a3dd146f6ed09\"")); | ||||
} | } | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
std::string strHash = request.params[0].get_str(); | std::string strHash = request.params[0].get_str(); | ||||
uint256 hash(uint256S(strHash)); | uint256 hash(uint256S(strHash)); | ||||
bool fVerbose = true; | int verbosity = 1; | ||||
if (request.params.size() > 1) { | if (request.params.size() > 1) { | ||||
fVerbose = request.params[1].get_bool(); | if (request.params[1].isNum()) { | ||||
verbosity = request.params[1].get_int(); | |||||
} else { | |||||
verbosity = request.params[1].get_bool() ? 1 : 0; | |||||
} | |||||
} | } | ||||
if (mapBlockIndex.count(hash) == 0) { | if (mapBlockIndex.count(hash) == 0) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlock block; | CBlock block; | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | CBlockIndex *pblockindex = mapBlockIndex[hash]; | ||||
if (fHavePruned && !pblockindex->nStatus.hasData() && | if (fHavePruned && !pblockindex->nStatus.hasData() && | ||||
pblockindex->nTx > 0) { | pblockindex->nTx > 0) { | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | ||||
} | } | ||||
if (!ReadBlockFromDisk(block, pblockindex, config)) { | if (!ReadBlockFromDisk(block, pblockindex, config)) { | ||||
// Block not found on disk. This could be because we have the block | // Block not found on disk. This could be because we have the block | ||||
// header in our index but don't have the block (for example if a | // header in our index but don't have the block (for example if a | ||||
// non-whitelisted node sends us an unrequested long chain of valid | // non-whitelisted node sends us an unrequested long chain of valid | ||||
// blocks, we add the headers to our index, but don't accept the block). | // blocks, we add the headers to our index, but don't accept the block). | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk"); | throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk"); | ||||
} | } | ||||
if (!fVerbose) { | if (verbosity <= 0) { | ||||
CDataStream ssBlock(SER_NETWORK, | CDataStream ssBlock(SER_NETWORK, | ||||
PROTOCOL_VERSION | RPCSerializationFlags()); | PROTOCOL_VERSION | RPCSerializationFlags()); | ||||
ssBlock << block; | ssBlock << block; | ||||
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | ||||
return strHex; | return strHex; | ||||
} | } | ||||
return blockToJSON(config, block, pblockindex); | return blockToJSON(config, block, pblockindex, verbosity >= 2); | ||||
} | } | ||||
struct CCoinsStats { | struct CCoinsStats { | ||||
int nHeight; | int nHeight; | ||||
uint256 hashBlock; | uint256 hashBlock; | ||||
uint64_t nTransactions; | uint64_t nTransactions; | ||||
uint64_t nTransactionOutputs; | uint64_t nTransactionOutputs; | ||||
uint64_t nBogoSize; | uint64_t nBogoSize; | ||||
▲ Show 20 Lines • Show All 884 Lines • ▼ Show 20 Lines | |||||
// clang-format off | // clang-format off | ||||
static const ContextFreeRPCCommand commands[] = { | static const ContextFreeRPCCommand commands[] = { | ||||
// category name actor (function) argNames | // category name actor (function) argNames | ||||
// ------------------- ------------------------ ---------------------- ---------- | // ------------------- ------------------------ ---------------------- ---------- | ||||
{ "blockchain", "getblockchaininfo", getblockchaininfo, {} }, | { "blockchain", "getblockchaininfo", getblockchaininfo, {} }, | ||||
{ "blockchain", "getchaintxstats", &getchaintxstats, {"nblocks", "blockhash"} }, | { "blockchain", "getchaintxstats", &getchaintxstats, {"nblocks", "blockhash"} }, | ||||
{ "blockchain", "getbestblockhash", getbestblockhash, {} }, | { "blockchain", "getbestblockhash", getbestblockhash, {} }, | ||||
{ "blockchain", "getblockcount", getblockcount, {} }, | { "blockchain", "getblockcount", getblockcount, {} }, | ||||
{ "blockchain", "getblock", getblock, {"blockhash","verbose"} }, | { "blockchain", "getblock", getblock, {"blockhash","verbosity|verbose"} }, | ||||
{ "blockchain", "getblockhash", getblockhash, {"height"} }, | { "blockchain", "getblockhash", getblockhash, {"height"} }, | ||||
{ "blockchain", "getblockheader", getblockheader, {"blockhash","verbose"} }, | { "blockchain", "getblockheader", getblockheader, {"blockhash","verbose"} }, | ||||
{ "blockchain", "getchaintips", getchaintips, {} }, | { "blockchain", "getchaintips", getchaintips, {} }, | ||||
{ "blockchain", "getdifficulty", getdifficulty, {} }, | { "blockchain", "getdifficulty", getdifficulty, {} }, | ||||
{ "blockchain", "getmempoolancestors", getmempoolancestors, {"txid","verbose"} }, | { "blockchain", "getmempoolancestors", getmempoolancestors, {"txid","verbose"} }, | ||||
{ "blockchain", "getmempooldescendants", getmempooldescendants, {"txid","verbose"} }, | { "blockchain", "getmempooldescendants", getmempooldescendants, {"txid","verbose"} }, | ||||
{ "blockchain", "getmempoolentry", getmempoolentry, {"txid"} }, | { "blockchain", "getmempoolentry", getmempoolentry, {"txid"} }, | ||||
{ "blockchain", "getmempoolinfo", getmempoolinfo, {} }, | { "blockchain", "getmempoolinfo", getmempoolinfo, {} }, | ||||
Show All 23 Lines |