From 9ba595e3bfe2480e1b5769b572ef0b33d02740d4 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sun, 7 Jan 2024 13:42:32 +0300 Subject: [PATCH] fix: store BLSVerificationVector on disk using basic bls scheme --- src/llmq/dkgsessionmgr.cpp | 22 ++++++++++++++++++---- src/llmq/quorums.cpp | 25 +++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/llmq/dkgsessionmgr.cpp b/src/llmq/dkgsessionmgr.cpp index a4c957803703b..eb90f0496fd4a 100644 --- a/src/llmq/dkgsessionmgr.cpp +++ b/src/llmq/dkgsessionmgr.cpp @@ -365,7 +365,12 @@ bool CDKGSessionManager::GetPrematureCommitment(const uint256& hash, CDKGPrematu void CDKGSessionManager::WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec) { - db->Write(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), *vvec); + CDataStream s(SER_DISK, CLIENT_VERSION); + WriteCompactSize(s, vvec->size()); + for (auto& pubkey : *vvec) { + s << CBLSPublicKeyVersionWrapper(pubkey, false); + } + db->Write(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), s); } void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const CBLSSecretKey& skContribution) @@ -395,11 +400,20 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, ContributionsCacheKey cacheKey = {llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash}; auto it = contributionsCache.find(cacheKey); if (it == contributionsCache.end()) { - auto vvecPtr = std::make_shared>(); - CBLSSecretKey skContribution; - if (!db->Read(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), *vvecPtr)) { + CDataStream s(SER_DISK, CLIENT_VERSION); + if (!db->ReadDataStream(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), s)) { return false; } + size_t vvec_size = ReadCompactSize(s); + CBLSPublicKey pubkey; + std::vector qv; + for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) { + s >> CBLSPublicKeyVersionWrapper(pubkey, false); + qv.emplace_back(pubkey); + } + auto vvecPtr = std::make_shared>(std::move(qv)); + + CBLSSecretKey skContribution; db->Read(std::make_tuple(DB_SKCONTRIB, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), skContribution); it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first; diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index bb32fb0e3690e..e27ca84b467ee 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -163,7 +163,12 @@ void CQuorum::WriteContributions(CEvoDB& evoDb) const LOCK(cs); if (HasVerificationVector()) { - evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), *quorumVvec); + CDataStream s(SER_DISK, CLIENT_VERSION); + WriteCompactSize(s, quorumVvec->size()); + for (auto& pubkey : *quorumVvec) { + s << CBLSPublicKeyVersionWrapper(pubkey, false); + } + evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s); } if (skShare.IsValid()) { evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare); @@ -173,17 +178,25 @@ void CQuorum::WriteContributions(CEvoDB& evoDb) const bool CQuorum::ReadContributions(CEvoDB& evoDb) { uint256 dbKey = MakeQuorumKey(*this); + CDataStream s(SER_DISK, CLIENT_VERSION); - std::vector qv; - if (evoDb.Read(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), qv)) { - WITH_LOCK(cs, quorumVvec = std::make_shared>(std::move(qv))); - } else { + if (!evoDb.GetRawDB().ReadDataStream(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s)) { return false; } + size_t vvec_size = ReadCompactSize(s); + CBLSPublicKey pubkey; + std::vector qv; + for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) { + s >> CBLSPublicKeyVersionWrapper(pubkey, false); + qv.emplace_back(pubkey); + } + + LOCK(cs); + quorumVvec = std::make_shared>(std::move(qv)); // We ignore the return value here as it is ok if this fails. If it fails, it usually means that we are not a // member of the quorum but observed the whole DKG process to have the quorum verification vector. - WITH_LOCK(cs, evoDb.Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare)); + evoDb.GetRawDB().Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare); return true; }