From 21f8be2b0dd58caa132db825e779c934af7d040b Mon Sep 17 00:00:00 2001 From: Thiago Silva Date: Mon, 19 Sep 2022 19:25:20 -0300 Subject: [PATCH] Preemptively return if existing_channel && !existing_channel->temporary process may only be set to true iff existing_channel == true, so we can exit if (existing_channel && !existing_channel->temporary) --- nano/node/transport/tcp.cpp | 71 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/nano/node/transport/tcp.cpp b/nano/node/transport/tcp.cpp index 80d91227..0c36be9e 100644 --- a/nano/node/transport/tcp.cpp +++ b/nano/node/transport/tcp.cpp @@ -679,52 +679,49 @@ void nano::transport::tcp_channels::start_tcp_receive_node_id (std::shared_ptrnetwork.tcp_channels.find_node_id (node_id)); - if (existing_channel) + if (existing_channel && !existing_channel->temporary) { - process = existing_channel->temporary; + return; } - if (process) + channel_a->set_node_id (node_id); + channel_a->set_last_packet_received (std::chrono::steady_clock::now ()); + boost::optional> response (std::make_pair (node_l->node_id.pub, nano::sign_message (node_l->node_id.prv, node_l->node_id.pub, *message.query))); + nano::node_id_handshake response_message (node_l->network_params.network, boost::none, response); + if (node_l->config.logging.network_node_id_handshake_logging ()) { - channel_a->set_node_id (node_id); - channel_a->set_last_packet_received (std::chrono::steady_clock::now ()); - boost::optional> response (std::make_pair (node_l->node_id.pub, nano::sign_message (node_l->node_id.prv, node_l->node_id.pub, *message.query))); - nano::node_id_handshake response_message (node_l->network_params.network, boost::none, response); - if (node_l->config.logging.network_node_id_handshake_logging ()) + node_l->logger.try_log (boost::str (boost::format ("Node ID handshake response sent with node ID %1% to %2%: query %3%") % node_l->node_id.pub.to_node_id () % endpoint_a % (*message.query).to_string ())); + } + channel_a->send (response_message, [node_w, channel_a, endpoint_a, cleanup_node_id_handshake_socket] (boost::system::error_code const & ec, std::size_t size_a) { + if (auto node_l = node_w.lock ()) { - node_l->logger.try_log (boost::str (boost::format ("Node ID handshake response sent with node ID %1% to %2%: query %3%") % node_l->node_id.pub.to_node_id () % endpoint_a % (*message.query).to_string ())); - } - channel_a->send (response_message, [node_w, channel_a, endpoint_a, cleanup_node_id_handshake_socket] (boost::system::error_code const & ec, std::size_t size_a) { - if (auto node_l = node_w.lock ()) + if (ec || !channel_a) { - if (ec || !channel_a) + if (node_l->config.logging.network_node_id_handshake_logging ()) { - if (node_l->config.logging.network_node_id_handshake_logging ()) - { - node_l->logger.try_log (boost::str (boost::format ("Error sending node_id_handshake to %1%: %2%") % endpoint_a % ec.message ())); - } - cleanup_node_id_handshake_socket (endpoint_a); - return; + node_l->logger.try_log (boost::str (boost::format ("Error sending node_id_handshake to %1%: %2%") % endpoint_a % ec.message ())); } - // Insert new node ID connection - if (auto socket_l = channel_a->socket.lock ()) - { - channel_a->set_last_packet_sent (std::chrono::steady_clock::now ()); - auto response_server = std::make_shared (socket_l, node_l); - node_l->network.tcp_channels.insert (channel_a, socket_l, response_server); - // Listen for possible responses - response_server->socket->type_set (nano::socket::type_t::realtime_response_server); - response_server->remote_node_id = channel_a->get_node_id (); - response_server->start (); + cleanup_node_id_handshake_socket (endpoint_a); + return; + } + // Insert new node ID connection + if (auto socket_l = channel_a->socket.lock ()) + { + channel_a->set_last_packet_sent (std::chrono::steady_clock::now ()); + auto response_server = std::make_shared (socket_l, node_l); + node_l->network.tcp_channels.insert (channel_a, socket_l, response_server); + // Listen for possible responses + response_server->socket->type_set (nano::socket::type_t::realtime_response_server); + response_server->remote_node_id = channel_a->get_node_id (); + response_server->start (); - if (!node_l->flags.disable_initial_telemetry_requests) - { - node_l->telemetry->get_metrics_single_peer_async (channel_a, [] (nano::telemetry_data_response const &) { - // Intentionally empty, starts the telemetry request cycle to more quickly disconnect from invalid peers - }); - } + if (!node_l->flags.disable_initial_telemetry_requests) + { + node_l->telemetry->get_metrics_single_peer_async (channel_a, [] (nano::telemetry_data_response const &) { + // Intentionally empty, starts the telemetry request cycle to more quickly disconnect from invalid peers + }); } } - }); - } + } + }); }); }