chore: relax Arc requirement for provider (#1429)

This commit is contained in:
Matthias Seitz
2023-02-17 14:40:20 +01:00
committed by GitHub
parent c834aee846
commit 7c9b212b4a
15 changed files with 70 additions and 79 deletions

View File

@@ -271,7 +271,7 @@ impl Command {
_pool: (),
) -> Result<NetworkHandle, NetworkError>
where
C: BlockProvider + HeaderProvider + 'static,
C: BlockProvider + HeaderProvider + Clone + Unpin + 'static,
{
let client = config.client.clone();
let (handle, network, _txpool, eth) =
@@ -324,7 +324,7 @@ impl Command {
.network_config(config, self.chain.clone())
.with_task_executor(Box::new(executor))
.set_head(head)
.build(Arc::new(ShareableDatabase::new(db, self.chain.clone())))
.build(ShareableDatabase::new(db, self.chain.clone()))
}
async fn build_pipeline<H, B, U>(
@@ -378,7 +378,7 @@ async fn run_network_until_shutdown<C>(
network: NetworkManager<C>,
persistent_peers_file: Option<PathBuf>,
) where
C: BlockProvider + HeaderProvider + 'static,
C: BlockProvider + HeaderProvider + Clone + Unpin + 'static,
{
pin_mut!(network, shutdown);

View File

@@ -5,7 +5,6 @@ use crate::{
NetworkManager,
};
use reth_transaction_pool::TransactionPool;
use std::sync::Arc;
use tokio::sync::mpsc;
/// A builder that can configure all components of the network.
@@ -47,7 +46,7 @@ impl<C, Tx, Eth> NetworkBuilder<C, Tx, Eth> {
/// Creates a new [`EthRequestHandler`] and wires it to the network.
pub fn request_handler<Client>(
self,
client: Arc<Client>,
client: Client,
) -> NetworkBuilder<C, Tx, EthRequestHandler<Client>> {
let NetworkBuilder { mut network, transactions, .. } = self;
let (tx, rx) = mpsc::unbounded_channel();

View File

@@ -15,7 +15,6 @@ use secp256k1::{SecretKey, SECP256K1};
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
sync::Arc,
};
/// reexports for convenience
@@ -37,7 +36,7 @@ pub fn rng_secret_key() -> SecretKey {
/// All network related initialization settings.
pub struct NetworkConfig<C> {
/// The client type that can interact with the chain.
pub client: Arc<C>,
pub client: C,
/// The node's secret key, from which the node's identity is derived.
pub secret_key: SecretKey,
/// All boot nodes to start network discovery with.
@@ -79,7 +78,7 @@ pub struct NetworkConfig<C> {
impl<C> NetworkConfig<C> {
/// Create a new instance with all mandatory fields set, rest is field with defaults.
pub fn new(client: Arc<C>, secret_key: SecretKey) -> Self {
pub fn new(client: C, secret_key: SecretKey) -> Self {
Self::builder(secret_key).build(client)
}
@@ -103,7 +102,7 @@ impl<C> NetworkConfig<C> {
impl<C> NetworkConfig<C>
where
C: BlockProvider + HeaderProvider + 'static,
C: BlockProvider + HeaderProvider + Clone + Unpin + 'static,
{
/// Starts the networking stack given a [NetworkConfig] and returns a handle to the network.
pub async fn start_network(self) -> Result<NetworkHandle, NetworkError> {
@@ -288,7 +287,7 @@ impl NetworkConfigBuilder {
/// Consumes the type and creates the actual [`NetworkConfig`]
/// for the given client type that can interact with the chain.
pub fn build<C>(self, client: Arc<C>) -> NetworkConfig<C> {
pub fn build<C>(self, client: C) -> NetworkConfig<C> {
let peer_id = self.get_peer_id();
let Self {
secret_key,
@@ -402,7 +401,7 @@ mod tests {
#[test]
fn test_network_dns_defaults() {
let config = builder().build(Arc::new(NoopProvider::default()));
let config = builder().build(NoopProvider::default());
let dns = config.dns_discovery_config.unwrap();
let bootstrap_nodes = dns.bootstrap_dns_networks.unwrap();
@@ -423,7 +422,7 @@ mod tests {
let genesis_fork_hash = ForkHash::from(chain_spec.genesis_hash());
// enforce that the fork_id set in the status is consistent with the generated fork filter
let config = builder().chain_spec(chain_spec).build(Arc::new(NoopProvider::default()));
let config = builder().chain_spec(chain_spec).build(NoopProvider::default());
let status = config.status;
let fork_filter = config.fork_filter;

View File

@@ -14,7 +14,6 @@ use std::{
future::Future,
hash::Hash,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::sync::{mpsc::UnboundedReceiver, oneshot};
@@ -49,7 +48,7 @@ const APPROX_HEADER_SIZE: usize = 500;
#[must_use = "Manager does nothing unless polled."]
pub struct EthRequestHandler<C> {
/// The client type that can interact with the chain.
client: Arc<C>,
client: C,
/// Used for reporting peers.
#[allow(unused)]
// TODO use to report spammers
@@ -62,7 +61,7 @@ pub struct EthRequestHandler<C> {
impl<C> EthRequestHandler<C> {
/// Create a new instance
pub fn new(
client: Arc<C>,
client: C,
peers: PeersHandle,
incoming: UnboundedReceiver<IncomingEthRequest>,
) -> Self {
@@ -195,7 +194,7 @@ where
/// This should be spawned or used as part of `tokio::select!`.
impl<C> Future for EthRequestHandler<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Unpin,
{
type Output = ();

View File

@@ -55,13 +55,12 @@
//!
//! ```
//! # async fn launch() {
//! use std::sync::Arc;
//! use reth_network::config::{rng_secret_key, mainnet_nodes};
//! use reth_network::{NetworkConfig, NetworkManager};
//! use reth_provider::test_utils::NoopProvider;
//!
//! // This block provider implementation is used for testing purposes.
//! let client = Arc::new(NoopProvider::default());
//! let client = NoopProvider::default();
//!
//! // The key that's used for encrypting sessions and to identify our node.
//! let local_key = rng_secret_key();
@@ -85,19 +84,18 @@
//! ```
//! use reth_provider::test_utils::NoopProvider;
//! use reth_transaction_pool::TransactionPool;
//! use std::sync::Arc;
//! use reth_discv4::bootnodes::mainnet_nodes;
//! use reth_network::config::rng_secret_key;
//! use reth_network::{NetworkConfig, NetworkManager};
//! async fn launch<Pool: TransactionPool>(pool: Pool) {
//! // This block provider implementation is used for testing purposes.
//! let client = Arc::new(NoopProvider::default());
//! let client = NoopProvider::default();
//!
//! // The key that's used for encrypting sessions and to identify our node.
//! let local_key = rng_secret_key();
//!
//! let config =
//! NetworkConfig::<NoopProvider>::builder(local_key).boot_nodes(mainnet_nodes()).build(Arc::clone(&client));
//! NetworkConfig::<NoopProvider>::builder(local_key).boot_nodes(mainnet_nodes()).build(client.clone());
//!
//! // create the network instance
//! let (handle, network, transactions, request_handler) = NetworkManager::builder(config)

View File

@@ -240,19 +240,18 @@ where
/// ```
/// use reth_provider::test_utils::NoopProvider;
/// use reth_transaction_pool::TransactionPool;
/// use std::sync::Arc;
/// use reth_discv4::bootnodes::mainnet_nodes;
/// use reth_network::config::rng_secret_key;
/// use reth_network::{NetworkConfig, NetworkManager};
/// async fn launch<Pool: TransactionPool>(pool: Pool) {
/// // This block provider implementation is used for testing purposes.
/// let client = Arc::new(NoopProvider::default());
/// let client = NoopProvider::default();
///
/// // The key that's used for encrypting sessions and to identify our node.
/// let local_key = rng_secret_key();
///
/// let config =
/// NetworkConfig::<NoopProvider>::builder(local_key).boot_nodes(mainnet_nodes()).build(Arc::clone(&client));
/// NetworkConfig::<NoopProvider>::builder(local_key).boot_nodes(mainnet_nodes()).build(client.clone());
///
/// // create the network instance
/// let (handle, network, transactions, request_handler) = NetworkManager::builder(config)
@@ -555,7 +554,7 @@ where
impl<C> Future for NetworkManager<C>
where
C: BlockProvider,
C: BlockProvider + Unpin,
{
type Output = ();

View File

@@ -51,7 +51,7 @@ pub struct NetworkState<C> {
/// Buffered messages until polled.
queued_messages: VecDeque<StateAction>,
/// The client type that can interact with the chain.
client: Arc<C>,
client: C,
/// Network discovery.
discovery: Discovery,
/// The genesis hash of the network we're on
@@ -69,7 +69,7 @@ where
{
/// Create a new state instance with the given params
pub(crate) fn new(
client: Arc<C>,
client: C,
discovery: Discovery,
peers_manager: PeersManager,
genesis_hash: H256,
@@ -531,7 +531,7 @@ mod tests {
active_peers: Default::default(),
peers_manager: Default::default(),
queued_messages: Default::default(),
client: Arc::new(NoopProvider::default()),
client: NoopProvider::default(),
discovery: Discovery::noop(),
genesis_hash: Default::default(),
state_fetcher: StateFetcher::new(handle, Default::default()),

View File

@@ -286,7 +286,7 @@ where
impl<C> Stream for Swarm<C>
where
C: BlockProvider,
C: BlockProvider + Unpin,
{
type Item = SwarmEvent;

View File

@@ -15,7 +15,6 @@ use std::{
future::Future,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::{
@@ -35,18 +34,18 @@ pub struct Testnet<C> {
impl<C> Testnet<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Clone,
{
/// Same as [`Self::try_create_with`] but panics on error
pub async fn create_with(num_peers: usize, provider: Arc<C>) -> Self {
pub async fn create_with(num_peers: usize, provider: C) -> Self {
Self::try_create_with(num_peers, provider).await.unwrap()
}
/// Creates a new [`Testnet`] with the given number of peers and the provider.
pub async fn try_create_with(num_peers: usize, provider: Arc<C>) -> Result<Self, NetworkError> {
pub async fn try_create_with(num_peers: usize, provider: C) -> Result<Self, NetworkError> {
let mut this = Self { peers: Vec::with_capacity(num_peers) };
for _ in 0..num_peers {
let config = PeerConfig::new(Arc::clone(&provider));
let config = PeerConfig::new(provider.clone());
this.add_peer_with_config(config).await?;
}
Ok(this)
@@ -123,7 +122,7 @@ where
impl<C> Testnet<C>
where
C: BlockProvider + HeaderProvider + 'static,
C: BlockProvider + HeaderProvider + Unpin + 'static,
{
/// Spawns the testnet to a separate task
pub fn spawn(self) -> TestnetHandle<C> {
@@ -177,7 +176,7 @@ impl<C> fmt::Debug for Testnet<C> {
impl<C> Future for Testnet<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Unpin,
{
type Output = ();
@@ -213,7 +212,7 @@ pub struct Peer<C> {
network: NetworkManager<C>,
#[pin]
request_handler: Option<EthRequestHandler<C>>,
client: Arc<C>,
client: C,
secret_key: SecretKey,
}
@@ -221,7 +220,7 @@ pub struct Peer<C> {
impl<C> Peer<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Clone,
{
/// Returns the number of connected peers.
pub fn num_peers(&self) -> usize {
@@ -243,14 +242,14 @@ where
let (tx, rx) = unbounded_channel();
self.network.set_eth_request_handler(tx);
let peers = self.network.peers_handle();
let request_handler = EthRequestHandler::new(Arc::clone(&self.client), peers, rx);
let request_handler = EthRequestHandler::new(self.client.clone(), peers, rx);
self.request_handler = Some(request_handler);
}
}
impl<C> Future for Peer<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Unpin,
{
type Output = ();
@@ -268,7 +267,7 @@ where
/// A helper config for setting up the reth networking stack.
pub struct PeerConfig<C = NoopProvider> {
config: NetworkConfig<C>,
client: Arc<C>,
client: C,
secret_key: SecretKey,
}
@@ -276,7 +275,7 @@ pub struct PeerConfig<C = NoopProvider> {
impl<C> PeerConfig<C>
where
C: BlockProvider + HeaderProvider,
C: BlockProvider + HeaderProvider + Clone,
{
/// Launches the network and returns the [Peer] that manages it
pub async fn launch(self) -> Result<Peer<C>, NetworkError> {
@@ -288,27 +287,27 @@ where
/// Initialize the network with a random secret key, allowing the devp2p and discovery to bind
/// to any available IP and port.
pub fn new(client: Arc<C>) -> Self {
pub fn new(client: C) -> Self {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let config = Self::network_config_builder(secret_key).build(Arc::clone(&client));
let config = Self::network_config_builder(secret_key).build(client.clone());
Self { config, client, secret_key }
}
/// Initialize the network with a given secret key, allowing devp2p and discovery to bind any
/// available IP and port.
pub fn with_secret_key(client: Arc<C>, secret_key: SecretKey) -> Self {
let config = Self::network_config_builder(secret_key).build(Arc::clone(&client));
pub fn with_secret_key(client: C, secret_key: SecretKey) -> Self {
let config = Self::network_config_builder(secret_key).build(client.clone());
Self { config, client, secret_key }
}
/// Initialize the network with a given capabilities.
pub fn with_capabilities(client: Arc<C>, capabilities: Vec<Capability>) -> Self {
pub fn with_capabilities(client: C, capabilities: Vec<Capability>) -> Self {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let builder = Self::network_config_builder(secret_key);
let hello_message =
HelloBuilder::new(builder.get_peer_id()).capabilities(capabilities).build();
let config = builder.hello_message(hello_message).build(Arc::clone(&client));
let config = builder.hello_message(hello_message).build(client.clone());
Self { config, client, secret_key }
}
@@ -324,7 +323,7 @@ where
impl Default for PeerConfig {
fn default() -> Self {
Self::new(Arc::new(NoopProvider::default()))
Self::new(NoopProvider::default())
}
}

View File

@@ -570,9 +570,9 @@ mod tests {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let client = Arc::new(NoopProvider::default());
let client = NoopProvider::default();
let pool = testing_pool();
let config = NetworkConfigBuilder::new(secret_key).build(Arc::clone(&client));
let config = NetworkConfigBuilder::new(secret_key).build(client);
let (handle, network, mut transactions, _) = NetworkManager::new(config)
.await
.unwrap()

View File

@@ -21,7 +21,7 @@ use reth_primitives::{HeadersDirection, NodeRecord, PeerId};
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::test_utils::testing_pool;
use secp256k1::SecretKey;
use std::{collections::HashSet, net::SocketAddr, sync::Arc, time::Duration};
use std::{collections::HashSet, net::SocketAddr, time::Duration};
use tokio::task;
#[tokio::test(flavor = "multi_thread")]
@@ -90,12 +90,12 @@ async fn test_already_connected() {
let mut net = Testnet::default();
let secret_key = SecretKey::new(&mut rand::thread_rng());
let client = Arc::new(NoopProvider::default());
let client = NoopProvider::default();
let p1 = PeerConfig::default();
// initialize two peers with the same identifier
let p2 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key);
let p3 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key);
let p2 = PeerConfig::with_secret_key(client, secret_key);
let p3 = PeerConfig::with_secret_key(client, secret_key);
net.extend_peer_with_config(vec![p1, p2, p3]).await.unwrap();
@@ -135,11 +135,11 @@ async fn test_get_peer() {
let mut net = Testnet::default();
let secret_key = SecretKey::new(&mut rand::thread_rng());
let secret_key_1 = SecretKey::new(&mut rand::thread_rng());
let client = Arc::new(NoopProvider::default());
let client = NoopProvider::default();
let p1 = PeerConfig::default();
let p2 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key);
let p3 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key_1);
let p2 = PeerConfig::with_secret_key(client, secret_key);
let p3 = PeerConfig::with_secret_key(client, secret_key_1);
net.extend_peer_with_config(vec![p1, p2, p3]).await.unwrap();
let mut handles = net.handles();
@@ -169,10 +169,10 @@ async fn test_get_peer_by_id() {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let secret_key_1 = SecretKey::new(&mut rand::thread_rng());
let client = Arc::new(NoopProvider::default());
let client = NoopProvider::default();
let p1 = PeerConfig::default();
let p2 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key);
let p3 = PeerConfig::with_secret_key(Arc::clone(&client), secret_key_1);
let p2 = PeerConfig::with_secret_key(client, secret_key);
let p3 = PeerConfig::with_secret_key(client, secret_key_1);
net.extend_peer_with_config(vec![p1, p2, p3]).await.unwrap();
@@ -204,9 +204,8 @@ async fn test_connect_with_boot_nodes() {
let mut discv4 = Discv4Config::builder();
discv4.add_boot_nodes(mainnet_nodes());
let config = NetworkConfigBuilder::new(secret_key)
.discovery(discv4)
.build(Arc::new(NoopProvider::default()));
let config =
NetworkConfigBuilder::new(secret_key).discovery(discv4).build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
let handle = network.handle().clone();
@@ -226,8 +225,8 @@ async fn test_connect_with_builder() {
let mut discv4 = Discv4Config::builder();
discv4.add_boot_nodes(mainnet_nodes());
let client = Arc::new(NoopProvider::default());
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(Arc::clone(&client));
let client = NoopProvider::default();
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(client);
let (handle, network, _, requests) = NetworkManager::new(config)
.await
.unwrap()
@@ -262,8 +261,8 @@ async fn test_connect_to_trusted_peer() {
let secret_key = SecretKey::new(&mut rand::thread_rng());
let discv4 = Discv4Config::builder();
let client = Arc::new(NoopProvider::default());
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(Arc::clone(&client));
let client = NoopProvider::default();
let config = NetworkConfigBuilder::new(secret_key).discovery(discv4).build(client);
let (handle, network, transactions, requests) = NetworkManager::new(config)
.await
.unwrap()
@@ -332,7 +331,7 @@ async fn test_incoming_node_id_blacklist() {
.listener_addr(reth_p2p)
.discovery_addr(reth_disc)
.peer_config(peer_config)
.build(Arc::new(NoopProvider::default()));
.build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
@@ -380,7 +379,7 @@ async fn test_incoming_connect_with_single_geth() {
let config = NetworkConfigBuilder::new(secret_key)
.listener_addr(reth_p2p)
.discovery_addr(reth_disc)
.build(Arc::new(NoopProvider::default()));
.build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
@@ -414,7 +413,7 @@ async fn test_outgoing_connect_with_single_geth() {
let config = NetworkConfigBuilder::new(secret_key)
.listener_addr(reth_p2p)
.discovery_addr(reth_disc)
.build(Arc::new(NoopProvider::default()));
.build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
let handle = network.handle().clone();
@@ -459,7 +458,7 @@ async fn test_geth_disconnect() {
let config = NetworkConfigBuilder::new(secret_key)
.listener_addr(reth_p2p)
.discovery_addr(reth_disc)
.build(Arc::new(NoopProvider::default()));
.build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
let handle = network.handle().clone();
@@ -563,7 +562,7 @@ async fn test_disconnect_incoming_when_exceeded_incoming_connections() {
.listener_addr(reth_p2p)
.discovery_addr(reth_disc)
.peer_config(peers_config)
.build(Arc::new(NoopProvider::default()));
.build(NoopProvider::default());
let network = NetworkManager::new(config).await.unwrap();
let other_peer_handle = net.handles().next().unwrap();

View File

@@ -8,7 +8,6 @@ use reth_network::{
};
use reth_network_api::{NetworkInfo, Peers};
use reth_provider::test_utils::NoopProvider;
use std::sync::Arc;
#[tokio::test(flavor = "multi_thread")]
async fn test_session_established_with_highest_version() {
@@ -51,7 +50,7 @@ async fn test_session_established_with_different_capability() {
let mut net = Testnet::create(1).await;
let capabilities = vec![Capability::new("eth".into(), EthVersion::Eth66 as usize)];
let p1 = PeerConfig::with_capabilities(Arc::new(NoopProvider::default()), capabilities);
let p1 = PeerConfig::with_capabilities(NoopProvider::default(), capabilities);
net.add_peer_with_config(p1).await.unwrap();
net.for_each(|peer| assert_eq!(0, peer.num_peers()));

View File

@@ -1,6 +1,5 @@
use crate::HeaderProvider;
use super::BlockHashProvider;
use crate::HeaderProvider;
use reth_interfaces::Result;
use reth_primitives::{
rpc::{BlockId, BlockNumber},
@@ -8,6 +7,7 @@ use reth_primitives::{
};
/// Api trait for fetching `Block` related data.
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockProvider: BlockHashProvider + HeaderProvider + Send + Sync {
/// Returns the current info for the chain.
fn chain_info(&self) -> Result<ChainInfo>;

View File

@@ -3,7 +3,7 @@ use reth_interfaces::Result;
use reth_primitives::{H256, U256};
/// Client trait for fetching block hashes by number.
#[auto_impl(&)]
#[auto_impl(&, Arc)]
pub trait BlockHashProvider: Send + Sync {
/// Get the hash of the block with the given number. Returns `None` if no block with this number
/// exists.

View File

@@ -4,7 +4,7 @@ use reth_primitives::{BlockHash, BlockHashOrNumber, BlockNumber, Header, U256};
use std::ops::RangeBounds;
/// Client trait for fetching `Header` related data.
#[auto_impl(&)]
#[auto_impl(&, Arc)]
pub trait HeaderProvider: Send + Sync {
/// Check if block is known
fn is_known(&self, block_hash: &BlockHash) -> Result<bool> {