Skip to content

Commit

Permalink
Move nano::transport::channel to a dedicated file (#4155)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev authored Feb 25, 2023
1 parent b07fa46 commit 159be4f
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 233 deletions.
2 changes: 2 additions & 0 deletions nano/node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ add_library(
state_block_signature_verification.cpp
telemetry.hpp
telemetry.cpp
transport/channel.hpp
transport/channel.cpp
transport/fake.hpp
transport/fake.cpp
transport/inproc.hpp
Expand Down
1 change: 1 addition & 0 deletions nano/node/bootstrap/bootstrap_server.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <nano/node/bootstrap/bootstrap_server.hpp>
#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/secure/store.hpp>
Expand Down
2 changes: 2 additions & 0 deletions nano/node/request_aggregator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <nano/lib/locks.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>

#include <boost/multi_index/hashed_index.hpp>
Expand All @@ -24,6 +25,7 @@ class node_config;
class stats;
class vote_generator;
class wallets;

/**
* Pools together confirmation requests, separately for each endpoint.
* Requests are added from network messages, and aggregated to minimize bandwidth and vote generation. Example:
Expand Down
63 changes: 63 additions & 0 deletions nano/node/transport/channel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <nano/node/common.hpp>
#include <nano/node/node.hpp>
#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>

#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/address_v4.hpp>
#include <boost/asio/ip/address_v6.hpp>
#include <boost/format.hpp>

nano::transport::channel::channel (nano::node & node_a) :
node{ node_a }
{
set_network_version (node_a.network_params.network.protocol_version);
}

void nano::transport::channel::send (nano::message & message_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a, nano::buffer_drop_policy drop_policy_a, nano::bandwidth_limit_type limiter_type)
{
auto buffer (message_a.to_shared_const_buffer ());
auto detail = nano::to_stat_detail (message_a.header.type);
auto is_droppable_by_limiter = drop_policy_a == nano::buffer_drop_policy::limiter;
auto should_pass (node.outbound_limiter.should_pass (buffer.size (), limiter_type));
if (!is_droppable_by_limiter || should_pass)
{
send_buffer (buffer, callback_a, drop_policy_a);
node.stats.inc (nano::stat::type::message, detail, nano::stat::dir::out);
}
else
{
if (callback_a)
{
node.background ([callback_a] () {
callback_a (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
});
}

node.stats.inc (nano::stat::type::drop, detail, nano::stat::dir::out);
if (node.config.logging.network_packet_logging ())
{
node.logger.always_log (boost::str (boost::format ("%1% of size %2% dropped") % nano::to_string (detail) % buffer.size ()));
}
}
}

void nano::transport::channel::set_peering_endpoint (nano::endpoint endpoint)
{
nano::lock_guard<nano::mutex> lock{ channel_mutex };
peering_endpoint = endpoint;
}

nano::endpoint nano::transport::channel::get_peering_endpoint () const
{
nano::unique_lock<nano::mutex> lock{ channel_mutex };
if (peering_endpoint)
{
return *peering_endpoint;
}
else
{
lock.unlock ();
return get_endpoint ();
}
}
176 changes: 176 additions & 0 deletions nano/node/transport/channel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#pragma once

#include <nano/lib/locks.hpp>
#include <nano/lib/stats.hpp>
#include <nano/node/bandwidth_limiter.hpp>
#include <nano/node/common.hpp>
#include <nano/node/messages.hpp>
#include <nano/node/socket.hpp>

#include <boost/asio/ip/network_v6.hpp>

namespace nano::transport
{
enum class transport_type : uint8_t
{
undefined = 0,
tcp = 1,
loopback = 2,
fake = 3
};

class channel
{
public:
explicit channel (nano::node &);
virtual ~channel () = default;

virtual std::size_t hash_code () const = 0;
virtual bool operator== (nano::transport::channel const &) const = 0;
void send (nano::message & message_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a = nullptr, nano::buffer_drop_policy policy_a = nano::buffer_drop_policy::limiter, nano::bandwidth_limit_type = nano::bandwidth_limit_type::standard);
// TODO: investigate clang-tidy warning about default parameters on virtual/override functions
//
virtual void send_buffer (nano::shared_const_buffer const &, std::function<void (boost::system::error_code const &, std::size_t)> const & = nullptr, nano::buffer_drop_policy = nano::buffer_drop_policy::limiter) = 0;
virtual std::string to_string () const = 0;
virtual nano::endpoint get_endpoint () const = 0;
virtual nano::tcp_endpoint get_tcp_endpoint () const = 0;
virtual nano::transport::transport_type get_type () const = 0;
virtual bool max ()
{
return false;
}
virtual bool alive () const
{
return true;
}

std::chrono::steady_clock::time_point get_last_bootstrap_attempt () const
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
return last_bootstrap_attempt;
}

void set_last_bootstrap_attempt (std::chrono::steady_clock::time_point const time_a)
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
last_bootstrap_attempt = time_a;
}

std::chrono::steady_clock::time_point get_last_packet_received () const
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
return last_packet_received;
}

void set_last_packet_received (std::chrono::steady_clock::time_point const time_a)
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
last_packet_received = time_a;
}

std::chrono::steady_clock::time_point get_last_packet_sent () const
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
return last_packet_sent;
}

void set_last_packet_sent (std::chrono::steady_clock::time_point const time_a)
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
last_packet_sent = time_a;
}

boost::optional<nano::account> get_node_id_optional () const
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
return node_id;
}

nano::account get_node_id () const
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
if (node_id.is_initialized ())
{
return node_id.get ();
}
else
{
return 0;
}
}

void set_node_id (nano::account node_id_a)
{
nano::lock_guard<nano::mutex> lk (channel_mutex);
node_id = node_id_a;
}

uint8_t get_network_version () const
{
return network_version;
}

void set_network_version (uint8_t network_version_a)
{
network_version = network_version_a;
}

nano::endpoint get_peering_endpoint () const;
void set_peering_endpoint (nano::endpoint endpoint);

mutable nano::mutex channel_mutex;

private:
std::chrono::steady_clock::time_point last_bootstrap_attempt{ std::chrono::steady_clock::time_point () };
std::chrono::steady_clock::time_point last_packet_received{ std::chrono::steady_clock::now () };
std::chrono::steady_clock::time_point last_packet_sent{ std::chrono::steady_clock::now () };
boost::optional<nano::account> node_id{ boost::none };
std::atomic<uint8_t> network_version{ 0 };
std::optional<nano::endpoint> peering_endpoint{};

protected:
nano::node & node;
};
}

namespace std
{
template <>
struct hash<::nano::transport::channel>
{
std::size_t operator() (::nano::transport::channel const & channel_a) const
{
return channel_a.hash_code ();
}
};
template <>
struct equal_to<std::reference_wrapper<::nano::transport::channel const>>
{
bool operator() (std::reference_wrapper<::nano::transport::channel const> const & lhs, std::reference_wrapper<::nano::transport::channel const> const & rhs) const
{
return lhs.get () == rhs.get ();
}
};
}

namespace boost
{
template <>
struct hash<::nano::transport::channel>
{
std::size_t operator() (::nano::transport::channel const & channel_a) const
{
std::hash<::nano::transport::channel> hash;
return hash (channel_a);
}
};
template <>
struct hash<std::reference_wrapper<::nano::transport::channel const>>
{
std::size_t operator() (std::reference_wrapper<::nano::transport::channel const> const & channel_a) const
{
std::hash<::nano::transport::channel> hash;
return hash (channel_a.get ());
}
};
}
1 change: 1 addition & 0 deletions nano/node/transport/fake.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>

namespace nano
Expand Down
3 changes: 2 additions & 1 deletion nano/node/transport/inproc.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>

namespace nano
Expand All @@ -8,7 +9,7 @@ namespace transport
{
/**
* In-process transport channel. Mostly useful for unit tests
**/
**/
namespace inproc
{
class channel final : public nano::transport::channel
Expand Down
1 change: 1 addition & 0 deletions nano/node/transport/tcp.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/node/common.hpp>
#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/transport.hpp>

#include <boost/multi_index/hashed_index.hpp>
Expand Down
54 changes: 0 additions & 54 deletions nano/node/transport/transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,6 @@ boost::asio::ip::address nano::transport::ipv4_address_or_ipv6_subnet (boost::as
return address_a.to_v6 ().is_v4_mapped () ? address_a : boost::asio::ip::make_network_v6 (address_a.to_v6 (), ipv6_address_prefix_length).network ();
}

nano::transport::channel::channel (nano::node & node_a) :
node (node_a)
{
set_network_version (node_a.network_params.network.protocol_version);
}

void nano::transport::channel::send (nano::message & message_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a, nano::buffer_drop_policy drop_policy_a, nano::bandwidth_limit_type limiter_type)
{
auto buffer (message_a.to_shared_const_buffer ());
auto detail = nano::to_stat_detail (message_a.header.type);
auto is_droppable_by_limiter = drop_policy_a == nano::buffer_drop_policy::limiter;
auto should_pass (node.outbound_limiter.should_pass (buffer.size (), limiter_type));
if (!is_droppable_by_limiter || should_pass)
{
send_buffer (buffer, callback_a, drop_policy_a);
node.stats.inc (nano::stat::type::message, detail, nano::stat::dir::out);
}
else
{
if (callback_a)
{
node.background ([callback_a] () {
callback_a (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
});
}

node.stats.inc (nano::stat::type::drop, detail, nano::stat::dir::out);
if (node.config.logging.network_packet_logging ())
{
node.logger.always_log (boost::str (boost::format ("%1% of size %2% dropped") % nano::to_string (detail) % buffer.size ()));
}
}
}

void nano::transport::channel::set_peering_endpoint (nano::endpoint endpoint)
{
nano::lock_guard<nano::mutex> lock{ channel_mutex };
peering_endpoint = endpoint;
}

nano::endpoint nano::transport::channel::get_peering_endpoint () const
{
nano::unique_lock<nano::mutex> lock{ channel_mutex };
if (peering_endpoint)
{
return *peering_endpoint;
}
else
{
lock.unlock ();
return get_endpoint ();
}
}

boost::asio::ip::address_v6 nano::transport::mapped_from_v4_bytes (unsigned long address_a)
{
return boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (address_a));
Expand Down
Loading

0 comments on commit 159be4f

Please sign in to comment.