From 661678dd848277be014f990edc8428c243b59ed9 Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 30 Oct 2026 22:27:41 -0500 Subject: [PATCH 1/3] miner: hold cs_main across TestBlockValidity in the RandomX solver (F15) The RandomX miner's solution check called TestBlockValidity(..., chainActive.LastTip(), ...) with cs_main NOT held (the LOCK was commented out). TestBlockValidity asserts pindexPrev == chainActive.Tip(), so a tip change (a peer block or a reorg) between the LastTip() read and the assert aborts the node. The lock had been removed only to avoid a ~256MB RandomX re-validation allocation; SetSkipRandomXValidation(true) already skips that re-validation here, so re-acquiring cs_main across the (now cheap) call is safe and closes the race. Adversarially reviewed: no lock-order cycle (datasetMtx(shared)->cs_main only), ProcessBlockFound stays outside the scope with its own staleness recheck. Co-Authored-By: Claude Opus 4.8 --- src/miner.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index b22cc406a..a7a1ba2ef 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1436,13 +1436,19 @@ void static RandomXMiner() } CValidationState state; - //{ LOCK(cs_main); - // Skip RandomX re-validation during TestBlockValidity — we already - // computed the correct hash, and re-verifying allocates ~256MB which - // can trigger the OOM killer on memory-constrained systems. - SetSkipRandomXValidation(true); - bool fValid = TestBlockValidity(state,B, chainActive.LastTip(), true, false); - SetSkipRandomXValidation(false); + bool fValid; + { + // Hold cs_main across the LastTip() read and TestBlockValidity: the latter + // asserts pindexPrev == chainActive.Tip(), so without the lock a tip change + // (a block arriving from a peer, or a reorg) between the read and the assert + // aborts the node. The lock was previously removed only to avoid a ~256MB + // RandomX re-validation alloc; SetSkipRandomXValidation() already skips that + // re-validation here, so holding the lock is cheap and safe. + LOCK(cs_main); + SetSkipRandomXValidation(true); + fValid = TestBlockValidity(state, B, chainActive.LastTip(), true, false); + SetSkipRandomXValidation(false); + } if ( !fValid ) { LogPrintf("RandomXMiner: TestBlockValidity FAILED at ht.%d, gotinvalid=1, state=%s\n", @@ -1450,7 +1456,6 @@ void static RandomXMiner() gotinvalid = 1; return(false); } - //} SetThreadPriority(THREAD_PRIORITY_NORMAL); LogPrintf("HushRandomXMiner:\n"); LogPrintf("proof-of-work found \n hash: %s \ntarget: %s\n", B.GetHash().GetHex(), HASHTarget.GetHex()); From 1c29fe86ff4817ba3de964d7ff668b1dcde9dfac Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 30 Oct 2026 22:28:06 -0500 Subject: [PATCH 2/3] net: cap per-peer header flooding in and after IBD, by side-branch growth (F14) AcceptBlockHeader runs CheckBlockHeader with fCheckPOW=0 (no RandomX/PoW check), so a peer can cheaply mint valid-looking sibling headers of the tip, each stored as a permanent mapBlockIndex entry. The existing per-peer cap ran only during IBD, leaving post-IBD growth unbounded -> a memory-exhaustion DoS from one connected peer. Two complementary per-peer caps, applied in and after IBD: (1) a TIGHT cap (2000) on newly-stored SIDE-BRANCH headers -- headers added to mapBlockIndex that did not become the best-known header. That is the actual growth vector; honest headers-first sync and new-block announcements are best-extending and never increment it, and duplicates (which do not grow mapBlockIndex) are excluded, so the cap can be small with no honest-peer risk. A sibling flood trips it after ~2000 entries instead of millions. (2) the prior loose cumulative cap (2*validated-height + 200000) as a backstop against a long FORWARD chain of correct-nBits headers (which do extend the best chain, so (1) never sees them). Adversarial review drove the tightening: the earlier IBD-gate-removal-only version bounded memory only at ~6.8M entries/peer. Counting just newly-stored non-best headers bounds it to a few thousand. Co-Authored-By: Claude Opus 4.8 --- src/main.cpp | 55 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index db0af063b..c82e8c25c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -328,8 +328,12 @@ namespace { bool fBulkHeaderSeen; //! (server side) time (us) we last served a bulk stream to this peer, for flood throttling. int64_t nLastBulkServeTime; - //! (#8 IBD header-flood cap) cumulative headers this peer made us process while in IBD. + //! (#8 header-flood cap) cumulative headers this peer made us process. int64_t nHeadersProcessed; + //! (#8 header-flood cap) newly-stored headers from this peer that did NOT extend the best + //! header chain (side-branch/sibling): the real mapBlockIndex-growth vector. Honest sync and + //! new-block announcements are best-extending and never increment this. + int64_t nSideBranchHeaders; CNodeState() { fCurrentlyConnected = false; @@ -351,6 +355,7 @@ namespace { fBulkHeaderSeen = false; nLastBulkServeTime = 0; nHeadersProcessed = 0; + nSideBranchHeaders = 0; } }; @@ -7875,6 +7880,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, } CBlockIndex *pindexLast = NULL; + int64_t nSideBranchThisMsg = 0; // newly-stored headers that did NOT extend the best header chain BOOST_FOREACH(const CBlockHeader& header, headers) { if(fDebug) { printf("%s: size.%i, solution size.%i\n", __func__, (int)sizeof(header), (int)header.nSolution.size()); @@ -7887,6 +7893,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return error("non-continuous headers sequence"); } int32_t futureblock; + const bool fWasNew = (mapBlockIndex.count(header.GetHash()) == 0); // not already stored (duplicates don't grow mapBlockIndex) if (!AcceptBlockHeader(&futureblock,header, state, &pindexLast)) { int nDoS; if (state.IsInvalid(nDoS) && futureblock == 0) @@ -7896,29 +7903,47 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return error("invalid header received"); } } + else if (fWasNew && pindexLast != NULL && pindexLast != pindexBestHeader) { + // Newly added to mapBlockIndex but NOT the best-known header: a side-branch/sibling, + // which is exactly the flood vector. Honest headers-first sync and new-block + // announcements extend the best chain, so they are never counted here. + nSideBranchThisMsg++; + } } - // SECURITY (#8: IBD header-flood cap): bound how many headers a single peer can make us - // store while in IBD. Honest headers-first sync needs at most ~chain-length headers from a - // peer; one that floods far past 2x the known chain length is only trying to bloat - // mapBlockIndex/leveldb (such headers are never selected -- nMinimumChainWork gates that -- - // but they still cost memory/disk). Cap per-peer and drop the peer. IBD-only: post-IBD there is - // no header-accept PoW check (removed as bug-prone), so a post-IBD flood is bounded only by - // nMinimumChainWork gating selection -- memory/disk growth there is accepted as low-severity. - if (IsInitialBlockDownload()) { + // SECURITY (#8: header-flood cap): bound how many headers a single peer can make us store, in and + // after IBD. Header-accept does no PoW check (removed as bug-prone), so without a cap a connected + // peer could flood sibling headers of the tip and grow mapBlockIndex linearly (a memory-exhaustion + // DoS). Two complementary per-peer caps, both dropping the peer when exceeded: + { CNodeState *hstate = State(pfrom->GetId()); if (hstate != NULL) { - hstate->nHeadersProcessed += (int64_t)nCount; - // Cap RELATIVE TO THE VALIDATED ACTIVE-CHAIN HEIGHT (attacker-hard -- advancing it requires - // connecting real PoW blocks), NOT pindexBestHeader: a forward-extending header flood advances - // pindexBestHeader in lockstep with the attacker, so a pindexBestHeader-relative cap never fires. - // The checkpoint height is a fixed floor so honest IBD (blocks still lagging headers) is never capped. + hstate->nHeadersProcessed += (int64_t)nCount; + hstate->nSideBranchHeaders += nSideBranchThisMsg; + + // (1) Tight cap on newly-stored SIDE-BRANCH headers -- the actual mapBlockIndex-growth + // vector. A legitimate competing fork is bounded by the reorg window (MAX_REORG_LENGTH); + // this allows many times that for slack while bounding a flood to a few thousand entries + // per peer. Honest best-extending sync never increments the counter, so it is safe to + // keep small. + const int64_t kSideBranchCap = 2000; + if (hstate->nSideBranchHeaders > kSideBranchCap) { + Misbehaving(pfrom->GetId(), 100); + return error("%s: peer=%d flooded %lld side-branch headers (cap %lld)", __func__, + pfrom->id, (long long)hstate->nSideBranchHeaders, (long long)kSideBranchCap); + } + + // (2) Loose cumulative backstop against an attacker who grinds a long FORWARD chain of + // correct-nBits headers (those do extend the best chain, so (1) never sees them). Cap + // RELATIVE TO THE VALIDATED ACTIVE-CHAIN HEIGHT (attacker-hard -- advancing it needs real + // PoW), NOT pindexBestHeader (a forward flood advances that in lockstep). The checkpoint + // height is a fixed floor so honest IBD (blocks lagging headers) is never capped. int knownH = std::max((int)chainActive.Height(), Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints())); int64_t headerCap = 2 * (int64_t)knownH + 200000; if (hstate->nHeadersProcessed > headerCap) { Misbehaving(pfrom->GetId(), 100); - return error("%s: peer=%d flooded %lld headers during IBD (cap %lld)", __func__, + return error("%s: peer=%d flooded %lld headers (cap %lld)", __func__, pfrom->id, (long long)hstate->nHeadersProcessed, (long long)headerCap); } } From 10217af8e40cc4b5222762c881e9d4c323142803 Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 30 Oct 2026 22:28:32 -0500 Subject: [PATCH 3/3] consensus: height/time-gated removal of the CRYPTO555/notary t-vout exemption (F16) On the fully-private chain (ac_private=1) all transparent outputs are banned except coinbase, but hush_isnotaryvout() exempts CRYPTO555 and dPoW notary addresses -- so a public transparent UTXO to those addresses is accepted on a chain users treat as fully shielded. Drop that exemption so such vouts are rejected like any other taddr. Dropping a previously-accepted output is a consensus change that could fork the live chain, so it is activated by block time (DRAGONX_NOTARYVOUT_BAN_TIME, same mechanism as HUSH_SAPING_ACTIVATION). It ships DEFAULT-OFF via the 0xFFFFFFFF sentinel (year 2106), so merging is a verified no-op; the operator sets a real future timestamp once (a) the upgrade window has passed and (b) they have confirmed no active dPoW notarization posts transparent notary vouts on-chain -- otherwise activation would reject notarization txs and halt/fork. Adversarially reviewed as SOUND (default-off, safe to merge): tiptime is deterministic at the authoritative connect paths (prev->nTime), the sentinel is byte-identical to prior behavior, and coinbase + ac_burn paths are preserved. Reviewer note: a height-based gate would be marginally cleaner than time if ever made live. Co-Authored-By: Claude Opus 4.8 --- src/hush_defs.h | 9 +++++++++ src/main.cpp | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/hush_defs.h b/src/hush_defs.h index 2264a7160..a31e68029 100644 --- a/src/hush_defs.h +++ b/src/hush_defs.h @@ -32,6 +32,15 @@ #define CRYPTO555_PUBSECPSTR "038a1bd41a08f38edda51042988022933c5775dfce81f7bae0b32a9179650352ac" #define CRYPTO555_HUSHADDR "RFetqf8WUfWnwNeXdknkm8ojk7EXnYFzrv" #define CRYPTO555_RMD160STR "deadbeefbadcaca748c4986b240ff7d7bc3fffb0" +// DragonX: block time at/after which the inherited CRYPTO555/notary transparent-vout exemption on +// the fully-private chain is removed, so those vouts become invalid like any other taddr. Dropping +// a previously-accepted output is a consensus change, so it is activated by block time (same +// mechanism as HUSH_SAPING_ACTIVATION) to let the whole network upgrade before it bites. +// 0xFFFFFFFF is a SENTINEL meaning "never active": the operator sets a real future timestamp once +// the upgrade window has passed AND they have confirmed no active dPoW notarization posts +// transparent notary vouts on-chain (which this ban would otherwise reject). Until then this commit +// is a no-op and changes no validation behavior. +#define DRAGONX_NOTARYVOUT_BAN_TIME 0xFFFFFFFF #define HUSH_FIRSTFUNGIBLEID 100 #define HUSH_SAPING_ACTIVATION 1544832000 // Dec 15th, 2018 #define HUSH_SAPING_DEADLINE 1550188800 // Feb 15th, 2019 diff --git a/src/main.cpp b/src/main.cpp index c82e8c25c..d71cb2309 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1598,7 +1598,14 @@ bool CheckTransactionWithoutProofVerification(uint32_t tiptime,const CTransactio char destaddr[65]; Getscriptaddress(destaddr,txout.scriptPubKey); - if ( hush_isnotaryvout(destaddr,tiptime) == 0 ) + // The CRYPTO555/notary allowlist exempts those transparent vouts from the private-chain + // ban, leaving a public UTXO on a chain users treat as fully shielded. At/after the + // activation time that exemption is dropped and such vouts are rejected like any other + // taddr (time-gated + default-off via the sentinel so the whole network upgrades first). + // The gate is checked first so the allowlist lookup is skipped entirely once active. + int32_t isNotaryVout = ( tiptime >= DRAGONX_NOTARYVOUT_BAN_TIME ) + ? 0 : hush_isnotaryvout(destaddr,tiptime); + if ( isNotaryVout == 0 ) { const bool isburn = (strcmp(destaddr,BURN_ADDRESS) == 0); if ((ASSETCHAINS_BURN == 1) && isburn && tx.vin.empty()) {