From f02daa75082b852750a403d68c9c8ed63ecf1f69 Mon Sep 17 00:00:00 2001 From: pasta Date: Wed, 11 Nov 2026 02:15:13 +0000 Subject: [PATCH] fix: stop rebuilding MNHF signal state from unvalidated blocks getdeploymentinfo and /rest/deploymentinfo accept any stored block. For a block that was never connected, CMNHFManager rebuilt the signal state from its unvalidated data, wrote it to EvoDB outside a transaction (aborting the node at the next flush), and could reach the out-of-range bit assert in IsValidMNActivation. Signal state now only comes from connecting a block. GetForBlock reads the cache and EvoDB and throws EvoDbMissingStateError when nothing is stored, which callers treat as a local error rather than an invalid block. The rebuild walk moved to RebuildSignals, which requires cs_main, refuses blocks that did not pass script validation, and only runs at startup, so the manager never acquires cs_main itself: through ForceSignalDBUpdate, and in ReplayBlocks for the last flushed tip, because replaying an interrupted flush reads that state before ForceSignalDBUpdate has migrated a datadir last run before v22. getdeploymentinfo also rejects invalid and unvalidated blocks up front, and IsValidMNActivation returns false for out-of-range bits instead of asserting. Co-Authored-By: GPT 6.1 Sol Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Fable 5.2 --- src/evo/creditpool.cpp | 2 +- src/evo/evodb.h | 9 +++++++ src/evo/mnhftx.cpp | 18 ++++++++++---- src/evo/mnhftx.h | 15 ++++++++---- src/evo/specialtxman.cpp | 10 ++++---- src/evo/specialtxman.h | 3 +++ src/kernel/chainparams.cpp | 2 +- src/rpc/blockchain.cpp | 4 ++++ src/test/evo_mnhf_tests.cpp | 38 ++++++++++++++++++++++++++++++ src/validation.cpp | 9 ++++++- test/functional/interface_rest.py | 39 ++++++++++++++++++++++++++++++- 11 files changed, 132 insertions(+), 17 deletions(-) diff --git a/src/evo/creditpool.cpp b/src/evo/creditpool.cpp index 980ebab3c236..bcc62e10542f 100644 --- a/src/evo/creditpool.cpp +++ b/src/evo/creditpool.cpp @@ -414,7 +414,7 @@ std::optional GetCreditPoolDiffForBlock(CCreditPoolManager& cpo } return creditPoolDiff; } catch (const EvoDbInconsistencyError& e) { - // Local EvoDB corruption (the node is already aborting): fail with + // Local EvoDB inconsistency (missing or mismatched state): fail with // M_ERROR so the block is not marked invalid. state.Error(e.what()); return std::nullopt; diff --git a/src/evo/evodb.h b/src/evo/evodb.h index f9485eb09b89..75d9bd67ebb4 100644 --- a/src/evo/evodb.h +++ b/src/evo/evodb.h @@ -48,6 +48,15 @@ class EvoDbInconsistencyError : public std::runtime_error using std::runtime_error::runtime_error; }; +/** Thrown when block-derived state that EvoDB should hold for a block is missing. Like a mismatch it + * is never evidence about the block, but it does not abort the node: an RPC may ask about a block + * whose state was never stored. */ +class EvoDbMissingStateError : public EvoDbInconsistencyError +{ +public: + using EvoDbInconsistencyError::EvoDbInconsistencyError; +}; + class CEvoDB; class CEvoDBScopedCommitter diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index f17b520c1189..70194ab32bda 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -250,7 +250,7 @@ std::optional CMNHFManager::ProcessBlock(const CBlock& bl AddToCache(signals, pindex); return signals; } catch (const EvoDbInconsistencyError& e) { - // Local EvoDB corruption (the node is already aborting): fail with + // Local EvoDB inconsistency (missing or mismatched state): fail with // M_ERROR so the block is not marked invalid. state.Error(e.what()); return std::nullopt; @@ -284,12 +284,21 @@ bool CMNHFManager::UndoBlock(const CBlock& block, const CBlockIndex* const pinde CMNHFManager::Signals CMNHFManager::GetForBlock(const CBlockIndex* pindex) { - if (pindex == nullptr) return {}; + if (auto signals = GetFromCache(pindex)) return *signals; + throw EvoDbMissingStateError(strprintf("MNHF signals unavailable for block %s", pindex->GetBlockHash().ToString())); +} + +void CMNHFManager::RebuildSignals(const CBlockIndex* pindex) +{ + AssertLockHeld(::cs_main); std::stack to_calculate; std::optional signalsTmp; while (!(signalsTmp = GetFromCache(pindex)).has_value()) { + if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) { + throw std::runtime_error("failed-getehfforblock-unvalidated"); + } to_calculate.push(pindex); pindex = pindex->pprev; } @@ -317,7 +326,6 @@ CMNHFManager::Signals CMNHFManager::GetForBlock(const CBlockIndex* pindex) to_calculate.pop(); } - return *signalsTmp; } std::optional CMNHFManager::GetFromCache(const CBlockIndex* const pindex) @@ -365,7 +373,7 @@ void CMNHFManager::AddToCache(const Signals& signals, const CBlockIndex* const p { assert(pindex != nullptr); const uint256& blockHash = pindex->GetBlockHash(); - if (DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_V20) && + if (DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_V20) && Assume(m_evoDb.HasActiveTransaction()) && !m_evoDb.WriteDerived(std::make_pair(DB_SIGNALS_v2, blockHash), signals)) { // A mismatch is local EvoDB corruption, not a statement about the // block. Abort here: some callers (miner, RPC) never pass through a @@ -389,7 +397,7 @@ bool CMNHFManager::ForceSignalDBUpdate(const CBlockIndex* tip) const bool last_legacy = bls::bls_legacy_scheme.load(); bls::bls_legacy_scheme.store(false); - GetSignalsStage(tip); + RebuildSignals(tip); bls::bls_legacy_scheme.store(last_legacy); dbTx->Commit(); diff --git a/src/evo/mnhftx.h b/src/evo/mnhftx.h index 36d4f47bee92..7370b2966d53 100644 --- a/src/evo/mnhftx.h +++ b/src/evo/mnhftx.h @@ -128,18 +128,25 @@ class CMNHFManager : public AbstractEHFManager */ bool UndoBlock(const CBlock& block, const CBlockIndex* const pindex) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache); - // Implements interface + // Implements interface. Only reads state stored when a block was connected, and throws if + // pindexPrev has none (e.g. it was never connected). Signals GetSignalsStage(const CBlockIndex* const pindexPrev) override EXCLUSIVE_LOCKS_REQUIRED(!cs_cache); bool ForceSignalDBUpdate(const CBlockIndex* tip) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, !cs_cache); + /** + * Recompute missing state from block data, walking back from pindex to the first block whose + * state is stored. Throws for a block that did not pass script validation. Only for startup, + * inside an EvoDB transaction: pindex must be a block that was connected. + */ + void RebuildSignals(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, !cs_cache); + private: void AddToCache(const Signals& signals, const CBlockIndex* const pindex) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache); /** - * This function returns list of signals available on previous block. - * if the signals for previous block is not available in cache it would read blocks from disk - * until state won't be recovered. + * Returns the signals stored for pindex, from the cache or evo db. Throws EvoDbMissingStateError + * if there are none, e.g. because the block was never connected. * NOTE: that some signals could expired between blocks. */ Signals GetForBlock(const CBlockIndex* const pindex) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache); diff --git a/src/evo/specialtxman.cpp b/src/evo/specialtxman.cpp index b125dcf078f5..e57cc0ff81ca 100644 --- a/src/evo/specialtxman.cpp +++ b/src/evo/specialtxman.cpp @@ -902,6 +902,8 @@ uint256 CSpecialTxProcessor::GetDeterministicMNListHash(gsl::not_null pindex) { m_dmnman.UpdatedBlockTip(pindex); } +void CSpecialTxProcessor::RebuildMNHFSignals(const CBlockIndex* pindex) { m_mnhfman->RebuildSignals(pindex); } + void CSpecialTxProcessor::ResetPlatformBans(const CBlock& block, const CBlockIndex& index) { AssertLockHeld(::cs_main); @@ -1107,8 +1109,8 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const LogPrintf("CSpecialTxProcessor::%s -- bls_legacy_scheme=%d\n", __func__, bls::bls_legacy_scheme.load()); } } catch (const EvoDbInconsistencyError& e) { - // Local EvoDB corruption detected below (the node is already - // aborting): fail with M_ERROR so the block is not marked invalid. + // Local EvoDB inconsistency detected below (missing or mismatched + // state): fail with M_ERROR so the block is not marked invalid. return state.Error(e.what()); } catch (const std::exception& e) { LogPrintf("CSpecialTxProcessor::%s -- FAILURE! %s\n", __func__, e.what()); @@ -1180,8 +1182,8 @@ bool CSpecialTxProcessor::CheckCreditPoolDiffForBlock(const CBlock& block, const } } catch (const EvoDbInconsistencyError& e) { - // Local EvoDB corruption detected below (the node is already - // aborting): fail with M_ERROR so the block is not marked invalid. + // Local EvoDB inconsistency detected below (missing or mismatched + // state): fail with M_ERROR so the block is not marked invalid. return state.Error(e.what()); } catch (const std::exception& e) { LogPrintf("CSpecialTxProcessor::%s -- FAILURE! %s\n", __func__, e.what()); diff --git a/src/evo/specialtxman.h b/src/evo/specialtxman.h index 5593f6be006d..4fa027b0f9b4 100644 --- a/src/evo/specialtxman.h +++ b/src/evo/specialtxman.h @@ -116,6 +116,9 @@ class CSpecialTxProcessor /** Passthrough to CDeterministicMNManager::UpdatedBlockTip */ void UpdatedMNListTip(gsl::not_null pindex); + /** Passthrough to CMNHFManager::RebuildSignals */ + void RebuildMNHFSignals(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); + void ResetPlatformBans(const CBlock& block, const CBlockIndex& index) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); void DiscardDeterministicMNBlock(const uint256& block_hash); diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 6d0f643ad20f..fc04d66a8bcd 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -119,7 +119,7 @@ static CBlock FindDevNetGenesisBlock(const CBlock &prevBlock, const std::string& bool CChainParams::IsValidMNActivation(int nBit, int64_t timePast) const { - assert(nBit < VERSIONBITS_NUM_BITS); + if (nBit < 0 || nBit >= VERSIONBITS_NUM_BITS) return false; for (int index = 0; index < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++index) { if (consensus.vDeployments[index].bit == nBit) { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a55f3d1ff444..681a64e911d3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1727,6 +1727,10 @@ RPCHelpMan getdeploymentinfo() if (!blockindex) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); } + // ConnectBlock skips the genesis block, so it never reaches BLOCK_VALID_SCRIPTS + if (blockindex->pprev && !blockindex->IsValid(BLOCK_VALID_SCRIPTS)) { + throw JSONRPCError(RPC_MISC_ERROR, "MNHF signals unavailable for an invalid or unvalidated block"); + } } const auto ehf_signals{CHECK_NONFATAL(node.special_tx)->m_mnhfman->GetSignalsStage(blockindex)}; diff --git a/src/test/evo_mnhf_tests.cpp b/src/test/evo_mnhf_tests.cpp index dde064eec1c4..9095c23404f1 100644 --- a/src/test/evo_mnhf_tests.cpp +++ b/src/test/evo_mnhf_tests.cpp @@ -6,13 +6,18 @@ #include #include #include +#include +#include #include #include +#include #include #include +#include