Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Make inspector use generics, rm associated types #1934

Merged
merged 15 commits into from
Dec 24, 2024
13 changes: 13 additions & 0 deletions crates/context/interface/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use blob::{calc_blob_gasprice, calc_excess_blob_gas, BlobExcessGasAndPrice};

use auto_impl::auto_impl;
use primitives::{Address, B256, U256};
use std::boxed::Box;

/// Trait for retrieving block information required for execution.
#[auto_impl(&, &mut, Box, Arc)]
Expand Down Expand Up @@ -79,3 +80,15 @@ pub trait BlockGetter {
pub trait BlockSetter: BlockGetter {
fn set_block(&mut self, block: <Self as BlockGetter>::Block);
}

impl<T: BlockSetter> BlockSetter for &mut T {
fn set_block(&mut self, block: <Self as BlockGetter>::Block) {
(**self).set_block(block)
}
}

impl<T: BlockSetter> BlockSetter for Box<T> {
fn set_block(&mut self, block: <Self as BlockGetter>::Block) {
(**self).set_block(block)
}
}
3 changes: 3 additions & 0 deletions crates/context/interface/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use auto_impl::auto_impl;

// TODO : Change the name of the trait
#[auto_impl(&mut, Box)]
pub trait ErrorGetter {
type Error;

Expand Down
22 changes: 5 additions & 17 deletions crates/context/interface/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,16 @@ pub use dummy::DummyHost;

use crate::{
journaled_state::{AccountLoad, Eip7702CodeLoad},
Block, Cfg, Transaction,
BlockGetter, CfgGetter, TransactionGetter,
};
use auto_impl::auto_impl;
use primitives::{Address, Bytes, Log, B256, U256};

/// EVM context host.
// TODO : Move to context-interface
pub trait Host {
/// Chain specification
type BLOCK: Block;
type TX: Transaction;
type CFG: Cfg;

/// Returns a reference to the environment.
fn tx(&self) -> &Self::TX;

/// Returns a mutable reference to the environment.
fn block(&self) -> &Self::BLOCK;

// TODO : Make it generic in future
fn cfg(&self) -> &Self::CFG;

/// Loads an account code.
#[auto_impl(&mut, Box)]
pub trait Host: TransactionGetter + BlockGetter + CfgGetter {
/// Load an account code.
fn load_account_delegated(&mut self, address: Address) -> Option<AccountLoad>;

/// Gets the block hash of the given block `number`.
Expand Down
31 changes: 18 additions & 13 deletions crates/context/interface/src/host/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Host, SStoreResult, SelfDestructResult};
use crate::{Block, Cfg, Transaction};
use crate::{Block, BlockGetter, Cfg, CfgGetter, Transaction, TransactionGetter};
use primitives::{hash_map::Entry, Address, Bytes, HashMap, Log, B256, KECCAK_EMPTY, U256};
use std::vec::Vec;

Expand Down Expand Up @@ -48,26 +48,31 @@ where
}
}

impl<TX: Transaction, BLOCK: Block, CFG: Cfg> Host for DummyHost<BLOCK, TX, CFG> {
type TX = TX;
type BLOCK = BLOCK;
type CFG = CFG;
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> BlockGetter for DummyHost<BLOCK, TX, CFG> {
type Block = BLOCK;

#[inline]
fn tx(&self) -> &Self::TX {
&self.tx
fn block(&self) -> &Self::Block {
&self.block
}
}

#[inline]
fn block(&self) -> &Self::BLOCK {
&self.block
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> TransactionGetter for DummyHost<BLOCK, TX, CFG> {
type Transaction = TX;

fn tx(&self) -> &Self::Transaction {
&self.tx
}
}

#[inline]
fn cfg(&self) -> &Self::CFG {
impl<BLOCK: Block, TX: Transaction, CFG: Cfg> CfgGetter for DummyHost<BLOCK, TX, CFG> {
type Cfg = CFG;

fn cfg(&self) -> &Self::Cfg {
&self.cfg
}
}

impl<TX: Transaction, BLOCK: Block, CFG: Cfg> Host for DummyHost<BLOCK, TX, CFG> {
#[inline]
fn load_account_delegated(&mut self, _address: Address) -> Option<AccountLoad> {
Some(AccountLoad::default())
Expand Down
22 changes: 16 additions & 6 deletions crates/context/interface/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,38 @@ impl<T> Eip7702CodeLoad<T> {
}
}

/// Helper that extracts database error from [`JournalStateGetter`]
pub type JournalStateGetterDBError<CTX> =
<<<CTX as JournalStateGetter>::Journal as Journal>::Database as Database>::Error;
/// Helper that extracts database error from [`JournalGetter`].
pub type JournalDBError<CTX> =
<<<CTX as JournalGetter>::Journal as Journal>::Database as Database>::Error;

pub trait JournalStateGetter: DatabaseGetter {
pub trait JournalGetter: DatabaseGetter {
type Journal: Journal<Database = <Self as DatabaseGetter>::Database>;

fn journal(&mut self) -> &mut Self::Journal;

fn journal_ref(&self) -> &Self::Journal;
}

impl<T: JournalStateGetter> JournalStateGetter for &mut T {
impl<T: JournalGetter> JournalGetter for &mut T {
type Journal = T::Journal;

fn journal(&mut self) -> &mut Self::Journal {
T::journal(*self)
}

fn journal_ref(&self) -> &Self::Journal {
T::journal_ref(*self)
}
}

impl<T: JournalStateGetter> JournalStateGetter for Box<T> {
impl<T: JournalGetter> JournalGetter for Box<T> {
type Journal = T::Journal;

fn journal(&mut self) -> &mut Self::Journal {
T::journal(self.as_mut())
}

fn journal_ref(&self) -> &Self::Journal {
T::journal_ref(self.as_ref())
}
}
2 changes: 1 addition & 1 deletion crates/context/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub use block::{Block, BlockGetter};
pub use cfg::{Cfg, CfgGetter, CreateScheme, TransactTo};
pub use database_interface::{DBErrorMarker, Database, DatabaseGetter};
pub use errors::ErrorGetter;
pub use journaled_state::{Journal, JournalStateGetter, JournalStateGetterDBError};
pub use journaled_state::{Journal, JournalDBError, JournalGetter};
pub use transaction::{Transaction, TransactionGetter, TransactionType};
13 changes: 13 additions & 0 deletions crates/context/interface/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use auto_impl::auto_impl;
use core::cmp::min;
use core::fmt::Debug;
use primitives::{TxKind, U256};
use std::boxed::Box;

/// Transaction validity error types.
pub trait TransactionError: Debug + core::error::Error {}
Expand Down Expand Up @@ -164,3 +165,15 @@ pub trait TransactionGetter {
pub trait TransactionSetter: TransactionGetter {
fn set_tx(&mut self, tx: <Self as TransactionGetter>::Transaction);
}

impl<T: TransactionSetter> TransactionSetter for &mut T {
fn set_tx(&mut self, block: <Self as TransactionGetter>::Transaction) {
(**self).set_tx(block)
}
}

impl<T: TransactionSetter> TransactionSetter for Box<T> {
fn set_tx(&mut self, block: <Self as TransactionGetter>::Transaction) {
(**self).set_tx(block)
}
}
38 changes: 13 additions & 25 deletions crates/context/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use context_interface::{
journaled_state::{AccountLoad, Eip7702CodeLoad},
result::EVMError,
transaction::TransactionSetter,
Block, BlockGetter, Cfg, CfgGetter, DatabaseGetter, ErrorGetter, Journal, JournalStateGetter,
Block, BlockGetter, Cfg, CfgGetter, DatabaseGetter, ErrorGetter, Journal, JournalGetter,
Transaction, TransactionGetter,
};
use database_interface::{Database, EmptyDB};
Expand All @@ -24,17 +24,17 @@ pub struct Context<
JOURNAL: Journal<Database = DB> = JournaledState<DB>,
CHAIN = (),
> {
/// Transaction information
pub tx: TX,
/// Block information
/// Block information.
pub block: BLOCK,
/// Configurations
/// Transaction information.
pub tx: TX,
/// Configurations.
pub cfg: CFG,
/// EVM State with journaling support and database
/// EVM State with journaling support and database.
pub journaled_state: JOURNAL,
/// Inner context
/// Inner context.
pub chain: CHAIN,
/// Error that happened during execution
/// Error that happened during execution.
pub error: Result<(), <DB as Database>::Error>,
}

Expand Down Expand Up @@ -380,22 +380,6 @@ where
DB: Database,
JOURNAL: Journal<Database = DB>,
{
type BLOCK = BLOCK;
type TX = TX;
type CFG = CFG;

fn tx(&self) -> &Self::TX {
&self.tx
}

fn block(&self) -> &Self::BLOCK {
&self.block
}

fn cfg(&self) -> &Self::CFG {
&self.cfg
}

fn block_hash(&mut self, requested_number: u64) -> Option<B256> {
let block_number = as_u64_saturated!(*self.block().number());

Expand Down Expand Up @@ -498,7 +482,7 @@ impl<BLOCK, TX, CFG: Cfg, DB: Database, JOURNAL: Journal<Database = DB>, CHAIN>
}
}

impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> JournalStateGetter
impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> JournalGetter
for Context<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN>
where
DB: Database,
Expand All @@ -509,6 +493,10 @@ where
fn journal(&mut self) -> &mut Self::Journal {
&mut self.journaled_state
}

fn journal_ref(&self) -> &Self::Journal {
&self.journaled_state
}
}

impl<BLOCK, TX, SPEC, DB, JOURNAL, CHAIN> DatabaseGetter
Expand Down
3 changes: 0 additions & 3 deletions crates/context/src/default.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/database/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::convert::Infallible;
use auto_impl::auto_impl;
use primitives::{Address, HashMap, B256, U256};
use state::{Account, AccountInfo, Bytecode};
use std::string::String;

#[cfg(feature = "asyncdb")]
pub mod async_db;
Expand All @@ -28,6 +29,7 @@ pub trait DBErrorMarker {}
/// Implement marker for `()`.
impl DBErrorMarker for () {}
impl DBErrorMarker for Infallible {}
impl DBErrorMarker for String {}

/// EVM database interface.
#[auto_impl(&mut, Box)]
Expand Down
22 changes: 8 additions & 14 deletions crates/handler/src/execution.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{frame_data::FrameResult, EthFrame, EthPrecompileProvider};
use bytecode::EOF_MAGIC_BYTES;
use context_interface::{
result::InvalidTransaction, BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalStateGetter,
JournalStateGetterDBError, Transaction, TransactionGetter,
result::InvalidTransaction, BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalDBError,
JournalGetter, Transaction, TransactionGetter,
};
use handler_interface::{util::FrameOrFrameResult, ExecutionHandler, Frame as FrameTrait};
use interpreter::{
Expand Down Expand Up @@ -125,30 +125,24 @@ impl<CTX, ERROR, FRAME> EthExecution<CTX, ERROR, FRAME> {
}

pub trait EthExecutionContext<ERROR>:
TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalStateGetter + CfgGetter
TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter
{
}

impl<
ERROR,
T: TransactionGetter
+ ErrorGetter<Error = ERROR>
+ BlockGetter
+ JournalStateGetter
+ CfgGetter,
T: TransactionGetter + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter,
> EthExecutionContext<ERROR> for T
{
}

pub trait EthExecutionError<CTX: JournalStateGetter>:
From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>
pub trait EthExecutionError<CTX: JournalGetter>:
From<InvalidTransaction> + From<JournalDBError<CTX>>
{
}

impl<
CTX: JournalStateGetter,
T: From<InvalidTransaction> + From<JournalStateGetterDBError<CTX>>,
> EthExecutionError<CTX> for T
impl<CTX: JournalGetter, T: From<InvalidTransaction> + From<JournalDBError<CTX>>>
EthExecutionError<CTX> for T
{
}

Expand Down
18 changes: 9 additions & 9 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::frame_data::*;
use bytecode::{Eof, EOF_MAGIC_BYTES};
use context_interface::{
journaled_state::{Journal, JournalCheckpoint},
BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalStateGetter, JournalStateGetterDBError,
Transaction, TransactionGetter,
BlockGetter, Cfg, CfgGetter, ErrorGetter, JournalDBError, JournalGetter, Transaction,
TransactionGetter,
};
use core::{cell::RefCell, cmp::min};
use handler_interface::{Frame, FrameOrResultGen, PrecompileProvider};
Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct EthFrame<CTX, ERROR, IW: InterpreterTypes, PRECOMPILE, INSTRUCTIONS>

impl<CTX, IW, ERROR, PRECOMP, INST> EthFrame<CTX, ERROR, IW, PRECOMP, INST>
where
CTX: JournalStateGetter,
CTX: JournalGetter,
IW: InterpreterTypes,
{
pub fn new(
Expand Down Expand Up @@ -792,7 +792,7 @@ pub fn return_eofcreate<JOURNAL: Journal>(
}

pub trait EthFrameContext<ERROR>:
TransactionGetter + Host + ErrorGetter<Error = ERROR> + BlockGetter + JournalStateGetter + CfgGetter
TransactionGetter + Host + ErrorGetter<Error = ERROR> + BlockGetter + JournalGetter + CfgGetter
{
}

Expand All @@ -801,19 +801,19 @@ impl<
CTX: TransactionGetter
+ ErrorGetter<Error = ERROR>
+ BlockGetter
+ JournalStateGetter
+ JournalGetter
+ CfgGetter
+ Host,
> EthFrameContext<ERROR> for CTX
{
}

pub trait EthFrameError<CTX: JournalStateGetter>:
From<JournalStateGetterDBError<CTX>> + From<PrecompileErrors>
pub trait EthFrameError<CTX: JournalGetter>:
From<JournalDBError<CTX>> + From<PrecompileErrors>
{
}

impl<CTX: JournalStateGetter, T: From<JournalStateGetterDBError<CTX>> + From<PrecompileErrors>>
EthFrameError<CTX> for T
impl<CTX: JournalGetter, T: From<JournalDBError<CTX>> + From<PrecompileErrors>> EthFrameError<CTX>
for T
{
}
Loading
Loading