From ddb57ce2a392a5a6ff56b264fe8803ed10d9b3f1 Mon Sep 17 00:00:00 2001 From: dustinface <35775977+xdustinface@users.noreply.github.com> Date: Mon, 18 Jan 2021 19:54:50 +0100 Subject: [PATCH] llmq: Some refactoring in CQuorumManger (#3935) * llmq: Refactor CQuorumManager::{BuildQuorumFromCommitment, GetQuorum} Construct and cache new quorums inside BuildQuorumFromCommitment * llmq: Make all methods of CQuorumManager const --- src/llmq/quorums.cpp | 45 ++++++++++++++++++++------------------------ src/llmq/quorums.h | 20 ++++++++++---------- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 4e66b00015..d7dec02f7f 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -161,7 +161,7 @@ CQuorumManager::CQuorumManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessi { } -void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitialDownload) +void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitialDownload) const { if (!masternodeSync.IsBlockchainSynced()) { return; @@ -172,7 +172,7 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial } } -void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexNew) +void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexNew) const { const auto& params = Params().GetConsensus().llmqs.at(llmqType); @@ -203,11 +203,20 @@ void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const } } -bool CQuorumManager::BuildQuorumFromCommitment(const CFinalCommitment& qc, const CBlockIndex* pindexQuorum, const uint256& minedBlockHash, std::shared_ptr& quorum) const +CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const { + AssertLockHeld(quorumsCacheCs); assert(pindexQuorum); + + CFinalCommitment qc; + const uint256& quorumHash{pindexQuorum->GetBlockHash()}; + uint256 minedBlockHash; + if (!quorumBlockProcessor->GetMinedCommitment(llmqType, quorumHash, qc, minedBlockHash)) { + return nullptr; + } assert(qc.quorumHash == pindexQuorum->GetBlockHash()); + auto quorum = std::make_shared(llmq::GetLLMQParams(llmqType), blsWorker); auto members = CLLMQUtils::GetAllQuorumMembers((Consensus::LLMQType)qc.llmqType, pindexQuorum); quorum->Init(qc, pindexQuorum, minedBlockHash, members); @@ -231,7 +240,9 @@ bool CQuorumManager::BuildQuorumFromCommitment(const CFinalCommitment& qc, const CQuorum::StartCachePopulatorThread(quorum); } - return true; + quorumsCache.emplace(std::make_pair(llmqType, quorumHash), quorum); + + return quorum; } bool CQuorumManager::BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr& quorum) const @@ -275,7 +286,7 @@ bool CQuorumManager::HasQuorum(Consensus::LLMQType llmqType, const uint256& quor return quorumBlockProcessor->HasMinedCommitment(llmqType, quorumHash); } -std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) +std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) const { const CBlockIndex* pindex; { @@ -285,7 +296,7 @@ std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp return ScanQuorums(llmqType, pindex, maxCount); } -std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) +std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) const { auto& params = Params().GetConsensus().llmqs.at(llmqType); @@ -333,7 +344,7 @@ std::vector CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp return result; } -CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) +CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const { CBlockIndex* pindexQuorum; { @@ -348,7 +359,7 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint25 return GetQuorum(llmqType, pindexQuorum); } -CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) +CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const { assert(pindexQuorum); @@ -367,23 +378,7 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const CBlock return it->second; } - CFinalCommitment qc; - uint256 minedBlockHash; - if (!quorumBlockProcessor->GetMinedCommitment(llmqType, quorumHash, qc, minedBlockHash)) { - return nullptr; - } - - auto& params = Params().GetConsensus().llmqs.at(llmqType); - - auto quorum = std::make_shared(params, blsWorker); - - if (!BuildQuorumFromCommitment(qc, pindexQuorum, minedBlockHash, quorum)) { - return nullptr; - } - - quorumsCache.emplace(std::make_pair(llmqType, quorumHash), quorum); - - return quorum; + return BuildQuorumFromCommitment(llmqType, pindexQuorum); } } // namespace llmq diff --git a/src/llmq/quorums.h b/src/llmq/quorums.h index dfe7b00a41..0cc059bfb6 100644 --- a/src/llmq/quorums.h +++ b/src/llmq/quorums.h @@ -85,32 +85,32 @@ private: CBLSWorker& blsWorker; CDKGSessionManager& dkgManager; - CCriticalSection quorumsCacheCs; - std::map, CQuorumPtr> quorumsCache; - unordered_lru_cache, std::vector, StaticSaltedHasher, 32> scanQuorumsCache; + mutable CCriticalSection quorumsCacheCs; + mutable std::map, CQuorumPtr> quorumsCache; + mutable unordered_lru_cache, std::vector, StaticSaltedHasher, 32> scanQuorumsCache; public: CQuorumManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager); - void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload); + void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload) const; static bool HasQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash); // all these methods will lock cs_main for a short period of time - CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash); - std::vector ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount); + CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const; + std::vector ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) const; // this one is cs_main-free - std::vector ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount); + std::vector ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) const; private: // all private methods here are cs_main-free - void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew); + void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew) const; - bool BuildQuorumFromCommitment(const CFinalCommitment& qc, const CBlockIndex* pindexQuorum, const uint256& minedBlockHash, std::shared_ptr& quorum) const; + CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const; bool BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr& quorum) const; - CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindex); + CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindex) const; }; extern CQuorumManager* quorumManager;