From 0cdf9aac3b7d21623e45f8b4de0f224b4b18c9bd Mon Sep 17 00:00:00 2001 From: wezrule Date: Fri, 6 Dec 2019 13:02:19 +0000 Subject: [PATCH] Remove representation table during upgrade --- nano/core_test/block_store.cpp | 41 ++++++++++++++++++++++++++---- nano/node/lmdb/lmdb.cpp | 21 +++++++++++++-- nano/node/lmdb/lmdb.hpp | 1 + nano/secure/blockstore_partial.hpp | 2 +- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 98005ce50c..b23f0ed1ee 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1698,11 +1698,6 @@ TEST (mdb_block_store, upgrade_v14_v15) ASSERT_FALSE (store.confirmation_height_get (transaction, nano::genesis_account, confirmation_height)); ASSERT_EQ (confirmation_height, 1); - // The representation table should be deleted - auto error_get_representation (mdb_get (store.env.tx (transaction), store.representation, nano::mdb_val (nano::genesis_account), value)); - ASSERT_NE (error_get_representation, MDB_SUCCESS); - ASSERT_EQ (store.representation, 0); - // accounts_v1, state_blocks_v1 & pending_v1 tables should be deleted auto error_get_accounts_v1 (mdb_get (store.env.tx (transaction), store.accounts_v1, nano::mdb_val (nano::genesis_account), value)); ASSERT_NE (error_get_accounts_v1, MDB_SUCCESS); @@ -1731,6 +1726,42 @@ TEST (mdb_block_store, upgrade_v14_v15) ASSERT_LT (14, store.version_get (transaction)); } +TEST (mdb_block_store, upgrade_v15_v16) +{ + auto path (nano::unique_path ()); + nano::mdb_val value; + { + nano::genesis genesis; + nano::logger_mt logger; + nano::mdb_store store (logger, path); + nano::stat stats; + nano::ledger ledger (store, stats); + auto transaction (store.tx_begin_write ()); + store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache); + // The representation table should get removed after, so readd it so that we can later confirm this actually happens + auto txn = store.env.tx (transaction); + ASSERT_FALSE (mdb_dbi_open (txn, "representation", MDB_CREATE, &store.representation)); + auto weight = ledger.rep_weights.representation_get (nano::genesis_account); + ASSERT_EQ (MDB_SUCCESS, mdb_put (txn, store.representation, nano::mdb_val (nano::genesis_account), nano::mdb_val (nano::uint128_union (weight)), 0)); + // Lower the database to the previous version + store.version_put (transaction, 15); + // Confirm the rep weight exists in the database + ASSERT_EQ (MDB_SUCCESS, mdb_get (store.env.tx (transaction), store.representation, nano::mdb_val (nano::genesis_account), value)); + } + + // Now do the upgrade + nano::logger_mt logger; + auto error (false); + nano::mdb_store store (logger, path); + ASSERT_FALSE (error); + auto transaction (store.tx_begin_read ()); + + // The representation table should now be deleted + auto error_get_representation (mdb_get (store.env.tx (transaction), store.representation, nano::mdb_val (nano::genesis_account), value)); + ASSERT_NE (MDB_SUCCESS, error_get_representation); + ASSERT_EQ (store.representation, 0); +} + TEST (mdb_block_store, upgrade_backup) { auto dir (nano::unique_path ()); diff --git a/nano/node/lmdb/lmdb.cpp b/nano/node/lmdb/lmdb.cpp index 2d7076794a..14b2e3fba2 100644 --- a/nano/node/lmdb/lmdb.cpp +++ b/nano/node/lmdb/lmdb.cpp @@ -176,6 +176,7 @@ void nano::mdb_store::open_databases (bool & error_a, nano::transaction const & error_a |= mdb_dbi_open (env.tx (transaction_a), "confirmation_height", flags, &confirmation_height) != 0; if (!full_sideband (transaction_a)) { + // The blocks_info database is no longer used, but need opening so that it can be deleted during an upgrade error_a |= mdb_dbi_open (env.tx (transaction_a), "blocks_info", flags, &blocks_info) != 0; } error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts", flags, &accounts_v0) != 0; @@ -183,8 +184,15 @@ void nano::mdb_store::open_databases (bool & error_a, nano::transaction const & error_a |= mdb_dbi_open (env.tx (transaction_a), "pending", flags, &pending_v0) != 0; pending = pending_v0; + if (version_get (transaction_a) < 16) + { + // The representation database is no longer used, but need opening so that it can be deleted during an upgrade + error_a |= mdb_dbi_open (env.tx (transaction_a), "representation", flags, &representation) != 0; + } + if (version_get (transaction_a) < 15) { + // These databases are no longer used, but need opening so they can be deleted during an upgrade error_a |= mdb_dbi_open (env.tx (transaction_a), "state", flags, &state_blocks_v0) != 0; state_blocks = state_blocks_v0; error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts_v1", flags, &accounts_v1) != 0; @@ -233,6 +241,8 @@ bool nano::mdb_store::do_upgrades (nano::write_transaction & transaction_a, bool upgrade_v14_to_v15 (transaction_a); needs_vacuuming = true; case 15: + upgrade_v15_to_v16 (transaction_a); + case 16: break; default: logger.always_log (boost::str (boost::format ("The version of the ledger (%1%) is too high for this node") % version_l)); @@ -662,15 +672,22 @@ void nano::mdb_store::upgrade_v14_to_v15 (nano::write_transaction & transaction_ mdb_put (env.tx (transaction_a), pending, nano::mdb_val (pending_key_pending_info_pair.first), nano::mdb_val (pending_key_pending_info_pair.second), MDB_APPEND); } + version_put (transaction_a, 15); + logger.always_log ("Finished epoch merge upgrade. Preparing vacuum..."); +} + +void nano::mdb_store::upgrade_v15_to_v16 (nano::write_transaction & transaction_a) +{ // Representation table is no longer used + assert (representation != 0); if (representation != 0) { auto status (mdb_drop (env.tx (transaction_a), representation, 1)); release_assert (status == MDB_SUCCESS); representation = 0; } - version_put (transaction_a, 15); - logger.always_log ("Finished epoch merge upgrade. Preparing vacuum..."); + + version_put (transaction_a, 16); } /** Takes a filepath, appends '_backup_' to the end (but before any extension) and saves that file in the same directory */ diff --git a/nano/node/lmdb/lmdb.hpp b/nano/node/lmdb/lmdb.hpp index bba263ce8f..5cb92dabc6 100644 --- a/nano/node/lmdb/lmdb.hpp +++ b/nano/node/lmdb/lmdb.hpp @@ -239,6 +239,7 @@ class mdb_store : public block_store_partial void upgrade_v12_to_v13 (nano::write_transaction &, size_t); void upgrade_v13_to_v14 (nano::write_transaction const &); void upgrade_v14_to_v15 (nano::write_transaction &); + void upgrade_v15_to_v16 (nano::write_transaction &); void open_databases (bool &, nano::transaction const &, unsigned); int drop (nano::write_transaction const & transaction_a, tables table_a) override; diff --git a/nano/secure/blockstore_partial.hpp b/nano/secure/blockstore_partial.hpp index 4e8ea84377..b98dbe3dae 100644 --- a/nano/secure/blockstore_partial.hpp +++ b/nano/secure/blockstore_partial.hpp @@ -770,7 +770,7 @@ class block_store_partial : public block_store nano::network_params network_params; std::unordered_map> vote_cache_l1; std::unordered_map> vote_cache_l2; - static int constexpr version{ 15 }; + static int constexpr version{ 16 }; template std::shared_ptr block_random (nano::transaction const & transaction_a, tables table_a)