Skip to content

Commit

Permalink
feat: Remove outdated quorum data from evodb
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Jul 7, 2023
1 parent 796a4d4 commit 900d0f7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial
}

TriggerQuorumDataRecoveryThreads(pindexNew);
CleanupOldQuorumData(pindexNew);
}

void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex* pindexNew) const
Expand Down Expand Up @@ -971,4 +972,67 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co
});
}

static void DataCleanupHelper(CDBWrapper& db, std::set<uint256> skip_list)
{
const auto prefixes = {DB_QUORUM_QUORUM_VVEC, DB_QUORUM_SK_SHARE};

CDBBatch batch(db);
std::unique_ptr<CDBIterator> pcursor(db.NewIterator());

for (const auto& prefix : prefixes) {
auto start = std::make_tuple(prefix, uint256());
pcursor->Seek(start);

int count{0};
while (pcursor->Valid()) {
decltype(start) k;

if (!pcursor->GetKey(k) || std::get<0>(k) != prefix) {
break;
}

pcursor->Next();

if (skip_list.find(std::get<1>(k)) != skip_list.end()) continue;

++count;
batch.Erase(k);

if (batch.SizeEstimate() >= (1 << 24)) {
db.WriteBatch(batch);
batch.Clear();
}
}

db.WriteBatch(batch);

LogPrint(BCLog::LLMQ, "CQuorumManager::%d -- %s removed %d\n", __func__, prefix, count);
}

pcursor.reset();
db.CompactFull();
}

void CQuorumManager::CleanupOldQuorumData(const CBlockIndex* pIndex) const
{
if (!fMasternodeMode || pIndex == nullptr || (pIndex->nHeight % 576 != 0)) {
return;
}

std::set<uint256> dbKeys;

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

for (const auto& params : Params().GetConsensus().llmqs) {
const auto vecQuorums = ScanQuorums(params.type, pIndex, params.keepOldConnections);
for (const auto& pQuorum : vecQuorums) {
dbKeys.insert(MakeQuorumKey(*pQuorum));
}
}

DataCleanupHelper(m_evoDb.GetRawDB(), dbKeys);

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

} // namespace llmq
2 changes: 2 additions & 0 deletions src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ class CQuorumManager

void StartCachePopulatorThread(const CQuorumCPtr pQuorum) const;
void StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex, uint16_t nDataMask) const;

void CleanupOldQuorumData(const CBlockIndex* pIndex) const;
};

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

0 comments on commit 900d0f7

Please sign in to comment.