gtest: regression guard for the hush_checkPOW height-0 RandomX re-check
A forged-RandomX block reaches hush_checkPOW(0,pblock,0) over the P2P accept path; the first CheckRandomXSolution runs at height 0 (skipped), the height is recovered from hashPrevBlock, and fix (a) re-runs CheckRandomXSolution at the real height. The test drives the same forged block through hush_checkPOW twice: with the parent unknown the height stays 0 and the block is not rejected here (the pre-fix outcome), and with the parent known at height 9 the recovered height makes the fix reject it. The A/B contrast fails if the re-check is reverted. Also asserts CheckRandomXSolution is skipped at height 0 and rejects the forgery at the real height. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ hush_gtest_SOURCES = \
|
||||
gtest/main.cpp \
|
||||
gtest/utils.cpp \
|
||||
gtest/test_randomx_preverify.cpp \
|
||||
gtest/test_randomx_heightrecover.cpp \
|
||||
gtest/test_hdtransparent.cpp \
|
||||
gtest/test_mnemonic_compat.cpp \
|
||||
gtest/test_stratum_jobid.cpp \
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright (c) 2024-2026 The DragonX developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
//
|
||||
// Regression guard for the hush_checkPOW height-0 RandomX fix. A block arriving over P2P reaches
|
||||
// hush_checkPOW with height==0; the first CheckRandomXSolution runs at height 0 (where
|
||||
// RandomXValidationRequired is false, so it is skipped), the real height is then recovered from
|
||||
// hashPrevBlock, and the fix re-runs CheckRandomXSolution at that height. Without the fix a block
|
||||
// with a forged RandomX solution would be accepted on that path. We prove: (1) CheckRandomXSolution
|
||||
// is skipped at height 0 but rejects the forged solution at the real height; (2) hush_checkPOW lets
|
||||
// the forged block through only while the parent is unknown (height cannot be recovered), and
|
||||
// rejects it once the parent is known and the height recovers -- which is exactly what the fix does.
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "arith_uint256.h"
|
||||
#include "chain.h"
|
||||
#include "chainparams.h"
|
||||
#include "chainparamsbase.h"
|
||||
#include "pow.h"
|
||||
#include "primitives/block.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "RandomX/src/randomx.h"
|
||||
#include "hush_defs.h"
|
||||
#include "main.h"
|
||||
#include "sync.h"
|
||||
#include "uint256.h"
|
||||
|
||||
extern int32_t HUSH_LOADINGBLOCKS;
|
||||
extern bool fCheckpointsEnabled;
|
||||
int32_t hush_checkPOW(int32_t slowflag, CBlock *pblock, int32_t height);
|
||||
|
||||
TEST(RandomXHeightRecover, ForgedSolutionRejectedOnceHeightIsRecovered)
|
||||
{
|
||||
// hush_checkPOW calls Params(); give it a selected chain. Done before the ASSETCHAINS_* globals
|
||||
// below so params init cannot clobber them. RandomX is enforced via ASSETCHAINS_RANDOMX_VALIDATION,
|
||||
// not the chain's activation height, so MAIN's high activation height is irrelevant here.
|
||||
SelectParams(CBaseChainParams::MAIN);
|
||||
|
||||
// Save the globals this test flips.
|
||||
uint32_t sAlgo = ASSETCHAINS_ALGO, sRx = ASSETCHAINS_RANDOMX;
|
||||
int32_t sVal = ASSETCHAINS_RANDOMX_VALIDATION, sLoad = HUSH_LOADINGBLOCKS;
|
||||
bool sCkpt = fCheckpointsEnabled;
|
||||
char sSym[sizeof(SMART_CHAIN_SYMBOL)];
|
||||
std::memcpy(sSym, SMART_CHAIN_SYMBOL, sizeof sSym);
|
||||
|
||||
ASSETCHAINS_RANDOMX = 2; // a distinct nonzero algo id
|
||||
ASSETCHAINS_ALGO = ASSETCHAINS_RANDOMX;
|
||||
ASSETCHAINS_RANDOMX_VALIDATION = 1; // enforce from height 1
|
||||
HUSH_LOADINGBLOCKS = 0; // not initial-load (else RandomX skipped)
|
||||
fCheckpointsEnabled = false; // avoid the below-checkpoint skip
|
||||
std::strcpy(SMART_CHAIN_SYMBOL, "DRGXTEST"); // non-empty: not the default regtest chain
|
||||
|
||||
// A block with a FORGED RandomX solution (all zero bytes -- overwhelmingly not the real hash).
|
||||
CBlock block;
|
||||
block.nVersion = 4;
|
||||
block.hashPrevBlock = uint256S("0x0000000000000000000000000000000000000000000000000000000000000011");
|
||||
block.hashMerkleRoot = uint256S("0x0000000000000000000000000000000000000000000000000000000000000022");
|
||||
block.hashFinalSaplingRoot = uint256S("0x0000000000000000000000000000000000000000000000000000000000000033");
|
||||
block.nTime = 1700000000;
|
||||
block.nBits = 0x200f0f0f;
|
||||
block.nNonce = uint256S("0x0000000000000000000000000000000000000000000000000000000000000001");
|
||||
block.nSolution.assign(RANDOMX_HASH_SIZE, 0);
|
||||
|
||||
// hush_block2pubkey33 reads vtx[0]; give the block a minimal coinbase so it is never a null deref.
|
||||
CMutableTransaction cb;
|
||||
cb.vin.resize(1);
|
||||
cb.vin[0].prevout.SetNull();
|
||||
cb.vout.resize(1);
|
||||
block.vtx.push_back(CTransaction(cb));
|
||||
|
||||
// Grind the nonce so the block's SHA target check passes. Then the ONLY thing that can reject it
|
||||
// in hush_checkPOW at the recovered height is the RandomX re-check, so the test isolates the fix
|
||||
// (without the fix the block would fall through the target check and be accepted).
|
||||
arith_uint256 bnTarget;
|
||||
bool neg, over;
|
||||
bnTarget.SetCompact(block.nBits, &neg, &over);
|
||||
for (uint32_t n = 1; n < 100000; n++) {
|
||||
block.nNonce = ArithToUint256(arith_uint256(n));
|
||||
if (UintToArith256(block.GetHash()) <= bnTarget) break;
|
||||
}
|
||||
ASSERT_LE(UintToArith256(block.GetHash()), bnTarget) << "could not grind a passing SHA target";
|
||||
|
||||
// (1) The primitive the fix relies on: skipped at height 0, rejects the forgery at the real height.
|
||||
EXPECT_TRUE(CheckRandomXSolution(&block, 0)) << "RandomX must be skipped at height 0";
|
||||
EXPECT_FALSE(CheckRandomXSolution(&block, 10)) << "a forged solution must be rejected at its real height";
|
||||
|
||||
// (2a) Parent unknown: hush_checkPOW cannot recover the height, so it stays 0 and the block is NOT
|
||||
// rejected here -- the pre-fix behavior for an unrecoverable height.
|
||||
EXPECT_GE(hush_checkPOW(0, &block, 0), 0)
|
||||
<< "with no recoverable height the forged block is not rejected on this path";
|
||||
|
||||
// (2b) Parent known at height 9: the height recovers to 10 and the fix re-checks RandomX -> rejected.
|
||||
CBlockIndex prev;
|
||||
prev.SetHeight(9);
|
||||
uint256 prevHash = block.hashPrevBlock;
|
||||
prev.phashBlock = &prevHash;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
mapBlockIndex[prevHash] = &prev;
|
||||
}
|
||||
EXPECT_LT(hush_checkPOW(0, &block, 0), 0)
|
||||
<< "once the height recovers, the fix must reject the forged RandomX solution";
|
||||
{
|
||||
LOCK(cs_main);
|
||||
mapBlockIndex.erase(prevHash);
|
||||
}
|
||||
|
||||
// Restore.
|
||||
ASSETCHAINS_ALGO = sAlgo; ASSETCHAINS_RANDOMX = sRx;
|
||||
ASSETCHAINS_RANDOMX_VALIDATION = sVal; HUSH_LOADINGBLOCKS = sLoad;
|
||||
fCheckpointsEnabled = sCkpt;
|
||||
std::memcpy(SMART_CHAIN_SYMBOL, sSym, sizeof sSym);
|
||||
}
|
||||
Reference in New Issue
Block a user