From ce924278df50faa5064d8473f15cba1b72d1e64c Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Fri, 10 Jan 2020 11:24:21 +0100 Subject: [PATCH] Don't load caches when blocks/chainstate was deleted and also delete old caches (#3280) * Don't load caches when blocks/chainstate was not present * Delete old cache files when we decided to not load them * Make sure cache files are of the exact format we expected them to be, flush empty objects into them instead of deleting files naively * Streamline logic a bit, rename fIgnoreCacheFiles to fLoadCacheFiles Co-authored-by: UdjinM6 --- src/init.cpp | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index fafc4a88b7..be7983b511 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2074,32 +2074,58 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler) // LOAD SERIALIZED DAT FILES INTO DATA CACHES FOR INTERNAL USE - bool fIgnoreCacheFiles = fLiteMode || fReindex || fReindexChainState; - if (!fIgnoreCacheFiles) { - fs::path pathDB = GetDataDir(); - std::string strDBName; + bool fLoadCacheFiles = !(fLiteMode || fReindex || fReindexChainState); + { + LOCK(cs_main); + // was blocks/chainstate deleted? + if (chainActive.Tip() == nullptr) { + fLoadCacheFiles = false; + } + } + fs::path pathDB = GetDataDir(); + std::string strDBName; - strDBName = "mncache.dat"; - uiInterface.InitMessage(_("Loading masternode cache...")); - CFlatDB flatdb1(strDBName, "magicMasternodeCache"); + strDBName = "mncache.dat"; + uiInterface.InitMessage(_("Loading masternode cache...")); + CFlatDB flatdb1(strDBName, "magicMasternodeCache"); + if (fLoadCacheFiles) { if(!flatdb1.Load(mmetaman)) { return InitError(_("Failed to load masternode cache from") + "\n" + (pathDB / strDBName).string()); } + } else { + CMasternodeMetaMan mmetamanTmp; + if(!flatdb1.Dump(mmetamanTmp)) { + return InitError(_("Failed to clear masternode cache at") + "\n" + (pathDB / strDBName).string()); + } + } - strDBName = "governance.dat"; - uiInterface.InitMessage(_("Loading governance cache...")); - CFlatDB flatdb3(strDBName, "magicGovernanceCache"); + strDBName = "governance.dat"; + uiInterface.InitMessage(_("Loading governance cache...")); + CFlatDB flatdb3(strDBName, "magicGovernanceCache"); + if (fLoadCacheFiles) { if(!flatdb3.Load(governance)) { return InitError(_("Failed to load governance cache from") + "\n" + (pathDB / strDBName).string()); } governance.InitOnLoad(); + } else { + CGovernanceManager governanceTmp; + if(!flatdb3.Dump(governanceTmp)) { + return InitError(_("Failed to clear governance cache at") + "\n" + (pathDB / strDBName).string()); + } + } - strDBName = "netfulfilled.dat"; - uiInterface.InitMessage(_("Loading fulfilled requests cache...")); - CFlatDB flatdb4(strDBName, "magicFulfilledCache"); + strDBName = "netfulfilled.dat"; + uiInterface.InitMessage(_("Loading fulfilled requests cache...")); + CFlatDB flatdb4(strDBName, "magicFulfilledCache"); + if (fLoadCacheFiles) { if(!flatdb4.Load(netfulfilledman)) { return InitError(_("Failed to load fulfilled requests cache from") + "\n" + (pathDB / strDBName).string()); } + } else { + CNetFulfilledRequestManager netfulfilledmanTmp; + if(!flatdb4.Dump(netfulfilledmanTmp)) { + return InitError(_("Failed to clear fulfilled requests cache at") + "\n" + (pathDB / strDBName).string()); + } } // ********************************************************* Step 10c: schedule Dash-specific tasks