Skip to content

Commit

Permalink
rest of it
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Feb 14, 2025
1 parent 6000a6b commit 0aa41a3
Show file tree
Hide file tree
Showing 30 changed files with 148 additions and 151 deletions.
2 changes: 1 addition & 1 deletion bins/revme/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- k256 compile error ([#451](https://github.com/bluealloy/revm/pull/451))

### Other
- *(EvmBuilder)* rename builder functions toHandlerTrCfg ([#1050](https://github.com/bluealloy/revm/pull/1050))
- *(EvmBuilder)* rename builder functions to HandlerCfg ([#1050](https://github.com/bluealloy/revm/pull/1050))
- *(Interpreter)* Split calls to separate functions ([#1005](https://github.com/bluealloy/revm/pull/1005))
- *(revme)* EmptyDb Blockhash string, json-outcome flag, set prevrandao in statetest ([#994](https://github.com/bluealloy/revm/pull/994))
- *(revme)* add recovery of address from secret key ([#992](https://github.com/bluealloy/revm/pull/992))
Expand Down
12 changes: 6 additions & 6 deletions book/src/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Revm consist of few traits that implement functionality of the EVM. The main tra
* **EvmTr**: This trait allows as to access main EVM fields and to run interpreter. It defines **Context**, **Precompiles**, **Instructions**. Docs
* **ContextTr**: is gained from EvmTr and consist of types needed for execution. It defines environment such as block and transaction, database for runtime fetching of accounts and storage, journal for status changes and revert handling and few more fields. Docs
* **Handler**: is a trait that by default implements Ethereum logic, it takes EvmTr as a input. Entry point is a `run` function. Docs
* **Frame**: is a associate type ofHandlerTr and contains runtime data of the call and logic of executing the call, default impl is a type is EthFrame. Docs
* **Frame**: is a associate type ofHandler and contains runtime data of the call and logic of executing the call, default impl is a type is EthFrame. Docs

Inspection for tracing is extensing above traits with:
* **InspectorEvmTrr** is derived from EvmTr and allows running Evm in Inspection mode. It contains **Inspector** associate type. Docs
* **InspectorHandler** is derived fromHandlerTr and allows running Evm in Inspection mode. Entry point is `inspect_run` function and it calls a alternative functions for execution loop that includes inspector calls. Docs
* **Inspector** is a a user oriented trait that is used for inspection of the EVM. It is used for tracing. It is part of Evm struct and it is called from InspectorHandler and InspectorEvmTrr. Docs
* **InspectorEvmTr** is derived from EvmTr and allows running Evm in Inspection mode. It contains **Inspector** associate type. Docs
* **InspectorHandler** is derived fromHandler and allows running Evm in Inspection mode. Entry point is `inspect_run` function and it calls a alternative functions for execution loop that includes inspector calls. Docs
* **Inspector** is a a user oriented trait that is used for inspection of the EVM. It is used for tracing. It is part of Evm struct and it is called from InspectorHandler and InspectorEvmTr. Docs


### Simplified code
Expand All @@ -27,7 +27,7 @@ pub trait EvmTr {
fn execute_interpreter(..);
}
pub trait HandlerTr {
pub trait Handler {
type Evm: EvmTr;
type Frame: Frame;
...
Expand All @@ -36,7 +36,7 @@ pub trait HandlerTr {
```

### flow of execution
Execution flow can be found here (TODO Move to codebase toHandlerTr trait):
Execution flow can be found here (TODO Move to codebase toHandler trait):
* It starts with creation of new EVM instance
* Building of the Context
* Building of the EVM. Inspector/Precompiles are created.
Expand Down
2 changes: 1 addition & 1 deletion book/src/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Steps to implement a new variant:
1. Include Revm
2. Add main Evm type `MyEvm` and give it a Context, Precompile, Instructions and optionally Inspector.
3. Implement EvmTr for `MyEvm`.
4. Create a empty `MyEvmHandler` type and implementHandlerTr for it. Override any logic that you want.
4. Create a empty `MyEvmHandler` type and implementHandler for it. Override any logic that you want.
5. Now you can use `MyEvmHandler` to run your own `MyEvm` instance. Other steps are more for agronomics and easier usage.
6. Create a `MyContextBuilder` trait that will build your context with default types you want to use.
7. Create a `MyEvmBuilder` trait that will build your Evm with default instructions and precompiles.
Expand Down
20 changes: 10 additions & 10 deletions crates/context/interface/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ use primitives::{Address, Bytes, Log, U256};
use state::EvmState;
use std::{boxed::Box, string::String, vec::Vec};

pub trait HaltReasonT: Clone + Debug + PartialEq + Eq + From<HaltReason> {}
pub trait HaltReasonTr: Clone + Debug + PartialEq + Eq + From<HaltReason> {}

impl<T> HaltReasonT for T where T: Clone + Debug + PartialEq + Eq + From<HaltReason> {}
impl<T> HaltReasonTr for T where T: Clone + Debug + PartialEq + Eq + From<HaltReason> {}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ResultAndState<HaltReasonT = HaltReason> {
pub struct ResultAndState<HaltReasonTr = HaltReason> {
/// Status of execution
pub result: ExecutionResult<HaltReasonT>,
pub result: ExecutionResult<HaltReasonTr>,
/// State that got updated
pub state: EvmState,
}

impl<HaltReasonT> ResultAndState<HaltReasonT> {
impl<HaltReasonTr> ResultAndState<HaltReasonTr> {
/// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
pub fn map_haltreason<F, OHR>(self, op: F) -> ResultAndState<OHR>
where
F: FnOnce(HaltReasonT) -> OHR,
F: FnOnce(HaltReasonTr) -> OHR,
{
ResultAndState {
result: self.result.map_haltreason(op),
Expand All @@ -34,7 +34,7 @@ impl<HaltReasonT> ResultAndState<HaltReasonT> {
/// Result of a transaction execution
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExecutionResult<HaltReasonT = HaltReason> {
pub enum ExecutionResult<HaltReasonTr = HaltReason> {
/// Returned successfully
Success {
reason: SuccessReason,
Expand All @@ -47,13 +47,13 @@ pub enum ExecutionResult<HaltReasonT = HaltReason> {
Revert { gas_used: u64, output: Bytes },
/// Reverted for various reasons and spend all gas
Halt {
reason: HaltReasonT,
reason: HaltReasonTr,
/// Halting will spend all the gas, and will be equal to gas_limit.
gas_used: u64,
},
}

impl<HaltReasonT> ExecutionResult<HaltReasonT> {
impl<HaltReasonTr> ExecutionResult<HaltReasonTr> {
/// Returns if transaction execution is successful.
///
/// 1 indicates success, 0 indicates revert.
Expand All @@ -66,7 +66,7 @@ impl<HaltReasonT> ExecutionResult<HaltReasonT> {
/// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
pub fn map_haltreason<F, OHR>(self, op: F) -> ExecutionResult<OHR>
where
F: FnOnce(HaltReasonT) -> OHR,
F: FnOnce(HaltReasonTr) -> OHR,
{
match self {
Self::Success {
Expand Down
8 changes: 4 additions & 4 deletions crates/context/interface/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod eip2930;
pub mod eip7702;
pub mod transaction_type;

pub use eip2930::AccessListT;
pub use eip7702::AuthorizationT;
pub use eip2930::AccessListTr;
pub use eip7702::AuthorizationTr;
use specification::eip4844::GAS_PER_BLOB;
pub use transaction_type::TransactionType;

Expand All @@ -24,8 +24,8 @@ pub trait TransactionError: Debug + core::error::Error {}
/// deprecated by not returning tx_type.
#[auto_impl(&, Box, Arc, Rc)]
pub trait Transaction {
type AccessList: AccessListT;
type Authorization: AuthorizationT;
type AccessList: AccessListTr;
type Authorization: AuthorizationTr;

/// Returns the transaction type.
///
Expand Down
8 changes: 4 additions & 4 deletions crates/context/interface/src/transaction/alloy_types.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::{AccessListT, AuthorizationT};
use super::{AccessListTr, AuthorizationTr};
use primitives::{Address, B256, U256};

use alloy_eip2930::AccessList;
use alloy_eip7702::{RecoveredAuthorization, SignedAuthorization};

impl AccessListT for AccessList {
impl AccessListTr for AccessList {
fn access_list(&self) -> impl Iterator<Item = (Address, impl Iterator<Item = B256>)> {
self.0
.iter()
.map(|item| (item.address, item.storage_keys.iter().cloned()))
}
}

impl AuthorizationT for SignedAuthorization {
impl AuthorizationTr for SignedAuthorization {
fn authority(&self) -> Option<Address> {
self.recover_authority().ok()
}
Expand All @@ -30,7 +30,7 @@ impl AuthorizationT for SignedAuthorization {
}
}

impl AuthorizationT for RecoveredAuthorization {
impl AuthorizationTr for RecoveredAuthorization {
fn authority(&self) -> Option<Address> {
self.authority()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/context/interface/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use primitives::{Address, B256};
///
/// Number of account and storage slots is used to calculate initial tx gas cost.
#[auto_impl(&, Box, Arc, Rc)]
pub trait AccessListT {
pub trait AccessListTr {
/// Iterate over access list.
fn access_list(&self) -> impl Iterator<Item = (Address, impl Iterator<Item = B256>)>;

Expand Down
2 changes: 1 addition & 1 deletion crates/context/interface/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use primitives::{Address, U256};

/// Authorization trait.
#[auto_impl(&, Box, Arc, Rc)]
pub trait AuthorizationT {
pub trait AuthorizationTr {
/// Authority address.
///
/// # Note
Expand Down
1 change: 0 additions & 1 deletion crates/database/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl DBErrorMarker for String {}
pub trait Database {
/// The database error type.
type Error: DBErrorMarker + Error;
//type Bytecode: BytecodeT;

/// Gets basic account information.
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
Expand Down
6 changes: 3 additions & 3 deletions crates/handler/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use auto_impl::auto_impl;
use context::Evm;
use context_interface::ContextTr;
use context_interface::{
result::{HaltReasonT, InvalidHeader, InvalidTransaction, ResultAndState},
result::{HaltReasonTr, InvalidHeader, InvalidTransaction, ResultAndState},
Cfg, Database, Journal, Transaction,
};
use core::mem;
Expand Down Expand Up @@ -97,7 +97,7 @@ pub trait EvmTr {
fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles);
}

pub trait HandlerTr {
pub trait Handler {
type Evm: EvmTr<Context: ContextTr<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>>;
type Error: EvmTrError<Self::Evm>;
// TODO `FrameResult` should be a generic trait.
Expand All @@ -109,7 +109,7 @@ pub trait HandlerTr {
FrameInit = FrameInput,
>;
// TODO `HaltReason` should be part of the output.
type HaltReason: HaltReasonT;
type HaltReason: HaltReasonTr;

#[inline]
fn run(
Expand Down
2 changes: 1 addition & 1 deletion crates/handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod validation;
// Public exports
pub use frame::{return_create, return_eofcreate, ContextTrDbError, EthFrame, Frame};
pub use frame_data::{FrameData, FrameResult};
pub use handler::{EvmTr, EvmTrError, HandlerTr};
pub use handler::{EvmTr, EvmTrError, Handler};
pub use item_or_result::{FrameInitOrResult, FrameOrResult, ItemOrResult};
pub use mainnet_handler::MainnetHandler;
pub use precompile_provider::{EthPrecompiles, PrecompileProvider};
4 changes: 2 additions & 2 deletions crates/handler/src/mainnet_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{EvmTrError, HandlerTr};
use super::{EvmTrError, Handler};
use crate::{EvmTr, Frame, FrameResult};
use context_interface::{result::HaltReason, ContextTr, Journal};
use interpreter::FrameInput;
Expand All @@ -10,7 +10,7 @@ pub struct MainnetHandler<CTX, ERROR, FRAME> {
pub _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME)>,
}

impl<EVM, ERROR, FRAME> HandlerTr for MainnetHandler<EVM, ERROR, FRAME>
impl<EVM, ERROR, FRAME> Handler for MainnetHandler<EVM, ERROR, FRAME>
where
EVM: EvmTr<Context: ContextTr<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>>,
ERROR: EvmTrError<EVM>,
Expand Down
4 changes: 2 additions & 2 deletions crates/handler/src/post_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::frame_data::FrameResult;
use context_interface::ContextTr;
use context_interface::{
journaled_state::Journal,
result::{ExecutionResult, HaltReasonT, ResultAndState},
result::{ExecutionResult, HaltReasonTr, ResultAndState},
Block, Cfg, Database, Transaction,
};
use interpreter::{Gas, InitialAndFloorGas, SuccessOrHalt};
Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn reward_beneficiary<CTX: ContextTr>(
/// TODO make Journal FinalOutput more generic.
pub fn output<
CTX: ContextTr<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>,
HALTREASON: HaltReasonT,
HALTREASON: HaltReasonTr,
>(
context: &mut CTX,
// TODO, make this more generic and nice.
Expand Down
2 changes: 1 addition & 1 deletion crates/handler/src/pre_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! They handle initial setup of the EVM, call loop and the final return of the EVM
use bytecode::Bytecode;
use context_interface::transaction::{AccessListT, AuthorizationT};
use context_interface::transaction::{AccessListTr, AuthorizationTr};
use context_interface::ContextTr;
use context_interface::{
journaled_state::Journal,
Expand Down
2 changes: 1 addition & 1 deletion crates/handler/src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use context_interface::transaction::AccessListT;
use context_interface::transaction::AccessListTr;
use context_interface::ContextTr;
use context_interface::{
journaled_state::Journal,
Expand Down
11 changes: 5 additions & 6 deletions crates/inspector/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{InspectorEvmTrr, InspectorFrame};
use crate::{InspectorEvmTr, InspectorFrame};
use auto_impl::auto_impl;
use revm::{
context::{Cfg, JournalEntry, JournaledState},
context_interface::{result::ResultAndState, ContextTr, Database, Transaction},
handler::{
execution, EvmTr, Frame, FrameInitOrResult, FrameOrResult, FrameResult, HandlerTr,
execution, EvmTr, Frame, FrameInitOrResult, FrameOrResult, FrameResult, Handler,
ItemOrResult,
},
interpreter::{
Expand Down Expand Up @@ -181,11 +181,10 @@ impl<DB: Database> JournalExt for JournaledState<DB> {
}
}

pub trait InspectorHandler: HandlerTr
pub trait InspectorHandler: Handler
where
Self::Evm: InspectorEvmTrr<
Inspector: Inspector<<<Self as HandlerTr>::Evm as EvmTr>::Context, Self::IT>,
>,
Self::Evm:
InspectorEvmTr<Inspector: Inspector<<<Self as Handler>::Evm as EvmTr>::Context, Self::IT>>,
Self::Frame: InspectorFrame<IT = Self::IT>,
{
type IT: InterpreterTypes;
Expand Down
8 changes: 4 additions & 4 deletions crates/inspector/src/mainnet_inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use revm::{
context::{setters::ContextSetters, Evm},
context_interface::{ContextTr, Journal},
handler::{
instructions::EthInstructions, EthFrame, EvmTr, EvmTrError, Frame, FrameResult, HandlerTr,
instructions::EthInstructions, EthFrame, EvmTr, EvmTrError, Frame, FrameResult, Handler,
MainnetHandler, PrecompileProvider,
},
interpreter::{interpreter::EthInterpreter, FrameInput, InterpreterResult},
Expand All @@ -13,15 +13,15 @@ use revm::{
use std::vec::Vec;

use crate::{
InspectCommitEvm, InspectEvm, Inspector, InspectorEvmTrr, InspectorFrame, InspectorHandler,
InspectCommitEvm, InspectEvm, Inspector, InspectorEvmTr, InspectorFrame, InspectorHandler,
JournalExt,
};

impl<EVM, ERROR, FRAME> InspectorHandler for MainnetHandler<EVM, ERROR, FRAME>
where
EVM: InspectorEvmTrr<
EVM: InspectorEvmTr<
Context: ContextTr<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>,
Inspector: Inspector<<<Self as HandlerTr>::Evm as EvmTr>::Context, EthInterpreter>,
Inspector: Inspector<<<Self as Handler>::Evm as EvmTr>::Context, EthInterpreter>,
>,
ERROR: EvmTrError<EVM>,
FRAME: Frame<Evm = EVM, Error = ERROR, FrameResult = FrameResult, FrameInit = FrameInput>
Expand Down
9 changes: 4 additions & 5 deletions crates/inspector/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{inspect_instructions, Inspector, JournalExt};
use revm::{
context::{setters::ContextSetters, Evm},
context_interface::ContextTr,
Expand All @@ -12,10 +13,8 @@ use revm::{
precompile::PrecompileErrors,
};

use crate::{inspect_instructions, Inspector, JournalExt};

/// Inspector EVM trait.
pub trait InspectorEvmTrr: EvmTr {
pub trait InspectorEvmTr: EvmTr {
type Inspector;

fn inspector(&mut self) -> &mut Self::Inspector;
Expand All @@ -30,7 +29,7 @@ pub trait InspectorEvmTrr: EvmTr {
) -> <Self::Instructions as InstructionProvider>::Output;
}

impl<CTX, INSP, I, P> InspectorEvmTrr for Evm<CTX, INSP, I, P>
impl<CTX, INSP, I, P> InspectorEvmTr for Evm<CTX, INSP, I, P>
where
CTX: ContextTr<Journal: JournalExt> + ContextSetters,
I: InstructionProvider<Context = CTX, Output = InterpreterAction>,
Expand Down Expand Up @@ -86,7 +85,7 @@ where
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
> + InspectorEvmTrr,
> + InspectorEvmTr,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileErrors>,
{
type IT = EthInterpreter;
Expand Down
Loading

0 comments on commit 0aa41a3

Please sign in to comment.