From 1302beb1c80238c1136986113d9a3bd6179af0b9 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 10 Nov 2023 01:13:27 +0300 Subject: [PATCH] fix: store BLSVerificationVector on disk using basic bls scheme --- src/llmq/dkgsessionmgr.cpp | 23 +++++++++++++++++++---- src/llmq/quorums.cpp | 26 ++++++++++++++++++++------ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/llmq/dkgsessionmgr.cpp b/src/llmq/dkgsessionmgr.cpp index 9cf5439e71e264..3fed1a198a30c7 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,21 @@ 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; + CBLSPublicKeyVersionWrapper pubkey_wrapped(pubkey, false); + std::vector qv; + for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) { + s >> pubkey_wrapped; + qv.emplace_back(pubkey_wrapped.Get()); + } + 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 879eb75701be5a..f409987caf050e 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,26 @@ 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; + CBLSPublicKeyVersionWrapper pubkey_wrapped(pubkey, false); + std::vector qv; + for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) { + s >> pubkey_wrapped; + qv.emplace_back(pubkey_wrapped.Get()); + } + + 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; }