Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
fix: cannot find parent block during livesync
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Aug 15, 2024
1 parent 250e3a8 commit 310c156
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 44 deletions.
4 changes: 2 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ impl Command {
#[cfg(not(feature = "bsc"))]
let executor = block_executor!(provider_factory.chain_spec()).executor(db);

let BlockExecutionOutput { state, receipts, requests, .. } =
executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?;
let BlockExecutionOutput { state, receipts, requests, .. } = executor
.execute((&block_with_senders.clone().unseal(), U256::MAX, None).into())?;
let execution_outcome = ExecutionOutcome::new(
state,
receipts.into(),
Expand Down
1 change: 1 addition & 0 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Command {
.with_recovered_senders()
.ok_or(BlockValidationError::SenderRecoveryError)?,
merkle_block_td + block.difficulty,
None,
)
.into(),
)?;
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Command {
provider_rw.static_file_provider().clone(),
),
));
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td, None).into())?;
executor.finalize().write_to_storage(&provider_rw, None, OriginalValuesKnown::Yes)?;

let checkpoint = Some(StageCheckpoint::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl AppendableChain {
let block_hash = block.hash();
let block = block.unseal();

let state = executor.execute((&block, U256::MAX).into())?;
let state = executor.execute((&block, U256::MAX, Some(parent_block.header())).into())?;
let BlockExecutionOutput { state, receipts, requests, .. } = state;
externals
.consensus
Expand Down
19 changes: 12 additions & 7 deletions crates/bsc/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,14 @@ where
&mut self,
block: &BlockWithSenders,
total_difficulty: U256,
parent_header: Option<&Header>,
) -> Result<BscExecuteOutput, BlockExecutionError> {
// 1. get parent header and snapshot
let parent = &(self.get_header_by_hash(block.parent_hash)?);
let parent = match parent_header {
// during live sync, the parent may not have been committed to the underlying database
Some(p) => p,
None => &(self.get_header_by_hash(block.parent_hash)?),
};
let snapshot_reader = SnapshotReader::new(self.provider.clone(), self.parlia.clone());
let snap = &(snapshot_reader.snapshot(parent, None)?);

Expand Down Expand Up @@ -670,7 +675,7 @@ where
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
P: ParliaProvider,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = BlockExecutionOutput<Receipt>;
type Error = BlockExecutionError;

Expand All @@ -682,9 +687,9 @@ where
///
/// State changes are committed to the database.
fn execute(mut self, input: Self::Input<'_>) -> Result<Self::Output, Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, parent_header } = input;
let BscExecuteOutput { receipts, gas_used, snapshot } =
self.execute_and_verify(block, total_difficulty)?;
self.execute_and_verify(block, total_difficulty, parent_header)?;

// NOTE: we need to merge keep the reverts for the bundle retention
self.state.merge_transitions(BundleRetention::Reverts);
Expand Down Expand Up @@ -726,15 +731,15 @@ where
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
P: ParliaProvider,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = ExecutionOutcome;
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let execute_start = Instant::now();
let BscExecuteOutput { receipts, gas_used: _, snapshot } =
self.executor.execute_and_verify(block, total_difficulty)?;
self.executor.execute_and_verify(block, total_difficulty, None)?;
self.stats.execution_duration += execute_start.elapsed();

validate_block_post_execution(block, self.executor.chain_spec(), &receipts)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl StorageInner {
requests: block_execution_requests,
gas_used,
..
} = executor.executor(&mut db).execute((&block, U256::ZERO).into())?;
} = executor.executor(&mut db).execute((&block, U256::ZERO, None).into())?;
let execution_outcome = ExecutionOutcome::new(
state,
receipts.into(),
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ where
let block_number = block.number;
let block_hash = block.hash();
let block = block.unseal();
let output = executor.execute((&block, U256::MAX).into()).unwrap();
let output = executor.execute((&block, U256::MAX, None).into()).unwrap();
self.consensus.validate_block_post_execution(
&block,
PostExecutionInput::new(&output.receipts, &output.requests),
Expand Down
8 changes: 4 additions & 4 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ where
EvmConfig: ConfigureEvm,
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = BlockExecutionOutput<Receipt>;
type Error = BlockExecutionError;

Expand All @@ -375,7 +375,7 @@ where
///
/// Returns an error if the block could not be executed or failed verification.
fn execute(mut self, input: Self::Input<'_>) -> Result<Self::Output, Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let EthExecuteOutput { receipts, requests, gas_used } =
self.execute_without_verification(block, total_difficulty)?;

Expand Down Expand Up @@ -419,12 +419,12 @@ where
EvmConfig: ConfigureEvm,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = ExecutionOutcome;
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let EthExecuteOutput { receipts, requests, gas_used: _ } =
self.executor.execute_without_verification(block, total_difficulty)?;

Expand Down
14 changes: 7 additions & 7 deletions crates/evm/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::execute::{
};
use reth_execution_errors::BlockExecutionError;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{BlockNumber, BlockWithSenders, Receipt};
use reth_primitives::{BlockNumber, BlockWithSenders, Header, Receipt};
use reth_prune_types::PruneModes;
use reth_storage_errors::provider::ProviderError;
use revm_primitives::db::Database;
Expand Down Expand Up @@ -51,19 +51,19 @@ impl<A, B, DB> Executor<DB> for Either<A, B>
where
A: for<'a> Executor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>,
B: for<'a> Executor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = BlockExecutionOutput<Receipt>;
type Error = BlockExecutionError;

Expand All @@ -79,19 +79,19 @@ impl<A, B, DB> BatchExecutor<DB> for Either<A, B>
where
A: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = ExecutionOutcome,
Error = BlockExecutionError,
>,
B: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = ExecutionOutcome,
Error = BlockExecutionError,
>,
DB: Database<Error: Into<ProviderError> + Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = ExecutionOutcome;
type Error = BlockExecutionError;

Expand Down
32 changes: 22 additions & 10 deletions crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Traits for execution.
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{parlia::Snapshot, BlockNumber, BlockWithSenders, Receipt, Request, U256};
use reth_primitives::{
parlia::Snapshot, BlockNumber, BlockWithSenders, Header, Receipt, Request, U256,
};
use reth_prune_types::PruneModes;
use revm::db::BundleState;
use revm_primitives::db::Database;
Expand Down Expand Up @@ -119,23 +121,33 @@ pub struct BlockExecutionOutput<T> {

/// A helper type for ethereum block inputs that consists of a block and the total difficulty.
#[derive(Debug)]
pub struct BlockExecutionInput<'a, Block> {
pub struct BlockExecutionInput<'a, Block, Header> {
/// The block to execute.
pub block: &'a Block,
/// The total difficulty of the block.
pub total_difficulty: U256,
/// The header of the block's parent
pub parent_header: Option<&'a Header>,
}

impl<'a, Block> BlockExecutionInput<'a, Block> {
impl<'a, Block, Header> BlockExecutionInput<'a, Block, Header> {
/// Creates a new input.
pub const fn new(block: &'a Block, total_difficulty: U256) -> Self {
Self { block, total_difficulty }
pub const fn new(
block: &'a Block,
total_difficulty: U256,
parent_header: Option<&'a Header>,
) -> Self {
Self { block, total_difficulty, parent_header }
}
}

impl<'a, Block> From<(&'a Block, U256)> for BlockExecutionInput<'a, Block> {
fn from((block, total_difficulty): (&'a Block, U256)) -> Self {
Self::new(block, total_difficulty)
impl<'a, Block, Header> From<(&'a Block, U256, Option<&'a Header>)>
for BlockExecutionInput<'a, Block, Header>
{
fn from(
(block, total_difficulty, parent_header): (&'a Block, U256, Option<&'a Header>),
) -> Self {
Self::new(block, total_difficulty, parent_header)
}
}

Expand All @@ -154,15 +166,15 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static {
/// the returned state.
type Executor<DB: Database<Error: Into<ProviderError> + Display>>: for<'a> Executor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = BlockExecutionOutput<Receipt>,
Error = BlockExecutionError,
>;

/// An executor that can execute a batch of blocks given a database.
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>>: for<'a> BatchExecutor<
DB,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders>,
Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>,
Output = ExecutionOutcome,
Error = BlockExecutionError,
>;
Expand Down
6 changes: 3 additions & 3 deletions crates/evm/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Display;

use reth_execution_errors::BlockExecutionError;
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{BlockNumber, BlockWithSenders, Receipt};
use reth_primitives::{BlockNumber, BlockWithSenders, Header, Receipt};
use reth_prune_types::PruneModes;
use reth_storage_errors::provider::ProviderError;
use revm_primitives::db::Database;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl BlockExecutorProvider for NoopBlockExecutorProvider {
}

impl<DB> Executor<DB> for NoopBlockExecutorProvider {
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = BlockExecutionOutput<Receipt>;
type Error = BlockExecutionError;

Expand All @@ -51,7 +51,7 @@ impl<DB> Executor<DB> for NoopBlockExecutorProvider {
}

impl<DB> BatchExecutor<DB> for NoopBlockExecutorProvider {
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = ExecutionOutcome;
type Error = BlockExecutionError;

Expand Down
2 changes: 1 addition & 1 deletion crates/exex/exex/src/backfill/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where

trace!(target: "exex::backfill", number = block_number, txs = block_with_senders.block.body.len(), "Executing block");

let block_execution_output = executor.execute((&block_with_senders, td).into())?;
let block_execution_output = executor.execute((&block_with_senders, td, None).into())?;

Ok((block_with_senders, block_execution_output))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/exex/exex/src/backfill/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ where
}
.with_senders_unchecked(senders);

executor.execute_and_verify_one((&block, td).into())?;
executor.execute_and_verify_one((&block, td, None).into())?;
execution_duration += execute_start.elapsed();

// TODO(alexey): report gas metrics using `block.header.gas_used`
Expand Down
8 changes: 4 additions & 4 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ where
EvmConfig: ConfigureEvm,
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = BlockExecutionOutput<Receipt>;
type Error = BlockExecutionError;

Expand All @@ -403,7 +403,7 @@ where
///
/// State changes are committed to the database.
fn execute(mut self, input: Self::Input<'_>) -> Result<Self::Output, Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let (receipts, gas_used) = self.execute_without_verification(block, total_difficulty)?;

// NOTE: we need to merge keep the reverts for the bundle retention
Expand Down Expand Up @@ -449,12 +449,12 @@ where
EvmConfig: ConfigureEvm,
DB: Database<Error: Into<ProviderError> + std::fmt::Display>,
{
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>;
type Input<'a> = BlockExecutionInput<'a, BlockWithSenders, Header>;
type Output = ExecutionOutcome;
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let (receipts, _gas_used) =
self.executor.execute_without_verification(block, total_difficulty)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ where
// Execute the block
let execute_start = Instant::now();

executor.execute_and_verify_one((&block, td).into()).map_err(|error| {
executor.execute_and_verify_one((&block, td, None).into()).map_err(|error| {
StageError::Block {
block: Box::new(block.header.clone().seal_slow()),
error: BlockErrorKind::Execution(error),
Expand Down

0 comments on commit 310c156

Please sign in to comment.