Skip to content

Commit

Permalink
fix: use associated error type, remove trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Oct 28, 2024
1 parent 1f24f5f commit fc7302d
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 45 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

extern crate alloc;

use core::convert::Infallible;

use alloc::{sync::Arc, vec::Vec};
use alloy_primitives::{Address, Bytes, TxKind, U256};
use reth_chainspec::{ChainSpec, Head};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes, NextCfgError};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_primitives::{transaction::FillTxEnv, Header, TransactionSigned};
use revm_primitives::{
AnalysisKind, BlobExcessGasAndPrice, BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, Env, SpecId, TxEnv,
Expand Down Expand Up @@ -59,6 +61,7 @@ impl EthEvmConfig {

impl ConfigureEvmEnv for EthEvmConfig {
type Header = Header;
type Error = Infallible;

fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
transaction.fill_tx_env(tx_env, sender);
Expand Down Expand Up @@ -131,7 +134,7 @@ impl ConfigureEvmEnv for EthEvmConfig {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error> {
// configure evm env based on parent block
let cfg = CfgEnv::default().with_chain_id(self.chain_spec.chain().id());

Expand Down
4 changes: 2 additions & 2 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use reth_basic_payload_builder::{
use reth_chain_state::ExecutedBlock;
use reth_chainspec::ChainSpec;
use reth_errors::RethError;
use reth_evm::{system_calls::SystemCaller, ConfigureEvm, NextBlockEnvAttributes, NextCfgError};
use reth_evm::{system_calls::SystemCaller, ConfigureEvm, NextBlockEnvAttributes};
use reth_evm_ethereum::{eip6110::parse_deposits_from_receipts, EthEvmConfig};
use reth_execution_types::ExecutionOutcome;
use reth_payload_builder::{EthBuiltPayload, EthPayloadBuilderAttributes};
Expand Down Expand Up @@ -73,7 +73,7 @@ where
&self,
config: &PayloadConfig<EthPayloadBuilderAttributes>,
parent: &Header,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), EvmConfig::Error> {
let next_attributes = NextBlockEnvAttributes {
timestamp: config.attributes.timestamp(),
suggested_fee_recipient: config.attributes.suggested_fee_recipient(),
Expand Down
19 changes: 8 additions & 11 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ futures-util.workspace = true
metrics = { workspace = true, optional = true }
parking_lot = { workspace = true, optional = true }

# misc
derive_more.workspace = true

[dev-dependencies]
parking_lot.workspace = true
reth-ethereum-forks.workspace = true
Expand All @@ -60,12 +57,12 @@ std = [
"revm/std",
]
test-utils = [
"dep:parking_lot",
"reth-chainspec/test-utils",
"reth-consensus/test-utils",
"reth-primitives/test-utils",
"reth-primitives-traits/test-utils",
"reth-revm/test-utils",
"revm/test-utils",
"reth-prune-types/test-utils"
"dep:parking_lot",
"reth-chainspec/test-utils",
"reth-consensus/test-utils",
"reth-primitives/test-utils",
"reth-primitives-traits/test-utils",
"reth-revm/test-utils",
"revm/test-utils",
"reth-prune-types/test-utils"
]
15 changes: 4 additions & 11 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
extern crate alloc;

use crate::builder::RethEvmBuilder;
use alloc::boxed::Box;
use alloy_primitives::{Address, Bytes, B256, U256};
use derive_more::{Display, From};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::BlockHeader;
use revm::{Database, Evm, GetInspector};
Expand Down Expand Up @@ -109,14 +107,6 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
fn default_external_context<'a>(&self) -> Self::DefaultExternalContext<'a>;
}

#[derive(Debug, From, Display)]
/// Error type for [`ConfigureEvmEnv::next_cfg_and_block_env`].
pub enum NextCfgError {
#[display("Invalid config error: {_0}")]
/// This is a generic error type that can be used to wrap any error type that implements
InvalidConfigError(#[from] Box<dyn core::error::Error + Send + Sync>),
}

/// This represents the set of methods used to configure the EVM's environment before block
/// execution.
///
Expand All @@ -126,6 +116,9 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
/// The header type used by the EVM.
type Header: BlockHeader;

/// The error type that is returned by [`Self::next_cfg_and_block_env`].
type Error: core::error::Error + Send + Sync;

/// Returns a [`TxEnv`] from a [`TransactionSigned`] and [`Address`].
fn tx_env(&self, transaction: &TransactionSigned, signer: Address) -> TxEnv {
let mut tx_env = TxEnv::default();
Expand Down Expand Up @@ -202,7 +195,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError>;
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error>;
}

/// Represents additional attributes required to configure the next block.
Expand Down
7 changes: 7 additions & 0 deletions crates/optimism/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ pub enum DecodeError {
InvalidElasticity,
}

impl core::error::Error for DecodeError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
// None of the errors have sub-errors
None
}
}

/// Extracts the Holcene 1599 parameters from the encoded form:
/// <https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#eip1559params-encoding>
pub fn decode_holocene_1559_params(extra_data: Bytes) -> Result<(u32, u32), DecodeError> {
Expand Down
9 changes: 5 additions & 4 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ extern crate alloc;

use alloc::{sync::Arc, vec::Vec};
use alloy_primitives::{Address, U256};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes, NextCfgError};
use reth_optimism_chainspec::OpChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_optimism_chainspec::{DecodeError, OpChainSpec};
use reth_primitives::{
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
transaction::FillTxEnv,
Expand Down Expand Up @@ -56,6 +56,7 @@ impl OptimismEvmConfig {

impl ConfigureEvmEnv for OptimismEvmConfig {
type Header = Header;
type Error = DecodeError;

fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
transaction.fill_tx_env(tx_env, sender);
Expand Down Expand Up @@ -134,7 +135,7 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error> {
// configure evm env based on parent block
let cfg = CfgEnv::default().with_chain_id(self.chain_spec.chain().id());

Expand All @@ -159,7 +160,7 @@ impl ConfigureEvmEnv for OptimismEvmConfig {
basefee: self
.chain_spec
.next_block_base_fee(parent, attributes.timestamp)
.map_err(|e| NextCfgError::InvalidConfigError(e.to_string().into()))?,
.map_err(|e| e.into())?,
// calculate excess gas based on parent block's blob gas usage
blob_excess_gas_and_price,
};
Expand Down
6 changes: 2 additions & 4 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use alloy_primitives::{Bytes, B64, U256};
use reth_basic_payload_builder::*;
use reth_chain_state::ExecutedBlock;
use reth_chainspec::{BaseFeeParams, ChainSpecProvider};
use reth_evm::{
system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes, NextCfgError,
};
use reth_evm::{system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_execution_types::ExecutionOutcome;
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_consensus::calculate_receipt_root_no_memo_optimism;
Expand Down Expand Up @@ -80,7 +78,7 @@ where
&self,
config: &PayloadConfig<OptimismPayloadBuilderAttributes>,
parent: &Header,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), EvmConfig::Error> {
let next_attributes = NextBlockEnvAttributes {
timestamp: config.attributes.timestamp(),
suggested_fee_recipient: config.attributes.suggested_fee_recipient(),
Expand Down
1 change: 0 additions & 1 deletion examples/custom-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ reth-node-ethereum = { workspace = true, features = ["test-utils"] }
reth-tracing.workspace = true
alloy-genesis.workspace = true
alloy-primitives.workspace = true
reth-evm.workspace = true

eyre.workspace = true
tokio.workspace = true
6 changes: 3 additions & 3 deletions examples/custom-evm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use reth::{
transaction_pool::TransactionPool,
};
use reth_chainspec::{Chain, ChainSpec};
use reth_evm::NextCfgError;
use reth_evm_ethereum::EthEvmConfig;
use reth_node_api::{
ConfigureEvm, ConfigureEvmEnv, FullNodeTypes, NextBlockEnvAttributes, NodeTypes,
Expand All @@ -39,7 +38,7 @@ use reth_primitives::{
Header, TransactionSigned,
};
use reth_tracing::{RethTracer, Tracer};
use std::sync::Arc;
use std::{convert::Infallible, sync::Arc};

/// Custom EVM configuration
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -88,6 +87,7 @@ impl MyEvmConfig {

impl ConfigureEvmEnv for MyEvmConfig {
type Header = Header;
type Error = Infallible;

fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
self.inner.fill_tx_env(tx_env, transaction, sender);
Expand Down Expand Up @@ -116,7 +116,7 @@ impl ConfigureEvmEnv for MyEvmConfig {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error> {
self.inner.next_cfg_and_block_env(parent, attributes)
}
}
Expand Down
1 change: 0 additions & 1 deletion examples/stateful-precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ reth-node-ethereum = { workspace = true, features = ["test-utils"] }
reth-tracing.workspace = true
alloy-genesis.workspace = true
alloy-primitives.workspace = true
reth-evm.workspace = true

eyre.workspace = true
parking_lot.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions examples/stateful-precompile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use reth::{
tasks::TaskManager,
};
use reth_chainspec::{Chain, ChainSpec};
use reth_evm::NextCfgError;
use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, FullNodeTypes, NodeTypes};
use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig};
use reth_node_ethereum::{
Expand All @@ -31,7 +30,7 @@ use reth_primitives::{
};
use reth_tracing::{RethTracer, Tracer};
use schnellru::{ByLength, LruMap};
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, convert::Infallible, sync::Arc};

/// Type alias for the LRU cache used within the [`PrecompileCache`].
type PrecompileLRUCache = LruMap<(Bytes, u64), PrecompileResult>;
Expand Down Expand Up @@ -148,6 +147,7 @@ impl StatefulPrecompileMut for WrappedPrecompile {

impl ConfigureEvmEnv for MyEvmConfig {
type Header = Header;
type Error = Infallible;

fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
self.inner.fill_tx_env(tx_env, transaction, sender)
Expand Down Expand Up @@ -176,7 +176,7 @@ impl ConfigureEvmEnv for MyEvmConfig {
&self,
parent: &Self::Header,
attributes: NextBlockEnvAttributes,
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), NextCfgError> {
) -> Result<(CfgEnvWithHandlerCfg, BlockEnv), Self::Error> {
self.inner.next_cfg_and_block_env(parent, attributes)
}
}
Expand Down

0 comments on commit fc7302d

Please sign in to comment.