|
20 | 20 |
|
21 | 21 | static const std::string DB_LIST_SNAPSHOT = "dmn_S"; |
22 | 22 | static const std::string DB_LIST_DIFF = "dmn_D"; |
23 | | -static const std::string DB_DMN_MGR_UPGRADED = "dmn_upgraded"; |
24 | 23 |
|
25 | 24 | CDeterministicMNManager* deterministicMNManager; |
26 | 25 |
|
@@ -1046,13 +1045,27 @@ void CDeterministicMNManager::UpgradeDBIfNeeded() |
1046 | 1045 | { |
1047 | 1046 | LOCK(cs_main); |
1048 | 1047 |
|
1049 | | - if (evoDb.GetRawDB().Exists(std::string(DB_DMN_MGR_UPGRADED))) { |
| 1048 | + if (chainActive.Tip() == nullptr) { |
1050 | 1049 | return; |
1051 | 1050 | } |
1052 | 1051 |
|
| 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 | + |
1053 | 1064 | if (chainActive.Height() < Params().GetConsensus().DIP0003Height) { |
1054 | 1065 | // 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(); |
1056 | 1069 | return; |
1057 | 1070 | } |
1058 | 1071 |
|
@@ -1083,6 +1096,10 @@ void CDeterministicMNManager::UpgradeDBIfNeeded() |
1083 | 1096 |
|
1084 | 1097 | LogPrintf("CDeterministicMNManager::%s -- done upgrading\n", __func__); |
1085 | 1098 |
|
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 | + |
1087 | 1104 | evoDb.GetRawDB().CompactFull(); |
1088 | 1105 | } |
0 commit comments