Expose RandomX seed in getblocktemplate

This commit is contained in:
Capri 2026-09-16 22:56:10 +00:00
commit 2c7dfb6a75
Signed by: VextaProject
SSH key fingerprint: SHA256:739BZv8LvIDY2tR7lXLy/IQ6MaGTQtYcG9GNvCjmpVM
2 changed files with 45 additions and 0 deletions

View file

@ -863,6 +863,10 @@ static RPCHelpMan getblocktemplate()
{RPCResult::Type::NUM_TIME, "curtime", "current timestamp in " + UNIX_EPOCH_TIME}, {RPCResult::Type::NUM_TIME, "curtime", "current timestamp in " + UNIX_EPOCH_TIME},
{RPCResult::Type::STR, "bits", "compressed target of next block"}, {RPCResult::Type::STR, "bits", "compressed target of next block"},
{RPCResult::Type::NUM, "height", "The height of the next block"}, {RPCResult::Type::NUM, "height", "The height of the next block"},
{RPCResult::Type::NUM, "pow_algo_id", "Mining algorithm id: 0 for SHA256D, 1 for RandomX"},
{RPCResult::Type::STR, "pow_algo", "Mining algorithm name: sha256d or randomx"},
{RPCResult::Type::NUM, "randomx_seed_height", /* optional */ true, "Height of the block used as the RandomX seed source"},
{RPCResult::Type::STR_HEX, "randomx_seed", /* optional */ true, "RandomX seed key in raw uint256 byte order"},
{RPCResult::Type::STR, "default_witness_commitment", /* optional */ true, "a valid witness commitment for the unmodified block template"}, {RPCResult::Type::STR, "default_witness_commitment", /* optional */ true, "a valid witness commitment for the unmodified block template"},
}}, }},
}, },
@ -1212,6 +1216,31 @@ static RPCHelpMan getblocktemplate()
result.pushKV("bits", strprintf("%08x", pblock->nBits)); result.pushKV("bits", strprintf("%08x", pblock->nBits));
result.pushKV("height", (int64_t)(pindexPrev->nHeight+1)); result.pushKV("height", (int64_t)(pindexPrev->nHeight+1));
result.pushKV("pow_algo_id", algo);
result.pushKV("pow_algo", GetMiningAlgoName(algo));
if (algo == ALGO_RANDOMX) {
const int blockHeight = pindexPrev->nHeight + 1;
const int seedHeight =
GetRandomXSeedHeight(blockHeight, consensusParams);
uint256 randomxSeed;
if (!GetRandomXSeed(
pindexPrev,
blockHeight,
consensusParams,
randomxSeed)) {
throw JSONRPCError(
RPC_INTERNAL_ERROR,
"Failed to resolve RandomX seed");
}
result.pushKV("randomx_seed_height", seedHeight);
result.pushKV(
"randomx_seed",
HexStr(MakeUCharSpan(randomxSeed)));
}
if (consensusParams.signet_blocks) { if (consensusParams.signet_blocks) {
result.pushKV("signet_challenge", HexStr(consensusParams.signet_challenge)); result.pushKV("signet_challenge", HexStr(consensusParams.signet_challenge));
} }

View file

@ -56,6 +56,10 @@ class RandomXGetBlockTemplateTest(DigiByteTestFramework):
sha_template["version"] & BLOCK_VERSION_ALGO, sha_template["version"] & BLOCK_VERSION_ALGO,
BLOCK_VERSION_SHA256D, BLOCK_VERSION_SHA256D,
) )
assert_equal(sha_template["pow_algo_id"], 0)
assert_equal(sha_template["pow_algo"], "sha256d")
assert "randomx_seed_height" not in sha_template
assert "randomx_seed" not in sha_template
self.log.info("Mining RPC reports only SHA256D before activation") self.log.info("Mining RPC reports only SHA256D before activation")
mining_info = node.getmininginfo() mining_info = node.getmininginfo()
@ -130,6 +134,14 @@ class RandomXGetBlockTemplateTest(DigiByteTestFramework):
randomx_template["version"] & BLOCK_VERSION_ALGO, randomx_template["version"] & BLOCK_VERSION_ALGO,
BLOCK_VERSION_RANDOMX, BLOCK_VERSION_RANDOMX,
) )
assert_equal(randomx_template["pow_algo_id"], 1)
assert_equal(randomx_template["pow_algo"], "randomx")
assert_equal(randomx_template["randomx_seed_height"], 0)
genesis_hash = node.getblockhash(0)
expected_randomx_seed = bytes.fromhex(genesis_hash)[::-1].hex()
assert_equal(randomx_template["randomx_seed"], expected_randomx_seed)
assert_equal(len(randomx_template["randomx_seed"]), 64)
self.log.info("Default GBT remains SHA256D after RandomX activation") self.log.info("Default GBT remains SHA256D after RandomX activation")
sha_template = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS) sha_template = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
@ -138,6 +150,10 @@ class RandomXGetBlockTemplateTest(DigiByteTestFramework):
sha_template["version"] & BLOCK_VERSION_ALGO, sha_template["version"] & BLOCK_VERSION_ALGO,
BLOCK_VERSION_SHA256D, BLOCK_VERSION_SHA256D,
) )
assert_equal(sha_template["pow_algo_id"], 0)
assert_equal(sha_template["pow_algo"], "sha256d")
assert "randomx_seed_height" not in sha_template
assert "randomx_seed" not in sha_template
self.log.info("Mining RPC exposes SHA256D and RandomX at activation") self.log.info("Mining RPC exposes SHA256D and RandomX at activation")
mining_info = node.getmininginfo() mining_info = node.getmininginfo()