Skip to content

Commit

Permalink
HANDSHAKE TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 12, 2024
1 parent fb603a2 commit 2787046
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
21 changes: 10 additions & 11 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,16 @@ TEST (network, send_node_id_handshake_tcp)
auto node1 (std::make_shared<nano::node> (system.io_ctx, system.get_available_port (), nano::unique_path (), system.work));
node1->start ();
system.nodes.push_back (node1);
auto initial (node0->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in));
auto initial_node1 (node1->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in));
auto initial_keepalive (node0->stats.count (nano::stat::type::message, nano::stat::detail::keepalive, nano::stat::dir::in));
node0->stats.clear ();
node1->stats.clear ();
std::weak_ptr<nano::node> node_w (node0);
node0->network.tcp_channels.start_tcp (node1->network.endpoint ());
ASSERT_EQ (0, node0->network.size ());
ASSERT_EQ (0, node1->network.size ());
ASSERT_TIMELY (10s, node0->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in) >= initial + 2);
ASSERT_TIMELY (5s, node1->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in) >= initial_node1 + 2);
ASSERT_TIMELY (5s, node0->stats.count (nano::stat::type::message, nano::stat::detail::keepalive, nano::stat::dir::in) >= initial_keepalive + 2);
ASSERT_TIMELY (5s, node1->stats.count (nano::stat::type::message, nano::stat::detail::keepalive, nano::stat::dir::in) >= initial_keepalive + 2);
ASSERT_TIMELY (10s, node0->stats.count (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake, nano::stat::dir::in) >= 1);
ASSERT_TIMELY (5s, node1->stats.count (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake, nano::stat::dir::in) >= 1);
ASSERT_TIMELY (5s, node0->stats.count (nano::stat::type::message, nano::stat::detail::keepalive, nano::stat::dir::in) >= 2);
ASSERT_TIMELY (5s, node1->stats.count (nano::stat::type::message, nano::stat::detail::keepalive, nano::stat::dir::in) >= 2);
ASSERT_EQ (1, node0->network.size ());
ASSERT_EQ (1, node1->network.size ());
auto list1 (node0->network.list (1));
Expand Down Expand Up @@ -1043,7 +1042,7 @@ TEST (network, tcp_message_manager)
consumers.emplace_back ([&] {
for (auto i = 0; i < message_count; ++i)
{
ASSERT_EQ (manager.get_message ().channel, channel3);
ASSERT_EQ (manager2.get_message ().channel, channel3);
}
});
}
Expand All @@ -1053,7 +1052,7 @@ TEST (network, tcp_message_manager)
producers.emplace_back ([&] {
for (auto i = 0; i < message_count; ++i)
{
manager.put_message (make_message (), channel3);
manager2.put_message (make_message (), channel3);
}
});
}
Expand Down Expand Up @@ -1185,15 +1184,15 @@ TEST (network, purge_dead_channel_outgoing)
connected_count++;
outgoing = socket.shared_from_this ();

std::cout << "connected: " << socket.remote_endpoint () << std::endl;
system.logger.debug (nano::log::type::test, "Connected: {}", fmt::streamed (socket.remote_endpoint ()));
});

std::atomic<int> accepted_count{ 0 };
node1.observers.socket_accepted.add ([&] (nano::transport::socket & socket) {
accepted_count++;
incoming = socket.shared_from_this ();

std::cout << "accepted: " << socket.remote_endpoint () << std::endl;
system.logger.debug (nano::log::type::test, "Accepted: {}", fmt::streamed (socket.remote_endpoint ()));
});

auto & node2 = *system.add_node (flags);
Expand Down
12 changes: 10 additions & 2 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,21 @@ enum class detail : uint8_t
confirm_req,
confirm_ack,
node_id_handshake,
node_id_handshake_initiate,
node_id_handshake_response,
telemetry_req,
telemetry_ack,
asc_pull_req,
asc_pull_ack,

// tcp_server
to_realtime,
to_bootstrap,
node_id_handshake_initiate,
node_id_handshake_responded,
node_id_handshake_received,
node_id_handshake_error,
node_id_handshake_network_error,
node_id_handshake_response_invalid,

// bootstrap, callback
initiate,
initiate_legacy_age,
Expand Down
54 changes: 40 additions & 14 deletions nano/node/transport/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ bool nano::transport::tcp_server::process_message (std::unique_ptr<nano::message
{
return false;
}
node->stats.inc (nano::stat::type::tcp_server, to_stat_detail (message->type ()), nano::stat::dir::in);

debug_assert (is_undefined_connection () || is_realtime_connection () || is_bootstrap_connection ());

Expand Down Expand Up @@ -513,8 +512,12 @@ void nano::transport::tcp_server::handshake_message_visitor::node_id_handshake (
{
return;
}

node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake, nano::stat::dir::in);

if (node->flags.disable_tcp_realtime)
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_error);
node->logger.debug (nano::log::type::tcp_server, "Handshake attempted with disabled realtime TCP ({})", nano::util::to_str (server->remote_endpoint));

