Skip to content

Commit

Permalink
llmq: Improve/Fix GetVerifiedContributions (dashpay#3911)
Browse files Browse the repository at this point in the history
* llmq: Fix GetVerifiedContribution to return false in case of failure

* llmq: Move GetVerifiedContribution into GetVerifiedContributions

* llmq: Drop GetVerifiedContribution

* llmq: Keep cache locked while building GetVerifiedContributions result

* llmq: Read from DB into vvecPtr directly
  • Loading branch information
xdustinface authored and gades committed Mar 13, 2022
1 parent 76f1d67 commit b70a4d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
48 changes: 15 additions & 33 deletions src/llmq/quorums_dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqTyp

bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet)
{
LOCK(contributionsCacheCs);
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pindexQuorum);

memberIndexesRet.clear();
Expand All @@ -217,47 +218,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<BLSVerificationVector>();
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<BLSVerificationVector>(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);
Expand Down
1 change: 0 additions & 1 deletion src/llmq/quorums_dkgsessionmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet);
bool GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet);

private:
void CleanupCache();
Expand Down

0 comments on commit b70a4d1

Please sign in to comment.