Add LMDB upgrade to v22: Remove unchecked table

In pull request #4021 the binary representation of `unchecked_info` was
changed. We have to clear the unchecked table, because the new binary
format is not compatible with the old one.

This commit only clears the unchecked table for the LMDB implementation.
The RocksDB implementation doesn't have an upgrade mechanism yet.

- Delete the unchecked DB from the LMDB environment
- Simplify the unchecked handle
This commit is contained in:
Gustav Schauwecker 2023-02-15 11:48:12 +01:00 committed by Thiago Silva
commit c7d77ed836
No known key found for this signature in database
GPG key ID: 034303EB8F453169
6 changed files with 53 additions and 25 deletions

View file

@ -2177,6 +2177,42 @@ namespace lmdb
auto transaction (store.tx_begin_read ());
ASSERT_LT (19, store.version.get (transaction));
}
TEST (mdb_block_store, upgrade_v21_v22)
{
if (nano::rocksdb_config::using_rocksdb_in_tests ())
{
// Don't test this in rocksdb mode
GTEST_SKIP ();
}
auto path (nano::unique_path ());
nano::logger_mt logger;
nano::stats stats;
auto const check_correct_state = [&] () {
nano::lmdb::store store (logger, path, nano::dev::constants);
auto transaction (store.tx_begin_write ());
ASSERT_EQ (store.version.get (transaction), store.version_current);
MDB_dbi unchecked_handle{ 0 };
ASSERT_EQ (MDB_NOTFOUND, mdb_dbi_open (store.env.tx (transaction), "unchecked", 0, &unchecked_handle));
};
// Testing current version doesn't contain the unchecked table
check_correct_state ();
// Setting the database to its 21st version state
{
nano::lmdb::store store (logger, path, nano::dev::constants);
auto transaction (store.tx_begin_write ());
store.version.put (transaction, 21);
MDB_dbi unchecked_handle{ 0 };
ASSERT_FALSE (mdb_dbi_open (store.env.tx (transaction), "unchecked", MDB_CREATE, &unchecked_handle));
ASSERT_EQ (store.version.get (transaction), 21);
}
// Testing the upgrade code worked
check_correct_state ();
}
}
}

View file

