diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 63abba543b6c..e70ba73509a2 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -208,6 +208,7 @@ void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqTyp bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, BLSSecretKeyVector& skContributionsRet) { + LOCK(contributionsCacheCs); auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pindexQuorum); memberIndexesRet.clear(); @@ -218,47 +219,28 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, skContributionsRet.reserve(members.size()); for (size_t i = 0; i < members.size(); i++) { if (validMembers[i]) { - BLSVerificationVectorPtr vvec; - CBLSSecretKey skContribution; - if (!GetVerifiedContribution(llmqType, pindexQuorum, members[i]->proTxHash, vvec, skContribution)) { - return false; + const uint256& proTxHash = members[i]->proTxHash; + ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash}; + auto it = contributionsCache.find(cacheKey); + if (it == contributionsCache.end()) { + BLSVerificationVectorPtr vvecPtr = std::make_shared(); + CBLSSecretKey skContribution; + if (!llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), *vvecPtr)) { + return false; + } + llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution); + + it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first; } memberIndexesRet.emplace_back(i); - vvecsRet.emplace_back(vvec); - skContributionsRet.emplace_back(skContribution); + vvecsRet.emplace_back(it->second.vvec); + skContributionsRet.emplace_back(it->second.skContribution); } } return true; } -bool CDKGSessionManager::GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet) -{ - LOCK(contributionsCacheCs); - ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash}; - auto it = contributionsCache.find(cacheKey); - if (it != contributionsCache.end()) { - vvecRet = it->second.vvec; - skContributionRet = it->second.skContribution; - return true; - } - - BLSVerificationVector vvec; - BLSVerificationVectorPtr vvecPtr; - CBLSSecretKey skContribution; - if (llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), vvec)) { - vvecPtr = std::make_shared(std::move(vvec)); - } - llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution); - - it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first; - - vvecRet = it->second.vvec; - skContributionRet = it->second.skContribution; - - return true; -} - void CDKGSessionManager::CleanupCache() { LOCK(contributionsCacheCs); diff --git a/src/llmq/quorums_dkgsessionmgr.h b/src/llmq/quorums_dkgsessionmgr.h index 4e8728365f9d..3ac9f01d5a1f 100644 --- a/src/llmq/quorums_dkgsessionmgr.h +++ b/src/llmq/quorums_dkgsessionmgr.h @@ -65,7 +65,6 @@ class CDKGSessionManager void WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec); void WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSSecretKey& skContribution); bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, BLSSecretKeyVector& skContributionsRet); - bool GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet); private: void CleanupCache();