Skip to content

Commit 13c1240

Browse files
authored
fix: actually erase old evodb data on db migration (#5570)
## Issue being fixed or feature implemented The old evodb data wasn't dropped on db migration really. `Erase(<db_key_prefix>)` does nothing. ## What was done? Loop through the old data and drop it in batches per db key. Do this both for nodes that are doing migration for the first time and for nodes that did migration in the past already. ## How Has This Been Tested? Running different versions on testnet ``` # reindex with 18.2.2 till block 850000 (pre-v19 block) $ du -hd1 ~/.dashcore/testnet3/evodb/ 276M .dashcore/testnet3/evodb # continue with develop, migration just finished, keep syncing till current tip, block 901000+ $ du -hd1 ~/.dashcore/testnet3/evodb/ 469M .dashcore/testnet3/evodb # continue with this PR, start at current tip, "migration already done. cleaned old data." $ du -hd1 ~/.dashcore/testnet3/evodb/ 302M .dashcore/testnet3/evodb ``` ## Breaking Changes Should be none. ## 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 fa64c64 commit 13c1240

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

src/evo/deterministicmns.cpp

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,32 @@ void CDeterministicMNManager::CleanupCache(int nHeight)
11811181

11821182
}
11831183

1184+
[[nodiscard]] static bool EraseOldDBData(CDBWrapper& db, const std::vector<std::string>& db_key_prefixes)
1185+
{
1186+
bool erased{false};
1187+
for(const auto& db_key_prefix : db_key_prefixes) {
1188+
CDBBatch batch{db};
1189+
std::unique_ptr<CDBIterator> it{db.NewIterator()};
1190+
std::pair firstKey{db_key_prefix, uint256()};
1191+
it->Seek(firstKey);
1192+
while (it->Valid()) {
1193+
decltype(firstKey) curKey;
1194+
if (!it->GetKey(curKey) || std::get<0>(curKey) != db_key_prefix) {
1195+
break;
1196+
}
1197+
batch.Erase(curKey);
1198+
erased = true;
1199+
it->Next();
1200+
}
1201+
if (erased) {
1202+
LogPrintf("CDeterministicMNManager::%s -- updating db...\n", __func__);
1203+
db.WriteBatch(batch);
1204+
LogPrintf("CDeterministicMNManager::%s -- done cleaning old data for %s\n", __func__, db_key_prefix);
1205+
}
1206+
}
1207+
return erased;
1208+
}
1209+
11841210
bool CDeterministicMNManager::MigrateDBIfNeeded()
11851211
{
11861212
static const std::string DB_OLD_LIST_SNAPSHOT = "dmn_S";
@@ -1199,7 +1225,19 @@ bool CDeterministicMNManager::MigrateDBIfNeeded()
11991225
}
12001226

12011227
if (m_evoDb.GetRawDB().Exists(EVODB_BEST_BLOCK) || m_evoDb.GetRawDB().Exists(DB_OLD_BEST_BLOCK2)) {
1202-
LogPrintf("CDeterministicMNManager::%s -- migration already done. skipping.\n", __func__);
1228+
if (EraseOldDBData(m_evoDb.GetRawDB(), {DB_OLD_LIST_DIFF, DB_OLD_LIST_SNAPSHOT})) {
1229+
// we messed up, make sure this time we actually drop old data
1230+
LogPrintf("CDeterministicMNManager::%s -- migration already done. cleaned old data.\n", __func__);
1231+
m_evoDb.GetRawDB().CompactFull();
1232+
LogPrintf("CDeterministicMNManager::%s -- done compacting database\n", __func__);
1233+
// flush it to disk
1234+
if (!m_evoDb.CommitRootTransaction()) {
1235+
LogPrintf("CDeterministicMNManager::%s -- failed to commit to evoDB\n", __func__);
1236+
return false;
1237+
}
1238+
} else {
1239+
LogPrintf("CDeterministicMNManager::%s -- migration already done. skipping.\n", __func__);
1240+
}
12031241
return true;
12041242
}
12051243

@@ -1263,10 +1301,9 @@ bool CDeterministicMNManager::MigrateDBIfNeeded()
12631301

12641302
LogPrintf("CDeterministicMNManager::%s -- done migrating\n", __func__);
12651303

1266-
m_evoDb.GetRawDB().Erase(DB_OLD_LIST_DIFF);
1267-
m_evoDb.GetRawDB().Erase(DB_OLD_LIST_SNAPSHOT);
1268-
1269-
LogPrintf("CDeterministicMNManager::%s -- done cleaning old data\n", __func__);
1304+
if (EraseOldDBData(m_evoDb.GetRawDB(), {DB_OLD_LIST_DIFF, DB_OLD_LIST_SNAPSHOT})) {
1305+
LogPrintf("CDeterministicMNManager::%s -- done cleaning old data\n", __func__);
1306+
}
12701307

12711308
m_evoDb.GetRawDB().CompactFull();
12721309

@@ -1298,7 +1335,19 @@ bool CDeterministicMNManager::MigrateDBIfNeeded2()
12981335
}
12991336

13001337
if (m_evoDb.GetRawDB().Exists(EVODB_BEST_BLOCK)) {
1301-
LogPrintf("CDeterministicMNManager::%s -- migration already done. skipping.\n", __func__);
1338+
if (EraseOldDBData(m_evoDb.GetRawDB(), {DB_OLD_LIST_DIFF, DB_OLD_LIST_SNAPSHOT})) {
1339+
// we messed up, make sure this time we actually drop old data
1340+
LogPrintf("CDeterministicMNManager::%s -- migration already done. cleaned old data.\n", __func__);
1341+
m_evoDb.GetRawDB().CompactFull();
1342+
LogPrintf("CDeterministicMNManager::%s -- done compacting database\n", __func__);
1343+
// flush it to disk
1344+
if (!m_evoDb.CommitRootTransaction()) {
1345+
LogPrintf("CDeterministicMNManager::%s -- failed to commit to evoDB\n", __func__);
1346+
return false;
1347+
}
1348+
} else {
1349+
LogPrintf("CDeterministicMNManager::%s -- migration already done. skipping.\n", __func__);
1350+
}
13021351
return true;
13031352
}
13041353

@@ -1362,10 +1411,9 @@ bool CDeterministicMNManager::MigrateDBIfNeeded2()
13621411

13631412
LogPrintf("CDeterministicMNManager::%s -- done migrating\n", __func__);
13641413

1365-
m_evoDb.GetRawDB().Erase(DB_OLD_LIST_DIFF);
1366-
m_evoDb.GetRawDB().Erase(DB_OLD_LIST_SNAPSHOT);
1367-
1368-
LogPrintf("CDeterministicMNManager::%s -- done cleaning old data\n", __func__);
1414+
if (EraseOldDBData(m_evoDb.GetRawDB(), {DB_OLD_LIST_DIFF, DB_OLD_LIST_SNAPSHOT})) {
1415+
LogPrintf("CDeterministicMNManager::%s -- done cleaning old data\n", __func__);
1416+
}
13691417

13701418
m_evoDb.GetRawDB().CompactFull();
13711419

0 commit comments

Comments
 (0)