Skip to content

Commit

Permalink
Feature: avoid connection with low reputation peers (#2054)
Browse files Browse the repository at this point in the history
* feature: avoid connection with low reputation peers

Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
  • Loading branch information
xDimon authored Jun 26, 2024
1 parent 81c606e commit 8ca1e3f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
namespace {
constexpr const char *syncPeerMetricName = "kagome_sync_peers";
constexpr const char *kPeersCountMetricName = "kagome_sub_libp2p_peers_count";
/// Reputation change for a node when we get disconnected from it.
/// Reputation value for a node when we get disconnected from it.
static constexpr int32_t kDisconnectReputation = -256;
/// Reputation change for a node when we get disconnected from it.
static constexpr int32_t kMinReputationForInnerConnection = -128;
static constexpr int32_t kMinReputationForOuterConnection = -128;
} // namespace

OUTCOME_CPP_DEFINE_CATEGORY(kagome::network, PeerManagerImpl::Error, e) {
Expand Down Expand Up @@ -421,6 +424,18 @@ namespace kagome::network {
return;
}

// Don't establish connection to bad (negative reputation) peers
const auto peer_reputation = reputation_repository_->reputation(peer_id);
if (peer_reputation < kMinReputationForOuterConnection) {
SL_DEBUG(log_,
"Attempt to establish connection to peer {} skipped: "
"peer has low ({}) reputation",
peer_id,
peer_reputation);
connecting_peers_.erase(peer_id);
return;
}

auto peer_info = host_.getPeerRepository().getPeerInfo(peer_id);
if (peer_info.addresses.empty()) {
SL_DEBUG(log_, "Not found addresses for peer {}", peer_id);
Expand Down Expand Up @@ -799,6 +814,19 @@ namespace kagome::network {
}
}

// Don't accept connection from bad (negative reputation) peers
const auto peer_reputation = reputation_repository_->reputation(peer_id);
if (peer_reputation < kMinReputationForInnerConnection) {
SL_DEBUG(log_,
"New connection from peer {} was dropped: "
"peer has low ({}) reputation",
peer_id,
peer_reputation);
connecting_peers_.erase(peer_id);
disconnectFromPeer(peer_id);
return;
}

PeerInfo peer_info{.id = peer_id, .addresses = {}};
openBlockAnnounceProtocol(
peer_info,
Expand Down

0 comments on commit 8ca1e3f

Please sign in to comment.