From 7a7c9f12a413016e2f459715465bb95249583ba4 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Wed, 27 Nov 2024 17:13:30 +0700 Subject: [PATCH 1/6] fix: early EHF and buried EHF are indistinguish --- src/evo/mnhftx.cpp | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 05e24db112..ebc17439c1 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -23,7 +23,7 @@ #include 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 +57,32 @@ 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 +285,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,7 +329,7 @@ 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; @@ -346,7 +347,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) From c6bb9a56857c69fcf1358ebf2d9e710dfece3a84 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Wed, 27 Nov 2024 18:22:14 +0700 Subject: [PATCH 2/6] perf: re-use evo data about signals between v20 and mn_rr as non-corrupted --- src/evo/mnhftx.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index ebc17439c1..79319477dd 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -23,6 +23,7 @@ #include static const std::string MNEHF_REQUESTID_PREFIX = "mnhf"; +static const std::string DB_SIGNALS = "mnhf"; static const std::string DB_SIGNALS_v2 = "mnhf_s2"; uint256 MNHFTxPayload::GetRequestId() const @@ -334,6 +335,14 @@ std::optional CMNHFManager::GetFromCache(const CBlockInde 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; } From 9ceba88cdbfaace00b6f850ea72b48232f4fba00 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Wed, 27 Nov 2024 20:14:44 +0700 Subject: [PATCH 3/6] style: clang suggestion Co-authored-by: UdjinM6 --- src/evo/mnhftx.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 79319477dd..1fe90958ef 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -75,7 +75,8 @@ CMNHFManager::Signals CMNHFManager::GetSignalsStage(const CBlockIndex* const pin 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", signal.first, signal.second, height); + LogPrintf("CMNHFManager::GetSignalsStage: mnhf signal bit=%d height:%d is expired at height=%d\n", + signal.first, signal.second, height); expired = true; } } From 94d80323d48c466784ca8feb7c28a70866df36c4 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Wed, 27 Nov 2024 20:25:43 +0700 Subject: [PATCH 4/6] fix: typo name of key Co-authored-by: UdjinM6 --- src/evo/mnhftx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 1fe90958ef..0a381cd44b 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -23,7 +23,7 @@ #include static const std::string MNEHF_REQUESTID_PREFIX = "mnhf"; -static const std::string DB_SIGNALS = "mnhf"; +static const std::string DB_SIGNALS = "mnhf_s"; static const std::string DB_SIGNALS_v2 = "mnhf_s2"; uint256 MNHFTxPayload::GetRequestId() const From 05041a4572bf125325764dc11f04ce8efcc1388f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Thu, 28 Nov 2024 13:22:47 +0300 Subject: [PATCH 5/6] fix: force ehf signal db update --- src/evo/mnhftx.cpp | 19 +++++++++++++++++++ src/evo/mnhftx.h | 2 ++ src/init.cpp | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 0a381cd44b..618a5a640a 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -375,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..4609d4d762 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(); + private: void AddToCache(const Signals& signals, const CBlockIndex* const pindex); diff --git a/src/init.cpp b/src/init.cpp index 8967159b7b..b193fc7211 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)) { From 4629bb9ba5c2d5f4e4d24ddef90d8d657116d501 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Tue, 3 Dec 2024 01:01:22 +0700 Subject: [PATCH 6/6] fix: add missing cs_main annotation for ForceSignalDBUpdate --- src/evo/mnhftx.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/evo/mnhftx.h b/src/evo/mnhftx.h index 4609d4d762..1d6b577f3c 100644 --- a/src/evo/mnhftx.h +++ b/src/evo/mnhftx.h @@ -155,7 +155,7 @@ public: */ void DisconnectManagers() { m_chainman = nullptr; m_qman = nullptr; }; - bool ForceSignalDBUpdate(); + bool ForceSignalDBUpdate() EXCLUSIVE_LOCKS_REQUIRED(cs_main); private: void AddToCache(const Signals& signals, const CBlockIndex* const pindex);