Skip to content

Commit

Permalink
chore: Rename NameTrait to NameTr (#2084)
Browse files Browse the repository at this point in the history
* feat: Rename NameTrait to NameT

* chore: Rename NameTrait to NameTr

* rest of it

* fea more traits
  • Loading branch information
rakita authored Feb 16, 2025
1 parent 3c11ded commit 24162b7
Show file tree
Hide file tree
Showing 58 changed files with 330 additions and 344 deletions.
24 changes: 12 additions & 12 deletions book/src/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ REVM as any EVM implement a list of [EIP's (Ethereum Improvement Protocol)](http
### Main components/traits:

Revm consist of few traits that implement functionality of the EVM. The main traits are:
* **EvmTrait**: This trait allows as to access main EVM fields and to run interpreter. It defines **Context**, **Precompiles**, **Instructions**. Docs
* **ContextTrait**: is gained from EvmTrait 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
* **EthHandler**: is a trait that by default implements Ethereum logic, it takes EvmTrait as a input. Entry point is a `run` function. Docs
* **Frame**: is a associate type of EthHandler and contains runtime data of the call and logic of executing the call, default impl is a type is EthFrame. Docs
* **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 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:
* **InspectorEvmTrait** is derived from EvmTrait and allows running Evm in Inspection mode. It contains **Inspector** associate type. Docs
* **EthInspectorHandler** is derived from EthHandler 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 EthInspectorHandler and InspectorEvmTrait. 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

```rust,ignore
pub trait EvmTrait {
type Context: ContextTrait;
pub trait EvmTr {
type Context: ContextTr;
...
fn execute_interpreter(..);
}
pub trait EthHandler {
type Evm: EvmTrait;
pub trait Handler {
type Evm: EvmTr;
type Frame: Frame;
...
fn run(evm);
}
```

### flow of execution
Execution flow can be found here (TODO Move to codebase to EthHandler 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
4 changes: 2 additions & 2 deletions book/src/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ The main traits that are used for creating a new variant are:
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 EvmTrait for `MyEvm`.
4. Create a empty `MyEvmHandler` type and implement EthHandler for it. Override any logic that you want.
3. Implement EvmTr for `MyEvm`.
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
2 changes: 1 addition & 1 deletion crates/context/interface/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Block, Cfg, Database, Journal, Transaction};
use auto_impl::auto_impl;

#[auto_impl(&mut, Box)]
pub trait ContextTrait {
pub trait ContextTr {
type Block: Block;
type Tx: Transaction;
type Cfg: Cfg;
Expand Down
6 changes: 3 additions & 3 deletions crates/context/interface/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub use crate::journaled_state::StateLoad;
use database_interface::Database;

use crate::{context::ContextTrait, journaled_state::AccountLoad, Block, Journal};
use crate::{context::ContextTr, journaled_state::AccountLoad, Block, Journal};
use primitives::{Address, Bytes, Log, B256, BLOCK_HASH_HISTORY, U256};

/// EVM context host.
pub trait Host: ContextTrait {
pub trait Host: ContextTr {
fn set_error(&mut self, error: <Self::Db as Database>::Error) {
*self.error() = Err(error);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ pub trait Host: ContextTrait {
}
}

impl<T: ContextTrait> Host for T {}
impl<T: ContextTr> Host for T {}

/// Represents the result of an `sstore` operation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/context/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod transaction;

pub use block::Block;
pub use cfg::{Cfg, CreateScheme, TransactTo};
pub use context::ContextTrait;
pub use context::ContextTr;
pub use database_interface::{DBErrorMarker, Database};
pub use journaled_state::Journal;
pub use transaction::{Transaction, TransactionType};
23 changes: 10 additions & 13 deletions crates/context/interface/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@ use primitives::{Address, Bytes, Log, U256};
use state::EvmState;
use std::{boxed::Box, string::String, vec::Vec};

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

impl<HaltReasonT> HaltReasonTrait for HaltReasonT where
HaltReasonT: 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<HaltReasonTy = HaltReason> {
/// Status of execution
pub result: ExecutionResult<HaltReasonT>,
pub result: ExecutionResult<HaltReasonTy>,
/// State that got updated
pub state: EvmState,
}

impl<HaltReasonT> ResultAndState<HaltReasonT> {
impl<HaltReasonTy> ResultAndState<HaltReasonTy> {
/// 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(HaltReasonTy) -> OHR,
{
ResultAndState {
result: self.result.map_haltreason(op),
Expand All @@ -37,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<HaltReasonTy = HaltReason> {
/// Returned successfully
Success {
reason: SuccessReason,
Expand All @@ -50,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: HaltReasonTy,
/// Halting will spend all the gas, and will be equal to gas_limit.
gas_used: u64,
},
}

impl<HaltReasonT> ExecutionResult<HaltReasonT> {
impl<HaltReasonTy> ExecutionResult<HaltReasonTy> {
/// Returns if transaction execution is successful.
///
/// 1 indicates success, 0 indicates revert.
Expand All @@ -69,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(HaltReasonTy) -> 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::AccessListTrait;
pub use eip7702::AuthorizationTrait;
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: AccessListTrait;
type Authorization: AuthorizationTrait;
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::{AccessListTrait, AuthorizationTrait};
use super::{AccessListTr, AuthorizationTr};
use primitives::{Address, B256, U256};

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

impl AccessListTrait 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 AuthorizationTrait for SignedAuthorization {
impl AuthorizationTr for SignedAuthorization {
fn authority(&self) -> Option<Address> {
self.recover_authority().ok()
}
Expand All @@ -30,7 +30,7 @@ impl AuthorizationTrait for SignedAuthorization {
}
}

impl AuthorizationTrait 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 AccessListTrait {
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 AuthorizationTrait {
pub trait AuthorizationTr {
/// Authority address.
///
/// # Note
Expand Down
4 changes: 2 additions & 2 deletions crates/context/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{block::BlockEnv, cfg::CfgEnv, journaled_state::JournaledState, tx::TxEnv};
use context_interface::{Block, Cfg, ContextTrait, Journal, Transaction};
use context_interface::{Block, Cfg, ContextTr, Journal, Transaction};
use database_interface::{Database, EmptyDB};
use derive_where::derive_where;
use specification::hardfork::SpecId;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl<
CFG: Cfg,
JOURNAL: Journal<Database = DB>,
CHAIN,
> ContextTrait for Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
> ContextTr for Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
{
type Block = BLOCK;
type Tx = TX;
Expand Down
4 changes: 0 additions & 4 deletions crates/database/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ pub mod empty_db;
pub use async_db::{DatabaseAsync, WrapDatabaseAsync};
pub use empty_db::{EmptyDB, EmptyDBTyped};

pub trait BytecodeTrait {
fn code(&self) -> &[u8];
}
/// Database error marker is needed to implement From conversion for Error type.
pub trait DBErrorMarker {}

Expand All @@ -37,7 +34,6 @@ impl DBErrorMarker for String {}
pub trait Database {
/// The database error type.
type Error: DBErrorMarker + Error;
//type Bytecode: BytecodeTrait;

/// Gets basic account information.
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
Expand Down
4 changes: 2 additions & 2 deletions crates/handler/src/execution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::frame_data::FrameResult;
use bytecode::EOF_MAGIC_BYTES;
use context_interface::ContextTrait;
use context_interface::ContextTr;
use context_interface::Transaction;
use interpreter::{
CallInputs, CallScheme, CallValue, CreateInputs, CreateScheme, EOFCreateInputs, EOFCreateKind,
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn create_init_frame(tx: &impl Transaction, spec: SpecId, gas_limit: u64) ->
}

/// TODO : Frame result should be a generic trait with needed functions.
pub fn last_frame_result<CTX: ContextTrait>(context: CTX, frame_result: &mut FrameResult) {
pub fn last_frame_result<CTX: ContextTr>(context: CTX, frame_result: &mut FrameResult) {
let instruction_result = frame_result.interpreter_result().result;
let gas = frame_result.gas_mut();
let remaining = gas.remaining();
Expand Down
22 changes: 11 additions & 11 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::frame_data::*;
use crate::{
handler::EvmTrait, instructions::InstructionProvider, precompile_provider::PrecompileProvider,
handler::EvmTr, instructions::InstructionProvider, precompile_provider::PrecompileProvider,
FrameInitOrResult, FrameOrResult, ItemOrResult,
};
use bytecode::{Eof, EOF_MAGIC_BYTES};
use context_interface::ContextTrait;
use context_interface::ContextTr;
use context_interface::{
journaled_state::{Journal, JournalCheckpoint},
Cfg, Database, Transaction,
Expand Down Expand Up @@ -73,15 +73,15 @@ pub struct EthFrame<EVM, ERROR, IW: InterpreterTypes> {

impl<EVM, ERROR> Frame for EthFrame<EVM, ERROR, EthInterpreter>
where
EVM: EvmTrait<
EVM: EvmTr<
Precompiles: PrecompileProvider<Context = EVM::Context, Output = InterpreterResult>,
Instructions: InstructionProvider<
Context = EVM::Context,
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
>,
ERROR: From<CtxTraitDbError<EVM::Context>> + From<PrecompileErrors>,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileErrors>,
{
type Evm = EVM;
type FrameInit = FrameInput;
Expand Down Expand Up @@ -117,7 +117,7 @@ where
}
}

pub type CtxTraitDbError<CTX> = <<CTX as ContextTrait>::Db as Database>::Error;
pub type ContextTrDbError<CTX> = <<CTX as ContextTr>::Db as Database>::Error;

impl<CTX, ERROR, IW> EthFrame<CTX, ERROR, IW>
where
Expand Down Expand Up @@ -145,12 +145,12 @@ where

impl<EVM, ERROR> EthFrame<EVM, ERROR, EthInterpreter>
where
EVM: EvmTrait<
Context: ContextTrait,
EVM: EvmTr<
Context: ContextTr,
Precompiles: PrecompileProvider<Context = EVM::Context, Output = InterpreterResult>,
Instructions: InstructionProvider,
>,
ERROR: From<CtxTraitDbError<EVM::Context>> + From<PrecompileErrors>,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileErrors>,
{
/// Make call frame
#[inline]
Expand Down Expand Up @@ -513,16 +513,16 @@ where

impl<EVM, ERROR> EthFrame<EVM, ERROR, EthInterpreter>
where
EVM: EvmTrait<
Context: ContextTrait,
EVM: EvmTr<
Context: ContextTr,
Precompiles: PrecompileProvider<Context = EVM::Context, Output = InterpreterResult>,
Instructions: InstructionProvider<
Context = EVM::Context,
InterpreterTypes = EthInterpreter,
Output = InterpreterAction,
>,
>,
ERROR: From<CtxTraitDbError<EVM::Context>> + From<PrecompileErrors>,
ERROR: From<ContextTrDbError<EVM::Context>> + From<PrecompileErrors>,
{
pub fn init_first(
evm: &mut EVM,
Expand Down
Loading

0 comments on commit 24162b7

Please sign in to comment.