Skip to content

Commit

Permalink
Move reputation types from reth-network-api to reth-network-types (
Browse files Browse the repository at this point in the history
…#9914)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
emhane and mattsse authored Jul 31, 2024
1 parent 02d2530 commit 78703b5
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 133 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/reth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub mod tasks {
/// Re-exported from `reth_network`.
pub mod network {
pub use reth_network::*;
pub use reth_network_api::{noop, reputation, NetworkInfo, PeerKind, Peers, PeersInfo};
pub use reth_network_api::{noop, NetworkInfo, PeerKind, Peers, PeersInfo};
}

/// Re-exported from `reth_transaction_pool`.
Expand Down
1 change: 1 addition & 0 deletions crates/net/network-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
reth-eth-wire.workspace = true
alloy-rpc-types-admin.workspace = true
reth-network-peers.workspace = true
reth-network-types.workspace = true

# ethereum
alloy-primitives.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};

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

use reth_eth_wire::{capability::Capabilities, DisconnectReason, EthVersion, Status};
use reth_network_peers::NodeRecord;
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
pub use reth_network_types::{Reputation, ReputationChangeKind};

/// The `PeerId` type.
pub type PeerId = alloy_primitives::B512;

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

/// Implementation of network traits for that does nothing.
pub mod noop;
Expand Down
52 changes: 0 additions & 52 deletions crates/net/network-api/src/reputation.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/net/network-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ workspace = true

[dependencies]
# reth
reth-network-api.workspace = true
reth-network-peers.workspace = true
reth-net-banlist.workspace = true

Expand Down
5 changes: 4 additions & 1 deletion crates/net/network-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

/// Types related to peering.
pub mod peers;
pub use peers::{ConnectionsConfig, PeersConfig, ReputationChangeWeights};
pub use peers::{
reputation::{Reputation, ReputationChangeKind, ReputationChangeWeights},
ConnectionsConfig, PeersConfig,
};

pub mod session;
pub use session::{SessionLimits, SessionsConfig};
Expand Down
55 changes: 53 additions & 2 deletions crates/net/network-types/src/peers/reputation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Peer reputation management

use reth_network_api::{Reputation, ReputationChangeKind};

/// The default reputation of a peer
pub const DEFAULT_REPUTATION: Reputation = 0;

Expand Down Expand Up @@ -50,6 +48,59 @@ pub const fn is_banned_reputation(reputation: i32) -> bool {
reputation < BANNED_REPUTATION
}

/// The type that tracks the reputation score.
pub type Reputation = i32;

/// Various kinds of reputation changes.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
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 message. E.g. Transactions which weren't recoverable.
BadTransactions,
/// Peer sent a bad announcement message, e.g. invalid transaction type for the configured
/// network.
BadAnnouncement,
/// Peer sent a message that included a hash or transaction that we already received from the
/// peer.
///
/// According to the [Eth spec](https://github.com/ethereum/devp2p/blob/master/caps/eth.md):
///
/// > A node should never send a transaction back to a peer that it can determine already knows
/// > of it (either because it was previously sent or because it was informed from this peer
/// > originally). This is usually achieved by remembering a set of transaction hashes recently
/// > relayed by the peer.
AlreadySeenTransaction,
/// 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,
/// Reset the reputation to the default value.
Reset,
/// Apply a reputation change by value
Other(Reputation),
}

impl ReputationChangeKind {
/// Returns true if the reputation change is a [`ReputationChangeKind::Reset`].
pub const fn is_reset(&self) -> bool {
matches!(self, Self::Reset)
}

/// Returns true if the reputation change is [`ReputationChangeKind::Dropped`].
pub const fn is_dropped(&self) -> bool {
matches!(self, Self::Dropped)
}
}

/// How the [`ReputationChangeKind`] are weighted.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/fetch/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::{fetch::DownloadRequest, flattened_response::FlattenedResponse, peers::PeersHandle};
use futures::{future, future::Either};

