From 84f8c9661ff64579409174d7a58ef16a0a205833 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Thu, 11 Mar 2021 09:37:12 +0200 Subject: [PATCH] Introduce db version check at runtime Signed-off-by: Anthony Fieroni --- src/flushablestorage.h | 3 +++ src/init.cpp | 9 ++++++--- src/masternodes/accountshistory.cpp | 24 ------------------------ src/masternodes/accountshistory.h | 3 --- src/masternodes/masternodes.cpp | 14 ++++++++++++++ src/masternodes/masternodes.h | 7 +++++++ 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/flushablestorage.h b/src/flushablestorage.h index 631de1065f5..86aac311550 100644 --- a/src/flushablestorage.h +++ b/src/flushablestorage.h @@ -146,6 +146,9 @@ class CStorageLevelDB : public CStorageKV { std::unique_ptr NewIterator() override { return MakeUnique(std::unique_ptr(db.NewIterator())); } + bool IsEmpty() { + return db.IsEmpty(); + } private: CDBWrapper db; diff --git a/src/init.cpp b/src/init.cpp index b16d4a73caf..1fafc5202de 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1586,13 +1586,16 @@ bool AppInitMain(InitInterfaces& interfaces) pcustomcsDB = MakeUnique(GetDataDir() / "enhancedcs", nDefaultDbCache << 20, false, fReset || fReindexChainState); pcustomcsview.reset(); pcustomcsview = MakeUnique(*pcustomcsDB.get()); - if (!fReset && gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) { - if (shouldMigrateOldRewardHistory(*pcustomcsview)) { - strLoadError = _("Account history needs rebuild").translated; + if (!fReset && !fReindexChainState) { + if (!pcustomcsDB->IsEmpty() && pcustomcsview->GetDbVersion() != CCustomCSView::DbVersion) { + strLoadError = _("Account database is unsuitable").translated; break; } } + // Ensure we are on latest DB version + pcustomcsview->SetDbVersion(CCustomCSView::DbVersion); + panchorauths.reset(); panchorauths = MakeUnique(); panchorAwaitingConfirms.reset(); diff --git a/src/masternodes/accountshistory.cpp b/src/masternodes/accountshistory.cpp index 59a8de23c18..9744ffc2c8a 100644 --- a/src/masternodes/accountshistory.cpp +++ b/src/masternodes/accountshistory.cpp @@ -23,27 +23,3 @@ Res CAccountsHistoryView::SetAccountHistory(const AccountHistoryKey& key, const WriteBy(key, value); return Res::Ok(); } - -bool shouldMigrateOldRewardHistory(CCustomCSView & view) -{ - auto it = view.GetRaw().NewIterator(); - try { - auto prefix = oldRewardHistoryPrefix; - auto oldKey = std::make_pair(prefix, oldRewardHistoryKey{}); - it->Seek(DbTypeToBytes(oldKey)); - if (it->Valid() && BytesToDbType(it->Key(), oldKey) && oldKey.first == prefix) { - return true; - } - bool hasOldAccountHistory = false; - view.ForEachAccountHistory([&](AccountHistoryKey const & key, CLazySerialize) { - if (key.txn == std::numeric_limits::max()) { - hasOldAccountHistory = true; - return false; - } - return true; - }, { {}, 0, std::numeric_limits::max() }); - return hasOldAccountHistory; - } catch(...) { - return true; - } -} diff --git a/src/masternodes/accountshistory.h b/src/masternodes/accountshistory.h index ab40475da3a..13005cb7c45 100644 --- a/src/masternodes/accountshistory.h +++ b/src/masternodes/accountshistory.h @@ -64,7 +64,4 @@ class CAccountsHistoryView : public virtual CStorageView static constexpr bool DEFAULT_ACINDEX = true; -class CCustomCSView; -bool shouldMigrateOldRewardHistory(CCustomCSView & view); - #endif //DEFI_MASTERNODES_ACCOUNTSHISTORY_H diff --git a/src/masternodes/masternodes.cpp b/src/masternodes/masternodes.cpp index aded172940d..271e85ee5c3 100644 --- a/src/masternodes/masternodes.cpp +++ b/src/masternodes/masternodes.cpp @@ -26,6 +26,7 @@ const unsigned char DB_MASTERNODES = 'M'; // main masternodes table const unsigned char DB_MN_OPERATORS = 'o'; // masternodes' operators index const unsigned char DB_MN_OWNERS = 'w'; // masternodes' owners index const unsigned char DB_MN_HEIGHT = 'H'; // single record with last processed chain height +const unsigned char DB_MN_VERSION = 'D'; const unsigned char DB_MN_ANCHOR_REWARD = 'r'; const unsigned char DB_MN_ANCHOR_CONFIRM = 'x'; const unsigned char DB_MN_CURRENT_TEAM = 't'; @@ -541,6 +542,19 @@ std::vector CAnchorConfirmsView::GetAnchorConfirmData() /* * CCustomCSView */ +int CCustomCSView::GetDbVersion() const +{ + int version; + if (Read(DB_MN_VERSION, version)) + return version; + return 0; +} + +void CCustomCSView::SetDbVersion(int version) +{ + Write(DB_MN_VERSION, version); +} + CTeamView::CTeam CCustomCSView::CalcNextTeam(const uint256 & stakeModifier) { if (stakeModifier == uint256()) diff --git a/src/masternodes/masternodes.h b/src/masternodes/masternodes.h index 71dada468ab..8dd52e863df 100644 --- a/src/masternodes/masternodes.h +++ b/src/masternodes/masternodes.h @@ -218,6 +218,9 @@ class CCustomCSView , public CAnchorConfirmsView { public: + // Increase version when underlaying tables are changed + static constexpr const int DbVersion = 1; + CCustomCSView() = default; CCustomCSView(CStorageKV & st) @@ -244,6 +247,10 @@ class CCustomCSView bool CalculateOwnerRewards(CScript const & owner, uint32_t height); + void SetDbVersion(int version); + + int GetDbVersion() const; + CStorageKV& GetRaw() { return DB(); }