From 5fdfa26482c863faaa674457c95fd3218cd62030 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 1 Jan 2021 13:24:38 +0100 Subject: [PATCH 1/5] llmq: Fix GetVerifiedContribution to return false in case of failure --- src/llmq/quorums_dkgsessionmgr.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 63abba543b6c3..93cfa21822c8f 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -246,11 +246,12 @@ bool CDKGSessionManager::GetVerifiedContribution(Consensus::LLMQType llmqType, c 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)); + if (!llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), vvec)) { + return false; } llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution); + vvecPtr = std::make_shared(std::move(vvec)); it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first; vvecRet = it->second.vvec; From 6348e4bd85e6bbce9af2d6d74bacfaa8a197719b Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 1 Jan 2021 15:05:03 +0100 Subject: [PATCH 2/5] llmq: Move GetVerifiedContribution into GetVerifiedContributions --- src/llmq/quorums_dkgsessionmgr.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 93cfa21822c8f..37321f035f7e4 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -218,15 +218,26 @@ 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; + LOCK(contributionsCacheCs); + const uint256& proTxHash = members[i]->proTxHash; + ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash}; + auto it = contributionsCache.find(cacheKey); + if (it == contributionsCache.end()) { + BLSVerificationVector vvec; + BLSVerificationVectorPtr vvecPtr; + CBLSSecretKey skContribution; + if (!llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), vvec)) { + return false; + } + llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution); + + vvecPtr = std::make_shared(std::move(vvec)); + 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; From f2dd2136abad52db8fb5e9af91253e0d7a03f1b8 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 1 Jan 2021 15:05:21 +0100 Subject: [PATCH 3/5] llmq: Drop GetVerifiedContribution --- src/llmq/quorums_dkgsessionmgr.cpp | 28 ---------------------------- src/llmq/quorums_dkgsessionmgr.h | 1 - 2 files changed, 29 deletions(-) diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 37321f035f7e4..e39fcbd84ea24 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -243,34 +243,6 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, 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)) { - return false; - } - llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution); - - vvecPtr = std::make_shared(std::move(vvec)); - 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 4e8728365f9d8..3ac9f01d5a1fe 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(); From b2ccae5d194a72e7840ed9a31bfaca45645a571b Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 1 Jan 2021 13:58:13 +0100 Subject: [PATCH 4/5] llmq: Keep cache locked while building GetVerifiedContributions result --- src/llmq/quorums_dkgsessionmgr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index e39fcbd84ea24..9518b3a02e7e9 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,7 +219,6 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, skContributionsRet.reserve(members.size()); for (size_t i = 0; i < members.size(); i++) { if (validMembers[i]) { - LOCK(contributionsCacheCs); const uint256& proTxHash = members[i]->proTxHash; ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash}; auto it = contributionsCache.find(cacheKey); From b99f06ed06aea2232b3d86bf580ed5be88bd5693 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Fri, 1 Jan 2021 15:07:54 +0100 Subject: [PATCH 5/5] llmq: Read from DB into vvecPtr directly --- src/llmq/quorums_dkgsessionmgr.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/llmq/quorums_dkgsessionmgr.cpp b/src/llmq/quorums_dkgsessionmgr.cpp index 9518b3a02e7e9..e70ba73509a22 100644 --- a/src/llmq/quorums_dkgsessionmgr.cpp +++ b/src/llmq/quorums_dkgsessionmgr.cpp @@ -223,15 +223,13 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash}; auto it = contributionsCache.find(cacheKey); if (it == contributionsCache.end()) { - BLSVerificationVector vvec; - BLSVerificationVectorPtr vvecPtr; + BLSVerificationVectorPtr vvecPtr = std::make_shared(); CBLSSecretKey skContribution; - if (!llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), vvec)) { + 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); - vvecPtr = std::make_shared(std::move(vvec)); it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first; }