Skip to content

Commit

Permalink
fix: store BLSVerificationVector on disk using basic bls scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Dec 6, 2023
1 parent 792b9ce commit 1302beb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
23 changes: 19 additions & 4 deletions src/llmq/dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<std::vector<CBLSPublicKey>>();
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<CBLSPublicKey> 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::vector<CBLSPublicKey>>(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;
Expand Down
26 changes: 20 additions & 6 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<CBLSPublicKey> qv;
if (evoDb.Read(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), qv)) {
WITH_LOCK(cs, quorumVvec = std::make_shared<std::vector<CBLSPublicKey>>(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<CBLSPublicKey> 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::vector<CBLSPublicKey>>(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;
}
Expand Down

0 comments on commit 1302beb

Please sign in to comment.