chore(net): move reth_network::BlockDownloaderProvider to reth-network-api (#10003)

This commit is contained in:
Emilia Hane
2024-08-03 15:42:44 +02:00
committed by GitHub
parent c2926de326
commit f3e79df300
7 changed files with 45 additions and 20 deletions

2
Cargo.lock generated
View File

@ -7558,7 +7558,9 @@ dependencies = [
"alloy-rpc-types-admin",
"auto_impl",
"enr",
"futures",
"reth-eth-wire",
"reth-network-p2p",
"reth-network-peers",
"reth-network-types",
"serde",

View File

@ -17,6 +17,7 @@ reth-eth-wire.workspace = true
alloy-rpc-types-admin.workspace = true
reth-network-peers.workspace = true
reth-network-types.workspace = true
reth-network-p2p.workspace = true
# ethereum
alloy-primitives.workspace = true
@ -24,6 +25,9 @@ alloy-primitives.workspace = true
# eth
enr = { workspace = true, default-features = false, features = ["rust-secp256k1"] }
# async
futures.workspace = true
# misc
thiserror.workspace = true
serde = { workspace = true, features = ["derive"], optional = true }

View File

@ -0,0 +1,16 @@
//! API related to syncing blocks.
use futures::Future;
use reth_network_p2p::BlockClient;
use tokio::sync::oneshot;
/// Provides client for downloading blocks.
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockDownloaderProvider {
/// Returns a new [`BlockClient`], used for fetching blocks from peers.
///
/// The client is the entrypoint for sending block requests to the network.
fn fetch_client(
&self,
) -> impl Future<Output = Result<impl BlockClient + 'static, oneshot::error::RecvError>> + Send;
}

View File

@ -13,15 +13,19 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
pub mod downloaders;
/// Network Error
pub mod error;
/// Implementation of network traits for that does nothing.
pub mod noop;
pub use alloy_rpc_types_admin::EthProtocolInfo;
pub use error::NetworkError;
pub use reth_network_p2p::BlockClient;
pub use reth_network_types::{PeerKind, PeersHandle, Reputation, ReputationChangeKind};
pub use downloaders::BlockDownloaderProvider;
pub use error::NetworkError;
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
use reth_eth_wire::{capability::Capabilities, DisconnectReason, EthVersion, Status};

View File

@ -138,7 +138,9 @@ mod state;
mod swarm;
pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols};
pub use reth_network_api::{NetworkInfo, Peers, PeersHandleProvider, PeersInfo};
pub use reth_network_api::{
BlockDownloaderProvider, NetworkInfo, Peers, PeersHandleProvider, PeersInfo,
};
pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState};
pub use reth_network_types::{PeersConfig, SessionsConfig};
pub use session::{
@ -155,8 +157,6 @@ pub use flattened_response::FlattenedResponse;
pub use manager::{DiscoveredEvent, NetworkEvent, NetworkManager};
pub use message::PeerRequest;
pub use metrics::TxTypesCounter;
pub use network::{
BlockDownloaderProvider, FullNetwork, NetworkEvents, NetworkHandle, NetworkProtocols,
};
pub use network::{FullNetwork, NetworkEvents, NetworkHandle, NetworkProtocols};
pub use swarm::NetworkConnectionState;
pub use transactions::{FilterAnnouncement, MessageFilter, ValidateTx68};

View File

@ -7,14 +7,17 @@ use std::{
};
use enr::Enr;
use futures::Future;
use parking_lot::Mutex;
use reth_discv4::Discv4;
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_network_api::{
NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers, PeersHandleProvider, PeersInfo,
BlockDownloaderProvider, NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers,
PeersHandleProvider, PeersInfo,
};
use reth_network_p2p::{
sync::{NetworkSyncUpdater, SyncState, SyncStateProvider},
BlockClient,
};
use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState, SyncStateProvider};
use reth_network_peers::{NodeRecord, PeerId};
use reth_network_types::{PeerAddr, PeerKind, PeersHandle, Reputation, ReputationChangeKind};
use reth_primitives::{Head, TransactionSigned, B256};
@ -393,19 +396,8 @@ impl NetworkSyncUpdater for NetworkHandle {
}
}
/// Provides [`FetchClient`] for downloading blocks.
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockDownloaderProvider {
/// Returns a new [`FetchClient`] that can be cloned and shared.
///
/// The [`FetchClient`] is the entrypoint for sending requests to the network.
fn fetch_client(
&self,
) -> impl Future<Output = Result<FetchClient, oneshot::error::RecvError>> + Send;
}
impl BlockDownloaderProvider for NetworkHandle {
async fn fetch_client(&self) -> Result<FetchClient, oneshot::error::RecvError> {
async fn fetch_client(&self) -> Result<impl BlockClient + 'static, oneshot::error::RecvError> {
let (tx, rx) = oneshot::channel();
let _ = self.manager().send(NetworkHandleMessage::FetchClient(tx));
rx.await

View File

@ -47,4 +47,11 @@ pub mod test_utils;
pub mod reputation;
pub use bodies::client::BodiesClient;
pub use headers::client::HeadersClient;
pub use reputation::{Reputation, ReputationChange, ReputationChangeKind, ReputationChangeWeights};
/// Helper trait that unifies network behaviour needed for fetching blocks.
pub trait BlockClient: HeadersClient + BodiesClient + Unpin + Clone {}
impl<T> BlockClient for T where T: HeadersClient + BodiesClient + Unpin + Clone {}