Skip to content

fix: validate block against header on reth import #14076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions crates/net/downloaders/src/file_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy_eips::BlockHashOrNumber;
use alloy_primitives::{BlockHash, BlockNumber, Sealable, B256};
use futures::Future;
use itertools::Either;
use reth_consensus::{ConsensusError, HeaderValidator};
use reth_consensus::{Consensus, ConsensusError};
use reth_network_p2p::{
bodies::client::{BodiesClient, BodiesFut},
download::DownloadClient,
Expand All @@ -15,7 +15,7 @@ use reth_network_p2p::{
BlockClient,
};
use reth_network_peers::PeerId;
use reth_primitives::SealedHeader;
use reth_primitives::{SealedBlock, SealedHeader};
use reth_primitives_traits::{Block, BlockBody, FullBlock};
use thiserror::Error;
use tokio::{fs::File, io::AsyncReadExt};
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<B: FullBlock> FileClient<B> {
/// Create a new file client from a file path.
pub async fn new<P: AsRef<Path>>(
path: P,
consensus: Arc<dyn HeaderValidator<B::Header>>,
consensus: Arc<dyn Consensus<B, Error = ConsensusError>>,
) -> Result<Self, FileClientError> {
let file = File::open(path).await?;
Self::from_file(file, consensus).await
Expand All @@ -93,7 +93,7 @@ impl<B: FullBlock> FileClient<B> {
/// Initialize the [`FileClient`] with a file directly.
pub(crate) async fn from_file(
mut file: File,
consensus: Arc<dyn HeaderValidator<B::Header>>,
consensus: Arc<dyn Consensus<B, Error = ConsensusError>>,
) -> Result<Self, FileClientError> {
// get file len from metadata before reading
let metadata = file.metadata().await?;
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<B: FullBlock> FileClient<B> {
}

struct FileClientBuilder<B: Block = reth_primitives::Block> {
pub consensus: Arc<dyn HeaderValidator<B::Header>>,
pub consensus: Arc<dyn Consensus<B, Error = ConsensusError>>,
pub parent_header: Option<SealedHeader<B::Header>>,
}

Expand Down Expand Up @@ -264,6 +264,12 @@ impl<B: FullBlock<Header: reth_primitives_traits::BlockHeader>> FromReader
parent_header = Some(sealed);
}

// Validate block against header
self.consensus.validate_block_pre_execution(&SealedBlock::new_unchecked(
block.clone(),
block_hash,
))?;

// add to the internal maps
headers.insert(block.header().number(), block.header().clone());
hash_to_number.insert(block_hash, block.header().number());
Expand Down Expand Up @@ -487,7 +493,7 @@ impl ChunkedFileReader {
/// Read next chunk from file. Returns [`FileClient`] containing decoded chunk.
pub async fn next_chunk<B: FullBlock>(
&mut self,
consensus: Arc<dyn HeaderValidator<B::Header>>,
consensus: Arc<dyn Consensus<B, Error = ConsensusError>>,
parent_header: Option<SealedHeader<B::Header>>,
) -> Result<Option<FileClient<B>>, FileClientError> {
let Some(next_chunk_byte_len) = self.read_next_chunk().await? else { return Ok(None) };
Expand Down
Loading