Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Jan 16, 2025
1 parent 195c7e4 commit 54f8e8d
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 180 deletions.
148 changes: 8 additions & 140 deletions crates/handler/src/eth_handler.rs → crates/handler/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,19 @@
use crate::{
execution, post_execution, pre_execution, validation, EthPrecompileProvider, FrameContext,
FrameResult,
};
use context::Context;
pub mod types;

pub use types::{EthContext, EthError, EthHandlerImpl};

use crate::{execution, post_execution, pre_execution, validation, FrameContext, FrameResult};
use context_interface::{
result::{HaltReason, InvalidHeader, InvalidTransaction, ResultAndState},
Block, BlockGetter, Cfg, CfgGetter, Database, DatabaseGetter, ErrorGetter, Journal,
JournalDBError, JournalGetter, PerformantContextAccess, Transaction, TransactionGetter,
Cfg, CfgGetter, ErrorGetter, Journal, JournalDBError, JournalGetter, Transaction,
TransactionGetter,
};
use handler_interface::{
util::FrameOrFrameResult, Frame, FrameOrResult, InitialAndFloorGas, PrecompileProvider,
};
use interpreter::{
interpreter::{EthInstructionProvider, EthInterpreter, InstructionProvider},
FrameInput, Host,
};
use precompile::PrecompileErrors;
use primitives::Log;
use specification::hardfork::SpecId;
use state::EvmState;
use interpreter::{interpreter::InstructionProvider, FrameInput};
use std::{vec, vec::Vec};

pub struct EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS> {
pub precompiles: PRECOMPILES,
pub instructions: INSTRUCTIONS,
pub _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS)>,
}

impl<CTX: Host, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
where
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter, Host = CTX>,
{
pub fn crete_frame_context(&self) -> FrameContext<PRECOMPILES, INSTRUCTIONS> {
FrameContext {
precompiles: self.precompiles.clone(),
instructions: self.instructions.clone(),
}
}
}

impl<CTX: Host, ERROR, FRAME> Default
for EthHandlerImpl<
CTX,
ERROR,
FRAME,
EthPrecompileProvider<CTX, ERROR>,
EthInstructionProvider<EthInterpreter, CTX>,
>
{
fn default() -> Self {
Self {
precompiles: EthPrecompileProvider::new(SpecId::LATEST),
instructions: EthInstructionProvider::new(),
_phantom: core::marker::PhantomData,
}
}
}

pub trait EthContext:
TransactionGetter
+ BlockGetter
+ DatabaseGetter
+ CfgGetter
+ PerformantContextAccess<Error = JournalDBError<Self>>
+ ErrorGetter<Error = JournalDBError<Self>>
+ JournalGetter<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>
+ Host
{
}

pub trait EthError<CTX: JournalGetter, FRAME: Frame>:
From<InvalidTransaction>
+ From<InvalidHeader>
+ From<JournalDBError<CTX>>
+ From<<FRAME as Frame>::Error>
+ From<PrecompileErrors>
{
}

impl<
CTX: JournalGetter,
FRAME: Frame,
T: From<InvalidTransaction>
+ From<InvalidHeader>
+ From<JournalDBError<CTX>>
+ From<<FRAME as Frame>::Error>
+ From<PrecompileErrors>,
> EthError<CTX, FRAME> for T
{
}

impl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS> EthHandler
for EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
where
CTX: EthContext,
ERROR: EthError<CTX, FRAME>,
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter, Host = CTX>,
// TODO `FrameResult` should be a generic trait.
// TODO `FrameInit` should be a generic.
FRAME: Frame<
Context = CTX,
Error = ERROR,
FrameResult = FrameResult,
FrameInit = FrameInput,
FrameContext = FrameContext<PRECOMPILES, INSTRUCTIONS>,
>,
{
type Context = CTX;
type Error = ERROR;
type Frame = FRAME;
type Precompiles = PRECOMPILES;
type Instructions = INSTRUCTIONS;

fn frame_context(
&mut self,
context: &mut Self::Context,
) -> <Self::Frame as Frame>::FrameContext {
self.precompiles.set_spec(context.cfg().spec().into());
self.crete_frame_context()
}
}

pub trait EthHandler {
type Context: EthContext;
type Error: From<InvalidTransaction>
Expand Down Expand Up @@ -446,25 +336,3 @@ pub trait EthHandler {
context.journal().clear();
}
}

impl<
BLOCK: Block,
TX: Transaction,
CFG: Cfg,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)>,
CHAIN,
> EthContext for Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
{
}

