Move PeersHandleProvider to new module reth_network_api::test_utils (#10060)

This commit is contained in:
Emilia Hane
2024-08-05 10:46:49 +02:00
committed by GitHub
parent fb5d94ece1
commit d12f0ab815
20 changed files with 62 additions and 67 deletions

3
Cargo.lock generated
View File

@ -7560,6 +7560,7 @@ dependencies = [
"alloy-primitives", "alloy-primitives",
"alloy-rpc-types-admin", "alloy-rpc-types-admin",
"auto_impl", "auto_impl",
"derive_more",
"enr", "enr",
"futures", "futures",
"reth-eth-wire-types", "reth-eth-wire-types",
@ -7609,7 +7610,6 @@ dependencies = [
name = "reth-network-types" name = "reth-network-types"
version = "1.0.3" version = "1.0.3"
dependencies = [ dependencies = [
"derive_more",
"humantime-serde", "humantime-serde",
"reth-ethereum-forks", "reth-ethereum-forks",
"reth-net-banlist", "reth-net-banlist",
@ -7617,7 +7617,6 @@ dependencies = [
"reth-network-peers", "reth-network-peers",
"serde", "serde",
"serde_json", "serde_json",
"tokio",
"tracing", "tracing",
] ]

View File

@ -127,7 +127,9 @@ pub mod tasks {
/// Re-exported from `reth_network`. /// Re-exported from `reth_network`.
pub mod network { pub mod network {
pub use reth_network::*; pub use reth_network::*;
pub use reth_network_api::{noop, NetworkInfo, Peers, PeersHandleProvider, PeersInfo}; pub use reth_network_api::{
noop, test_utils::PeersHandleProvider, NetworkInfo, Peers, PeersInfo,
};
} }
/// Re-exported from `reth_transaction_pool`. /// Re-exported from `reth_transaction_pool`.

View File

@ -33,6 +33,7 @@ thiserror.workspace = true
serde = { workspace = true, features = ["derive"], optional = true } serde = { workspace = true, features = ["derive"], optional = true }
tokio = { workspace = true, features = ["sync"] } tokio = { workspace = true, features = ["sync"] }
auto_impl.workspace = true auto_impl.workspace = true
derive_more.workspace = true
[features] [features]
default = ["serde"] default = ["serde"]

View File

@ -18,10 +18,11 @@ pub mod downloaders;
pub mod error; pub mod error;
/// Implementation of network traits for that does nothing. /// Implementation of network traits for that does nothing.
pub mod noop; pub mod noop;
pub mod test_utils;
pub use alloy_rpc_types_admin::EthProtocolInfo; pub use alloy_rpc_types_admin::EthProtocolInfo;
pub use reth_network_p2p::BlockClient; pub use reth_network_p2p::BlockClient;
pub use reth_network_types::{PeerKind, PeersHandle, Reputation, ReputationChangeKind}; pub use reth_network_types::{PeerKind, Reputation, ReputationChangeKind};
pub use downloaders::BlockDownloaderProvider; pub use downloaders::BlockDownloaderProvider;
pub use error::NetworkError; pub use error::NetworkError;
@ -162,15 +163,6 @@ pub trait Peers: PeersInfo {
) -> impl Future<Output = Result<Option<Reputation>, NetworkError>> + Send; ) -> impl Future<Output = Result<Option<Reputation>, NetworkError>> + Send;
} }
/// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersHandleProvider {
/// Returns the [`PeersHandle`] that can be cloned and shared.
///
/// The [`PeersHandle`] can be used to interact with the network's peer set.
fn peers_handle(&self) -> &PeersHandle;
}
/// Info about an active peer session. /// Info about an active peer session.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PeerInfo { pub struct PeerInfo {

View File

@ -0,0 +1,5 @@
//! API for integration testing network components.
pub mod peers_manager;
pub use peers_manager::{PeerCommand, PeersHandle, PeersHandleProvider};

View File

@ -1,12 +1,21 @@
//! Async peer handle. //! Interaction with `reth_network::PeersManager`, for integration testing. Otherwise
//! `reth_network::NetworkManager` manages `reth_network::PeersManager`.
use std::net::SocketAddr; use std::net::SocketAddr;
use derive_more::Constructor; use derive_more::Constructor;
use reth_network_peers::{NodeRecord, PeerId}; use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{Peer, ReputationChangeKind};
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use crate::{Peer, PeerCommand, ReputationChangeKind}; /// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersHandleProvider {
/// Returns the [`PeersHandle`] that can be cloned and shared.
///
/// The [`PeersHandle`] can be used to interact with the network's peer set.
fn peers_handle(&self) -> &PeersHandle;
}
/// A communication channel to the `PeersManager` to apply manual changes to the peer set. /// A communication channel to the `PeersManager` to apply manual changes to the peer set.
#[derive(Clone, Debug, Constructor)] #[derive(Clone, Debug, Constructor)]
@ -53,3 +62,20 @@ impl PeersHandle {
rx.await.unwrap_or_default() rx.await.unwrap_or_default()
} }
} }
/// Commands the `PeersManager` listens for.
#[derive(Debug)]
pub enum PeerCommand {
/// Command for manually add
Add(PeerId, SocketAddr),
/// Remove a peer from the set
///
/// If currently connected this will disconnect the session
Remove(PeerId),
/// Apply a reputation change to the given peer.
ReputationChange(PeerId, ReputationChangeKind),
/// Get information about a peer
GetPeer(PeerId, oneshot::Sender<Option<Peer>>),
/// Get node information on all peers
GetPeers(oneshot::Sender<Vec<NodeRecord>>),
}

