Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent dropping of unchecked on restart if node is not synchronized #2257

Merged
2 changes: 1 addition & 1 deletion nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ TEST (mdb_block_store, upgrade_backup)

// Now do the upgrade and confirm that backup is saved
nano::logger_mt logger;
nano::mdb_store store (logger, path, nano::txn_tracking_config{}, std::chrono::seconds (5), 128, false, 512, true);
nano::mdb_store store (logger, path, nano::txn_tracking_config{}, std::chrono::seconds (5), 128, 512, true);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_read ());
ASSERT_LT (14, store.version_get (transaction));
Expand Down
8 changes: 1 addition & 7 deletions nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void mdb_val::convert_buffer_to_value ()
}
}

nano::mdb_store::mdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t const batch_size, bool backup_before_upgrade) :
nano::mdb_store::mdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, size_t const batch_size, bool backup_before_upgrade) :
logger (logger_a),
env (error, path_a, lmdb_max_dbs, true),
mdb_txn_tracker (logger_a, txn_tracking_config_a, block_processor_batch_max_time_a),
Expand Down Expand Up @@ -79,12 +79,6 @@ txn_tracking_enabled (txn_tracking_config_a.enable)
auto transaction (tx_begin_read ());
open_databases (error, transaction, 0);
}

if (!error && drop_unchecked)
{
auto transaction (tx_begin_write ({ nano::tables::cached_counts, tables::unchecked }));
unchecked_clear (transaction);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion nano/node/lmdb/lmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class mdb_store : public block_store_partial<MDB_val, mdb_store>
using block_store_partial::block_exists;
using block_store_partial::unchecked_put;

mdb_store (nano::logger_mt &, boost::filesystem::path const &, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
mdb_store (nano::logger_mt &, boost::filesystem::path const &, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, size_t batch_size = 512, bool backup_before_upgrade = false);
nano::write_transaction tx_begin_write (std::vector<nano::tables> const & tables_requiring_lock = {}, std::vector<nano::tables> const & tables_no_lock = {}) override;
nano::read_transaction tx_begin_read () override;

Expand Down
27 changes: 19 additions & 8 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ flags (flags_a),
alarm (alarm_a),
work (work_a),
logger (config_a.logging.min_time_between_log_output),
store_impl (nano::make_store (logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, !flags.disable_unchecked_drop, flags.sideband_batch_size, config_a.backup_before_upgrade)),
store_impl (nano::make_store (logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, flags.sideband_batch_size, config_a.backup_before_upgrade)),
store (*store_impl),
wallets_store_impl (std::make_unique<nano::mdb_wallets_store> (application_path_a / "wallets.ldb", config_a.lmdb_max_dbs)),
wallets_store (*wallets_store_impl),
Expand Down Expand Up @@ -413,17 +413,20 @@ startup_time (std::chrono::steady_clock::now ())
node_id = nano::keypair ();
logger.always_log ("Node ID: ", node_id.pub.to_node_id ());

const uint8_t * weight_buffer = network_params.network.is_live_network () ? nano_bootstrap_weights_live : nano_bootstrap_weights_beta;
size_t weight_size = network_params.network.is_live_network () ? nano_bootstrap_weights_live_size : nano_bootstrap_weights_beta_size;
if (network_params.network.is_live_network () || network_params.network.is_beta_network ())
if ((network_params.network.is_live_network () || network_params.network.is_beta_network ()) && !flags.inactive_node)
{
// Use bootstrap weights if initial bootstrap is not completed
bool use_bootstrap_weight (false);
const uint8_t * weight_buffer = network_params.network.is_live_network () ? nano_bootstrap_weights_live : nano_bootstrap_weights_beta;
size_t weight_size = network_params.network.is_live_network () ? nano_bootstrap_weights_live_size : nano_bootstrap_weights_beta_size;
nano::bufferstream weight_stream ((const uint8_t *)weight_buffer, weight_size);
nano::uint128_union block_height;
if (!nano::try_read (weight_stream, block_height))
{
auto max_blocks = (uint64_t)block_height.number ();
auto transaction (store.tx_begin_read ());
if (ledger.store.block_count (transaction).sum () < max_blocks)
use_bootstrap_weight = ledger.store.block_count (transaction).sum () < max_blocks;
if (use_bootstrap_weight)
{
ledger.bootstrap_weight_max_blocks = max_blocks;
while (true)
Expand All @@ -443,6 +446,13 @@ startup_time (std::chrono::steady_clock::now ())
}
}
}
// Drop unchecked blocks if initial bootstrap is completed
if (!flags.disable_unchecked_drop && !use_bootstrap_weight && !flags.read_only)
{
auto transaction (store.tx_begin_write ());
store.unchecked_clear (transaction);
logger.always_log ("Dropping unchecked blocks");
}
}
}
node_initialized_latch.count_down ();
Expand Down Expand Up @@ -1667,17 +1677,18 @@ nano::inactive_node::~inactive_node ()
nano::node_flags const & nano::inactive_node_flag_defaults ()
{
static nano::node_flags node_flags;
node_flags.inactive_node = true;
node_flags.read_only = true;
node_flags.cache_representative_weights_from_frontiers = false;
node_flags.cache_cemented_count_from_frontiers = false;
return node_flags;
}

