diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 355d479..92e9793 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -203,28 +203,10 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex result.pushKV("chainwork", blockindex->nChainWork.GetHex()); result.pushKV("nTx", (uint64_t)blockindex->nTx); - if (blockindex->pprev) - result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()); - if (pnext) - result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex()); - return result; -} - -UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails) -{ - UniValue result = blockheaderToJSON(tip, blockindex); - - result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); - result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION)); - result.pushKV("weight", (int)::GetBlockWeight(block)); - UniValue txs(UniValue::VARR); - result.pushKV("height", blockindex->nHeight); - result.pushKV("version", block.nVersion); - result.pushKV("versionHex", strprintf("%08x", block.nVersion)); - const Consensus::Params& consensusParams = Params().GetConsensus(); + const CBlockHeader block = blockindex->GetBlockHeader(); const int algo = - block.GetHash() == consensusParams.hashGenesisBlock + blockindex->GetBlockHash() == consensusParams.hashGenesisBlock ? ALGO_SHA256D : block.GetAlgo(); @@ -232,7 +214,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn if (algo == ALGO_SHA256D) { result.pushKV("pow_algo", "sha256d"); - result.pushKV("pow_hash", block.GetHash().GetHex()); + result.pushKV("pow_hash", blockindex->GetBlockHash().GetHex()); } else if (algo == ALGO_RANDOMX) { result.pushKV("pow_algo", "randomx"); @@ -255,6 +237,25 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn result.pushKV("pow_hash", UniValue()); } + if (blockindex->pprev) + result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()); + if (pnext) + result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex()); + return result; +} + +UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails) +{ + UniValue result = blockheaderToJSON(tip, blockindex); + + result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); + result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION)); + result.pushKV("weight", (int)::GetBlockWeight(block)); + UniValue txs(UniValue::VARR); + result.pushKV("height", blockindex->nHeight); + result.pushKV("version", block.nVersion); + result.pushKV("versionHex", strprintf("%08x", block.nVersion)); + result.pushKV("merkleroot", block.hashMerkleRoot.GetHex()); if (txDetails) { CBlockUndo blockUndo; @@ -923,6 +924,9 @@ static RPCHelpMan getblockheader() {RPCResult::Type::NUM, "nonce", "The nonce"}, {RPCResult::Type::STR_HEX, "bits", "The bits"}, {RPCResult::Type::NUM, "difficulty", "The difficulty"}, + {RPCResult::Type::NUM, "pow_algo_id", "The proof-of-work algorithm identifier"}, + {RPCResult::Type::STR, "pow_algo", "The proof-of-work algorithm name"}, + {RPCResult::Type::STR_HEX, "pow_hash", "The proof-of-work hash"}, {RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the current chain"}, {RPCResult::Type::NUM, "nTx", "The number of transactions in the block"}, {RPCResult::Type::STR_HEX, "previousblockhash", /* optional */ true, "The hash of the previous block (if available)"}, @@ -1032,6 +1036,9 @@ static RPCHelpMan getblock() {RPCResult::Type::NUM, "nonce", "The nonce"}, {RPCResult::Type::STR_HEX, "bits", "The bits"}, {RPCResult::Type::NUM, "difficulty", "The difficulty"}, + {RPCResult::Type::NUM, "pow_algo_id", "The proof-of-work algorithm identifier"}, + {RPCResult::Type::STR, "pow_algo", "The proof-of-work algorithm name"}, + {RPCResult::Type::STR_HEX, "pow_hash", "The proof-of-work hash"}, {RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the chain up to this block (in hex)"}, {RPCResult::Type::NUM, "nTx", "The number of transactions in the block"}, {RPCResult::Type::STR_HEX, "previousblockhash", /* optional */ true, "The hash of the previous block (if available)"}, diff --git a/test/functional/mining_randomx_gbt.py b/test/functional/mining_randomx_gbt.py index d24166a..00de44d 100755 --- a/test/functional/mining_randomx_gbt.py +++ b/test/functional/mining_randomx_gbt.py @@ -126,6 +126,12 @@ class RandomXGetBlockTemplateTest(DigiByteTestFramework): assert mined_block["pow_hash"] is not None assert mined_block["pow_hash"] != mined_block["hash"] + self.log.info("getblockheader exposes the same RandomX PoW metadata") + mined_header = node.getblockheader(randomx_blocks[0]) + assert_equal(mined_header["pow_algo_id"], 1) + assert_equal(mined_header["pow_algo"], "randomx") + assert_equal(mined_header["pow_hash"], mined_block["pow_hash"]) + self.log.info("RandomX mining statistics remain separated from SHA256D") mining_info = node.getmininginfo() assert_equal(set(mining_info["difficulties"]), {"sha256d", "randomx"})