Skip to content

Commit

Permalink
UNIQUE PTR
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 22, 2024
1 parent 1fcab67 commit 9256a88
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
18 changes: 9 additions & 9 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST (network, construction_with_specified_port)
auto const node = system.add_node (nano::node_config{ port });
EXPECT_EQ (port, node->network.port);
EXPECT_EQ (port, node->network.endpoint ().port ());
EXPECT_EQ (port, node->tcp_listener->endpoint ().port ());
EXPECT_EQ (port, node->tcp_listener.endpoint ().port ());
}

TEST (network, construction_without_specified_port)
Expand All @@ -75,7 +75,7 @@ TEST (network, construction_without_specified_port)
auto const port = node->network.port.load ();
EXPECT_NE (0, port);
EXPECT_EQ (port, node->network.endpoint ().port ());
EXPECT_EQ (port, node->tcp_listener->endpoint ().port ());
EXPECT_EQ (port, node->tcp_listener.endpoint ().port ());
}

TEST (network, send_node_id_handshake_tcp)
Expand Down Expand Up @@ -651,7 +651,7 @@ TEST (tcp_listener, tcp_node_id_handshake)
{
nano::test::system system (1);
auto socket (std::make_shared<nano::transport::socket> (*system.nodes[0]));
auto bootstrap_endpoint (system.nodes[0]->tcp_listener->endpoint ());
auto bootstrap_endpoint (system.nodes[0]->tcp_listener.endpoint ());
auto cookie (system.nodes[0]->network.syn_cookies.assign (nano::transport::map_tcp_to_endpoint (bootstrap_endpoint)));
ASSERT_TRUE (cookie);
nano::node_id_handshake::query_payload query{ *cookie };
Expand Down Expand Up @@ -690,7 +690,7 @@ TEST (tcp_listener, DISABLED_tcp_listener_timeout_empty)
auto node0 (system.nodes[0]);
auto socket (std::make_shared<nano::transport::socket> (*node0));
std::atomic<bool> connected (false);
socket->async_connect (node0->tcp_listener->endpoint (), [&connected] (boost::system::error_code const & ec) {
socket->async_connect (node0->tcp_listener.endpoint (), [&connected] (boost::system::error_code const & ec) {
ASSERT_FALSE (ec);
connected = true;
});
Expand All @@ -699,7 +699,7 @@ TEST (tcp_listener, DISABLED_tcp_listener_timeout_empty)
system.deadline_set (std::chrono::seconds (6));
while (!disconnected)
{
disconnected = node0->tcp_listener->connection_count () == 0;
disconnected = node0->tcp_listener.connection_count () == 0;
ASSERT_NO_ERROR (system.poll ());
}
}
Expand All @@ -709,24 +709,24 @@ TEST (tcp_listener, tcp_listener_timeout_node_id_handshake)
nano::test::system system (1);
auto node0 (system.nodes[0]);
auto socket (std::make_shared<nano::transport::socket> (*node0));
auto cookie (node0->network.syn_cookies.assign (nano::transport::map_tcp_to_endpoint (node0->tcp_listener->endpoint ())));
auto cookie (node0->network.syn_cookies.assign (nano::transport::map_tcp_to_endpoint (node0->tcp_listener.endpoint ())));
ASSERT_TRUE (cookie);
nano::node_id_handshake::query_payload query{ *cookie };
nano::node_id_handshake node_id_handshake{ nano::dev::network_params.network, query };
auto channel = std::make_shared<nano::transport::channel_tcp> (*node0, socket);
socket->async_connect (node0->tcp_listener->endpoint (), [&node_id_handshake, channel] (boost::system::error_code const & ec) {
socket->async_connect (node0->tcp_listener.endpoint (), [&node_id_handshake, channel] (boost::system::error_code const & ec) {
ASSERT_FALSE (ec);
channel->send (node_id_handshake, [] (boost::system::error_code const & ec, size_t size_a) {
ASSERT_FALSE (ec);
});
});
ASSERT_TIMELY (5s, node0->stats.count (nano::stat::type::tcp_server, nano::stat::detail::node_id_handshake) != 0);
ASSERT_EQ (node0->tcp_listener->connection_count (), 1);
ASSERT_EQ (node0->tcp_listener.connection_count (), 1);
bool disconnected (false);
system.deadline_set (std::chrono::seconds (20));
while (!disconnected)
{
disconnected = node0->tcp_listener->connection_count () == 0;
disconnected = node0->tcp_listener.connection_count () == 0;
ASSERT_NO_ERROR (system.poll ());
}
}
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,7 @@ TEST (node, peers)
node2->start ();
ASSERT_TIMELY (10s, !node2->network.empty () && !node1->network.empty ())
// Wait to finish TCP node ID handshakes
ASSERT_TIMELY (10s, node1->tcp_listener->realtime_count () != 0 && node2->tcp_listener->realtime_count () != 0);
ASSERT_TIMELY (10s, node1->tcp_listener.realtime_count () != 0 && node2->tcp_listener.realtime_count () != 0);
// Confirm that the peers match with the endpoints we are expecting
ASSERT_EQ (1, node1->network.size ());
auto list1 (node1->network.list (2));
Expand Down
13 changes: 7 additions & 6 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
// Thus, be very careful if you change the order: if `bootstrap` gets constructed before `network`,
// the latter would inherit the port from the former (if TCP is active, otherwise `network` picks first)
//
tcp_listener{ std::make_shared<nano::transport::tcp_listener> (network.port, *this, config.tcp_incoming_connections_max) },
tcp_listener_impl{ std::make_unique<nano::transport::tcp_listener> (network.port, *this, config.tcp_incoming_connections_max) },
tcp_listener{ *tcp_listener_impl },
application_path (application_path_a),
port_mapping (*this),
block_processor (*this, write_database_queue),
Expand Down Expand Up @@ -536,7 +537,7 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (no
composite->add_component (collect_container_info (node.ledger, "ledger"));
composite->add_component (collect_container_info (node.active, "active"));
composite->add_component (collect_container_info (node.bootstrap_initiator, "bootstrap_initiator"));
composite->add_component (node.tcp_listener->collect_container_info ("tcp_listener"));
composite->add_component (node.tcp_listener.collect_container_info ("tcp_listener"));
composite->add_component (collect_container_info (node.network, "network"));
composite->add_component (node.telemetry.collect_container_info ("telemetry"));
composite->add_component (collect_container_info (node.workers, "workers"));
Expand Down Expand Up @@ -617,12 +618,12 @@ void nano::node::start ()
bool tcp_enabled = false;
if (config.tcp_incoming_connections_max > 0 && !(flags.disable_bootstrap_listener && flags.disable_tcp_realtime))
{
tcp_listener->start ();
tcp_listener.start ();
tcp_enabled = true;

if (network.port != tcp_listener->endpoint ().port ())
if (network.port != tcp_listener.endpoint ().port ())
{
network.port = tcp_listener->endpoint ().port ();
network.port = tcp_listener.endpoint ().port ();
}

logger.info (nano::log::type::node, "Node peering port: {}", network.port.load ());
Expand Down Expand Up @@ -705,7 +706,7 @@ void nano::node::stop ()
websocket.stop ();
bootstrap_server.stop ();
bootstrap_initiator.stop ();
tcp_listener->stop ();
tcp_listener.stop ();
port_mapping.stop ();
wallets.stop ();
stats.stop ();
Expand Down
3 changes: 2 additions & 1 deletion nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ class node final : public std::enable_shared_from_this<node>
nano::telemetry telemetry;
nano::bootstrap_initiator bootstrap_initiator;
nano::bootstrap_server bootstrap_server;
std::shared_ptr<nano::transport::tcp_listener> tcp_listener;
std::unique_ptr<nano::transport::tcp_listener> tcp_listener_impl;
nano::transport::tcp_listener & tcp_listener;
std::filesystem::path application_path;
nano::node_observers observers;
nano::port_mapping port_mapping;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/transport/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
{
return false;
}
if (node->tcp_listener->bootstrap_count () >= node->config.bootstrap_connections_max)
if (node->tcp_listener.bootstrap_count () >= node->config.bootstrap_connections_max)
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions nano/test_common/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ std::shared_ptr<nano::node> nano::test::system::add_node (nano::node_config cons
auto starting_size_1 = node1->network.size ();
auto starting_size_2 = node2->network.size ();

auto starting_realtime_1 = node1->tcp_listener->realtime_count ();
auto starting_realtime_2 = node2->tcp_listener->realtime_count ();
auto starting_realtime_1 = node1->tcp_listener.realtime_count ();
auto starting_realtime_2 = node2->tcp_listener.realtime_count ();

auto starting_keepalives_1 = node1->stats.count (stat::type::message, stat::detail::keepalive, stat::dir::in);
auto starting_keepalives_2 = node2->stats.count (stat::type::message, stat::detail::keepalive, stat::dir::in);
Expand All @@ -146,8 +146,8 @@ std::shared_ptr<nano::node> nano::test::system::add_node (nano::node_config cons
{
// Wait for initial connection finish
auto ec = poll_until_true (5s, [&node1, &node2, starting_realtime_1, starting_realtime_2] () {
auto realtime_1 = node1->tcp_listener->realtime_count ();
auto realtime_2 = node2->tcp_listener->realtime_count ();
auto realtime_1 = node1->tcp_listener.realtime_count ();
auto realtime_2 = node2->tcp_listener.realtime_count ();
return realtime_1 > starting_realtime_1 && realtime_2 > starting_realtime_2;
});
debug_assert (!ec);
Expand Down

0 comments on commit 9256a88

Please sign in to comment.