Skip to content

Commit

Permalink
Add LMDB upgrade to v22: Clear unchecked table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simpago committed Feb 15, 2023
1 parent 5425c44 commit 1a216cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
30 changes: 30 additions & 0 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,36 @@ 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
return;
}
auto path (nano::unique_path ());
nano::logger_mt logger;
nano::stats stats;
{
nano::lmdb::store store (logger, path, nano::dev::constants);
nano::ledger ledger (store, stats, nano::dev::constants);
auto transaction (store.tx_begin_write ());
store.initialize (transaction, ledger.cache, ledger.constants);
// Add data to unchecked store
store.unchecked_store.put (transaction, nano::dev::genesis->hash (), nano::unchecked_info{ nano::dev::genesis });
store.version.put (transaction, 21);
ASSERT_EQ (1, store.unchecked_store.count (transaction));
}
// Upgrading should clear the unchecked table
nano::lmdb::store store (logger, path, nano::dev::constants);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_read ());
ASSERT_EQ (0, store.unchecked_store.count (transaction));

// Version should be correct
ASSERT_LT (21, store.version.get (transaction));
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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));
Expand Down Expand Up @@ -777,6 +780,18 @@ 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)
{
// In the following pull request the binary representation of `unchecked_info` was changed:
// https://github.com/nanocurrency/nano-node/pull/4021
// We have to clear the unchecked table, because the new binary format is not compatible with
// the old one.
logger.always_log ("Preparing v21 to v22 database upgrade...");
mdb_drop (env.tx (transaction_a), unchecked_store.unchecked_handle, 0);
version.put (transaction_a, 22);
logger.always_log ("Finished clearing 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)
{
Expand Down
2 changes: 2 additions & 0 deletions nano/node/lmdb/lmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,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;
Expand Down Expand Up @@ -188,6 +189,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 &);
Expand Down
3 changes: 2 additions & 1 deletion nano/secure/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ class unchecked_map;
class store
{
friend class rocksdb_block_store_tombstone_count_Test;
friend class mdb_block_store_upgrade_v21_v22_Test;

public:
// clang-format off
Expand Down Expand Up @@ -859,7 +860,7 @@ class store
account_store & account;
pending_store & pending;
static int constexpr version_minimum{ 14 };
static int constexpr version_current{ 21 };
static int constexpr version_current{ 22 };

private:
unchecked_store & unchecked;
Expand Down

0 comments on commit 1a216cb

Please sign in to comment.