Skip to content

Commit

Permalink
Limit max peers per IP for live & beta networks to 5
Browse files Browse the repository at this point in the history
and 10 for test network
  • Loading branch information
SergiySW committed Feb 25, 2020
1 parent fc4a462 commit 0345ad7
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ TEST (network, peer_max_tcp_attempts)
// Add nodes that can accept TCP connection, but not node ID handshake
nano::node_flags node_flags;
node_flags.disable_tcp_realtime = true;
for (auto i (0); i < nano::transport::max_peers_per_ip; ++i)
for (auto i (0); i < node->network_params.node.max_peers_per_ip; ++i)
{
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::get_available_port (), nano::unique_path (), system.alarm, system.logging, system.work, node_flags));
node2->start ();
Expand Down
2 changes: 1 addition & 1 deletion nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ boost::optional<nano::uint256_union> nano::syn_cookies::assign (nano::endpoint c
nano::lock_guard<std::mutex> lock (syn_cookie_mutex);
unsigned & ip_cookies = cookies_per_ip[ip_addr];
boost::optional<nano::uint256_union> result;
if (ip_cookies < nano::transport::max_peers_per_ip)
if (ip_cookies < node.network_params.node.max_peers_per_ip)
{
if (cookies.find (endpoint_a) == cookies.end ())
{
Expand Down
4 changes: 2 additions & 2 deletions nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ void nano::transport::tcp_channels::stop ()
bool nano::transport::tcp_channels::max_ip_connections (nano::tcp_endpoint const & endpoint_a)
{
nano::unique_lock<std::mutex> lock (mutex);
bool result (channels.get<ip_address_tag> ().count (endpoint_a.address ()) >= nano::transport::max_peers_per_ip);
bool result (channels.get<ip_address_tag> ().count (endpoint_a.address ()) >= node.network_params.node.max_peers_per_ip);
if (!result)
{
result = attempts.get<ip_address_tag> ().count (endpoint_a.address ()) >= nano::transport::max_peers_per_ip;
result = attempts.get<ip_address_tag> ().count (endpoint_a.address ()) >= node.network_params.node.max_peers_per_ip;
}
return result;
}
Expand Down
2 changes: 0 additions & 2 deletions nano/node/transport/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ namespace transport
nano::tcp_endpoint map_endpoint_to_tcp (nano::endpoint const &);
// Unassigned, reserved, self
bool reserved_address (nano::endpoint const &, bool = false);
// Maximum number of peers per IP
static size_t constexpr max_peers_per_ip = 10;
static std::chrono::seconds constexpr syn_cookie_cutoff = std::chrono::seconds (5);
enum class transport_type : uint8_t
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/transport/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ std::shared_ptr<nano::transport::channel> nano::transport::udp_channels::create
bool nano::transport::udp_channels::max_ip_connections (nano::endpoint const & endpoint_a)
{
nano::unique_lock<std::mutex> lock (mutex);
bool result (channels.get<ip_address_tag> ().count (endpoint_a.address ()) >= nano::transport::max_peers_per_ip);
bool result (channels.get<ip_address_tag> ().count (endpoint_a.address ()) >= node.network_params.node.max_peers_per_ip);
return result;
}

Expand Down
1 change: 1 addition & 0 deletions nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ nano::node_constants::node_constants (nano::network_constants & network_constant
peer_interval = search_pending_interval;
unchecked_cleaning_interval = std::chrono::minutes (30);
process_confirmed_interval = network_constants.is_test_network () ? std::chrono::milliseconds (50) : std::chrono::milliseconds (500);
max_peers_per_ip = network_constants.is_test_network () ? 10 : 5;
max_weight_samples = network_constants.is_live_network () ? 4032 : 864;
weight_period = 5 * 60; // 5 minutes
}
Expand Down
2 changes: 2 additions & 0 deletions nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ class node_constants
std::chrono::seconds peer_interval;
std::chrono::minutes unchecked_cleaning_interval;
std::chrono::milliseconds process_confirmed_interval;
/** Maximum number of peers per IP */
size_t max_peers_per_ip;

/** The maximum amount of samples for a 2 week period on live or 3 days on beta */
uint64_t max_weight_samples;
Expand Down

0 comments on commit 0345ad7

Please sign in to comment.