@ -136,7 +136,6 @@ add_library(
lmdb/pruned_store.cpp
lmdb/version_store.hpp
lmdb/version_store.cpp
lmdb/unchecked_store.hpp
lmdb/lmdb.hpp
lmdb/lmdb.cpp
lmdb/lmdb_env.hpp

View file

@ -62,7 +62,6 @@ nano::lmdb::store::store (nano::logger_mt & logger_a, boost::filesystem::path co
peer_store{ *this },
confirmation_height_store{ *this },
final_vote_store{ *this },
unchecked_store{},
version_store{ *this },
logger (logger_a),
env (error, path_a, nano::mdb_env::options::make ().set_config (lmdb_config_a).set_use_no_mem_init (true)),
@ -208,7 +207,6 @@ nano::mdb_txn_callbacks nano::lmdb::store::create_txn_callbacks () const
void nano::lmdb::store::open_databases (bool & error_a, nano::transaction const & transaction_a, unsigned flags)
{
error_a |= mdb_dbi_open (env.tx (transaction_a), "frontiers", flags, &frontier_store.frontiers_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "unchecked", flags, &unchecked_store.unchecked_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "online_weight", flags, &online_weight_store.online_weight_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "meta", flags, &block_store.meta_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "peers", flags, &peer_store.peers_handle) != 0;
@ -303,6 +301,9 @@ bool nano::lmdb::store::do_upgrades (nano::write_transaction & transaction_a, na
upgrade_v20_to_v21 (transaction_a);
[[fallthrough]];
case 21:
upgrade_v21_to_v22 (transaction_a);
[[fallthrough]];
case 22:
break;
default:
logger.always_log (boost::str (boost::format ("The version of the ledger (%1%) is too high for this node") % version_l));
@ -776,6 +777,16 @@ void nano::lmdb::store::upgrade_v20_to_v21 (nano::write_transaction const & tran
logger.always_log ("Finished creating new final_vote table");
}
void nano::lmdb::store::upgrade_v21_to_v22 (nano::write_transaction const & transaction_a)
{
logger.always_log ("Preparing v21 to v22 database upgrade...");
MDB_dbi unchecked_handle{ 0 };
release_assert (!mdb_dbi_open (env.tx (transaction_a), "unchecked", MDB_CREATE, &unchecked_handle));
release_assert (!mdb_drop (env.tx (transaction_a), unchecked_handle, 1)); // del = 1, to delete it from the environment and close the DB handle.
version.put (transaction_a, 22);
logger.always_log ("Finished removing unchecked table");
}
/** Takes a filepath, appends '_backup_<timestamp>' to the end (but before any extension) and saves that file in the same directory */
void nano::lmdb::store::create_backup_file (nano::mdb_env & env_a, boost::filesystem::path const & filepath_a, nano::logger_mt & logger_a)
{
@ -865,8 +876,6 @@ MDB_dbi nano::lmdb::store::table_to_dbi (tables table_a) const
return block_store.blocks_handle;
case tables::pending:
return pending_store.pending_handle;
case tables::unchecked:
return unchecked_store.unchecked_handle;
case tables::online_weight:
return online_weight_store.online_weight_handle;
case tables::meta:

View file

@ -16,7 +16,6 @@
#include <nano/node/lmdb/peer_store.hpp>
#include <nano/node/lmdb/pending_store.hpp>
#include <nano/node/lmdb/pruned_store.hpp>
#include <nano/node/lmdb/unchecked_store.hpp>
#include <nano/node/lmdb/version_store.hpp>
#include <nano/secure/common.hpp>
#include <nano/secure/versioning.hpp>
@ -57,7 +56,6 @@ namespace lmdb
nano::lmdb::peer_store peer_store;
nano::lmdb::pending_store pending_store;
nano::lmdb::pruned_store pruned_store;
nano::lmdb::unchecked_store unchecked_store;
nano::lmdb::version_store version_store;
friend class nano::lmdb::account_store;
@ -69,7 +67,6 @@ namespace lmdb
friend class nano::lmdb::peer_store;
friend class nano::lmdb::pending_store;
friend class nano::lmdb::pruned_store;
friend class nano::lmdb::unchecked_store;
friend class nano::lmdb::version_store;
public:
@ -136,6 +133,7 @@ namespace lmdb
void upgrade_v18_to_v19 (nano::write_transaction const &);
void upgrade_v19_to_v20 (nano::write_transaction const &);
void upgrade_v20_to_v21 (nano::write_transaction const &);
void upgrade_v21_to_v22 (nano::write_transaction const &);
std::shared_ptr<nano::block> block_get_v18 (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const;
nano::mdb_val block_raw_get_v18 (nano::transaction const & transaction_a, nano::block_hash const & hash_a, nano::block_type & type_a) const;
@ -188,6 +186,7 @@ namespace lmdb
friend class mdb_block_store_upgrade_v18_v19_Test;
friend class mdb_block_store_upgrade_v19_v20_Test;
friend class mdb_block_store_upgrade_v20_v21_Test;
friend class mdb_block_store_upgrade_v21_v22_Test;
friend class block_store_DISABLED_change_dupsort_Test;
friend void write_sideband_v14 (nano::lmdb::store &, nano::transaction &, nano::block const &, MDB_dbi);
friend void write_sideband_v15 (nano::lmdb::store &, nano::transaction &, nano::block const &);

View file

@ -1,16 +0,0 @@
#pragma once
#include <lmdb/libraries/liblmdb/lmdb.h>
namespace nano::lmdb
{
class unchecked_store
{
public:
/**
* Unchecked bootstrap blocks info.
* nano::block_hash -> nano::unchecked_info
*/
MDB_dbi unchecked_handle{ 0 };
};
}

View file

@ -772,6 +772,7 @@ public:
class store
{
friend class rocksdb_block_store_tombstone_count_Test;
friend class mdb_block_store_upgrade_v21_v22_Test;
public:
// clang-format off
@ -802,7 +803,7 @@ public:
account_store & account;
pending_store & pending;
static int constexpr version_minimum{ 14 };
static int constexpr version_current{ 21 };
static int constexpr version_current{ 22 };
public:
online_weight_store & online_weight;