Skip to content

Commit

Permalink
Merge branch 'main' into matt/introduce-sealed-block-in-reth-primitiv…
Browse files Browse the repository at this point in the history
…es-traits
  • Loading branch information
mattsse committed Jan 14, 2025
2 parents b1077ea + df00877 commit 990cc33
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 116 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| [Crate Docs](https://reth.rs/docs)

[gh-ci]: https://github.com/paradigmxyz/reth/actions/workflows/unit.yml
[gh-deny]: https://github.com/paradigmxyz/reth/actions/workflows/deny.yml
[gh-deny]: https://github.com/paradigmxyz/reth/actions/workflows/lint.yml
[tg-badge]: https://img.shields.io/endpoint?color=neon&logo=telegram&label=chat&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fparadigm%5Freth

## What is Reth?
Expand Down
7 changes: 3 additions & 4 deletions crates/cli/commands/src/stage/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use reth_provider::{
providers::ProviderNodeTypes, BlockExecutionWriter, BlockNumReader, ChainStateBlockReader,
ChainStateBlockWriter, ProviderFactory, StaticFileProviderFactory, StorageLocation,
};
use reth_prune::PruneModes;
use reth_stages::{
sets::{DefaultStages, OfflineStages},
stages::ExecutionStage,
Expand Down Expand Up @@ -120,7 +119,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>

let builder = if self.offline {
Pipeline::<N>::builder().add_stages(
OfflineStages::new(executor, config.stages, PruneModes::default())
OfflineStages::new(executor, config.stages, prune_modes.clone())
.builder()
.disable(reth_stages::StageId::SenderRecovery),
)
Expand All @@ -145,15 +144,15 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
max_duration: None,
},
stage_conf.execution_external_clean_threshold(),
prune_modes,
prune_modes.clone(),
ExExManagerHandle::empty(),
)),
)
};

let pipeline = builder.build(
provider_factory.clone(),
StaticFileProducer::new(provider_factory, PruneModes::default()),
StaticFileProducer::new(provider_factory, prune_modes),
);
Ok(pipeline)
}
Expand Down
20 changes: 4 additions & 16 deletions crates/engine/invalid-block-hooks/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use pretty_assertions::Comparison;
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_engine_primitives::InvalidBlockHook;
use reth_evm::{
env::EvmEnv, state_change::post_block_balance_increments, system_calls::SystemCaller,
ConfigureEvm,
state_change::post_block_balance_increments, system_calls::SystemCaller, ConfigureEvm,
};
use reth_primitives::{NodePrimitives, RecoveredBlock, SealedHeader};
use reth_primitives_traits::{BlockBody, SignedTransaction};
use reth_provider::{BlockExecutionOutput, ChainSpecProvider, StateProviderFactory};
use reth_revm::{
database::StateProviderDatabase, db::states::bundle_state::BundleRetention,
primitives::EnvWithHandlerCfg, DatabaseCommit, StateBuilder,
database::StateProviderDatabase, db::states::bundle_state::BundleRetention, DatabaseCommit,
StateBuilder,
};
use reth_rpc_api::DebugApiClient;
use reth_tracing::tracing::warn;
Expand Down Expand Up @@ -77,19 +76,8 @@ where
.with_bundle_update()
.build();

// Setup environment for the execution.
let EvmEnv { cfg_env_with_handler_cfg, block_env } =
self.evm_config.cfg_and_block_env(block.header());

// Setup EVM
let mut evm = self.evm_config.evm_with_env(
&mut db,
EnvWithHandlerCfg::new_with_cfg_env(
cfg_env_with_handler_cfg,
block_env,
Default::default(),
),
);
let mut evm = self.evm_config.evm_for_block(&mut db, block.header());

