diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index 6ba098f37..5a07ec250 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -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] diff --git a/crates/net/network-api/src/reputation.rs b/crates/net/network-api/src/reputation.rs new file mode 100644 index 000000000..a36135561 --- /dev/null +++ b/crates/net/network-api/src/reputation.rs @@ -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), +} diff --git a/crates/net/network/src/fetch/client.rs b/crates/net/network/src/fetch/client.rs index a43f5d643..80a9804e0 100644 --- a/crates/net/network/src/fetch/client.rs +++ b/crates/net/network/src/fetch/client.rs @@ -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, @@ -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}, diff --git a/crates/net/network/src/fetch/mod.rs b/crates/net/network/src/fetch/mod.rs index c89dfc7e1..bc14730bf 100644 --- a/crates/net/network/src/fetch/mod.rs +++ b/crates/net/network/src/fetch/mod.rs @@ -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}, @@ -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. diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 148656348..55f0210fa 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -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}, @@ -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::{ diff --git a/crates/net/network/src/network.rs b/crates/net/network/src/network.rs index b02b5de4c..0c6dcde1c 100644 --- a/crates/net/network/src/network.rs +++ b/crates/net/network/src/network.rs @@ -2,7 +2,7 @@ use crate::{ config::NetworkMode, manager::NetworkEvent, message::PeerRequest, - peers::{PeerKind, PeersHandle, ReputationChangeKind}, + peers::{PeerKind, PeersHandle}, session::PeerInfo, FetchClient, }; @@ -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, diff --git a/crates/net/network/src/peers/manager.rs b/crates/net/network/src/peers/manager.rs index ffab80e5a..962233d8a 100644 --- a/crates/net/network/src/peers/manager.rs +++ b/crates/net/network/src/peers/manager.rs @@ -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}, @@ -1067,7 +1068,7 @@ mod test { error::BackoffKind, peers::{ manager::{ConnectionInfo, PeerBackoffDurations, PeerConnectionState}, - PeerAction, ReputationChangeKind, + PeerAction, }, session::PendingSessionHandshakeError, PeersConfig, @@ -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, diff --git a/crates/net/network/src/peers/mod.rs b/crates/net/network/src/peers/mod.rs index 7808b9360..60ec3c0a8 100644 --- a/crates/net/network/src/peers/mod.rs +++ b/crates/net/network/src/peers/mod.rs @@ -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; diff --git a/crates/net/network/src/peers/reputation.rs b/crates/net/network/src/peers/reputation.rs index 6f476c472..738862f6b 100644 --- a/crates/net/network/src/peers/reputation.rs +++ b/crates/net/network/src/peers/reputation.rs @@ -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; @@ -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))] diff --git a/crates/net/network/src/transactions.rs b/crates/net/network/src/transactions.rs index 2baeadd38..f8718e20c 100644 --- a/crates/net/network/src/transactions.rs +++ b/crates/net/network/src/transactions.rs @@ -6,7 +6,6 @@ use crate::{ message::{PeerRequest, PeerRequestSender}, metrics::TransactionsManagerMetrics, network::NetworkHandleMessage, - peers::ReputationChangeKind, NetworkHandle, }; use futures::{stream::FuturesUnordered, FutureExt, StreamExt}; @@ -14,6 +13,7 @@ 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, };