@@ -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
5581UniValue 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+ " \n If 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+ " \n Arguments:\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+ " \n Result (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+ " \n Result (for verbose=false):\n "
309+ " \" data\" (string) A string that is serialized, hex-encoded data for block 'hash'.\n "
310+ " \n Examples:\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+
258340UniValue getblock (const UniValue& params, bool fHelp )
259341{
260342 if (fHelp || params.size () < 1 || params.size () > 2 )
0 commit comments