From 52ff69dd578cf494c753fe9f7d054730271e8463 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 23 Mar 2015 20:36:16 +0300 Subject: [PATCH 1/2] add getblockheader rpc command --- src/rpcblockchain.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++ src/rpcclient.cpp | 1 + src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + 4 files changed, 70 insertions(+) diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index a80a0a213666..b36a84f347cf 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -80,6 +80,20 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex) } +Object blockHeaderToJSON(const CBlock& block, const CBlockIndex* blockindex) +{ + Object result; + result.push_back(Pair("version", block.nVersion)); + if (blockindex->pprev) + result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); + result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); + result.push_back(Pair("time", block.GetBlockTime())); + result.push_back(Pair("bits", HexBits(block.nBits))); + result.push_back(Pair("nonce", (uint64_t)block.nNonce)); + return result; +} + + Value getblockcount(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) @@ -292,6 +306,59 @@ Value getblock(const Array& params, bool fHelp) return blockToJSON(block, pblockindex); } +Value getblockheader(const Array& params, bool fHelp) +{ + if (fHelp || params.size() < 1 || params.size() > 2) + throw runtime_error( + "getblockheader \"hash\" ( verbose )\n" + "\nIf verbose is false, returns a string that is serialized, hex-encoded data for block 'hash' header.\n" + "If verbose is true, returns an Object with information about block header.\n" + "\nArguments:\n" + "1. \"hash\" (string, required) The block hash\n" + "2. verbose (boolean, optional, default=true) true for a json object, false for the hex encoded data\n" + "\nResult (for verbose = true):\n" + "{\n" + " \"version\" : n, (numeric) The block version\n" + " \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n" + " \"merkleroot\" : \"xxxx\", (string) The merkle root\n" + " \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n" + " \"bits\" : \"1d00ffff\", (string) The bits\n" + " \"nonce\" : n, (numeric) The nonce\n" + "}\n" + "\nResult (for verbose=false):\n" + "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash' header.\n" + "\nExamples:\n" + + HelpExampleCli("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") + + HelpExampleRpc("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") + ); + + std::string strHash = params[0].get_str(); + uint256 hash(strHash); + + bool fVerbose = true; + if (params.size() > 1) + fVerbose = params[1].get_bool(); + + if (mapBlockIndex.count(hash) == 0) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); + + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[hash]; + + if(!ReadBlockFromDisk(block, pblockindex)) + throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); + + if (!fVerbose) + { + CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); + ssBlock << block.GetBlockHeader(); + std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); + return strHex; + } + + return blockHeaderToJSON(block, pblockindex); +} + Value gettxoutsetinfo(const Array& params, bool fHelp) { if (fHelp || params.size() != 0) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index c8e232cce20d..50be60586052 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -163,6 +163,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 1) ConvertTo(params[1]); if (strMethod == "listunspent" && n > 2) ConvertTo(params[2]); if (strMethod == "getblock" && n > 1) ConvertTo(params[1]); + if (strMethod == "getblockheader" && n > 1) ConvertTo(params[1]); if (strMethod == "getrawtransaction" && n > 1) ConvertTo(params[1]); if (strMethod == "createrawtransaction" && n > 0) ConvertTo(params[0]); if (strMethod == "createrawtransaction" && n > 1) ConvertTo(params[1]); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 16787961f96c..619d0d4e17b6 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -243,6 +243,7 @@ static const CRPCCommand vRPCCommands[] = { "getbestblockhash", &getbestblockhash, true, false, false }, { "getblockcount", &getblockcount, true, false, false }, { "getblock", &getblock, false, false, false }, + { "getblockheader", &getblockheader, false, false, false }, { "getblockhash", &getblockhash, false, false, false }, { "getdifficulty", &getdifficulty, true, false, false }, { "getrawmempool", &getrawmempool, true, false, false }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 34ae37e0d9a1..77284bcb34cf 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -185,6 +185,7 @@ extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp) extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value getblockheader(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value verifychain(const json_spirit::Array& params, bool fHelp); From ef98ad50390d3dbcc7da2ef8fac5861b399c3365 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 23 Mar 2015 20:57:03 +0300 Subject: [PATCH 2/2] example block hash btc->dash --- src/rpcblockchain.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index b36a84f347cf..224daa493ca6 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -275,8 +275,8 @@ Value getblock(const Array& params, bool fHelp) "\nResult (for verbose=false):\n" "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n" "\nExamples:\n" - + HelpExampleCli("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") - + HelpExampleRpc("getblock", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") + + HelpExampleCli("getblock", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") + + HelpExampleRpc("getblock", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") ); std::string strHash = params[0].get_str(); @@ -328,8 +328,8 @@ Value getblockheader(const Array& params, bool fHelp) "\nResult (for verbose=false):\n" "\"data\" (string) A string that is serialized, hex-encoded data for block 'hash' header.\n" "\nExamples:\n" - + HelpExampleCli("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") - + HelpExampleRpc("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"") + + HelpExampleCli("getblockheader", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") + + HelpExampleRpc("getblockheader", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") ); std::string strHash = params[0].get_str();