From a8e490e108a50d6fa73f4ce4e33ca400ef307d27 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 2 Jul 2020 14:06:33 +0300 Subject: [PATCH] blockchain db migration implemented: 93,94->95 (aliases rebuild) --- src/currency_core/blockchain_storage.cpp | 55 +++++++++++++++++++++++- src/currency_core/currency_config.h | 2 +- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 7c7831d5..7fca4c8c 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -299,12 +299,65 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro if (m_db_blocks.size() != 0) { #ifndef TESTNET - if (m_db_storage_major_compatibility_version == 93 && BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION == 94) + if ((m_db_storage_major_compatibility_version == 93 || m_db_storage_major_compatibility_version == 94) && BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION == 95) + { + // migrate DB to rebuild aliases container + LOG_PRINT_MAGENTA("Migrating DB: " << m_db_storage_major_compatibility_version << " -> " << BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION, LOG_LEVEL_0); + + res = m_db_aliases.deinit(); + CHECK_AND_ASSERT_MES(res, false, "Unable to deinit db container"); + + typedef tools::db::cached_key_value_accessor, true, true> aliases_container_old; + aliases_container_old db_aliases_old(m_db); + res = db_aliases_old.init(BLOCKCHAIN_STORAGE_CONTAINER_ALIASES); + CHECK_AND_ASSERT_MES(res, false, "Unable to init db container"); + + // temporary set db compatibility version to zero during migration in order to trigger db reinit on the next lanunch in case the process stops in the middle + m_db.begin_transaction(); + uint64_t tmp_db_maj_version = m_db_storage_major_compatibility_version; + m_db_storage_major_compatibility_version = 0; + m_db.commit_transaction(); + + typedef std::vector>> tmp_container_t; + tmp_container_t temp_container; + db_aliases_old.enumerate_items([&temp_container](uint64_t i, const std::string& alias, const std::list& alias_entries) + { + std::pair> p(alias, std::list()); + for(auto& entry : alias_entries) + p.second.push_back(static_cast(entry)); // here conversion to the new format goes + temp_container.emplace_back(p); + return true; + }); + + // clear and close old format container + m_db.begin_transaction(); + db_aliases_old.clear(); + m_db.commit_transaction(); + db_aliases_old.deinit(); + + res = m_db_aliases.init(BLOCKCHAIN_STORAGE_CONTAINER_ALIASES); + CHECK_AND_ASSERT_MES(res, false, "Unable to init db container"); + + // re-populate all alias entries back + m_db.begin_transaction(); + for(auto& el : temp_container) + m_db_aliases.set(el.first, el.second); + m_db.commit_transaction(); + + // restore db maj compartibility + m_db.begin_transaction(); + m_db_storage_major_compatibility_version = tmp_db_maj_version; + m_db.commit_transaction(); + + LOG_PRINT_MAGENTA("Migrating DB: successfully done", LOG_LEVEL_0); + } + else if (m_db_storage_major_compatibility_version == 93 && BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION == 94) { // do not reinit db if moving from version 93 to version 94 LOG_PRINT_MAGENTA("DB storage does not need reinit because moving from v93 to v94", LOG_LEVEL_0); } #else + // TESTNET if (m_db_storage_major_compatibility_version == 95 && BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION == 96) { // do not reinit TESTNET db if moving from version 95 to version 96 diff --git a/src/currency_core/currency_config.h b/src/currency_core/currency_config.h index b599c604..958b0c5d 100644 --- a/src/currency_core/currency_config.h +++ b/src/currency_core/currency_config.h @@ -212,7 +212,7 @@ #define CURRENT_TRANSACTION_CHAIN_ENTRY_ARCHIVE_VER 3 #define CURRENT_BLOCK_EXTENDED_INFO_ARCHIVE_VER 1 -#define BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION CURRENCY_FORMATION_VERSION + 10 +#define BLOCKCHAIN_STORAGE_MAJOR_COMPATIBILITY_VERSION CURRENCY_FORMATION_VERSION + 11 #define BLOCKCHAIN_STORAGE_MINOR_COMPATIBILITY_VERSION 1