From e7810db8d02845100aa27e81f5ceb66c4db7db56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:21:50 +0200 Subject: [PATCH] Move functions in the node source file --- nano/node/node.cpp | 114 ++++++++++++++++++++++----------------------- nano/node/node.hpp | 9 ++-- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 3bcaa939fd..182d1cd5ef 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -60,63 +60,6 @@ extern std::size_t nano_bootstrap_weights_beta_size; * node */ -void nano::node::keepalive (std::string const & address_a, uint16_t port_a) -{ - auto node_l (shared_from_this ()); - network.resolver.async_resolve (boost::asio::ip::tcp::resolver::query (address_a, std::to_string (port_a)), [node_l, address_a, port_a] (boost::system::error_code const & ec, boost::asio::ip::tcp::resolver::iterator i_a) { - if (!ec) - { - for (auto i (i_a), n (boost::asio::ip::tcp::resolver::iterator{}); i != n; ++i) - { - auto endpoint (nano::transport::map_endpoint_to_v6 (i->endpoint ())); - std::weak_ptr node_w (node_l); - auto channel (node_l->network.find_channel (endpoint)); - if (!channel) - { - node_l->network.tcp_channels.start_tcp (endpoint); - } - else - { - node_l->network.send_keepalive (channel); - } - } - } - else - { - node_l->logger.error (nano::log::type::node, "Error resolving address for keepalive: {}:{} ({})", address_a, port_a, ec.message ()); - } - }); -} - -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 ()) - { - 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; - release_assert (node_private_key.size () == 64); - nano::keypair kp = nano::keypair (node_private_key); - return kp; - } - else - { - // no node_id found, generate new one - 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); - ofs << kp.prv.to_string () << std::endl - << std::flush; - ofs.close (); - release_assert (!ofs.fail ()); - return kp; - } -} - nano::node::node (std::shared_ptr io_ctx_a, uint16_t peering_port_a, std::filesystem::path const & application_path_a, nano::work_pool & work_a, nano::node_flags flags_a, unsigned seq) : node (io_ctx_a, application_path_a, nano::node_config (peering_port_a), work_a, flags_a, seq) { @@ -554,6 +497,34 @@ bool nano::node::copy_with_compaction (std::filesystem::path const & destination return store.copy_db (destination); } +void nano::node::keepalive (std::string const & address_a, uint16_t port_a) +{ + auto node_l (shared_from_this ()); + network.resolver.async_resolve (boost::asio::ip::tcp::resolver::query (address_a, std::to_string (port_a)), [node_l, address_a, port_a] (boost::system::error_code const & ec, boost::asio::ip::tcp::resolver::iterator i_a) { + if (!ec) + { + for (auto i (i_a), n (boost::asio::ip::tcp::resolver::iterator{}); i != n; ++i) + { + auto endpoint (nano::transport::map_endpoint_to_v6 (i->endpoint ())); + std::weak_ptr node_w (node_l); + auto channel (node_l->network.find_channel (endpoint)); + if (!channel) + { + node_l->network.tcp_channels.start_tcp (endpoint); + } + else + { + node_l->network.send_keepalive (channel); + } + } + } + else + { + node_l->logger.error (nano::log::type::node, "Error resolving address for keepalive: {}:{} ({})", address_a, port_a, ec.message ()); + } + }); +} + std::unique_ptr nano::collect_container_info (node & node, std::string const & name) { auto composite = std::make_unique (name); @@ -1356,3 +1327,32 @@ std::string nano::node::make_logger_identifier (const nano::keypair & node_id) // Node identifier consists of first 10 characters of node id return node_id.pub.to_node_id ().substr (0, 10); } + +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 ()) + { + 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; + release_assert (node_private_key.size () == 64); + nano::keypair kp = nano::keypair (node_private_key); + return kp; + } + else + { + // no node_id found, generate new one + 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); + ofs << kp.prv.to_string () << std::endl + << std::flush; + ofs.close (); + release_assert (!ofs.fail ()); + return kp; + } +} \ No newline at end of file diff --git a/nano/node/node.hpp b/nano/node/node.hpp index d51137d5c5..b31578a190 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -80,16 +80,19 @@ class node final : public std::enable_shared_from_this ~node (); public: + void start (); + void stop (); + + std::shared_ptr shared (); + template void background (T action_a) { io_ctx.post (action_a); } + bool copy_with_compaction (std::filesystem::path const &); void keepalive (std::string const &, uint16_t); - void start (); - void stop (); - std::shared_ptr shared (); int store_version (); void process_confirmed (nano::election_status const &, uint64_t = 0); void process_active (std::shared_ptr const &);