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
This commit is contained in:
dustinface 2021-01-18 19:54:50 +01:00 committed by GitHub
parent bdbadc1809
commit ddb57ce2a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 35 deletions

View File

@ -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()) { if (!masternodeSync.IsBlockchainSynced()) {
return; 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); 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<CQuorum>& quorum) const CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const
{ {
AssertLockHeld(quorumsCacheCs);
assert(pindexQuorum); 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()); assert(qc.quorumHash == pindexQuorum->GetBlockHash());
auto quorum = std::make_shared<CQuorum>(llmq::GetLLMQParams(llmqType), blsWorker);
auto members = CLLMQUtils::GetAllQuorumMembers((Consensus::LLMQType)qc.llmqType, pindexQuorum); auto members = CLLMQUtils::GetAllQuorumMembers((Consensus::LLMQType)qc.llmqType, pindexQuorum);
quorum->Init(qc, pindexQuorum, minedBlockHash, members); quorum->Init(qc, pindexQuorum, minedBlockHash, members);
@ -231,7 +240,9 @@ bool CQuorumManager::BuildQuorumFromCommitment(const CFinalCommitment& qc, const
CQuorum::StartCachePopulatorThread(quorum); CQuorum::StartCachePopulatorThread(quorum);
} }
return true; quorumsCache.emplace(std::make_pair(llmqType, quorumHash), quorum);
return quorum;
} }
bool CQuorumManager::BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr<CQuorum>& quorum) const bool CQuorumManager::BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr<CQuorum>& quorum) const
@ -275,7 +286,7 @@ bool CQuorumManager::HasQuorum(Consensus::LLMQType llmqType, const uint256& quor
return quorumBlockProcessor->HasMinedCommitment(llmqType, quorumHash); return quorumBlockProcessor->HasMinedCommitment(llmqType, quorumHash);
} }
std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) const
{ {
const CBlockIndex* pindex; const CBlockIndex* pindex;
{ {
@ -285,7 +296,7 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
return ScanQuorums(llmqType, pindex, maxCount); return ScanQuorums(llmqType, pindex, maxCount);
} }
std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) const
{ {
auto& params = Params().GetConsensus().llmqs.at(llmqType); auto& params = Params().GetConsensus().llmqs.at(llmqType);
@ -333,7 +344,7 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
return result; return result;
} }
CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const
{ {
CBlockIndex* pindexQuorum; CBlockIndex* pindexQuorum;
{ {
@ -348,7 +359,7 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint25
return GetQuorum(llmqType, pindexQuorum); 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); assert(pindexQuorum);
@ -367,23 +378,7 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const CBlock
return it->second; return it->second;
} }
CFinalCommitment qc; return BuildQuorumFromCommitment(llmqType, pindexQuorum);
uint256 minedBlockHash;
if (!quorumBlockProcessor->GetMinedCommitment(llmqType, quorumHash, qc, minedBlockHash)) {
return nullptr;
}
auto& params = Params().GetConsensus().llmqs.at(llmqType);
auto quorum = std::make_shared<CQuorum>(params, blsWorker);
if (!BuildQuorumFromCommitment(qc, pindexQuorum, minedBlockHash, quorum)) {
return nullptr;
}
quorumsCache.emplace(std::make_pair(llmqType, quorumHash), quorum);
return quorum;
} }
} // namespace llmq } // namespace llmq

View File

@ -85,32 +85,32 @@ private:
CBLSWorker& blsWorker; CBLSWorker& blsWorker;
CDKGSessionManager& dkgManager; CDKGSessionManager& dkgManager;
CCriticalSection quorumsCacheCs; mutable CCriticalSection quorumsCacheCs;
std::map<std::pair<Consensus::LLMQType, uint256>, CQuorumPtr> quorumsCache; mutable std::map<std::pair<Consensus::LLMQType, uint256>, CQuorumPtr> quorumsCache;
unordered_lru_cache<std::pair<Consensus::LLMQType, uint256>, std::vector<CQuorumCPtr>, StaticSaltedHasher, 32> scanQuorumsCache; mutable unordered_lru_cache<std::pair<Consensus::LLMQType, uint256>, std::vector<CQuorumCPtr>, StaticSaltedHasher, 32> scanQuorumsCache;
public: public:
CQuorumManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager); 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); static bool HasQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash);
// all these methods will lock cs_main for a short period of time // all these methods will lock cs_main for a short period of time
CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash); CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount); std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, size_t maxCount) const;
// this one is cs_main-free // this one is cs_main-free
std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount); std::vector<CQuorumCPtr> ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t maxCount) const;
private: private:
// all private methods here are cs_main-free // 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<CQuorum>& quorum) const; CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const;
bool BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr<CQuorum>& quorum) const; bool BuildQuorumContributions(const CFinalCommitment& fqc, std::shared_ptr<CQuorum>& quorum) const;
CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindex); CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindex) const;
}; };
extern CQuorumManager* quorumManager; extern CQuorumManager* quorumManager;