Skip to content

Commit

Permalink
Merge pull request #2892 from Taraxa-project/trusted_nodes
Browse files Browse the repository at this point in the history
chore: trusted nodes
  • Loading branch information
mfrankovi authored Nov 25, 2024
2 parents ed975e0 + ed13008 commit 154cda3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions libraries/aleth/libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ void Host::startPeerSession(Public const& _id, RLP const& _hello, unique_ptr<RLP
}
}
}
if (!disconnect_reason && !peerSlotsAvailable()) {
auto is_trusted_node = m_netConfig.trustedNodes.contains(peer->address());
if (!disconnect_reason && (!peerSlotsAvailable() && !is_trusted_node)) {
cnetdetails << "Too many peers, can't connect. peer count: " << peer_count_()
<< " pending peers: " << m_pendingPeerConns.size();
disconnect_reason = TooManyPeers;
Expand Down Expand Up @@ -411,7 +412,9 @@ void Host::runAcceptor() {
return;
}
auto socket = make_shared<RLPXSocket>(std::move(_socket));
if (peer_count_() > peerSlots(Ingress)) {
// Since a connecting peer might be a trusted node which should always connect allow up to max number of trusted
// nodes above the limit
if (peer_count_() > (peerSlots(Ingress) + m_netConfig.trustedNodes.size())) {
cnetdetails << "Dropping incoming connect due to maximum peer count (" << Ingress
<< " * ideal peer count): " << socket->remoteEndpoint();
socket->close();
Expand Down
3 changes: 3 additions & 0 deletions libraries/aleth/libp2p/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ struct NetworkConfig {
std::string listenIPAddress;
uint16_t listenPort = c_defaultListenPort;

/// Trusted Nodes
std::unordered_set<dev::p2p::NodeID> trustedNodes;

/// Preferences

bool traverseNAT = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"listen_port": 10002,
"transaction_interval_ms": 100,
"ideal_peer_count": 10,
"max_peer_count": 50,
"max_peer_count": 20,
"sync_level_size": 10,
"packets_processing_threads": 14,
"peer_blacklist_timeout": 600,
Expand Down
2 changes: 2 additions & 0 deletions libraries/config/include/config/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

#include "common/types.hpp"
#include "libp2p/Common.h"

namespace taraxa {

Expand Down Expand Up @@ -82,6 +83,7 @@ struct NetworkConfig {
bool disable_peer_blacklist = false;
uint16_t deep_syncing_threshold = 10;
DdosProtectionConfig ddos_protection;
std::unordered_set<dev::p2p::NodeID> trusted_nodes;

std::optional<ConnectionConfig> rpc;
std::optional<ConnectionConfig> graphql;
Expand Down
6 changes: 6 additions & 0 deletions libraries/config/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ void dec_json(const Json::Value &json, NetworkConfig &network) {
network.listen_port = getConfigDataAsUInt(json, {"listen_port"});
network.transaction_interval_ms = getConfigDataAsUInt(json, {"transaction_interval_ms"});
network.ideal_peer_count = getConfigDataAsUInt(json, {"ideal_peer_count"});
Json::Value priority_nodes = json["priority_nodes"];
if (!priority_nodes.isNull()) {
for (const auto &item : priority_nodes) {
network.trusted_nodes.insert(dev::p2p::NodeID(item.asString()));
}
}
network.max_peer_count = getConfigDataAsUInt(json, {"max_peer_count"});
network.sync_level_size = getConfigDataAsUInt(json, {"sync_level_size"});
network.packets_processing_threads = getConfigDataAsUInt(json, {"packets_processing_threads"});
Expand Down
1 change: 1 addition & 0 deletions libraries/core_libs/network/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi
net_conf.traverseNAT = false;
net_conf.publicIPAddress = config.network.public_ip;
net_conf.pin = false;
net_conf.trustedNodes = config.network.trusted_nodes;

dev::p2p::TaraxaNetworkConfig taraxa_net_conf;
taraxa_net_conf.ideal_peer_count = config.network.ideal_peer_count;
Expand Down

0 comments on commit 154cda3

Please sign in to comment.