Skip to content

Commit b70a4d1

Browse files
xdustinfacegades
authored andcommitted
llmq: Improve/Fix GetVerifiedContributions (dashpay#3911)
* 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
1 parent 76f1d67 commit b70a4d1

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

src/llmq/quorums_dkgsessionmgr.cpp

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqTyp
207207

208208
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)
209209
{
210+
LOCK(contributionsCacheCs);
210211
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pindexQuorum);
211212

212213
memberIndexesRet.clear();
@@ -217,47 +218,28 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType,
217218
skContributionsRet.reserve(members.size());
218219
for (size_t i = 0; i < members.size(); i++) {
219220
if (validMembers[i]) {
220-
BLSVerificationVectorPtr vvec;
221-
CBLSSecretKey skContribution;
222-
if (!GetVerifiedContribution(llmqType, pindexQuorum, members[i]->proTxHash, vvec, skContribution)) {
223-
return false;
221+
const uint256& proTxHash = members[i]->proTxHash;
222+
ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash};
223+
auto it = contributionsCache.find(cacheKey);
224+
if (it == contributionsCache.end()) {
225+
BLSVerificationVectorPtr vvecPtr = std::make_shared<BLSVerificationVector>();
226+
CBLSSecretKey skContribution;
227+
if (!llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), *vvecPtr)) {
228+
return false;
229+
}
230+
llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution);
231+
232+
it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first;
224233
}
225234

226235
memberIndexesRet.emplace_back(i);
227-
vvecsRet.emplace_back(vvec);
228-
skContributionsRet.emplace_back(skContribution);
236+
vvecsRet.emplace_back(it->second.vvec);
237+
skContributionsRet.emplace_back(it->second.skContribution);
229238
}
230239
}
231240
return true;
232241
}
233242

234-
bool CDKGSessionManager::GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet)
235-
{
236-
LOCK(contributionsCacheCs);
237-
ContributionsCacheKey cacheKey = {llmqType, pindexQuorum->GetBlockHash(), proTxHash};
238-
auto it = contributionsCache.find(cacheKey);
239-
if (it != contributionsCache.end()) {
240-
vvecRet = it->second.vvec;
241-
skContributionRet = it->second.skContribution;
242-
return true;
243-
}
244-
245-
BLSVerificationVector vvec;
246-
BLSVerificationVectorPtr vvecPtr;
247-
CBLSSecretKey skContribution;
248-
if (llmqDb.Read(std::make_tuple(DB_VVEC, llmqType, pindexQuorum->GetBlockHash(), proTxHash), vvec)) {
249-
vvecPtr = std::make_shared<BLSVerificationVector>(std::move(vvec));
250-
}
251-
llmqDb.Read(std::make_tuple(DB_SKCONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution);
252-
253-
it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first;
254-
255-
vvecRet = it->second.vvec;
256-
skContributionRet = it->second.skContribution;
257-
258-
return true;
259-
}
260-
261243
void CDKGSessionManager::CleanupCache()
262244
{
263245
LOCK(contributionsCacheCs);

src/llmq/quorums_dkgsessionmgr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class CDKGSessionManager
6666
void WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec);
6767
void WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSSecretKey& skContribution);
6868
bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet);
69-
bool GetVerifiedContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, BLSVerificationVectorPtr& vvecRet, CBLSSecretKey& skContributionRet);
7069

7170
private:
7271
void CleanupCache();

0 commit comments

Comments
 (0)