View File

@ -16,9 +16,6 @@ workspace = true
reth-network-peers.workspace = true reth-network-peers.workspace = true
reth-net-banlist.workspace = true reth-net-banlist.workspace = true
reth-ethereum-forks.workspace = true reth-ethereum-forks.workspace = true
# async
tokio = { workspace = true, features = ["sync"] }
reth-network-p2p.workspace = true reth-network-p2p.workspace = true
# io # io
@ -28,7 +25,6 @@ serde_json = { workspace = true }
# misc # misc
tracing.workspace = true tracing.workspace = true
derive_more.workspace = true
[features] [features]
serde = ["dep:serde", "dep:humantime-serde", "reth-network-p2p/serde"] serde = ["dep:serde", "dep:humantime-serde", "reth-network-p2p/serde"]

View File

@ -24,10 +24,9 @@ pub use reth_network_p2p::reputation::{Reputation, ReputationChangeKind, Reputat
pub use backoff::BackoffKind; pub use backoff::BackoffKind;
pub use peers::{ pub use peers::{
addr::PeerAddr, addr::PeerAddr,
handle::PeersHandle,
kind::PeerKind, kind::PeerKind,
reputation::{is_banned_reputation, ReputationChangeOutcome, DEFAULT_REPUTATION}, reputation::{is_banned_reputation, ReputationChangeOutcome, DEFAULT_REPUTATION},
state::PeerConnectionState, state::PeerConnectionState,
ConnectionsConfig, Peer, PeerCommand, PeersConfig, ConnectionsConfig, Peer, PeersConfig,
}; };
pub use session::{SessionLimits, SessionsConfig}; pub use session::{SessionLimits, SessionsConfig};

View File

@ -1,25 +0,0 @@
//! Commands sent to peer manager.
use std::net::SocketAddr;
use reth_network_peers::{NodeRecord, PeerId};
use tokio::sync::oneshot;
use crate::{Peer, ReputationChangeKind};
/// Commands the `PeersManager` listens for.
#[derive(Debug)]
pub enum PeerCommand {
/// Command for manually add
Add(PeerId, SocketAddr),
/// Remove a peer from the set
///
/// If currently connected this will disconnect the session
Remove(PeerId),
/// Apply a reputation change to the given peer.
ReputationChange(PeerId, ReputationChangeKind),
/// Get information about a peer
GetPeer(PeerId, oneshot::Sender<Option<Peer>>),
/// Get node information on all peers
GetPeers(oneshot::Sender<Vec<NodeRecord>>),
}

View File

@ -1,13 +1,10 @@
pub mod addr; pub mod addr;
pub mod cmd;
pub mod config; pub mod config;
pub mod handle;
pub mod kind; pub mod kind;
pub mod state; pub mod state;
pub use reth_network_p2p::reputation; pub use reth_network_p2p::reputation;
pub use cmd::PeerCommand;
pub use config::{ConnectionsConfig, PeersConfig}; pub use config::{ConnectionsConfig, PeersConfig};
pub use reputation::ReputationChangeWeights; pub use reputation::ReputationChangeWeights;

View File

@ -1,6 +1,6 @@
//! Builder support for configuring the entire setup. //! Builder support for configuring the entire setup.
use reth_network_api::PeersHandleProvider; use reth_network_api::test_utils::PeersHandleProvider;
use reth_transaction_pool::TransactionPool; use reth_transaction_pool::TransactionPool;
use tokio::sync::mpsc; use tokio::sync::mpsc;

View File

@ -13,9 +13,9 @@ use reth_eth_wire::{
BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, GetReceipts, BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, GetReceipts,
HeadersDirection, NodeData, Receipts, HeadersDirection, NodeData, Receipts,
}; };
use reth_network_api::test_utils::PeersHandle;
use reth_network_p2p::error::RequestResult; use reth_network_p2p::error::RequestResult;
use reth_network_peers::PeerId; use reth_network_peers::PeerId;
use reth_network_types::PeersHandle;
use reth_primitives::{BlockBody, BlockHashOrNumber, Header}; use reth_primitives::{BlockBody, BlockHashOrNumber, Header};
use reth_storage_api::{BlockReader, HeaderProvider, ReceiptProvider}; use reth_storage_api::{BlockReader, HeaderProvider, ReceiptProvider};
use tokio::sync::{mpsc::Receiver, oneshot}; use tokio::sync::{mpsc::Receiver, oneshot};