// Stop invalid handshake
Expand All @@ -524,6 +527,7 @@ void nano::transport::tcp_server::handshake_message_visitor::node_id_handshake (

if (message.query && server->handshake_query_received)
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_error);
node->logger.debug (nano::log::type::tcp_server, "Detected multiple handshake queries ({})", nano::util::to_str (server->remote_endpoint));

// Stop invalid handshake
Expand All @@ -546,12 +550,18 @@ void nano::transport::tcp_server::handshake_message_visitor::node_id_handshake (
bool success = server->to_realtime_connection (message.response->node_id);
if (!success)
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_error);
node->logger.debug (nano::log::type::tcp_server, "Error occurred when attempting to upgrade to realtime connection ({})", nano::util::to_str (server->remote_endpoint));

server->stop ();
return;
}
}
else
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_response_invalid);
node->logger.debug (nano::log::type::tcp_server, "Invalid handshake response received ({})", nano::util::to_str (server->remote_endpoint));

// Stop invalid handshake
server->stop ();
return;
Expand Down Expand Up @@ -582,14 +592,16 @@ void nano::transport::tcp_server::send_handshake_query ()
}
if (ec)
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_network_error);
node->logger.debug (nano::log::type::tcp_server, "Error sending handshake query: {} ({})", ec.message (), nano::util::to_str (this_l->remote_endpoint));

// Stop invalid handshake
this_l->stop ();
}
else
{
node->stats.inc (nano::stat::type::message, nano::stat::detail::node_id_handshake_initiate, nano::stat::dir::out);
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake, nano::stat::dir::out);
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_initiate, nano::stat::dir::out);
}
});
}
Expand All @@ -601,6 +613,7 @@ void nano::transport::tcp_server::send_handshake_response (nano::node_id_handsha
{
return;
}

auto response = node->network.prepare_handshake_response (query, v2);
auto own_query = node->network.prepare_handshake_query (nano::transport::map_tcp_to_endpoint (remote_endpoint));
nano::node_id_handshake handshake_response{ node->network_params.network, own_query, response };
Expand All @@ -617,14 +630,16 @@ void nano::transport::tcp_server::send_handshake_response (nano::node_id_handsha
}
if (ec)
{
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_network_error);
node->logger.debug (nano::log::type::tcp_server, "Error sending handshake response: {} ({})", ec.message (), nano::util::to_str (this_l->remote_endpoint));

// Stop invalid handshake
this_l->stop ();
}
else
{
node->stats.inc (nano::stat::type::message, nano::stat::detail::node_id_handshake_response, nano::stat::dir::out);
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake, nano::stat::dir::out);
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake_responded, nano::stat::dir::out);
}
});
}
Expand Down Expand Up @@ -841,6 +856,7 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
{
return false;
}

if (!allow_bootstrap)
{
return false;
Expand All @@ -861,6 +877,7 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
++node->tcp_listener->bootstrap_count;
socket->type_set (nano::transport::socket::type_t::bootstrap);

node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::to_bootstrap);
node->logger.debug (nano::log::type::tcp_server, "Switched to bootstrap mode ({})", nano::util::to_str (remote_endpoint));

return true;
Expand All @@ -873,21 +890,30 @@ bool nano::transport::tcp_server::to_realtime_connection (nano::account const &
{
return false;
}
if (socket->type () == nano::transport::socket::type_t::undefined && !node->flags.disable_tcp_realtime)

if (node->flags.disable_tcp_realtime)
{
auto channel_l = node->network.tcp_channels.create (socket, shared_from_this (), node_id);
if (channel_l)
{
channel = channel_l;
remote_node_id = node_id;
++node->tcp_listener->realtime_count;
socket->type_set (nano::transport::socket::type_t::realtime);
return false;
}
if (socket->type () != nano::transport::socket::type_t::undefined)
{
return false;
}

auto channel_l = node->network.tcp_channels.create (socket, shared_from_this (), node_id);
if (channel_l)
{
channel = channel_l;
remote_node_id = node_id;
++node->tcp_listener->realtime_count;
socket->type_set (nano::transport::socket::type_t::realtime);

node->logger.debug (nano::log::type::tcp_server, "Switched to realtime mode ({})", nano::util::to_str (remote_endpoint));
node->stats.inc (nano::stat::type::tcp_server, nano::stat::detail::to_realtime);
node->logger.debug (nano::log::type::tcp_server, "Switched to realtime mode ({})", nano::util::to_str (remote_endpoint));

return true;
}
return true;
}

return false;
}

Expand Down

0 comments on commit 2787046

Please sign in to comment.