Skip to content

Commit 726e286

Browse files
committed
Merge pull request #6247
076badb Add getblockheader RPC call (Peter Todd)
2 parents 60abba1 + 076badb commit 726e286

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/rpcblockchain.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ double GetDifficulty(const CBlockIndex* blockindex)
5151
return dDiff;
5252
}
5353

54+
UniValue blockheaderToJSON(const CBlockIndex* blockindex)
55+
{
56+
UniValue result(UniValue::VOBJ);
57+
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex()));
58+
int confirmations = -1;
59+
// Only report confirmations if the block is on the main chain
60+
if (chainActive.Contains(blockindex))
61+
confirmations = chainActive.Height() - blockindex->nHeight + 1;
62+
result.push_back(Pair("confirmations", confirmations));
63+
result.push_back(Pair("height", blockindex->nHeight));
64+
result.push_back(Pair("version", blockindex->nVersion));
65+
result.push_back(Pair("merkleroot", blockindex->hashMerkleRoot.GetHex()));
66+
result.push_back(Pair("time", (int64_t)blockindex->nTime));
67+
result.push_back(Pair("nonce", (uint64_t)blockindex->nNonce));
68+
result.push_back(Pair("bits", strprintf("%08x", blockindex->nBits)));
69+
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
70+
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
71+
72+
if (blockindex->pprev)
73+
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
74+
CBlockIndex *pnext = chainActive.Next(blockindex);
75+
if (pnext)
76+
result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex()));
77+
return result;
78+
}
79+
5480

5581
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
5682
{
@@ -255,6 +281,62 @@ UniValue getblockhash(const UniValue& params, bool fHelp)
255281
return pblockindex->GetBlockHash().GetHex();
256282
}
257283

284+
UniValue getblockheader(const UniValue& params, bool fHelp)
285+
{
286+
if (fHelp || params.size() < 1 || params.size() > 2)
287+
throw runtime_error(
288+
"getblockheader \"hash\" ( verbose )\n"
289+
"\nIf verbose is false, returns a string that is serialized, hex-encoded data for blockheader 'hash'.\n"
290+
"If verbose is true, returns an Object with information about blockheader <hash>.\n"
291+
"\nArguments:\n"
292+
"1. \"hash\" (string, required) The block hash\n"
293+
"2. verbose (boolean, optional, default=true) true for a json object, false for the hex encoded data\n"
294+
"\nResult (for verbose = true):\n"
295+
"{\n"
296+
" \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
297+
" \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n"
298+
" \"height\" : n, (numeric) The block height or index\n"
299+
" \"version\" : n, (numeric) The block version\n"
300+
" \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
301+
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
302+
" \"nonce\" : n, (numeric) The nonce\n"
303+
" \"bits\" : \"1d00ffff\", (string) The bits\n"
304+
" \"difficulty\" : x.xxx, (numeric) The difficulty\n"
305+
" \"previousblockhash\" : \"hash\", (string) The hash of the previous block\n"
306+
" \"nextblockhash\" : \"hash\" (string) The hash of the next block\n"
307+
"}\n"
308+
"\nResult (for verbose=false):\n"
309+
"\"data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n"
310+
"\nExamples:\n"
311+
+ HelpExampleCli("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
312+
+ HelpExampleRpc("getblockheader", "\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09\"")
313+
);
314+
315+
LOCK(cs_main);
316+
317+
std::string strHash = params[0].get_str();
318+
uint256 hash(uint256S(strHash));
319+
320+
bool fVerbose = true;
321+
if (params.size() > 1)
322+
fVerbose = params[1].get_bool();
323+
324+
if (mapBlockIndex.count(hash) == 0)
325+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
326+
327+
CBlockIndex* pblockindex = mapBlockIndex[hash];
328+
329+
if (!fVerbose)
330+
{
331+
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
332+
ssBlock << pblockindex->GetBlockHeader();
333+
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
334+
return strHex;
335+
}
336+
337+
return blockheaderToJSON(pblockindex);
338+
}
339+
258340
UniValue getblock(const UniValue& params, bool fHelp)
259341
{
260342
if (fHelp || params.size() < 1 || params.size() > 2)

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
7171
{ "listunspent", 1 },
7272
{ "listunspent", 2 },
7373
{ "getblock", 1 },
74+
{ "getblockheader", 1 },
7475
{ "gettransaction", 1 },
7576
{ "getrawtransaction", 1 },
7677
{ "createrawtransaction", 0 },

src/rpcserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ static const CRPCCommand vRPCCommands[] =
289289
{ "blockchain", "getblockcount", &getblockcount, true },
290290
{ "blockchain", "getblock", &getblock, true },
291291
{ "blockchain", "getblockhash", &getblockhash, true },
292+
{ "blockchain", "getblockheader", &getblockheader, true },
292293
{ "blockchain", "getchaintips", &getchaintips, true },
293294
{ "blockchain", "getdifficulty", &getdifficulty, true },
294295
{ "blockchain", "getmempoolinfo", &getmempoolinfo, true },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ extern UniValue settxfee(const UniValue& params, bool fHelp);
234234
extern UniValue getmempoolinfo(const UniValue& params, bool fHelp);
235235
extern UniValue getrawmempool(const UniValue& params, bool fHelp);
236236
extern UniValue getblockhash(const UniValue& params, bool fHelp);
237+
extern UniValue getblockheader(const UniValue& params, bool fHelp);
237238
extern UniValue getblock(const UniValue& params, bool fHelp);
238239
extern UniValue gettxoutsetinfo(const UniValue& params, bool fHelp);
239240
extern UniValue gettxout(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)