diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 05e24db112..618a5a640a 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -24,6 +24,7 @@ static const std::string MNEHF_REQUESTID_PREFIX = "mnhf"; static const std::string DB_SIGNALS = "mnhf_s"; +static const std::string DB_SIGNALS_v2 = "mnhf_s2"; uint256 MNHFTxPayload::GetRequestId() const { @@ -57,34 +58,33 @@ CMNHFManager::Signals CMNHFManager::GetSignalsStage(const CBlockIndex* const pin { if (!DeploymentActiveAfter(pindexPrev, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) return {}; - Signals signals = GetForBlock(pindexPrev); + Signals signals_tmp = GetForBlock(pindexPrev); + if (pindexPrev == nullptr) return {}; const int height = pindexPrev->nHeight + 1; - for (auto it = signals.begin(); it != signals.end(); ) { - bool found{false}; - const auto signal_pindex = pindexPrev->GetAncestor(it->second); + + Signals signals_ret; + + for (auto signal : signals_tmp) { + bool expired{false}; + const auto signal_pindex = pindexPrev->GetAncestor(signal.second); assert(signal_pindex != nullptr); const int64_t signal_time = signal_pindex->GetMedianTimePast(); for (int index = 0; index < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++index) { const auto& deployment = Params().GetConsensus().vDeployments[index]; - if (deployment.bit != it->first) continue; + if (deployment.bit != signal.first) continue; if (signal_time < deployment.nStartTime) { // new deployment is using the same bit as the old one - LogPrintf("CMNHFManager::GetSignalsStage: mnhf signal bit=%d height:%d is expired at height=%d\n", it->first, it->second, height); - it = signals.erase(it); - } else { - ++it; + LogPrintf("CMNHFManager::GetSignalsStage: mnhf signal bit=%d height:%d is expired at height=%d\n", + signal.first, signal.second, height); + expired = true; } - found = true; - break; } - if (!found) { - // no deployment means we buried it and aren't using the same bit (yet) - LogPrintf("CMNHFManager::GetSignalsStage: mnhf signal bit=%d height:%d is not known at height=%d\n", it->first, it->second, height); - it = signals.erase(it); + if (!expired) { + signals_ret.insert(signal); } } - return signals; + return signals_ret; } bool MNHFTx::Verify(const llmq::CQuorumManager& qman, const uint256& quorumHash, const uint256& requestId, const uint256& msgHash, TxValidationState& state) const @@ -287,6 +287,9 @@ CMNHFManager::Signals CMNHFManager::GetForBlock(const CBlockIndex* pindex) const Consensus::Params& consensusParams{Params().GetConsensus()}; while (!to_calculate.empty()) { const CBlockIndex* pindex_top{to_calculate.top()}; + if (pindex_top->nHeight % 1000 == 0) { + LogPrintf("re-index EHF signals at block %d\n", pindex_top->nHeight); + } CBlock block; if (!ReadBlockFromDisk(block, pindex_top, consensusParams)) { throw std::runtime_error("failed-getehfforblock-read"); @@ -328,11 +331,19 @@ std::optional CMNHFManager::GetFromCache(const CBlockInde return signals; } } - if (m_evoDb.Read(std::make_pair(DB_SIGNALS, blockHash), signals)) { + if (m_evoDb.Read(std::make_pair(DB_SIGNALS_v2, blockHash), signals)) { LOCK(cs_cache); mnhfCache.insert(blockHash, signals); return signals; } + if (!DeploymentActiveAt(*pindex, Params().GetConsensus(), Consensus::DEPLOYMENT_MN_RR)) { + // before mn_rr activation we are safe + if (m_evoDb.Read(std::make_pair(DB_SIGNALS, blockHash), signals)) { + LOCK(cs_cache); + mnhfCache.insert(blockHash, signals); + return signals; + } + } return std::nullopt; } @@ -346,7 +357,7 @@ void CMNHFManager::AddToCache(const Signals& signals, const CBlockIndex* const p } if (!DeploymentActiveAt(*pindex, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) return; - m_evoDb.Write(std::make_pair(DB_SIGNALS, blockHash), signals); + m_evoDb.Write(std::make_pair(DB_SIGNALS_v2, blockHash), signals); } void CMNHFManager::AddSignal(const CBlockIndex* const pindex, int bit) @@ -364,6 +375,25 @@ void CMNHFManager::ConnectManagers(gsl::not_null chainman, g m_qman = qman; } +bool CMNHFManager::ForceSignalDBUpdate() +{ + // force ehf signals db update + auto dbTx = m_evoDb.BeginTransaction(); + + const bool last_legacy = bls::bls_legacy_scheme.load(); + bls::bls_legacy_scheme.store(false); + GetSignalsStage(m_chainman->ActiveChainstate().m_chain.Tip()); + bls::bls_legacy_scheme.store(last_legacy); + + dbTx->Commit(); + // flush it to disk + if (!m_evoDb.CommitRootTransaction()) { + LogPrintf("CMNHFManager::%s -- failed to commit to evoDB\n", __func__); + return false; + } + return true; +} + std::string MNHFTx::ToString() const { return strprintf("MNHFTx(versionBit=%d, quorumHash=%s, sig=%s)", diff --git a/src/evo/mnhftx.h b/src/evo/mnhftx.h index eb92f1db82..1d6b577f3c 100644 --- a/src/evo/mnhftx.h +++ b/src/evo/mnhftx.h @@ -155,6 +155,8 @@ public: */ void DisconnectManagers() { m_chainman = nullptr; m_qman = nullptr; }; + bool ForceSignalDBUpdate() EXCLUSIVE_LOCKS_REQUIRED(cs_main); + private: void AddToCache(const Signals& signals, const CBlockIndex* const pindex); diff --git a/src/init.cpp b/src/init.cpp index 8d9edaa273..1abb710513 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2058,6 +2058,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) strLoadError = _("Error upgrading evo database"); break; } + if (!node.mnhf_manager->ForceSignalDBUpdate()) { + strLoadError = _("Error upgrading evo database for EHF"); + break; + } for (CChainState* chainstate : chainman.GetAll()) { if (!is_coinsview_empty(chainstate)) {