Skip to content

Commit

Permalink
Add decodeblock rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
blondfrogs committed Jul 7, 2020
1 parent 627a6f0 commit ae65a29
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,36 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
return result;
}

UniValue decodeblockToJSON(const CBlock& block)
{
UniValue result(UniValue::VOBJ);
result.push_back(Pair("hash", block.GetHash().GetHex()));

result.push_back(Pair("strippedsize", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)));
result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION)));
result.push_back(Pair("weight", (int)::GetBlockWeight(block)));
result.push_back(Pair("height", (int)block.nHeight));
result.push_back(Pair("version", block.nVersion));
result.push_back(Pair("versionHex", strprintf("%08x", block.nVersion)));
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
UniValue txs(UniValue::VARR);
for(const auto& tx : block.vtx)
{
UniValue objTx(UniValue::VOBJ);
TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags());
txs.push_back(objTx);
}
result.push_back(Pair("tx", txs));
result.push_back(Pair("time", block.GetBlockTime()));
result.push_back(Pair("nonce", (uint64_t)block.nNonce));
result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
result.push_back(Pair("headerhash", block.GetKAWPOWHeaderHash().GetHex()));
result.push_back(Pair("mixhash", block.mix_hash.GetHex()));
result.push_back(Pair("nonce64", (uint64_t)block.nNonce64));

return result;
}

UniValue getblockcount(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
Expand Down Expand Up @@ -990,6 +1020,45 @@ UniValue getblock(const JSONRPCRequest& request)
return blockToJSON(block, pblockindex, verbosity >= 2);
}

UniValue decodeblock(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1)
throw std::runtime_error(
"decodeblock \"blockhex\"\n"
"\nArguments:\n"
"1. \"blockhex\" (string, required) The block hex\n"
"\nResult:\n"
"{\n"
" \"hash\" : \"hash\", (string) the block hash (same as provided)\n"
" \"size\" : n, (numeric) The block size\n"
" \"strippedsize\" : n, (numeric) The block size excluding witness data\n"
" \"weight\" : n (numeric) The block weight as defined in BIP 141\n"
" \"height\" : n, (numeric) The block height or index\n"
" \"version\" : n, (numeric) The block version\n"
" \"versionHex\" : \"00000000\", (string) The block version formatted in hexadecimal\n"
" \"merkleroot\" : \"xxxx\", (string) The merkle root\n"
" \"tx\" : [ (array of string) The transaction ids\n"
" \"transactionid\" (string) The transaction id\n"
" ,...\n"
" ],\n"
" \"time\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
" \"nonce\" : n, (numeric) The nonce\n"
" \"bits\" : \"1d00ffff\", (string) The bits\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("decodeblock", "\"xxxx\"")
+ HelpExampleRpc("decodeblock", "\"xxxx\"")
);

std::string strHex = request.params[0].get_str();
CBlock block;
DecodeHexBlk(block, strHex);

return decodeblockToJSON(block);
}



struct CCoinsStats
{
int nHeight;
Expand Down Expand Up @@ -1833,6 +1902,7 @@ static const CRPCCommand commands[] =
{ "blockchain", "getbestblockhash", &getbestblockhash, {} },
{ "blockchain", "getblockcount", &getblockcount, {} },
{ "blockchain", "getblock", &getblock, {"blockhash","verbosity|verbose"} },
{ "blockchain", "decodeblock", &decodeblock, {"blockhex",} },
{ "blockchain", "getblockdeltas", &getblockdeltas, {} },
{ "blockchain", "getblockhashes", &getblockhashes, {} },
{ "blockchain", "getblockhash", &getblockhash, {"height"} },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);

/** Block description to JSON */
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
UniValue decodeblockToJSON(const CBlock& block);

/** Mempool information to JSON */
UniValue mempoolInfoToJSON();
Expand Down

0 comments on commit ae65a29

Please sign in to comment.