Merge branch 'fix/reorg-shutdown-dos' into dev
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);
|
||||
}
|
||||
@@ -1713,6 +1713,22 @@ int32_t hush_checkPOW(int32_t slowflag,CBlock *pblock,int32_t height)
|
||||
height = pprev->GetHeight() + 1;
|
||||
if ( height == 0 )
|
||||
return(0);
|
||||
// The CheckRandomXSolution() above ran with height==0, where RandomXValidationRequired(0)
|
||||
// is false, so RandomX was NOT verified. Re-verify now that the real height is recovered
|
||||
// from hashPrevBlock. Without this, a peer block entering via ProcessNewBlock(0,0,...) (the
|
||||
// net BLOCK handler, main.cpp) becomes a setBlockIndexCandidate with its PoW never checked
|
||||
// at its real height: both CheckBlock() calls on that accept path pass fCheckPOW=0, so
|
||||
// CheckBlockHeader's RandomX check never runs there -- this hush_checkPOW(0,...) is the ONLY
|
||||
// real-height PoW gate before candidacy. A forged-RandomX fork could then trip the reorg
|
||||
// guard before the connect-time check rejects it. This is a no-op wherever the first call
|
||||
// was already a no-op: RandomXValidationRequired() gates on algo, the activation height
|
||||
// (regtest heights are far below it), HUSH_LOADINGBLOCKS (IBD/reindex), and the last
|
||||
// in-index checkpoint, and the thread-local fSkipRandomXValidation still skips the miner self-check.
|
||||
if ( !CheckRandomXSolution(pblock, height) )
|
||||
{
|
||||
fprintf(stderr,"hush_checkPOW slowflag.%d ht.%d CheckRandomXSolution failed (post height-recovery)\n",slowflag,height);
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
|
||||
if ( (SMART_CHAIN_SYMBOL[0] != 0) && bhash > bnTarget ) {
|
||||
|
||||
+43
-14
@@ -4315,23 +4315,52 @@ static bool ActivateBestChainStep(bool fSkipdpow, CValidationState &state, CBloc
|
||||
assert(MAX_REORG_LENGTH > 0);//, "We must be able to reorg some distance");
|
||||
if ( reorgLength > MAX_REORG_LENGTH)
|
||||
{
|
||||
// A peer-delivered fork that would roll back more than MAX_REORG_LENGTH
|
||||
// (-maxreorg, default 99) blocks. This used to StartShutdown() the whole
|
||||
// process, which is a per-reachability remote DoS: the attacker's fork is
|
||||
// re-pulled from addrman after every restart and re-kills the node. Instead
|
||||
// refuse the fork, poison the attacker branch so it leaves
|
||||
// setBlockIndexCandidates and is never re-selected or re-pulled, stay up on
|
||||
// the current chain, and penalize the delivering peer (DoS(100); the default
|
||||
// banscore 101 bans on the second such trip). This mirrors the
|
||||
// past-notarized-height branch above, which already refuses a fork with
|
||||
// InvalidateBlock + DoS(100) and keeps running.
|
||||
//
|
||||
// Invalidate the BASE of the attacker branch (the first block above the fork
|
||||
// point), not just its tip: FindMostWorkChain's fFailedChain sweep then
|
||||
// erases the whole branch from the candidate set in one pass, and
|
||||
// AcceptBlockHeader rejects any future extension (pprev is BLOCK_FAILED_CHILD).
|
||||
// The base is strictly on the fork side (height forkHeight+1), so it is never
|
||||
// in chainActive and no honest block is disconnected or poisoned.
|
||||
CBlockIndex *pindexBadBase = (pindexFork != 0) ? pindexMostWork->GetAncestor(pindexFork->GetHeight() + 1) : pindexMostWork;
|
||||
if (pindexBadBase == 0)
|
||||
pindexBadBase = pindexMostWork;
|
||||
// Because the refusal is PERSISTED (BLOCK_FAILED on the base), raising
|
||||
// -maxreorg alone does NOT make the node follow this fork after a trip --
|
||||
// recovery for a genuine deep reorg is an explicit `reconsiderblock <base>`
|
||||
// (optionally with a raised -maxreorg so it isn't re-refused). Say so, and
|
||||
// surface it in getinfo "errors" so a refused node is never silently stranded.
|
||||
auto msg = strprintf(_(
|
||||
"A block chain reorganization has been detected that would roll back %d blocks!!! "
|
||||
"This is larger than the maximum of %d blocks, and so the node is shutting down for your safety."
|
||||
), reorgLength, MAX_REORG_LENGTH) + "\n\n" +
|
||||
"A block chain reorganization that would roll back %d blocks (more than the maximum of %d) "
|
||||
"was refused; staying on the current chain."
|
||||
), reorgLength, MAX_REORG_LENGTH) + "\n" +
|
||||
_("Reorganization details") + ":\n" +
|
||||
"- " + strprintf(_("Current tip: %s, height %d, work %s\n"),
|
||||
pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" +
|
||||
"- " + strprintf(_("New tip: %s, height %d, work %s\n"),
|
||||
"- " + strprintf(_("Current tip: %s, height %d, work %s"),
|
||||
pindexOldTip->phashBlock->GetHex(), pindexOldTip->GetHeight(), pindexOldTip->chainPower.chainWork.GetHex()) + "\n" +
|
||||
"- " + strprintf(_("Rejected tip: %s, height %d, work %s"),
|
||||
pindexMostWork->phashBlock->GetHex(), pindexMostWork->GetHeight(), pindexMostWork->chainPower.chainWork.GetHex()) + "\n" +
|
||||
"- " + strprintf(_("Fork point: %s %s, height %d"),
|
||||
SMART_CHAIN_SYMBOL,pindexFork->phashBlock->GetHex(), pindexFork->GetHeight()) + "\n\n" +
|
||||
_("Please help me, wise human!");
|
||||
LogPrintf("*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg,reorgLength+10);
|
||||
fprintf(stderr,"*** %s\nif you launch with -maxreorg=%d it might be able to resolve this automatically", msg.c_str(),reorgLength+10);
|
||||
uiInterface.ThreadSafeMessageBox(msg, "", CClientUIInterface::MSG_ERROR);
|
||||
StartShutdown();
|
||||
return false;
|
||||
"- " + strprintf(_("Fork point: %s %s, height %d"),
|
||||
SMART_CHAIN_SYMBOL, pindexFork ? pindexFork->phashBlock->GetHex() : "(none)", pindexFork ? pindexFork->GetHeight() : -1) + "\n" +
|
||||
strprintf(_("If this is a legitimate deep reorg, recover with: reconsiderblock %s (raise -maxreorg first if needed)"),
|
||||
pindexBadBase->phashBlock->GetHex());
|
||||
LogPrintf("*** %s\n", msg);
|
||||
fprintf(stderr,"*** %s\n", msg.c_str());
|
||||
strMiscWarning = msg; // surfaces in getinfo/getnetworkinfo (GetWarnings), not fatal
|
||||
|
||||
CValidationState tmpstate;
|
||||
InvalidateBlock(tmpstate, pindexBadBase);
|
||||
return state.DoS(100, error("ActivateBestChainStep(): reorg of %d blocks exceeds maximum of %d; refusing fork and staying on current chain",
|
||||
reorgLength, MAX_REORG_LENGTH), REJECT_INVALID, "oversized-reorg");
|
||||
}
|
||||
|
||||
// Disconnect active blocks which are no longer in the best chain.
|
||||
|
||||
Reference in New Issue
Block a user