Skip to content

Commit

Permalink
Add in-memory cache to CQuorumBlockProcessor::HasMinedCommitment
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Feb 27, 2019
1 parent f305cf7 commit 677c004
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/llmq/quorums_blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/llmq/quorums_blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <map>
#include <unordered_map>

class CNode;
class CConnman;
Expand All @@ -29,6 +31,8 @@ class CQuorumBlockProcessor
std::map<std::pair<Consensus::LLMQType, uint256>, uint256> minableCommitmentsByQuorum;
std::map<uint256, CFinalCommitment> minableCommitments;

std::unordered_map<std::pair<Consensus::LLMQType, uint256>, bool> hasMinedCommitmentCache;

public:
CQuorumBlockProcessor(CEvoDB& _evoDb) : evoDb(_evoDb) {}

Expand Down

0 comments on commit 677c004

Please sign in to comment.