Skip to content

Commit

Permalink
feat: Split quorum contribution db out of evodb
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Nov 14, 2023
1 parent c96addf commit fd02d2f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/llmq/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LLMQContext::LLMQContext(CChainState& chainstate, CConnman& connman, CEvoDB& evo
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainstate, connman, *dkg_debugman, *quorum_block_processor, sporkman, peerman, unit_tests, wipe)},
qman{[&]() -> llmq::CQuorumManager* const {
assert(llmq::quorumManager == nullptr);
llmq::quorumManager = std::make_unique<llmq::CQuorumManager>(*bls_worker, chainstate, connman, *qdkgsman, evo_db, *quorum_block_processor, ::masternodeSync, peerman);
llmq::quorumManager = std::make_unique<llmq::CQuorumManager>(*bls_worker, chainstate, connman, *qdkgsman, evo_db, *quorum_block_processor, ::masternodeSync, peerman, unit_tests, wipe);
return llmq::quorumManager.get();
}()},
sigman{std::make_unique<llmq::CSigningManager>(connman, *llmq::quorumManager, peerman, unit_tests, wipe)},
Expand Down
43 changes: 31 additions & 12 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ int CQuorum::GetMemberIndex(const uint256& proTxHash) const
return -1;
}

void CQuorum::WriteContributions(CEvoDB& evoDb) const
void CQuorum::WriteContributions(const std::unique_ptr<CDBWrapper>& db) const
{
uint256 dbKey = MakeQuorumKey(*this);

Expand All @@ -168,19 +168,19 @@ void CQuorum::WriteContributions(CEvoDB& evoDb) const
for (auto& pubkey : *quorumVvec) {
s << CBLSPublicKeyVersionWrapper(pubkey, false);
}
evoDb.GetRawDB().Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s);
db->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);
db->Write(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
}
}

bool CQuorum::ReadContributions(CEvoDB& evoDb)
bool CQuorum::ReadContributions(const std::unique_ptr<CDBWrapper>& db)
{
uint256 dbKey = MakeQuorumKey(*this);
CDataStream s(SER_DISK, CLIENT_VERSION);

if (!evoDb.GetRawDB().ReadDataStream(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s)) {
if (!db->ReadDataStream(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s)) {
return false;
}

Expand All @@ -197,19 +197,19 @@ bool CQuorum::ReadContributions(CEvoDB& evoDb)
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.
evoDb.GetRawDB().Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
db->Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);

return true;
}

CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGSessionManager& _dkgManager,
CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const std::unique_ptr<CMasternodeSync>& mn_sync,
const std::unique_ptr<PeerManager>& peerman) :
const std::unique_ptr<PeerManager>& peerman, bool unit_tests, bool wipe) :
db(std::make_unique<CDBWrapper>(unit_tests ? "" : (GetDataDir() / "llmq/quorumdb"), 1 << 20, unit_tests, wipe)),
blsWorker(_blsWorker),
m_chainstate(chainstate),
connman(_connman),
dkgManager(_dkgManager),
m_evoDb(_evoDb),
quorumBlockProcessor(_quorumBlockProcessor),
m_mn_sync(mn_sync),
m_peerman(peerman)
Expand All @@ -218,6 +218,7 @@ CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate,
utils::InitQuorumsCache(scanQuorumsCache);

quorumThreadInterrupt.reset();
EraseOldQuorumDB(_evoDb);
}

void CQuorumManager::Start()
Expand Down Expand Up @@ -397,11 +398,11 @@ CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType l
quorum->Init(std::move(qc), pQuorumBaseBlockIndex, minedBlockHash, members);

bool hasValidVvec = false;
if (quorum->ReadContributions(m_evoDb)) {
if (WITH_LOCK(cs_db, return quorum->ReadContributions(db))) {
hasValidVvec = true;
} else {
if (BuildQuorumContributions(quorum->qc, quorum)) {
quorum->WriteContributions(m_evoDb);
WITH_LOCK(cs_db, quorum->WriteContributions(db));
hasValidVvec = true;
} else {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] quorumIndex[%d] quorum.ReadContributions and BuildQuorumContributions for quorumHash[%s] failed\n", __func__, ToUnderlying(llmqType), quorum->qc->quorumIndex, quorum->qc->quorumHash.ToString());
Expand Down Expand Up @@ -821,7 +822,7 @@ void CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, C
return;
}
}
pQuorum->WriteContributions(m_evoDb);
WITH_LOCK(cs_db, pQuorum->WriteContributions(db));
return;
}
}
Expand Down Expand Up @@ -1034,7 +1035,25 @@ void CQuorumManager::CleanupOldQuorumData(const CBlockIndex* pIndex) const
}
}

DataCleanupHelper(m_evoDb.GetRawDB(), dbKeysToSkip);
LOCK(cs_db);
DataCleanupHelper(*db, dbKeysToSkip);

LogPrint(BCLog::LLMQ, "CQuorumManager::%d -- done\n", __func__);
}

void CQuorumManager::EraseOldQuorumDB(CEvoDB& evoDb) const
{
// NOTE: We do not migrate old data here because we have no idea
// which bls scheme was used to store it originally. This is ok
// cause the data can be re-requested from other nodes, see
// TriggerQuorumDataRecoveryThreads.

if (WITH_LOCK(cs_db, return !db->IsEmpty())) return;

LogPrint(BCLog::LLMQ, "CQuorumManager::%d -- start\n", __func__);

DataCleanupHelper(evoDb.GetRawDB(), {});
evoDb.CommitRootTransaction();

LogPrint(BCLog::LLMQ, "CQuorumManager::%d -- done\n", __func__);
}
Expand Down
11 changes: 7 additions & 4 deletions src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ class CQuorum
CBLSSecretKey GetSkShare() const;

private:
void WriteContributions(CEvoDB& evoDb) const;
bool ReadContributions(CEvoDB& evoDb);
void WriteContributions(const std::unique_ptr<CDBWrapper>& db) const;
bool ReadContributions(const std::unique_ptr<CDBWrapper>& db);
};

/**
Expand All @@ -218,11 +218,13 @@ class CQuorum
class CQuorumManager
{
private:
mutable Mutex cs_db;
std::unique_ptr<CDBWrapper> db GUARDED_BY(cs_db) {nullptr};

CBLSWorker& blsWorker;
CChainState& m_chainstate;
CConnman& connman;
CDKGSessionManager& dkgManager;
CEvoDB& m_evoDb;
CQuorumBlockProcessor& quorumBlockProcessor;
const std::unique_ptr<CMasternodeSync>& m_mn_sync;
const std::unique_ptr<PeerManager>& m_peerman;
Expand All @@ -238,7 +240,7 @@ class CQuorumManager
public:
CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGSessionManager& _dkgManager,
CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const std::unique_ptr<CMasternodeSync>& mn_sync,
const std::unique_ptr<PeerManager>& peerman);
const std::unique_ptr<PeerManager>& peerman, bool unit_tests, bool wipe);
~CQuorumManager() { Stop(); };

void Start();
Expand Down Expand Up @@ -278,6 +280,7 @@ class CQuorumManager
void StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex, uint16_t nDataMask) const;

void CleanupOldQuorumData(const CBlockIndex* pIndex) const;
void EraseOldQuorumDB(CEvoDB& evoDb) const;
};

extern std::unique_ptr<CQuorumManager> quorumManager;
Expand Down

0 comments on commit fd02d2f

Please sign in to comment.