feat: make downloaders and clients generic over block parts (#12469)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Arsenii Kulikov
2024-11-12 19:13:21 +04:00
committed by GitHub
parent 3a337cd7d4
commit aece53ae88
60 changed files with 631 additions and 409 deletions

View File

@ -760,7 +760,7 @@ where
/// necessary
pub async fn max_block<C>(&self, client: C) -> eyre::Result<Option<BlockNumber>>
where
C: HeadersClient,
C: HeadersClient<Header = reth_primitives::Header>,
{
self.node_config().max_block(client, self.provider_factory().clone()).await
}

View File

@ -12,7 +12,7 @@ use reth_downloaders::{
use reth_evm::execute::BlockExecutorProvider;
use reth_exex::ExExManagerHandle;
use reth_network_p2p::{
bodies::downloader::BodyDownloader, headers::downloader::HeaderDownloader, BlockClient,
bodies::downloader::BodyDownloader, headers::downloader::HeaderDownloader, EthBlockClient,
};
use reth_provider::{providers::ProviderNodeTypes, ProviderFactory};
use reth_stages::{prelude::DefaultStages, stages::ExecutionStage, Pipeline, StageSet};
@ -38,7 +38,7 @@ pub fn build_networked_pipeline<N, Client, Executor>(
) -> eyre::Result<Pipeline<N>>
where
N: ProviderNodeTypes,
Client: BlockClient + 'static,
Client: EthBlockClient + 'static,
Executor: BlockExecutorProvider,
{
// building network downloaders using the fetch client
@ -84,8 +84,8 @@ pub fn build_pipeline<N, H, B, Executor>(
) -> eyre::Result<Pipeline<N>>
where
N: ProviderNodeTypes,
H: HeaderDownloader + 'static,
B: BodyDownloader + 'static,
H: HeaderDownloader<Header = reth_primitives::Header> + 'static,
B: BodyDownloader<Body = reth_primitives::BlockBody> + 'static,
Executor: BlockExecutorProvider,
{
let mut builder = Pipeline::<N>::builder();

View File

@ -13,7 +13,9 @@ workspace = true
[dependencies]
# reth
reth-chainspec.workspace = true
reth-consensus.workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-cli-util.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-storage-errors.workspace = true
@ -30,7 +32,6 @@ reth-discv4.workspace = true
reth-discv5.workspace = true
reth-net-nat.workspace = true
reth-network-peers.workspace = true
reth-consensus-common.workspace = true
reth-prune-types.workspace = true
reth-stages-types.workspace = true

View File

@ -8,6 +8,7 @@ use crate::{
dirs::{ChainPath, DataDirPath},
utils::get_single_header,
};
use alloy_consensus::BlockHeader;
use eyre::eyre;
use reth_chainspec::{ChainSpec, EthChainSpec, MAINNET};
use reth_config::config::PruneConfig;
@ -273,7 +274,7 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
) -> eyre::Result<Option<BlockNumber>>
where
Provider: HeaderProvider,
Client: HeadersClient,
Client: HeadersClient<Header: reth_primitives_traits::BlockHeader>,
{
let max_block = if let Some(block) = self.debug.max_block {
Some(block)
@ -332,7 +333,7 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
) -> ProviderResult<u64>
where
Provider: HeaderProvider,
Client: HeadersClient,
Client: HeadersClient<Header: reth_primitives_traits::BlockHeader>,
{
let header = provider.header_by_hash_or_number(tip.into())?;
@ -342,7 +343,7 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
return Ok(header.number)
}
Ok(self.fetch_tip_from_network(client, tip.into()).await.number)
Ok(self.fetch_tip_from_network(client, tip.into()).await.number())
}
/// Attempt to look up the block with the given number and return the header.
@ -352,9 +353,9 @@ impl<ChainSpec> NodeConfig<ChainSpec> {
&self,
client: Client,
tip: BlockHashOrNumber,
) -> SealedHeader
) -> SealedHeader<Client::Header>
where
Client: HeadersClient,
Client: HeadersClient<Header: reth_primitives_traits::BlockHeader>,
{
info!(target: "reth::cli", ?tip, "Fetching tip block from the network.");
let mut fetch_failures = 0;

View File

@ -1,12 +1,12 @@
//! Utility functions for node startup and shutdown, for example path parsing and retrieving single
//! blocks from the network.
use alloy_consensus::BlockHeader;
use alloy_eips::BlockHashOrNumber;
use alloy_primitives::Sealable;
use alloy_rpc_types_engine::{JwtError, JwtSecret};
use eyre::Result;
use reth_chainspec::ChainSpec;
use reth_consensus_common::validation::validate_block_pre_execution;
use reth_consensus::Consensus;
use reth_network_p2p::{
bodies::client::BodiesClient,
headers::client::{HeadersClient, HeadersDirection, HeadersRequest},
@ -16,7 +16,6 @@ use reth_primitives::{SealedBlock, SealedHeader};
use std::{
env::VarError,
path::{Path, PathBuf},
sync::Arc,
};
use tracing::{debug, info};
@ -41,9 +40,9 @@ pub fn get_or_create_jwt_secret_from_path(path: &Path) -> Result<JwtSecret, JwtE
pub async fn get_single_header<Client>(
client: Client,
id: BlockHashOrNumber,
) -> Result<SealedHeader>
) -> Result<SealedHeader<Client::Header>>
where
Client: HeadersClient,
Client: HeadersClient<Header: reth_primitives_traits::BlockHeader>,
{
let request = HeadersRequest { direction: HeadersDirection::Rising, limit: 1, start: id };
@ -61,7 +60,7 @@ where
let valid = match id {
BlockHashOrNumber::Hash(hash) => header.hash() == hash,
BlockHashOrNumber::Number(number) => header.number == number,
BlockHashOrNumber::Number(number) => header.number() == number,
};
if !valid {
@ -77,11 +76,11 @@ where
}
/// Get a body from network based on header
pub async fn get_single_body<Client>(
pub async fn get_single_body<H, Client>(
client: Client,
chain_spec: Arc<ChainSpec>,
header: SealedHeader,
) -> Result<SealedBlock>
header: SealedHeader<H>,
consensus: impl Consensus<H, Client::Body>,
) -> Result<SealedBlock<H, Client::Body>>
where
Client: BodiesClient,
{
@ -95,7 +94,7 @@ where
let body = response.unwrap();
let block = SealedBlock { header, body };
validate_block_pre_execution(&block, &chain_spec)?;
consensus.validate_block_pre_execution(&block)?;
Ok(block)
}