View File

@ -6,6 +6,7 @@ use std::sync::{
}; };
use futures::{future, future::Either}; use futures::{future, future::Either};
use reth_network_api::test_utils::PeersHandle;
use reth_network_p2p::{ use reth_network_p2p::{
bodies::client::{BodiesClient, BodiesFut}, bodies::client::{BodiesClient, BodiesFut},
download::DownloadClient, download::DownloadClient,
@ -14,7 +15,7 @@ use reth_network_p2p::{
priority::Priority, priority::Priority,
}; };
use reth_network_peers::PeerId; use reth_network_peers::PeerId;
use reth_network_types::{PeersHandle, ReputationChangeKind}; use reth_network_types::ReputationChangeKind;
use reth_primitives::{Header, B256}; use reth_primitives::{Header, B256};
use tokio::sync::{mpsc::UnboundedSender, oneshot}; use tokio::sync::{mpsc::UnboundedSender, oneshot};

View File

@ -15,13 +15,14 @@ use std::{
use futures::StreamExt; use futures::StreamExt;
use reth_eth_wire::{GetBlockBodies, GetBlockHeaders}; use reth_eth_wire::{GetBlockBodies, GetBlockHeaders};
use reth_network_api::test_utils::PeersHandle;
use reth_network_p2p::{ use reth_network_p2p::{
error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult}, error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult},
headers::client::HeadersRequest, headers::client::HeadersRequest,
priority::Priority, priority::Priority,
}; };
use reth_network_peers::PeerId; use reth_network_peers::PeerId;
use reth_network_types::{PeersHandle, ReputationChangeKind}; use reth_network_types::ReputationChangeKind;
use reth_primitives::{BlockBody, Header, B256}; use reth_primitives::{BlockBody, Header, B256};
use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot}; use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot};
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;

View File

@ -138,9 +138,7 @@ mod state;
mod swarm; mod swarm;
pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols}; pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols};
pub use reth_network_api::{ pub use reth_network_api::{BlockDownloaderProvider, NetworkInfo, Peers, PeersInfo};
BlockDownloaderProvider, NetworkInfo, Peers, PeersHandleProvider, PeersInfo,
};
pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState}; pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState};
pub use reth_network_types::{PeersConfig, SessionsConfig}; pub use reth_network_types::{PeersConfig, SessionsConfig};
pub use session::{ pub use session::{

View File

@ -34,9 +34,9 @@ use reth_eth_wire::{
}; };
use reth_fs_util::{self as fs, FsPathError}; use reth_fs_util::{self as fs, FsPathError};
use reth_metrics::common::mpsc::UnboundedMeteredSender; use reth_metrics::common::mpsc::UnboundedMeteredSender;
use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo}; use reth_network_api::{test_utils::PeersHandle, EthProtocolInfo, NetworkStatus, PeerInfo};
use reth_network_peers::{NodeRecord, PeerId}; use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{PeerAddr, PeersHandle, ReputationChangeKind}; use reth_network_types::{PeerAddr, ReputationChangeKind};
use reth_primitives::ForkId; use reth_primitives::ForkId;
use reth_storage_api::BlockNumReader; use reth_storage_api::BlockNumReader;
use reth_tasks::shutdown::GracefulShutdown; use reth_tasks::shutdown::GracefulShutdown;

