Skip to content

Commit

Permalink
feat: move ReputationChangeKind to network-api (paradigmxyz#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa committed Jan 25, 2023
1 parent e493720 commit 0666e29
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 38 deletions.
4 changes: 4 additions & 0 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use std::net::SocketAddr;

/// Network Error
pub mod error;
/// Reputation score
pub mod reputation;

pub use error::NetworkError;
pub use reputation::{Reputation, ReputationChangeKind};

/// Provides general purpose information about the network.
#[async_trait]
Expand Down
25 changes: 25 additions & 0 deletions crates/net/network-api/src/reputation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// The type that tracks the reputation score.
pub type Reputation = i32;

/// Various kinds of reputation changes.
#[derive(Debug, Copy, Clone)]
pub enum ReputationChangeKind {
/// Received an unspecific bad message from the peer
BadMessage,
/// Peer sent a bad block.
///
/// Note: this will we only used in pre-merge, pow consensus, since after no more block announcements are sent via devp2p: [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
BadBlock,
/// Peer sent a bad transaction messages. E.g. Transactions which weren't recoverable.
BadTransactions,
/// Peer failed to respond in time.
Timeout,
/// Peer does not adhere to network protocol rules.
BadProtocol,
/// Failed to establish a connection to the peer.
FailedToConnect,
/// Connection dropped by peer.
Dropped,
/// Apply a reputation change by value
Other(Reputation),
}
6 changes: 2 additions & 4 deletions crates/net/network/src/fetch/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! A client implementation that can interact with the network and download data.

use crate::{
fetch::DownloadRequest,
peers::{PeersHandle, ReputationChangeKind},
};
use crate::{fetch::DownloadRequest, peers::PeersHandle};
use reth_eth_wire::{BlockBody, BlockHeaders};
use reth_interfaces::p2p::{
bodies::client::BodiesClient,
Expand All @@ -12,6 +9,7 @@ use reth_interfaces::p2p::{
headers::client::{HeadersClient, HeadersRequest},
priority::Priority,
};
use reth_network_api::ReputationChangeKind;
use reth_primitives::{PeerId, WithPeerId, H256};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use reth_interfaces::p2p::{
headers::client::HeadersRequest,
priority::Priority,
};
use reth_network_api::ReputationChangeKind;
use reth_primitives::{Header, PeerId, H256};
use std::{
collections::{HashMap, VecDeque},
Expand All @@ -21,7 +22,6 @@ use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot};
use tokio_stream::wrappers::UnboundedReceiverStream;

mod client;
use crate::peers::ReputationChangeKind;
pub use client::FetchClient;

/// Manages data fetching operations.
Expand Down
4 changes: 2 additions & 2 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
message::{NewBlockMessage, PeerMessage, PeerRequest, PeerRequestSender},
metrics::NetworkMetrics,
network::{NetworkHandle, NetworkHandleMessage},
peers::{PeersHandle, PeersManager, ReputationChangeKind},
peers::{PeersHandle, PeersManager},
session::SessionManager,
state::NetworkState,
swarm::{Swarm, SwarmEvent},
Expand All @@ -39,7 +39,7 @@ use reth_eth_wire::{
DisconnectReason, Status,
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::{EthProtocolInfo, NetworkStatus};
use reth_network_api::{EthProtocolInfo, NetworkStatus, ReputationChangeKind};
use reth_primitives::{PeerId, H256};
use reth_provider::BlockProvider;
use std::{
Expand Down
4 changes: 2 additions & 2 deletions crates/net/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
config::NetworkMode,
manager::NetworkEvent,
message::PeerRequest,
peers::{PeerKind, PeersHandle, ReputationChangeKind},
peers::{PeerKind, PeersHandle},
session::PeerInfo,
FetchClient,
};
Expand All @@ -14,7 +14,7 @@ use reth_interfaces::{
sync::{SyncState, SyncStateProvider, SyncStateUpdater},
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::{NetworkError, NetworkInfo, NetworkStatus, PeersInfo};
use reth_network_api::{NetworkError, NetworkInfo, NetworkStatus, PeersInfo, ReputationChangeKind};
use reth_primitives::{NodeRecord, PeerId, TransactionSigned, TxHash, H256, U256};
use std::{
net::SocketAddr,
Expand Down
6 changes: 4 additions & 2 deletions crates/net/network/src/peers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::{
error::{BackoffKind, SessionError},
peers::{
reputation::{is_banned_reputation, BACKOFF_REPUTATION_CHANGE, DEFAULT_REPUTATION},
ReputationChangeKind, ReputationChangeWeights,
ReputationChangeWeights,
},
session::{Direction, PendingSessionHandshakeError},
};
use futures::StreamExt;
use reth_eth_wire::{errors::EthStreamError, DisconnectReason};
use reth_net_common::ban_list::BanList;
use reth_network_api::ReputationChangeKind;
use reth_primitives::{ForkId, NodeRecord, PeerId};
use std::{
collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
Expand Down Expand Up @@ -1067,7 +1068,7 @@ mod test {
error::BackoffKind,
peers::{
manager::{ConnectionInfo, PeerBackoffDurations, PeerConnectionState},
PeerAction, ReputationChangeKind,
PeerAction,
},
session::PendingSessionHandshakeError,
PeersConfig,
Expand All @@ -1078,6 +1079,7 @@ mod test {
DisconnectReason,
};
use reth_net_common::ban_list::BanList;
use reth_network_api::ReputationChangeKind;
use reth_primitives::{PeerId, H512};
use std::{
collections::HashSet,
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/peers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod reputation;

pub(crate) use manager::{InboundConnectionError, PeerAction, PeersManager};
pub use manager::{PeerKind, PeersConfig, PeersHandle};
pub use reputation::{ReputationChangeKind, ReputationChangeWeights};
pub use reputation::ReputationChangeWeights;
26 changes: 1 addition & 25 deletions crates/net/network/src/peers/reputation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Peer reputation management

/// The type that tracks the reputation score.
pub(crate) type Reputation = i32;
use reth_network_api::{Reputation, ReputationChangeKind};

/// The default reputation of a peer
pub(crate) const DEFAULT_REPUTATION: Reputation = 0;
Expand Down Expand Up @@ -37,29 +36,6 @@ pub(crate) fn is_banned_reputation(reputation: i32) -> bool {
reputation < BANNED_REPUTATION
}

/// Various kinds of reputation changes.
#[derive(Debug, Copy, Clone)]
pub enum ReputationChangeKind {
/// Received an unspecific bad message from the peer
BadMessage,
/// Peer sent a bad block.
///
/// Note: this will we only used in pre-merge, pow consensus, since after no more block announcements are sent via devp2p: [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
BadBlock,
/// Peer sent a bad transaction messages. E.g. Transactions which weren't recoverable.
BadTransactions,
/// Peer failed to respond in time.
Timeout,
/// Peer does not adhere to network protocol rules.
BadProtocol,
/// Failed to establish a connection to the peer.
FailedToConnect,
/// Connection dropped by peer.
Dropped,
/// Apply a reputation change by value
Other(Reputation),
}

/// How the [`ReputationChangeKind`] are weighted.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
message::{PeerRequest, PeerRequestSender},
metrics::TransactionsManagerMetrics,
network::NetworkHandleMessage,
peers::ReputationChangeKind,
NetworkHandle,
};
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
use reth_eth_wire::{
GetPooledTransactions, NewPooledTransactionHashes, PooledTransactions, Transactions,
};
use reth_interfaces::{p2p::error::RequestResult, sync::SyncStateProvider};
use reth_network_api::ReputationChangeKind;
use reth_primitives::{
FromRecoveredTransaction, IntoRecoveredTransaction, PeerId, TransactionSigned, TxHash, H256,
};
Expand Down

0 comments on commit 0666e29

Please sign in to comment.