From 677c0040cd01260c81848042ced58befd984d60d Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Tue, 26 Feb 2019 07:20:47 +0100 Subject: [PATCH] Add in-memory cache to CQuorumBlockProcessor::HasMinedCommitment --- src/llmq/quorums_blockprocessor.cpp | 24 +++++++++++++++++++++++- src/llmq/quorums_blockprocessor.h | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/llmq/quorums_blockprocessor.cpp b/src/llmq/quorums_blockprocessor.cpp index 49f4425b2afca..82861dab321a1 100644 --- a/src/llmq/quorums_blockprocessor.cpp +++ b/src/llmq/quorums_blockprocessor.cpp @@ -199,6 +199,11 @@ bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindex, const C evoDb.Write(std::make_pair(DB_FIRST_MINED_COMMITMENT, (uint8_t)params.type), quorumHash); } + { + LOCK(minableCommitmentsCs); + hasMinedCommitmentCache.erase(std::make_pair(params.type, quorumHash)); + } + LogPrintf("CQuorumBlockProcessor::%s -- processed commitment from block. type=%d, quorumHash=%s, signers=%s, validMembers=%d, quorumPublicKey=%s\n", __func__, qc.llmqType, quorumHash.ToString(), qc.CountSigners(), qc.CountValidMembers(), qc.quorumPublicKey.ToString()); @@ -222,6 +227,10 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pi } evoDb.Erase(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash))); + { + LOCK(minableCommitmentsCs); + hasMinedCommitmentCache.erase(std::make_pair((Consensus::LLMQType)qc.llmqType, qc.quorumHash)); + } // if a reorg happened, we should allow to mine this commitment later AddMinableCommitment(qc); @@ -309,8 +318,21 @@ uint256 CQuorumBlockProcessor::GetQuorumBlockHash(Consensus::LLMQType llmqType, bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) { + auto cacheKey = std::make_pair(llmqType, quorumHash); + { + LOCK(minableCommitmentsCs); + auto cacheIt = hasMinedCommitmentCache.find(cacheKey); + if (cacheIt != hasMinedCommitmentCache.end()) { + return cacheIt->second; + } + } + auto key = std::make_pair(DB_MINED_COMMITMENT, std::make_pair((uint8_t)llmqType, quorumHash)); - return evoDb.Exists(key); + bool ret = evoDb.Exists(key); + + LOCK(minableCommitmentsCs); + hasMinedCommitmentCache.emplace(cacheKey, ret); + return ret; } bool CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, CFinalCommitment& ret) diff --git a/src/llmq/quorums_blockprocessor.h b/src/llmq/quorums_blockprocessor.h index 0afd2a8f0e481..330338d44984c 100644 --- a/src/llmq/quorums_blockprocessor.h +++ b/src/llmq/quorums_blockprocessor.h @@ -6,12 +6,14 @@ #define DASH_QUORUMS_BLOCKPROCESSOR_H #include "llmq/quorums_commitment.h" +#include "llmq/quorums_utils.h" #include "consensus/params.h" #include "primitives/transaction.h" #include "sync.h" #include +#include class CNode; class CConnman; @@ -29,6 +31,8 @@ class CQuorumBlockProcessor std::map, uint256> minableCommitmentsByQuorum; std::map minableCommitments; + std::unordered_map, bool> hasMinedCommitmentCache; + public: CQuorumBlockProcessor(CEvoDB& _evoDb) : evoDb(_evoDb) {}