Skip to content

Commit

Permalink
Make network settings configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 30, 2025
1 parent 568ab07 commit 4ccb3bb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
26 changes: 26 additions & 0 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ TEST (toml_config, daemon_config_deserialize_defaults)
[node.lmdb]
[node.rocksdb]
[node.tcp]
[node.network]
[opencl]
[rpc]
[rpc.child_process]
Expand Down Expand Up @@ -425,6 +426,14 @@ TEST (toml_config, daemon_config_deserialize_defaults)
ASSERT_EQ (conf.node.tcp.connect_timeout, defaults.node.tcp.connect_timeout);
ASSERT_EQ (conf.node.tcp.handshake_timeout, defaults.node.tcp.handshake_timeout);
ASSERT_EQ (conf.node.tcp.io_timeout, defaults.node.tcp.io_timeout);

ASSERT_EQ (conf.node.network.peer_reachout.count (), defaults.node.network.peer_reachout.count ());
ASSERT_EQ (conf.node.network.cached_peer_reachout.count (), defaults.node.network.cached_peer_reachout.count ());
ASSERT_EQ (conf.node.network.max_peers_per_ip, defaults.node.network.max_peers_per_ip);
ASSERT_EQ (conf.node.network.max_peers_per_subnetwork, defaults.node.network.max_peers_per_subnetwork);
ASSERT_EQ (conf.node.network.duplicate_filter_size, defaults.node.network.duplicate_filter_size);
ASSERT_EQ (conf.node.network.duplicate_filter_cutoff, defaults.node.network.duplicate_filter_cutoff);
ASSERT_EQ (conf.node.network.minimum_fanout, defaults.node.network.minimum_fanout);
}

/** Deserialize a node config with non-default values */
Expand Down Expand Up @@ -656,6 +665,15 @@ TEST (toml_config, daemon_config_deserialize_no_defaults)
handshake_timeout = 999
io_timeout = 999
[node.network]
peer_reachout = 999
cached_peer_reachout = 9999
max_peers_per_ip = 99
max_peers_per_subnetwork = 99
duplicate_filter_size = 9999999
duplicate_filter_cutoff = 999
minimum_fanout = 99
[opencl]
device = 999
enable = true
Expand Down Expand Up @@ -844,6 +862,14 @@ TEST (toml_config, daemon_config_deserialize_no_defaults)
ASSERT_NE (conf.node.tcp.connect_timeout, defaults.node.tcp.connect_timeout);
ASSERT_NE (conf.node.tcp.handshake_timeout, defaults.node.tcp.handshake_timeout);
ASSERT_NE (conf.node.tcp.io_timeout, defaults.node.tcp.io_timeout);

ASSERT_NE (conf.node.network.peer_reachout.count (), defaults.node.network.peer_reachout.count ());
ASSERT_NE (conf.node.network.cached_peer_reachout.count (), defaults.node.network.cached_peer_reachout.count ());
ASSERT_NE (conf.node.network.max_peers_per_ip, defaults.node.network.max_peers_per_ip);
ASSERT_NE (conf.node.network.max_peers_per_subnetwork, defaults.node.network.max_peers_per_subnetwork);
ASSERT_NE (conf.node.network.duplicate_filter_size, defaults.node.network.duplicate_filter_size);
ASSERT_NE (conf.node.network.duplicate_filter_cutoff, defaults.node.network.duplicate_filter_cutoff);
ASSERT_NE (conf.node.network.minimum_fanout, defaults.node.network.minimum_fanout);
}

/** There should be no required values **/
Expand Down
32 changes: 31 additions & 1 deletion nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ std::deque<std::shared_ptr<nano::transport::channel>> nano::network::list_non_pr
// Simulating with sqrt_broadcast_simulate shows we only need to broadcast to sqrt(total_peers) random peers in order to successfully publish to everyone with high probability
std::size_t nano::network::fanout (float scale) const
{
auto fanout_l = std::max (2.0f, size_log ());
auto fanout_l = std::max (static_cast<float> (config.minimum_fanout), size_log ());
return static_cast<std::size_t> (std::ceil (scale * fanout_l));
}

Expand Down Expand Up @@ -738,3 +738,33 @@ nano::container_info nano::syn_cookies::container_info () const
info.put ("syn_cookies_per_ip", cookies_per_ip.size ());
return info;
}

/*
* network_config
*/

nano::error nano::network_config::serialize (nano::tomlconfig & toml) const
{
toml.put ("peer_reachout", peer_reachout.count (), "Time between attempts to reach out to peers. \ntype:milliseconds");
toml.put ("cached_peer_reachout", cached_peer_reachout.count (), "Time between attempts to reach out to cached peers. \ntype:milliseconds");
toml.put ("max_peers_per_ip", max_peers_per_ip, "Maximum number of peers allowed from a single IP address. \ntype:size_t");
toml.put ("max_peers_per_subnetwork", max_peers_per_subnetwork, "Maximum number of peers allowed from the same subnetwork. \ntype:size_t");
toml.put ("duplicate_filter_size", duplicate_filter_size, "Size of the duplicate detection filter. \ntype:size_t");
toml.put ("duplicate_filter_cutoff", duplicate_filter_cutoff, "Time in seconds before a duplicate entry expires. \ntype:uint64");
toml.put ("minimum_fanout", minimum_fanout, "Minimum number of peers to fan out messages to. \ntype:size_t");

return toml.get_error ();
}

nano::error nano::network_config::deserialize (nano::tomlconfig & toml)
{
toml.get_duration ("peer_reachout", peer_reachout);
toml.get_duration ("cached_peer_reachout", cached_peer_reachout);
toml.get ("max_peers_per_ip", max_peers_per_ip);
toml.get ("max_peers_per_subnetwork", max_peers_per_subnetwork);
toml.get ("duplicate_filter_size", duplicate_filter_size);
toml.get ("duplicate_filter_cutoff", duplicate_filter_cutoff);
toml.get ("minimum_fanout", minimum_fanout);

return toml.get_error ();
}
5 changes: 4 additions & 1 deletion nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class network_config final
}
}

// TODO: Serialization & deserialization
nano::error deserialize (nano::tomlconfig &);
nano::error serialize (nano::tomlconfig &) const;

public:
std::chrono::milliseconds peer_reachout{ 250ms };
Expand All @@ -79,6 +80,8 @@ class network_config final

size_t duplicate_filter_size{ 1024 * 1024 };
uint64_t duplicate_filter_cutoff{ 60 };

size_t minimum_fanout{ 2 };
};

class network final
Expand Down
10 changes: 10 additions & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ nano::error nano::node_config::serialize_toml (nano::tomlconfig & toml) const
tcp.serialize (tcp_l);
toml.put_child ("tcp", tcp_l);

nano::tomlconfig network_l;
network.serialize (network_l);
toml.put_child ("network", network_l);

nano::tomlconfig request_aggregator_l;
request_aggregator.serialize (request_aggregator_l);
toml.put_child ("request_aggregator", request_aggregator_l);
Expand Down Expand Up @@ -391,6 +395,12 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
tcp.deserialize (config_l);
}

if (toml.has_key ("network"))
{
auto config_l = toml.get_required_child ("network");
network.deserialize (config_l);
}

if (toml.has_key ("request_aggregator"))
{
auto config_l = toml.get_required_child ("request_aggregator");
Expand Down

0 comments on commit 4ccb3bb

Please sign in to comment.