Skip to content

Commit a7af16a

Browse files
committed
Better logic to determine if an upgrade is needed
Reuse the "best block" logic to figure out if an upgrade is needed. Also use it to ensure that older nodes are unable to start after the upgrade was performed.
1 parent b94a8aa commit a7af16a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/evo/deterministicmns.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
static const std::string DB_LIST_SNAPSHOT = "dmn_S";
2222
static const std::string DB_LIST_DIFF = "dmn_D";
23-
static const std::string DB_DMN_MGR_UPGRADED = "dmn_upgraded";
2423

2524
CDeterministicMNManager* deterministicMNManager;
2625

@@ -1046,13 +1045,27 @@ void CDeterministicMNManager::UpgradeDBIfNeeded()
10461045
{
10471046
LOCK(cs_main);
10481047

1049-
if (evoDb.GetRawDB().Exists(std::string(DB_DMN_MGR_UPGRADED))) {
1048+
if (chainActive.Tip() == nullptr) {
10501049
return;
10511050
}
10521051

1052+
if (evoDb.GetRawDB().Exists(EVODB_BEST_BLOCK)) {
1053+
return;
1054+
}
1055+
1056+
// Removing the old EVODB_BEST_BLOCK value early results in older version to crash immediately, even if the upgrade
1057+
// process is cancelled in-between. But if the new version sees that the old EVODB_BEST_BLOCK is already removed,
1058+
// then we must assume that the upgrade process was already running before but was interrupted.
1059+
if (chainActive.Height() > 1 && !evoDb.GetRawDB().Exists(std::string("b_b"))) {
1060+
LogPrintf("CDeterministicMNManager::%s -- ERROR, upgrade process was interrupted and can't be continued. You need to reindex now.\n", __func__);
1061+
}
1062+
evoDb.GetRawDB().Erase(std::string("b_b"));
1063+
10531064
if (chainActive.Height() < Params().GetConsensus().DIP0003Height) {
10541065
// not reached DIP3 height yet, so no upgrade needed
1055-
evoDb.GetRawDB().Write(std::string(DB_DMN_MGR_UPGRADED), (uint8_t)1);
1066+
auto dbTx = evoDb.BeginTransaction();
1067+
evoDb.WriteBestBlock(chainActive.Tip()->GetBlockHash());
1068+
dbTx->Commit();
10561069
return;
10571070
}
10581071

@@ -1083,6 +1096,10 @@ void CDeterministicMNManager::UpgradeDBIfNeeded()
10831096

10841097
LogPrintf("CDeterministicMNManager::%s -- done upgrading\n", __func__);
10851098

1086-
evoDb.GetRawDB().Write(std::string(DB_DMN_MGR_UPGRADED), (uint8_t)1);
1099+
// Writing EVODB_BEST_BLOCK (which is b_b2 now) marks the DB as upgraded
1100+
auto dbTx = evoDb.BeginTransaction();
1101+
evoDb.WriteBestBlock(chainActive.Tip()->GetBlockHash());
1102+
dbTx->Commit();
1103+
10871104
evoDb.GetRawDB().CompactFull();
10881105
}

src/evo/evodb.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "sync.h"
1010
#include "uint256.h"
1111

12-
static const std::string EVODB_BEST_BLOCK = "b_b";
12+
// "b_b" was used in the initial version of deterministic MN storage
13+
// "b_b2" was used after compact diffs were introduced
14+
static const std::string EVODB_BEST_BLOCK = "b_b2";
1315

1416
class CEvoDB
1517
{

0 commit comments

Comments
 (0)