std::unique_ptr<nano::block_store> nano::make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t batch_size, bool backup_before_upgrade)
std::unique_ptr<nano::block_store> nano::make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, size_t batch_size, bool backup_before_upgrade)
{
#if NANO_ROCKSDB
return std::make_unique<nano::rocksdb_store> (logger, add_db_postfix ? path / "rocksdb" : path, drop_unchecked, read_only);
return std::make_unique<nano::rocksdb_store> (logger, add_db_postfix ? path / "rocksdb" : path, read_only);
#else
return std::make_unique<nano::mdb_store> (logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, drop_unchecked, batch_size, backup_before_upgrade);
return std::make_unique<nano::mdb_store> (logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, batch_size, backup_before_upgrade);
#endif
}
1 change: 1 addition & 0 deletions nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class node_flags final
bool cache_representative_weights_from_frontiers{ true };
/** Whether to read all frontiers and construct the total cemented count */
bool cache_cemented_count_from_frontiers{ true };
bool inactive_node{ false };
size_t sideband_batch_size{ 512 };
size_t block_processor_batch_size{ 0 };
size_t block_processor_full_size{ 65536 };
Expand Down
10 changes: 2 additions & 8 deletions nano/node/rocksdb/rocksdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void rocksdb_val::convert_buffer_to_value ()
}
}

nano::rocksdb_store::rocksdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, bool drop_unchecked_a, bool open_read_only_a) :
nano::rocksdb_store::rocksdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, bool open_read_only_a) :
logger (logger_a)
{
boost::system::error_code error_mkdir, error_chmod;
Expand All @@ -58,12 +58,6 @@ logger (logger_a)
}
open (error, path_a, open_read_only_a);
}

if (!error && !open_read_only_a && drop_unchecked_a)
{
auto transaction (tx_begin_write ({ nano::tables::cached_counts, tables::unchecked }));
unchecked_clear (transaction);
}
}

nano::rocksdb_store::~rocksdb_store ()
Expand Down Expand Up @@ -608,7 +602,7 @@ bool nano::rocksdb_store::copy_db (boost::filesystem::path const & destination_p
// Open it so that it flushes all WAL files
if (status.ok ())
{
nano::rocksdb_store rocksdb_store (logger, destination_path.string (), false, false);
nano::rocksdb_store rocksdb_store (logger, destination_path.string (), false);
return !rocksdb_store.init_error ();
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/rocksdb/rocksdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class logging_mt;
class rocksdb_store : public block_store_partial<rocksdb::Slice, rocksdb_store>
{
public:
rocksdb_store (nano::logger_mt &, boost::filesystem::path const &, bool drop_unchecked = false, bool open_read_only = false);
rocksdb_store (nano::logger_mt &, boost::filesystem::path const &, bool open_read_only = false);
~rocksdb_store ();
nano::write_transaction tx_begin_write (std::vector<nano::tables> const & tables_requiring_lock = {}, std::vector<nano::tables> const & tables_no_lock = {}) override;
nano::read_transaction tx_begin_read () override;
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/blockstore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ class block_store
virtual nano::read_transaction tx_begin_read () = 0;
};

std::unique_ptr<nano::block_store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
std::unique_ptr<nano::block_store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, size_t batch_size = 512, bool backup_before_upgrade = false);
}

namespace std
Expand Down