Skip to content

Commit

Permalink
Introduce db version check at runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Mar 11, 2021
1 parent 46832f7 commit dce6ce8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 71 deletions.
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ DEFI_CORE_H = \
masternodes/mn_checks.h \
masternodes/mn_rpc.h \
masternodes/res.h \
masternodes/rewardhistoryold.h \
masternodes/tokens.h \
masternodes/poolpairs.h \
masternodes/undo.h \
Expand Down
3 changes: 3 additions & 0 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class CStorageLevelDB : public CStorageKV {
std::unique_ptr<CStorageKVIterator> NewIterator() override {
return MakeUnique<CStorageLevelDBIterator>(std::unique_ptr<CDBIterator>(db.NewIterator()));
}
bool IsEmpty() {
return db.IsEmpty();
}

private:
CDBWrapper db;
Expand Down
9 changes: 6 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,13 +1586,16 @@ bool AppInitMain(InitInterfaces& interfaces)
pcustomcsDB = MakeUnique<CStorageLevelDB>(GetDataDir() / "enhancedcs", nDefaultDbCache << 20, false, fReset || fReindexChainState);
pcustomcsview.reset();
pcustomcsview = MakeUnique<CCustomCSView>(*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<CAnchorAuthIndex>();
panchorAwaitingConfirms.reset();
Expand Down
25 changes: 0 additions & 25 deletions src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <masternodes/accountshistory.h>
#include <masternodes/accounts.h>
#include <masternodes/masternodes.h>
#include <masternodes/rewardhistoryold.h>
#include <key_io.h>

#include <limits>
Expand All @@ -23,27 +22,3 @@ Res CAccountsHistoryView::SetAccountHistory(const AccountHistoryKey& key, const
WriteBy<ByAccountHistoryKey>(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<AccountHistoryValue>) {
if (key.txn == std::numeric_limits<uint32_t>::max()) {
hasOldAccountHistory = true;
return false;
}
return true;
}, { {}, 0, std::numeric_limits<uint32_t>::max() });
return hasOldAccountHistory;
} catch(...) {
return true;
}
}
3 changes: 0 additions & 3 deletions src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -541,6 +542,19 @@ std::vector<CAnchorConfirmDataPlus> 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())
Expand Down
7 changes: 7 additions & 0 deletions src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
Expand Down
38 changes: 0 additions & 38 deletions src/masternodes/rewardhistoryold.h

This file was deleted.

1 change: 0 additions & 1 deletion src/test/storage_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <key_io.h>
#include <masternodes/accountshistory.h>
#include <masternodes/masternodes.h>
#include <masternodes/rewardhistoryold.h>
#include <rpc/rawtransaction_util.h>
#include <test/setup_common.h>

Expand Down

0 comments on commit dce6ce8

Please sign in to comment.