Skip to content

Commit

Permalink
feat: Remove outdated quorum data from evodb (#5576)
Browse files Browse the repository at this point in the history
## Issue being fixed or feature implemented
Grabbed this from #5480. 

## What was done?
Cleans quorum data from evoDB for old quorums.

## How Has This Been Tested?


## Breaking Changes


## 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)_

Co-authored-by: UdjinM6 <[email protected]>
  • Loading branch information
ogabrielides and UdjinM6 authored Sep 19, 2023
1 parent 135be62 commit 19aa3ab
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 @@ -295,6 +295,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 @@ -958,4 +959,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 @@ -274,6 +274,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 19aa3ab

Please sign in to comment.