mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: make downloaders and clients generic over block parts (#12469)
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
//! API related to syncing blocks.
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use futures::Future;
|
||||
use reth_network_p2p::BlockClient;
|
||||
use tokio::sync::oneshot;
|
||||
@ -7,10 +9,13 @@ use tokio::sync::oneshot;
|
||||
/// Provides client for downloading blocks.
|
||||
#[auto_impl::auto_impl(&, Arc)]
|
||||
pub trait BlockDownloaderProvider {
|
||||
/// The client this type can provide.
|
||||
type Client: BlockClient<Header: Debug, Body: Debug> + Send + Sync + Clone + 'static;
|
||||
|
||||
/// 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;
|
||||
) -> impl Future<Output = Result<Self::Client, oneshot::error::RecvError>> + Send;
|
||||
}
|
||||
|
||||
@ -4,8 +4,9 @@ use std::{fmt, net::SocketAddr, sync::Arc};
|
||||
|
||||
use reth_eth_wire_types::{
|
||||
message::RequestPair, BlockBodies, BlockHeaders, Capabilities, DisconnectReason, EthMessage,
|
||||
EthVersion, GetBlockBodies, GetBlockHeaders, GetNodeData, GetPooledTransactions, GetReceipts,
|
||||
NodeData, PooledTransactions, Receipts, Status,
|
||||
EthNetworkPrimitives, EthVersion, GetBlockBodies, GetBlockHeaders, GetNodeData,
|
||||
GetPooledTransactions, GetReceipts, NetworkPrimitives, NodeData, PooledTransactions, Receipts,
|
||||
Status,
|
||||
};
|
||||
use reth_ethereum_forks::ForkId;
|
||||
use reth_network_p2p::error::{RequestError, RequestResult};
|
||||
@ -30,8 +31,8 @@ pub trait NetworkEventListenerProvider: Send + Sync {
|
||||
///
|
||||
/// This includes any event types that may be relevant to tasks, for metrics, keep track of peers
|
||||
/// etc.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NetworkEvent {
|
||||
#[derive(Debug)]
|
||||
pub enum NetworkEvent<R = PeerRequest> {
|
||||
/// Closed the peer session.
|
||||
SessionClosed {
|
||||
/// The identifier of the peer to which a session was closed.
|
||||
@ -50,7 +51,7 @@ pub enum NetworkEvent {
|
||||
/// Capabilities the peer announced
|
||||
capabilities: Arc<Capabilities>,
|
||||
/// A request channel to the session task.
|
||||
messages: PeerRequestSender,
|
||||
messages: PeerRequestSender<R>,
|
||||
/// The status of the peer to which a session was established.
|
||||
status: Arc<Status>,
|
||||
/// negotiated eth version of the session
|
||||
@ -62,6 +63,35 @@ pub enum NetworkEvent {
|
||||
PeerRemoved(PeerId),
|
||||
}
|
||||
|
||||
impl<R> Clone for NetworkEvent<R> {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Self::SessionClosed { peer_id, reason } => {
|
||||
Self::SessionClosed { peer_id: *peer_id, reason: *reason }
|
||||
}
|
||||
Self::SessionEstablished {
|
||||
peer_id,
|
||||
remote_addr,
|
||||
client_version,
|
||||
capabilities,
|
||||
messages,
|
||||
status,
|
||||
version,
|
||||
} => Self::SessionEstablished {
|
||||
peer_id: *peer_id,
|
||||
remote_addr: *remote_addr,
|
||||
client_version: client_version.clone(),
|
||||
capabilities: capabilities.clone(),
|
||||
messages: messages.clone(),
|
||||
status: status.clone(),
|
||||
version: *version,
|
||||
},
|
||||
Self::PeerAdded(peer) => Self::PeerAdded(*peer),
|
||||
Self::PeerRemoved(peer) => Self::PeerRemoved(*peer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Events produced by the `Discovery` manager.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum DiscoveryEvent {
|
||||
@ -98,7 +128,7 @@ pub enum DiscoveredEvent {
|
||||
|
||||
/// Protocol related request messages that expect a response
|
||||
#[derive(Debug)]
|
||||
pub enum PeerRequest {
|
||||
pub enum PeerRequest<N: NetworkPrimitives = EthNetworkPrimitives> {
|
||||
/// Requests block headers from the peer.
|
||||
///
|
||||
/// The response should be sent through the channel.
|
||||
@ -106,7 +136,7 @@ pub enum PeerRequest {
|
||||
/// The request for block headers.
|
||||
request: GetBlockHeaders,
|
||||
/// The channel to send the response for block headers.
|
||||
response: oneshot::Sender<RequestResult<BlockHeaders>>,
|
||||
response: oneshot::Sender<RequestResult<BlockHeaders<N::BlockHeader>>>,
|
||||
},
|
||||
/// Requests block bodies from the peer.
|
||||
///
|
||||
@ -115,7 +145,7 @@ pub enum PeerRequest {
|
||||
/// The request for block bodies.
|
||||
request: GetBlockBodies,
|
||||
/// The channel to send the response for block bodies.
|
||||
response: oneshot::Sender<RequestResult<BlockBodies>>,
|
||||
response: oneshot::Sender<RequestResult<BlockBodies<N::BlockBody>>>,
|
||||
},
|
||||
/// Requests pooled transactions from the peer.
|
||||
///
|
||||
@ -148,7 +178,7 @@ pub enum PeerRequest {
|
||||
|
||||
// === impl PeerRequest ===
|
||||
|
||||
impl PeerRequest {
|
||||
impl<N: NetworkPrimitives> PeerRequest<N> {
|
||||
/// Invoked if we received a response which does not match the request
|
||||
pub fn send_bad_response(self) {
|
||||
self.send_err_response(RequestError::BadResponse)
|
||||
@ -166,7 +196,7 @@ impl PeerRequest {
|
||||
}
|
||||
|
||||
/// Returns the [`EthMessage`] for this type
|
||||
pub fn create_request_message(&self, request_id: u64) -> EthMessage {
|
||||
pub fn create_request_message(&self, request_id: u64) -> EthMessage<N> {
|
||||
match self {
|
||||
Self::GetBlockHeaders { request, .. } => {
|
||||
EthMessage::GetBlockHeaders(RequestPair { request_id, message: *request })
|
||||
@ -199,24 +229,29 @@ impl PeerRequest {
|
||||
}
|
||||
|
||||
/// A Cloneable connection for sending _requests_ directly to the session of a peer.
|
||||
#[derive(Clone)]
|
||||
pub struct PeerRequestSender {
|
||||
pub struct PeerRequestSender<R = PeerRequest> {
|
||||
/// id of the remote node.
|
||||
pub peer_id: PeerId,
|
||||
/// The Sender half connected to a session.
|
||||
pub to_session_tx: mpsc::Sender<PeerRequest>,
|
||||
pub to_session_tx: mpsc::Sender<R>,
|
||||
}
|
||||
|
||||
impl<R> Clone for PeerRequestSender<R> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { peer_id: self.peer_id, to_session_tx: self.to_session_tx.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
// === impl PeerRequestSender ===
|
||||
|
||||
impl PeerRequestSender {
|
||||
impl<R> PeerRequestSender<R> {
|
||||
/// Constructs a new sender instance that's wired to a session
|
||||
pub const fn new(peer_id: PeerId, to_session_tx: mpsc::Sender<PeerRequest>) -> Self {
|
||||
pub const fn new(peer_id: PeerId, to_session_tx: mpsc::Sender<R>) -> Self {
|
||||
Self { peer_id, to_session_tx }
|
||||
}
|
||||
|
||||
/// Attempts to immediately send a message on this Sender
|
||||
pub fn try_send(&self, req: PeerRequest) -> Result<(), mpsc::error::TrySendError<PeerRequest>> {
|
||||
pub fn try_send(&self, req: R) -> Result<(), mpsc::error::TrySendError<R>> {
|
||||
self.to_session_tx.try_send(req)
|
||||
}
|
||||
|
||||
@ -226,7 +261,7 @@ impl PeerRequestSender {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for PeerRequestSender {
|
||||
impl<R> fmt::Debug for PeerRequestSender<R> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("PeerRequestSender").field("peer_id", &self.peer_id).finish_non_exhaustive()
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ pub use events::{
|
||||
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
|
||||
|
||||
use reth_eth_wire_types::{capability::Capabilities, DisconnectReason, EthVersion, Status};
|
||||
use reth_network_p2p::EthBlockClient;
|
||||
use reth_network_peers::NodeRecord;
|
||||
|
||||
/// The `PeerId` type.
|
||||
@ -43,7 +44,7 @@ pub type PeerId = alloy_primitives::B512;
|
||||
|
||||
/// Helper trait that unifies network API needed to launch node.
|
||||
pub trait FullNetwork:
|
||||
BlockDownloaderProvider
|
||||
BlockDownloaderProvider<Client: EthBlockClient>
|
||||
+ NetworkSyncUpdater
|
||||
+ NetworkInfo
|
||||
+ NetworkEventListenerProvider
|
||||
@ -55,7 +56,7 @@ pub trait FullNetwork:
|
||||
}
|
||||
|
||||
impl<T> FullNetwork for T where
|
||||
T: BlockDownloaderProvider
|
||||
T: BlockDownloaderProvider<Client: EthBlockClient>
|
||||
+ NetworkSyncUpdater
|
||||
+ NetworkInfo
|
||||
+ NetworkEventListenerProvider
|
||||
|
||||
Reference in New Issue
Block a user