From 60bc3a82a27989a5d4b47b09110c54db5eb78976 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 28 Dec 2023 03:09:20 +0700 Subject: [PATCH] refactor: moving options helpers 'Is Quorum <...>' to llmq/options --- src/llmq/blockprocessor.cpp | 4 +- src/llmq/dkgsessionmgr.cpp | 2 +- src/llmq/options.cpp | 77 +++++++++++++++++++++++++++++++++++- src/llmq/options.h | 8 ++++ src/llmq/quorums.cpp | 2 +- src/llmq/utils.cpp | 76 ----------------------------------- src/llmq/utils.h | 11 +----- src/miner.cpp | 4 +- src/rpc/quorums.cpp | 8 ++-- src/test/evo_utils_tests.cpp | 6 +-- 10 files changed, 98 insertions(+), 100 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index 491ef8abcf..12eac0670c 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -28,7 +28,7 @@ static void PreComputeQuorumMembers(const CBlockIndex* pindex, bool reset_cache = false) { - for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindex->pprev)) { + for (const Consensus::LLMQParams& params : llmq::GetEnabledQuorumParams(pindex->pprev)) { if (llmq::IsQuorumRotationEnabled(params, pindex) && (pindex->nHeight % params.dkgInterval == 0)) { llmq::utils::GetAllQuorumMembers(params.type, pindex, reset_cache); } @@ -166,7 +166,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_nullpprev)) { + for (const Consensus::LLMQParams& params : GetEnabledQuorumParams(pindex->pprev)) { // skip these checks when replaying blocks after the crash if (m_chainstate.m_chain.Tip() == nullptr) { break; diff --git a/src/llmq/dkgsessionmgr.cpp b/src/llmq/dkgsessionmgr.cpp index c271f2f05e..2f94a4dc54 100644 --- a/src/llmq/dkgsessionmgr.cpp +++ b/src/llmq/dkgsessionmgr.cpp @@ -247,7 +247,7 @@ void CDKGSessionManager::ProcessMessage(CNode& pfrom, const std::string& msg_typ return; } - if (!utils::IsQuorumTypeEnabled(llmqType, pQuorumBaseBlockIndex->pprev)) { + if (!IsQuorumTypeEnabled(llmqType, pQuorumBaseBlockIndex->pprev)) { LogPrintf("CDKGSessionManager -- llmqType [%d] quorums aren't active\n", ToUnderlying(llmqType)); m_peerman->Misbehaving(pfrom.GetId(), 100); return; diff --git a/src/llmq/options.cpp b/src/llmq/options.cpp index 1a8f16078b..447e1dc160 100644 --- a/src/llmq/options.cpp +++ b/src/llmq/options.cpp @@ -10,9 +10,12 @@ #include #include #include +#include -#include #include +#include + +static constexpr int TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT = 847000; namespace llmq { @@ -111,4 +114,76 @@ std::map GetEnabledQuorumVvecSyncEntries() return mapQuorumVvecSyncEntries; } +bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null pindexPrev) +{ + return IsQuorumTypeEnabledInternal(llmqType, pindexPrev, std::nullopt); +} + +bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null pindexPrev, + std::optional optDIP0024IsActive) +{ + const Consensus::Params& consensusParams = Params().GetConsensus(); + + const bool fDIP0024IsActive{optDIP0024IsActive.value_or(DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0024))}; + switch (llmqType) + { + case Consensus::LLMQType::LLMQ_DEVNET: + return true; + case Consensus::LLMQType::LLMQ_50_60: + if (Params().NetworkIDString() == CBaseChainParams::TESTNET) return true; + // fall through + case Consensus::LLMQType::LLMQ_TEST_INSTANTSEND: + return !fDIP0024IsActive; + + case Consensus::LLMQType::LLMQ_TEST: + case Consensus::LLMQType::LLMQ_TEST_PLATFORM: + case Consensus::LLMQType::LLMQ_400_60: + case Consensus::LLMQType::LLMQ_400_85: + case Consensus::LLMQType::LLMQ_DEVNET_PLATFORM: + return true; + + case Consensus::LLMQType::LLMQ_TEST_V17: { + return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_TESTDUMMY); + } + case Consensus::LLMQType::LLMQ_100_67: + return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0020); + + case Consensus::LLMQType::LLMQ_60_75: + case Consensus::LLMQType::LLMQ_DEVNET_DIP0024: + case Consensus::LLMQType::LLMQ_TEST_DIP0024: { + return fDIP0024IsActive; + } + case Consensus::LLMQType::LLMQ_25_67: + return pindexPrev->nHeight >= TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT; + + default: + throw std::runtime_error(strprintf("%s: Unknown LLMQ type %d", __func__, ToUnderlying(llmqType))); + } + + // Something wrong with conditions above, they are not consistent + assert(false); +} + +std::vector GetEnabledQuorumTypes(gsl::not_null pindex) +{ + std::vector ret; + ret.reserve(Params().GetConsensus().llmqs.size()); + for (const auto& params : Params().GetConsensus().llmqs) { + if (IsQuorumTypeEnabled(params.type, pindex)) { + ret.push_back(params.type); + } + } + return ret; +} + +std::vector> GetEnabledQuorumParams(gsl::not_null pindex) +{ + std::vector> ret; + ret.reserve(Params().GetConsensus().llmqs.size()); + + std::copy_if(Params().GetConsensus().llmqs.begin(), Params().GetConsensus().llmqs.end(), std::back_inserter(ret), + [&pindex](const auto& params){return IsQuorumTypeEnabled(params.type, pindex);}); + + return ret; +} } // namespace llmq diff --git a/src/llmq/options.h b/src/llmq/options.h index 4c65263369..17e85dc0f1 100644 --- a/src/llmq/options.h +++ b/src/llmq/options.h @@ -9,6 +9,8 @@ #include #include +#include +#include class CBlockIndex; @@ -40,6 +42,12 @@ bool IsWatchQuorumsEnabled(); /// Returns the parsed entries given by `-llmq-qvvec-sync` std::map GetEnabledQuorumVvecSyncEntries(); +bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null pindexPrev); +bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null pindexPrev, std::optional optDIP0024IsActive); + +std::vector GetEnabledQuorumTypes(gsl::not_null pindex); +std::vector> GetEnabledQuorumParams(gsl::not_null pindex); + } // namespace llmq #endif // BITCOIN_LLMQ_OPTIONS_H diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index dab3a4f322..6e99b231e7 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -495,7 +495,7 @@ std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t nCountRequested) const { - if (pindexStart == nullptr || nCountRequested == 0 || !utils::IsQuorumTypeEnabled(llmqType, pindexStart)) { + if (pindexStart == nullptr || nCountRequested == 0 || !IsQuorumTypeEnabled(llmqType, pindexStart)) { return {}; } diff --git a/src/llmq/utils.cpp b/src/llmq/utils.cpp index 9fea9ecc08..ad7a512153 100644 --- a/src/llmq/utils.cpp +++ b/src/llmq/utils.cpp @@ -20,15 +20,12 @@ #include #include #include -#include #include #include class CBLSSignature; -static constexpr int TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT = 847000; - /** * Forward declarations */ @@ -850,79 +847,6 @@ void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not } } -bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null pindexPrev) -{ - return IsQuorumTypeEnabledInternal(llmqType, pindexPrev, std::nullopt); -} - -bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null pindexPrev, - std::optional optDIP0024IsActive) -{ - const Consensus::Params& consensusParams = Params().GetConsensus(); - - const bool fDIP0024IsActive{optDIP0024IsActive.value_or(DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0024))}; - switch (llmqType) - { - case Consensus::LLMQType::LLMQ_DEVNET: - return true; - case Consensus::LLMQType::LLMQ_50_60: - if (Params().NetworkIDString() == CBaseChainParams::TESTNET) return true; - // fall through - case Consensus::LLMQType::LLMQ_TEST_INSTANTSEND: - return !fDIP0024IsActive; - - case Consensus::LLMQType::LLMQ_TEST: - case Consensus::LLMQType::LLMQ_TEST_PLATFORM: - case Consensus::LLMQType::LLMQ_400_60: - case Consensus::LLMQType::LLMQ_400_85: - case Consensus::LLMQType::LLMQ_DEVNET_PLATFORM: - return true; - - case Consensus::LLMQType::LLMQ_TEST_V17: { - return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_TESTDUMMY); - } - case Consensus::LLMQType::LLMQ_100_67: - return DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_DIP0020); - - case Consensus::LLMQType::LLMQ_60_75: - case Consensus::LLMQType::LLMQ_DEVNET_DIP0024: - case Consensus::LLMQType::LLMQ_TEST_DIP0024: { - return fDIP0024IsActive; - } - case Consensus::LLMQType::LLMQ_25_67: - return pindexPrev->nHeight >= TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT; - - default: - throw std::runtime_error(strprintf("%s: Unknown LLMQ type %d", __func__, ToUnderlying(llmqType))); - } - - // Something wrong with conditions above, they are not consistent - assert(false); -} - -std::vector GetEnabledQuorumTypes(gsl::not_null pindex) -{ - std::vector ret; - ret.reserve(Params().GetConsensus().llmqs.size()); - for (const auto& params : Params().GetConsensus().llmqs) { - if (IsQuorumTypeEnabled(params.type, pindex)) { - ret.push_back(params.type); - } - } - return ret; -} - -std::vector> GetEnabledQuorumParams(gsl::not_null pindex) -{ - std::vector> ret; - ret.reserve(Params().GetConsensus().llmqs.size()); - - std::copy_if(Params().GetConsensus().llmqs.begin(), Params().GetConsensus().llmqs.end(), std::back_inserter(ret), - [&pindex](const auto& params){return IsQuorumTypeEnabled(params.type, pindex);}); - - return ret; -} - template void InitQuorumsCache(CacheType& cache, bool limit_by_connections) { diff --git a/src/llmq/utils.h b/src/llmq/utils.h index 88f0b2ea4f..08db78467c 100644 --- a/src/llmq/utils.h +++ b/src/llmq/utils.h @@ -6,13 +6,12 @@ #define BITCOIN_LLMQ_UTILS_H #include -#include #include #include #include #include -#include +#include #include class CConnman; @@ -24,8 +23,6 @@ using CDeterministicMNCPtr = std::shared_ptr; namespace llmq { -class CQuorumSnapshot; - namespace utils { @@ -40,12 +37,6 @@ std::set CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash); void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash); -bool IsQuorumTypeEnabled(Consensus::LLMQType llmqType, gsl::not_null pindexPrev); -bool IsQuorumTypeEnabledInternal(Consensus::LLMQType llmqType, gsl::not_null pindexPrev, std::optional optDIP0024IsActive); - -std::vector GetEnabledQuorumTypes(gsl::not_null pindex); -std::vector> GetEnabledQuorumParams(gsl::not_null pindex); - template void InitQuorumsCache(CacheType& cache, bool limit_by_connections = true); diff --git a/src/miner.cpp b/src/miner.cpp index 54f629fe92..05c867851c 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include @@ -156,7 +156,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc : pblock->GetBlockTime(); if (fDIP0003Active_context) { - for (const Consensus::LLMQParams& params : llmq::utils::GetEnabledQuorumParams(pindexPrev)) { + for (const Consensus::LLMQParams& params : llmq::GetEnabledQuorumParams(pindexPrev)) { std::vector vqcTx; if (quorum_block_processor.GetMineableCommitmentsTx(params, nHeight, diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index 3f42dbf038..37059788f1 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -76,7 +76,7 @@ static UniValue quorum_list(const JSONRPCRequest& request, const ChainstateManag CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()); - for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) { + for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) { const auto& llmq_params_opt = Params().GetLLMQ(type); CHECK_NONFATAL(llmq_params_opt.has_value()); UniValue v(UniValue::VARR); @@ -141,7 +141,7 @@ static UniValue quorum_list_extended(const JSONRPCRequest& request, const Chains CBlockIndex* pblockindex = nHeight != -1 ? WITH_LOCK(cs_main, return chainman.ActiveChain()[nHeight]) : WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()); - for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pblockindex)) { + for (const auto& type : llmq::GetEnabledQuorumTypes(pblockindex)) { const auto& llmq_params_opt = Params().GetLLMQ(type); CHECK_NONFATAL(llmq_params_opt.has_value()); const auto& llmq_params = llmq_params_opt.value(); @@ -299,7 +299,7 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, const Chainstate UniValue minableCommitments(UniValue::VARR); UniValue quorumArrConnections(UniValue::VARR); - for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) { + for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) { const auto& llmq_params_opt = Params().GetLLMQ(type); CHECK_NONFATAL(llmq_params_opt.has_value()); const auto& llmq_params = llmq_params_opt.value(); @@ -403,7 +403,7 @@ static UniValue quorum_memberof(const JSONRPCRequest& request, const ChainstateM } UniValue result(UniValue::VARR); - for (const auto& type : llmq::utils::GetEnabledQuorumTypes(pindexTip)) { + for (const auto& type : llmq::GetEnabledQuorumTypes(pindexTip)) { const auto& llmq_params_opt = Params().GetLLMQ(type); CHECK_NONFATAL(llmq_params_opt.has_value()); size_t count = llmq_params_opt->signingActiveQuorumCount; diff --git a/src/test/evo_utils_tests.cpp b/src/test/evo_utils_tests.cpp index 2660ddcbde..1810f5aef1 100644 --- a/src/test/evo_utils_tests.cpp +++ b/src/test/evo_utils_tests.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include @@ -13,12 +13,12 @@ #include -/* TODO: rename this file and test to llmq_utils_test */ +/* TODO: rename this file and test to llmq_options_test */ BOOST_AUTO_TEST_SUITE(evo_utils_tests) void Test(NodeContext& node) { - using namespace llmq::utils; + using namespace llmq; auto tip = node.chainman->ActiveTip(); const auto& consensus_params = Params().GetConsensus(); BOOST_CHECK_EQUAL(IsQuorumTypeEnabledInternal(consensus_params.llmqTypeDIP0024InstantSend, tip, false), false);