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 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 22:27:41 -05:00
co-authored by Claude Opus 4.8
parent fe988d6fbe
commit 661678dd84
+13 -8
View File
@@ -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());