Skip to content

Commit e83e32b

Browse files
committed
Add in-memory cache for CRecoveredSigsDb::HasRecoveredSigForHash
1 parent 677c004 commit e83e32b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/llmq/quorums_signing.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,23 @@ bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash)
8181

8282
bool CRecoveredSigsDb::HasRecoveredSigForHash(const uint256& hash)
8383
{
84+
int64_t t = GetTimeMillis();
85+
86+
{
87+
LOCK(cs);
88+
auto it = hasSigForHashCache.find(hash);
89+
if (it != hasSigForHashCache.end()) {
90+
it->second.second = t;
91+
return it->second.first;
92+
}
93+
}
94+
8495
auto k = std::make_tuple('h', hash);
85-
return db.Exists(k);
96+
bool ret = db.Exists(k);
97+
98+
LOCK(cs);
99+
hasSigForHashCache.emplace(hash, std::make_pair(ret, t));
100+
return ret;
86101
}
87102

88103
bool CRecoveredSigsDb::ReadRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret)
@@ -154,6 +169,7 @@ void CRecoveredSigsDb::WriteRecoveredSig(const llmq::CRecoveredSig& recSig)
154169
LOCK(cs);
155170
hasSigForIdCache[std::make_pair((Consensus::LLMQType)recSig.llmqType, recSig.id)] = std::make_pair(true, t);
156171
hasSigForSessionCache[signHash] = std::make_pair(true, t);
172+
hasSigForHashCache[recSig.GetHash()] = std::make_pair(true, t);
157173
}
158174
}
159175

@@ -239,10 +255,12 @@ void CRecoveredSigsDb::CleanupOldRecoveredSigs(int64_t maxAge)
239255

240256
hasSigForIdCache.erase(std::make_pair((Consensus::LLMQType)recSig.llmqType, recSig.id));
241257
hasSigForSessionCache.erase(signHash);
258+
hasSigForHashCache.erase(recSig.GetHash());
242259
}
243260

244261
TruncateCacheMap(hasSigForIdCache, MAX_CACHE_SIZE, MAX_CACHE_TRUNCATE_THRESHOLD);
245262
TruncateCacheMap(hasSigForSessionCache, MAX_CACHE_SIZE, MAX_CACHE_TRUNCATE_THRESHOLD);
263+
TruncateCacheMap(hasSigForHashCache, MAX_CACHE_SIZE, MAX_CACHE_TRUNCATE_THRESHOLD);
246264
}
247265

248266
for (auto& e : toDelete2) {

src/llmq/quorums_signing.h

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class CRecoveredSigsDb
8888
CCriticalSection cs;
8989
std::unordered_map<std::pair<Consensus::LLMQType, uint256>, std::pair<bool, int64_t>> hasSigForIdCache;
9090
std::unordered_map<uint256, std::pair<bool, int64_t>> hasSigForSessionCache;
91+
std::unordered_map<uint256, std::pair<bool, int64_t>> hasSigForHashCache;
9192

9293
public:
9394
CRecoveredSigsDb(bool fMemory);

0 commit comments

Comments
 (0)