diff --git a/src/llmq/quorums_blockprocessor.cpp b/src/llmq/quorums_blockprocessor.cpp index 49f4425b2..82861dab3 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 0afd2a8f0..330338d44 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 @@ private: std::map, uint256> minableCommitmentsByQuorum; std::map minableCommitments; + std::unordered_map, bool> hasMinedCommitmentCache; + public: CQuorumBlockProcessor(CEvoDB& _evoDb) : evoDb(_evoDb) {}