let mut system_caller =
SystemCaller::new(self.evm_config.clone(), self.provider.chain_spec());
Expand Down
17 changes: 5 additions & 12 deletions crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use reth_engine_primitives::{
use reth_errors::{BlockExecutionError, BlockValidationError, RethError, RethResult};
use reth_ethereum_forks::EthereumHardforks;
use reth_evm::{
env::EvmEnv, state_change::post_block_withdrawals_balance_increments,
system_calls::SystemCaller, ConfigureEvm,
state_change::post_block_withdrawals_balance_increments, system_calls::SystemCaller,
ConfigureEvm,
};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{
Expand All @@ -29,7 +29,7 @@ use reth_revm::{
DatabaseCommit,
};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use revm_primitives::{EVMError, EnvWithHandlerCfg};
use revm_primitives::EVMError;
use std::{
collections::VecDeque,
future::Future,
Expand Down Expand Up @@ -297,15 +297,8 @@ where
.with_bundle_update()
.build();

// Configure environments
let EvmEnv { cfg_env_with_handler_cfg, block_env } =
evm_config.cfg_and_block_env(&reorg_target.header);
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg_env_with_handler_cfg,
block_env,
Default::default(),
);
let mut evm = evm_config.evm_with_env(&mut state, env);
// Configure EVM
let mut evm = evm_config.evm_for_block(&mut state, &reorg_target.header);

// apply eip-4788 pre block contract call
let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec.clone());
Expand Down
29 changes: 4 additions & 25 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use reth_chainspec::{ChainSpec, EthereumHardfork, EthereumHardforks, MAINNET};
use reth_consensus::ConsensusError;
use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
env::EvmEnv,
execute::{
balance_increment_state, BasicBlockExecutorProvider, BlockExecutionError,
BlockExecutionStrategy, BlockExecutionStrategyFactory, BlockValidationError, ExecuteOutput,
Expand All @@ -27,7 +26,7 @@ use reth_primitives_traits::BlockBody;
use reth_revm::db::State;
use revm_primitives::{
db::{Database, DatabaseCommit},
EnvWithHandlerCfg, ResultAndState,
ResultAndState,
};

/// Factory for [`EthExecutionStrategy`].
Expand Down Expand Up @@ -114,23 +113,6 @@ where
}
}

impl<DB, EvmConfig> EthExecutionStrategy<DB, EvmConfig>
where
DB: Database<Error: Into<ProviderError> + Display>,
EvmConfig: ConfigureEvm,
{
/// Configures a new evm configuration and block environment for the given block.
///
/// # Caution
///
/// This does not initialize the tx environment.
fn evm_env_for_block(&self, header: &EvmConfig::Header) -> EnvWithHandlerCfg {
let EvmEnv { cfg_env_with_handler_cfg, block_env } =
self.evm_config.cfg_and_block_env(header);
EnvWithHandlerCfg::new_with_cfg_env(cfg_env_with_handler_cfg, block_env, Default::default())
}
}

impl<DB, EvmConfig> BlockExecutionStrategy for EthExecutionStrategy<DB, EvmConfig>
where
DB: Database<Error: Into<ProviderError> + Display>,
Expand All @@ -157,8 +139,7 @@ where
(*self.chain_spec).is_spurious_dragon_active_at_block(block.number());
self.state.set_state_clear_flag(state_clear_flag);

let env = self.evm_env_for_block(block.header());
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
let mut evm = self.evm_config.evm_for_block(&mut self.state, block.header());

self.system_caller.apply_pre_execution_changes(block.header(), &mut evm)?;

