From 661678dd848277be014f990edc8428c243b59ed9 Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 30 Oct 2026 22:27:41 -0500 Subject: [PATCH] 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());