View File

@ -11,15 +11,15 @@ use parking_lot::Mutex;
use reth_discv4::Discv4; use reth_discv4::Discv4;
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions}; use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_network_api::{ use reth_network_api::{
BlockDownloaderProvider, NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers, test_utils::{PeersHandle, PeersHandleProvider},
PeersHandleProvider, PeersInfo, BlockDownloaderProvider, NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers, PeersInfo,
}; };
use reth_network_p2p::{ use reth_network_p2p::{
sync::{NetworkSyncUpdater, SyncState, SyncStateProvider}, sync::{NetworkSyncUpdater, SyncState, SyncStateProvider},
BlockClient, BlockClient,
}; };
use reth_network_peers::{NodeRecord, PeerId}; use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{PeerAddr, PeerKind, PeersHandle, Reputation, ReputationChangeKind}; use reth_network_types::{PeerAddr, PeerKind, Reputation, ReputationChangeKind};
use reth_primitives::{Head, TransactionSigned, B256}; use reth_primitives::{Head, TransactionSigned, B256};
use reth_tokio_util::{EventSender, EventStream}; use reth_tokio_util::{EventSender, EventStream};
use secp256k1::SecretKey; use secp256k1::SecretKey;

View File

@ -12,14 +12,15 @@ use std::{
use futures::StreamExt; use futures::StreamExt;
use reth_eth_wire::{errors::EthStreamError, DisconnectReason}; use reth_eth_wire::{errors::EthStreamError, DisconnectReason};
use reth_net_banlist::BanList; use reth_net_banlist::BanList;
use reth_network_api::test_utils::{PeerCommand, PeersHandle};
use reth_network_peers::{NodeRecord, PeerId}; use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{ use reth_network_types::{
peers::{ peers::{
config::PeerBackoffDurations, config::PeerBackoffDurations,
reputation::{DEFAULT_REPUTATION, MAX_TRUSTED_PEER_REPUTATION_CHANGE}, reputation::{DEFAULT_REPUTATION, MAX_TRUSTED_PEER_REPUTATION_CHANGE},
}, },
ConnectionsConfig, Peer, PeerAddr, PeerCommand, PeerConnectionState, PeerKind, PeersConfig, ConnectionsConfig, Peer, PeerAddr, PeerConnectionState, PeerKind, PeersConfig,
PeersHandle, ReputationChangeKind, ReputationChangeOutcome, ReputationChangeWeights, ReputationChangeKind, ReputationChangeOutcome, ReputationChangeWeights,
}; };
use reth_primitives::ForkId; use reth_primitives::ForkId;
use thiserror::Error; use thiserror::Error;

View File

@ -12,9 +12,11 @@ use futures::{FutureExt, StreamExt};
use pin_project::pin_project; use pin_project::pin_project;
use reth_chainspec::MAINNET; use reth_chainspec::MAINNET;
use reth_eth_wire::{protocol::Protocol, DisconnectReason, HelloMessageWithProtocols}; use reth_eth_wire::{protocol::Protocol, DisconnectReason, HelloMessageWithProtocols};
use reth_network_api::{NetworkInfo, Peers, PeersHandleProvider}; use reth_network_api::{
test_utils::{PeersHandle, PeersHandleProvider},
NetworkInfo, Peers,
};
use reth_network_peers::PeerId; use reth_network_peers::PeerId;
use reth_network_types::PeersHandle;
use reth_provider::test_utils::NoopProvider; use reth_provider::test_utils::NoopProvider;
use reth_storage_api::{BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory}; use reth_storage_api::{BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory};
use reth_tasks::TokioTaskExecutor; use reth_tasks::TokioTaskExecutor;

View File

@ -15,9 +15,9 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use reth::builder::NodeHandle; use reth::builder::NodeHandle;
use reth_network::{ use reth_network::{
config::SecretKey, protocol::IntoRlpxSubProtocol, NetworkConfig, NetworkManager, config::SecretKey, protocol::IntoRlpxSubProtocol, NetworkConfig, NetworkManager,
NetworkProtocols, PeersHandleProvider, NetworkProtocols,
}; };
use reth_network_api::NetworkInfo; use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo};
use reth_node_ethereum::EthereumNode; use reth_node_ethereum::EthereumNode;
use reth_provider::test_utils::NoopProvider; use reth_provider::test_utils::NoopProvider;
use subprotocol::{ use subprotocol::{