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

@@ -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)
}