Expand All @@ -169,8 +150,7 @@ where
&mut self,
block: &RecoveredBlock<reth_primitives::Block>,
) -> Result<ExecuteOutput<Receipt>, Self::Error> {
let env = self.evm_env_for_block(block.header());
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
let mut evm = self.evm_config.evm_for_block(&mut self.state, block.header());

let mut cumulative_gas_used = 0;
let mut receipts = Vec::with_capacity(block.body().transaction_count());
Expand Down Expand Up @@ -231,8 +211,7 @@ where
block: &RecoveredBlock<reth_primitives::Block>,
receipts: &[Receipt],
) -> Result<Requests, Self::Error> {
let env = self.evm_env_for_block(block.header());
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
let mut evm = self.evm_config.evm_for_block(&mut self.state, block.header());

let requests = if self.chain_spec.is_prague_active_at_timestamp(block.timestamp) {
// Collect all EIP-6110 deposits
Expand Down
80 changes: 79 additions & 1 deletion crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
extern crate alloc;

use crate::builder::RethEvmBuilder;
use alloc::boxed::Box;
use alloy_consensus::BlockHeader as _;
use alloy_primitives::{Address, Bytes, B256, U256};
use reth_primitives_traits::{BlockHeader, SignedTransaction};
Expand All @@ -41,7 +42,6 @@ pub mod system_calls;
pub mod test_utils;

/// Trait for configuring the EVM for executing full blocks.
#[auto_impl::auto_impl(&, Arc)]
pub trait ConfigureEvm: ConfigureEvmEnv {
/// Associated type for the default external context that should be configured for the EVM.
type DefaultExternalContext<'a>;
Expand Down Expand Up @@ -70,6 +70,31 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
evm
}

/// Returns a new EVM with the given database configured with `cfg` and `block_env`
/// configuration derived from the given header. Relies on
/// [`ConfigureEvmEnv::cfg_and_block_env`].
///
/// # Caution
///
/// This does not initialize the tx environment.
fn evm_for_block<DB: Database>(
&self,
db: DB,
header: &Self::Header,
) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
let EvmEnv {
cfg_env_with_handler_cfg: CfgEnvWithHandlerCfg { cfg_env, handler_cfg },
block_env,
} = self.cfg_and_block_env(header);
self.evm_with_env(
db,
EnvWithHandlerCfg {
env: Box::new(Env { cfg: cfg_env, block: block_env, tx: Default::default() }),
handler_cfg,
},
)
}

/// Returns a new EVM with the given database configured with the given environment settings,
/// including the spec id.
///
Expand Down Expand Up @@ -109,6 +134,59 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
fn default_external_context<'a>(&self) -> Self::DefaultExternalContext<'a>;
}

impl<'b, T> ConfigureEvm for &'b T
where
T: ConfigureEvm,
&'b T: ConfigureEvmEnv<Header = T::Header>,
{
type DefaultExternalContext<'a> = T::DefaultExternalContext<'a>;

fn default_external_context<'a>(&self) -> Self::DefaultExternalContext<'a> {
(*self).default_external_context()
}

fn evm<DB: Database>(&self, db: DB) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
(*self).evm(db)
}

fn evm_for_block<DB: Database>(
&self,
db: DB,
header: &Self::Header,
) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
(*self).evm_for_block(db, header)
}

fn evm_with_env<DB: Database>(
&self,
db: DB,
env: EnvWithHandlerCfg,
) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
(*self).evm_with_env(db, env)
}

fn evm_with_env_and_inspector<DB, I>(
&self,
db: DB,
env: EnvWithHandlerCfg,
inspector: I,
) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
(*self).evm_with_env_and_inspector(db, env, inspector)
}

fn evm_with_inspector<DB, I>(&self, db: DB, inspector: I) -> Evm<'_, I, DB>
where
DB: Database,
I: GetInspector<DB>,
{
(*self).evm_with_inspector(db, inspector)
}
}

