Skip to content

Commit

Permalink
NODE ID LOADING
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 16, 2024
1 parent 934d918 commit 3b4f663
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
8 changes: 4 additions & 4 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4314,19 +4314,19 @@ TEST (node_config, node_id_private_key_persistence)
auto priv_key_filename = path / "node_id_private.key";

// check that the key generated is random when the key does not exist
nano::keypair kp1 = nano::load_or_create_node_id (path, system.nlogger);
nano::keypair kp1 = nano::load_or_create_node_id (path);
std::filesystem::remove (priv_key_filename);
nano::keypair kp2 = nano::load_or_create_node_id (path, system.nlogger);
nano::keypair kp2 = nano::load_or_create_node_id (path);
ASSERT_NE (kp1.prv, kp2.prv);

// check that the key persists
nano::keypair kp3 = nano::load_or_create_node_id (path, system.nlogger);
nano::keypair kp3 = nano::load_or_create_node_id (path);
ASSERT_EQ (kp2.prv, kp3.prv);

// write the key file manually and check that right key is loaded
std::ofstream ofs (priv_key_filename.string (), std::ofstream::out | std::ofstream::trunc);
ofs << "3F28D035B8AA75EA53DF753BFD065CF6138E742971B2C99B84FD8FE328FED2D9" << std::flush;
ofs.close ();
nano::keypair kp4 = nano::load_or_create_node_id (path, system.nlogger);
nano::keypair kp4 = nano::load_or_create_node_id (path);
ASSERT_EQ (kp4.prv, nano::keypair ("3F28D035B8AA75EA53DF753BFD065CF6138E742971B2C99B84FD8FE328FED2D9").prv);
}
1 change: 1 addition & 0 deletions nano/lib/logging_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum class type
all = 0, // reserved

generic,
init,
config,
logging,
node,
Expand Down
2 changes: 2 additions & 0 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
nano::network_params network_params{ nano::network_constants::active_network };
nano::daemon_config config{ data_path, network_params };
auto error = nano::read_node_config_toml (data_path, config, flags.config_overrides);

nano::set_use_memory_pools (config.node.use_memory_pools);

if (!error)
{
error = nano::flags_config_conflicts (flags, config.node);
Expand Down
2 changes: 1 addition & 1 deletion nano/nano_node/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class node_flags;

class daemon
{
nano::nlogger nlogger;
nano::nlogger nlogger{ "daemon" };

public:
void run (std::filesystem::path const &, nano::node_flags const & flags);
Expand Down
19 changes: 12 additions & 7 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (re
return composite;
}

nano::keypair nano::load_or_create_node_id (std::filesystem::path const & application_path, nano::nlogger & nlogger)
nano::keypair nano::load_or_create_node_id (std::filesystem::path const & application_path)
{
auto node_private_key_path = application_path / "node_id_private.key";
std::ifstream ifs (node_private_key_path.c_str ());
if (ifs.good ())
{
nlogger.debug (nano::log::type::node, "Reading node id from: '{}'", node_private_key_path.string ());
nano::default_logger ().info (nano::log::type::init, "Reading node id from: '{}'", node_private_key_path.string ());

std::string node_private_key;
ifs >> node_private_key;
Expand All @@ -121,7 +121,7 @@ nano::keypair nano::load_or_create_node_id (std::filesystem::path const & applic
else
{
// no node_id found, generate new one
nlogger.debug (nano::log::type::node, "Generating a new node id, saving to: '{}'", node_private_key_path.string ());
nano::default_logger ().info (nano::log::type::init, "Generating a new node id, saving to: '{}'", node_private_key_path.string ());

nano::keypair kp;
std::ofstream ofs (node_private_key_path.c_str (), std::ofstream::out | std::ofstream::trunc);
Expand All @@ -139,12 +139,13 @@ nano::node::node (boost::asio::io_context & io_ctx_a, uint16_t peering_port_a, s
}

nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path const & application_path_a, nano::node_config const & config_a, nano::work_pool & work_a, nano::node_flags flags_a, unsigned seq) :
node_id{ load_or_create_node_id (application_path_a) },
write_database_queue (!flags_a.force_use_write_database_queue && (config_a.rocksdb_config.enable)),
io_ctx (io_ctx_a),
node_initialized_latch (1),
config (config_a),
network_params{ config.network_params },
nlogger{ "node" },
nlogger{ make_logger_identifier (node_id) },
stats (config.stats_config),
workers{ config.background_threads, nano::thread_role::name::worker },
bootstrap_workers{ config.bootstrap_serving_threads, nano::thread_role::name::bootstrap_worker },
Expand Down Expand Up @@ -353,6 +354,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons
nlogger.info (nano::log::type::node, "Data path: {}", application_path.string ());
nlogger.info (nano::log::type::node, "Work pool threads: {} ({})", work.threads.size (), (work.opencl ? "OpenCL" : "CPU"));
nlogger.info (nano::log::type::node, "Work peers: {}", config.work_peers.size ());
nlogger.info (nano::log::type::node, "Node ID: {}", node_id.pub.to_node_id ());

if (!work_generation_enabled ())
{
Expand Down Expand Up @@ -403,9 +405,6 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons
}
}

node_id = nano::load_or_create_node_id (application_path, nlogger);
nlogger.info (nano::log::type::node, "Node ID: {}", node_id.pub.to_node_id ());

if ((network_params.network.is_live_network () || network_params.network.is_beta_network ()) && !flags.inactive_node)
{
auto const bootstrap_weights = get_bootstrap_weights ();
Expand Down Expand Up @@ -1476,6 +1475,12 @@ nano::telemetry_data nano::node::local_telemetry () const
return telemetry_data;
}

std::string nano::node::make_logger_identifier (const nano::keypair & node_id)
{
// Only log the last 6 characters of the node ID for readability
return "node_" + node_id.pub.to_account ().substr (60, 6);
}

/*
* node_wrapper
*/
Expand Down
6 changes: 4 additions & 2 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class node final : public std::enable_shared_from_this<nano::node>
nano::telemetry_data local_telemetry () const;

public:
const nano::keypair node_id;
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
boost::latch node_initialized_latch;
Expand Down Expand Up @@ -175,7 +176,6 @@ class node final : public std::enable_shared_from_this<nano::node>
nano::block_processor block_processor;
nano::block_arrival block_arrival;
nano::local_vote_history history;
nano::keypair node_id;
nano::block_uniquer block_uniquer;
nano::vote_uniquer vote_uniquer;
nano::confirmation_height_processor confirmation_height_processor;
Expand Down Expand Up @@ -227,9 +227,11 @@ class node final : public std::enable_shared_from_this<nano::node>

private:
void long_inactivity_cleanup ();

static std::string make_logger_identifier (nano::keypair const & node_id);
};

nano::keypair load_or_create_node_id (std::filesystem::path const & application_path, nano::nlogger &);
nano::keypair load_or_create_node_id (std::filesystem::path const & application_path);

std::unique_ptr<container_info_component> collect_container_info (node & node, std::string const & name);

Expand Down

0 comments on commit 3b4f663

Please sign in to comment.