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());