use reth_network_api::ReputationChangeKind;
use reth_network_p2p::{
bodies::client::{BodiesClient, BodiesFut},
download::DownloadClient,
Expand All @@ -12,6 +11,7 @@ use reth_network_p2p::{
priority::Priority,
};
use reth_network_peers::PeerId;
use reth_network_types::ReputationChangeKind;
use reth_primitives::{Header, B256};
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 @@ -3,13 +3,13 @@
use crate::{message::BlockRequest, peers::PeersHandle};
use futures::StreamExt;
use reth_eth_wire::{GetBlockBodies, GetBlockHeaders};
use reth_network_api::ReputationChangeKind;
use reth_network_p2p::{
error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult},
headers::client::HeadersRequest,
priority::Priority,
};
use reth_network_peers::PeerId;
use reth_network_types::ReputationChangeKind;
use reth_primitives::{BlockBody, Header, B256};
use std::{
collections::{HashMap, VecDeque},
Expand Down
61 changes: 32 additions & 29 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@
//! (IP+port) of our node is published via discovery, remote peers can initiate inbound connections
//! to the local node. Once a (tcp) connection is established, both peers start to authenticate a [RLPx session](https://github.com/ethereum/devp2p/blob/master/rlpx.md) via a handshake. If the handshake was successful, both peers announce their capabilities and are now ready to exchange sub-protocol messages via the `RLPx` session.

use std::{
net::SocketAddr,
path::Path,
pin::Pin,
sync::{
atomic::{AtomicU64, AtomicUsize, Ordering},
Arc,
},
task::{Context, Poll},
time::{Duration, Instant},
};

use futures::{Future, StreamExt};
use parking_lot::Mutex;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
DisconnectReason, EthVersion, Status,
};
use reth_fs_util::{self as fs, FsPathError};
use reth_metrics::common::mpsc::UnboundedMeteredSender;
use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo};
use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::ReputationChangeKind;
use reth_primitives::ForkId;
use reth_storage_api::BlockNumReader;
use reth_tasks::shutdown::GracefulShutdown;
use reth_tokio_util::EventSender;
use secp256k1::SecretKey;
use tokio::sync::mpsc::{self, error::TrySendError};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing::{debug, error, trace, warn};

use crate::{
budget::{DEFAULT_BUDGET_TRY_DRAIN_NETWORK_HANDLE_CHANNEL, DEFAULT_BUDGET_TRY_DRAIN_SWARM},
config::NetworkConfig,
Expand All @@ -35,35 +67,6 @@ use crate::{
transactions::NetworkTransactionEvent,
FetchClient, NetworkBuilder,
};
use futures::{Future, StreamExt};
use parking_lot::Mutex;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
DisconnectReason, EthVersion, Status,
};
use reth_fs_util::{self as fs, FsPathError};
use reth_metrics::common::mpsc::UnboundedMeteredSender;
use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo, ReputationChangeKind};
use reth_network_peers::{NodeRecord, PeerId};
use reth_primitives::ForkId;
use reth_storage_api::BlockNumReader;
use reth_tasks::shutdown::GracefulShutdown;
use reth_tokio_util::EventSender;
use secp256k1::SecretKey;
use std::{
net::SocketAddr,
path::Path,
pin::Pin,
sync::{
atomic::{AtomicU64, AtomicUsize, Ordering},
Arc,
},
task::{Context, Poll},
time::{Duration, Instant},
};
use tokio::sync::mpsc::{self, error::TrySendError};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing::{debug, error, trace, warn};

#[cfg_attr(doc, aquamarine::aquamarine)]
/// Manages the _entire_ state of the network.
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 @@ -14,11 +14,11 @@ use parking_lot::Mutex;
use reth_discv4::Discv4;
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_network_api::{
NetworkError, NetworkInfo, NetworkStatus, PeerInfo, PeerKind, Peers, PeersInfo, Reputation,
ReputationChangeKind,
NetworkError, NetworkInfo, NetworkStatus, PeerInfo, PeerKind, Peers, PeersInfo,
};
use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState, SyncStateProvider};
use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{Reputation, ReputationChangeKind};
use reth_primitives::{Head, TransactionSigned, B256};
use reth_tokio_util::{EventSender, EventStream};
use secp256k1::SecretKey;
Expand Down
Loading

0 comments on commit 78703b5

Please sign in to comment.