Skip to content

Commit f3f4c16

Browse files
authored
fix: Store BLSVerificationVector on disk using basic bls scheme (dashpay#5480)
## Issue being fixed or feature implemented Shouldn't change the way data is stored on mainnet/testnet nodes since they use basic bls scheme anyway now. For devnets/regtest (which activate v19 again and again) this patch should fix potential issues reading pre-fork data right after the fork. ## What was done? Pls see individual commits ## How Has This Been Tested? Run tests, run a node on testnet ## Breaking Changes n/a ## Checklist: - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_
1 parent 3a2bcb1 commit f3f4c16

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/llmq/dkgsessionmgr.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,12 @@ bool CDKGSessionManager::GetPrematureCommitment(const uint256& hash, CDKGPrematu
378378

379379
void CDKGSessionManager::WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec)
380380
{
381-
db->Write(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), *vvec);
381+
CDataStream s(SER_DISK, CLIENT_VERSION);
382+
WriteCompactSize(s, vvec->size());
383+
for (auto& pubkey : *vvec) {
384+
s << CBLSPublicKeyVersionWrapper(pubkey, false);
385+
}
386+
db->Write(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), s);
382387
}
383388

384389
void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const CBLSSecretKey& skContribution)
@@ -408,11 +413,20 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType,
408413
ContributionsCacheKey cacheKey = {llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash};
409414
auto it = contributionsCache.find(cacheKey);
410415
if (it == contributionsCache.end()) {
411-
auto vvecPtr = std::make_shared<std::vector<CBLSPublicKey>>();
412-
CBLSSecretKey skContribution;
413-
if (!db->Read(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), *vvecPtr)) {
416+
CDataStream s(SER_DISK, CLIENT_VERSION);
417+
if (!db->ReadDataStream(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), s)) {
414418
return false;
415419
}
420+
size_t vvec_size = ReadCompactSize(s);
421+
CBLSPublicKey pubkey;
422+
std::vector<CBLSPublicKey> qv;
423+
for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) {
424+
s >> CBLSPublicKeyVersionWrapper(pubkey, false);
425+
qv.emplace_back(pubkey);
426+
}
427+
auto vvecPtr = std::make_shared<std::vector<CBLSPublicKey>>(std::move(qv));
428+
429+
CBLSSecretKey skContribution;
416430
db->Read(std::make_tuple(DB_SKCONTRIB, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), skContribution);
417431

418432
it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first;

src/llmq/quorums.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ void CQuorum::WriteContributions(CEvoDB& evoDb) const
164164

165165
LOCK(cs);
166166
if (HasVerificationVector()) {
167-
evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), *quorumVvec);
167+
CDataStream s(SER_DISK, CLIENT_VERSION);
168+
WriteCompactSize(s, quorumVvec->size());
169+
for (auto& pubkey : *quorumVvec) {
170+
s << CBLSPublicKeyVersionWrapper(pubkey, false);
171+
}
172+
evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s);
168173
}
169174
if (skShare.IsValid()) {
170175
evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
@@ -174,17 +179,25 @@ void CQuorum::WriteContributions(CEvoDB& evoDb) const
174179
bool CQuorum::ReadContributions(CEvoDB& evoDb)
175180
{
176181
uint256 dbKey = MakeQuorumKey(*this);
182+
CDataStream s(SER_DISK, CLIENT_VERSION);
177183

178-
std::vector<CBLSPublicKey> qv;
179-
if (evoDb.Read(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), qv)) {
180-
WITH_LOCK(cs, quorumVvec = std::make_shared<std::vector<CBLSPublicKey>>(std::move(qv)));
181-
} else {
184+
if (!evoDb.GetRawDB().ReadDataStream(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s)) {
182185
return false;
183186
}
184187

188+
size_t vvec_size = ReadCompactSize(s);
189+
CBLSPublicKey pubkey;
190+
std::vector<CBLSPublicKey> qv;
191+
for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) {
192+
s >> CBLSPublicKeyVersionWrapper(pubkey, false);
193+
qv.emplace_back(pubkey);
194+
}
195+
196+
LOCK(cs);
197+
quorumVvec = std::make_shared<std::vector<CBLSPublicKey>>(std::move(qv));
185198
// We ignore the return value here as it is ok if this fails. If it fails, it usually means that we are not a
186199
// member of the quorum but observed the whole DKG process to have the quorum verification vector.
187-
WITH_LOCK(cs, evoDb.Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare));
200+
evoDb.GetRawDB().Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
188201

189202
return true;
190203
}

0 commit comments

Comments
 (0)