impl<
BLOCK: Block,
TX: Transaction,
CFG: Cfg,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)>,
CHAIN,
> EthContext for &mut Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
{
}
144 changes: 144 additions & 0 deletions crates/handler/src/handler/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use context::Context;
use context_interface::{
result::{InvalidHeader, InvalidTransaction},
Block, BlockGetter, Cfg, CfgGetter, Database, DatabaseGetter, ErrorGetter, Journal,
JournalDBError, JournalGetter, PerformantContextAccess, Transaction, TransactionGetter,
};
use handler_interface::{Frame, PrecompileProvider};
use interpreter::{
interpreter::{EthInstructionProvider, EthInterpreter, InstructionProvider},
FrameInput, Host,
};
use precompile::PrecompileErrors;
use primitives::Log;
use specification::hardfork::SpecId;
use state::EvmState;

use crate::{EthPrecompileProvider, FrameContext, FrameResult};

use super::EthHandler;

pub struct EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS> {
pub precompiles: PRECOMPILES,
pub instructions: INSTRUCTIONS,
pub _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS)>,
}

impl<CTX: Host, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
where
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter, Host = CTX>,
{
pub fn crete_frame_context(&self) -> FrameContext<PRECOMPILES, INSTRUCTIONS> {
FrameContext {
precompiles: self.precompiles.clone(),
instructions: self.instructions.clone(),
}
}
}

impl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS> EthHandler
for EthHandlerImpl<CTX, ERROR, FRAME, PRECOMPILES, INSTRUCTIONS>
where
CTX: EthContext,
ERROR: EthError<CTX, FRAME>,
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter, Host = CTX>,
// TODO `FrameResult` should be a generic trait.
// TODO `FrameInit` should be a generic.
FRAME: Frame<
Context = CTX,
Error = ERROR,
FrameResult = FrameResult,
FrameInit = FrameInput,
FrameContext = FrameContext<PRECOMPILES, INSTRUCTIONS>,
>,
{
type Context = CTX;
type Error = ERROR;
type Frame = FRAME;
type Precompiles = PRECOMPILES;
type Instructions = INSTRUCTIONS;

fn frame_context(
&mut self,
context: &mut Self::Context,
) -> <Self::Frame as Frame>::FrameContext {
self.precompiles.set_spec(context.cfg().spec().into());
self.crete_frame_context()
}
}

impl<CTX: Host, ERROR, FRAME> Default
for EthHandlerImpl<
CTX,
ERROR,
FRAME,
EthPrecompileProvider<CTX, ERROR>,
EthInstructionProvider<EthInterpreter, CTX>,
>
{
fn default() -> Self {
Self {
precompiles: EthPrecompileProvider::new(SpecId::LATEST),
instructions: EthInstructionProvider::new(),
_phantom: core::marker::PhantomData,
}
}
}

pub trait EthContext:
TransactionGetter
+ BlockGetter
+ DatabaseGetter
+ CfgGetter
+ PerformantContextAccess<Error = JournalDBError<Self>>
+ ErrorGetter<Error = JournalDBError<Self>>
+ JournalGetter<Journal: Journal<FinalOutput = (EvmState, Vec<Log>)>>
+ Host
{
}

pub trait EthError<CTX: JournalGetter, FRAME: Frame>:
From<InvalidTransaction>
+ From<InvalidHeader>
+ From<JournalDBError<CTX>>
+ From<<FRAME as Frame>::Error>
+ From<PrecompileErrors>
{
}

impl<
CTX: JournalGetter,
FRAME: Frame,
T: From<InvalidTransaction>
+ From<InvalidHeader>
+ From<JournalDBError<CTX>>
+ From<<FRAME as Frame>::Error>
+ From<PrecompileErrors>,
> EthError<CTX, FRAME> for T
{
}

impl<
BLOCK: Block,
TX: Transaction,
CFG: Cfg,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)>,
CHAIN,
> EthContext for Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
{
}

impl<
BLOCK: Block,
TX: Transaction,
CFG: Cfg,
DB: Database,
JOURNAL: Journal<Database = DB, FinalOutput = (EvmState, Vec<Log>)>,
CHAIN,
> EthContext for &mut Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
{
}
2 changes: 1 addition & 1 deletion crates/handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ extern crate alloc as std;

// Mainnet related handlers.

pub mod eth_handler;
pub mod execution;
mod frame;
mod frame_data;
pub mod handler;
pub mod post_execution;
pub mod pre_execution;
mod precompile_provider;
Expand Down
Loading

0 comments on commit 54f8e8d

Please sign in to comment.