/// This represents the set of methods used to configure the EVM's environment before block
/// execution.
///
Expand Down
1 change: 0 additions & 1 deletion crates/exex/types/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl<P: NodePrimitives> From<CanonStateNotification<P>> for ExExNotification<P>
pub(super) mod serde_bincode_compat {
use reth_execution_types::serde_bincode_compat::Chain;
use reth_primitives::{EthPrimitives, NodePrimitives};
use reth_primitives_traits::serde_bincode_compat::SerdeBincodeCompat;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::sync::Arc;
Expand Down
4 changes: 0 additions & 4 deletions crates/net/eth-wire/src/errors/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ pub enum P2PStreamError {
#[error("mismatched protocol version in Hello message: {0}")]
MismatchedProtocolVersion(GotExpected<ProtocolVersion>),

/// Ping started before the handshake completed.
#[error("started ping task before the handshake completed")]
PingBeforeHandshake,

/// Too many messages buffered before sending.
#[error("too many messages buffered before sending")]
SendBufferFull,
Expand Down
30 changes: 6 additions & 24 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
use crate::{l1::ensure_create2_deployer, OpBlockExecutionError, OpEvmConfig};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{BlockHeader, Eip658Value, Header, Receipt, Transaction as _};
use alloy_consensus::{BlockHeader, Eip658Value, Receipt, Transaction as _};
use alloy_eips::eip7685::Requests;
use core::fmt::Display;
use op_alloy_consensus::{OpDepositReceipt, OpTxType};
use reth_chainspec::EthereumHardforks;
use reth_consensus::ConsensusError;
use reth_evm::{
env::EvmEnv,
execute::{
balance_increment_state, BasicBlockExecutorProvider, BlockExecutionError,
BlockExecutionStrategy, BlockExecutionStrategyFactory, BlockValidationError, ExecuteOutput,
Expand All @@ -26,7 +25,7 @@ use reth_optimism_primitives::{OpBlock, OpPrimitives, OpReceipt, OpTransactionSi
use reth_primitives::RecoveredBlock;
use reth_primitives_traits::{BlockBody, SignedTransaction};
use reth_revm::{Database, State};
use revm_primitives::{db::DatabaseCommit, EnvWithHandlerCfg, ResultAndState};
use revm_primitives::{db::DatabaseCommit, ResultAndState};
use tracing::trace;

/// Factory for [`OpExecutionStrategy`].
Expand Down Expand Up @@ -104,21 +103,6 @@ where
}
}

impl<DB, EvmConfig> OpExecutionStrategy<DB, EvmConfig>
where
DB: Database<Error: Into<ProviderError> + Display>,
EvmConfig: ConfigureEvm<Header = alloy_consensus::Header>,
{
/// Configures a new evm configuration and block environment for the given block.
///
/// Caution: this does not initialize the tx environment.
fn evm_env_for_block(&self, header: &Header) -> EnvWithHandlerCfg {
let evm_env = self.evm_config.cfg_and_block_env(header);
let EvmEnv { cfg_env_with_handler_cfg, block_env } = evm_env;
EnvWithHandlerCfg::new_with_cfg_env(cfg_env_with_handler_cfg, block_env, Default::default())
}
}

impl<DB, EvmConfig> BlockExecutionStrategy for OpExecutionStrategy<DB, EvmConfig>
where
DB: Database<Error: Into<ProviderError> + Display>,
Expand All @@ -141,8 +125,7 @@ where
(*self.chain_spec).is_spurious_dragon_active_at_block(block.number());
self.state.set_state_clear_flag(state_clear_flag);

let env = self.evm_env_for_block(block.header());
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
let mut evm = self.evm_config.evm_for_block(&mut self.state, block.header());

self.system_caller.apply_beacon_root_contract_call(
block.timestamp,
Expand All @@ -165,11 +148,10 @@ where
&mut self,
block: &RecoveredBlock<OpBlock>,
) -> Result<ExecuteOutput<OpReceipt>, Self::Error> {
let env = self.evm_env_for_block(block.header());
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
let mut evm = self.evm_config.evm_for_block(&mut self.state, block.header());

let is_regolith =
self.chain_spec.fork(OpHardfork::Regolith).active_at_timestamp(block.timestamp);
self.chain_spec.fork(OpHardfork::Regolith).active_at_timestamp(block.timestamp());

let mut cumulative_gas_used = 0;
let mut receipts = Vec::with_capacity(block.body().transaction_count());
Expand Down Expand Up @@ -317,7 +299,7 @@ impl OpExecutorProvider {
mod tests {
use super::*;
use crate::OpChainSpec;
use alloy_consensus::TxEip1559;
use alloy_consensus::{Header, TxEip1559};
use alloy_primitives::{
b256, Address, PrimitiveSignature as Signature, StorageKey, StorageValue, U256,
};
Expand Down
Loading

0 comments on commit 990cc33

Please sign in to comment.