Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Erc20 example, need to fix logic
  • Loading branch information
rakita committed Jan 17, 2025
commit d7fcbe0699cca1e12b0ed195c6075bd19bbd4c0f
69 changes: 64 additions & 5 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ members = [
"crates/handler",

# variants
# "crates/optimism",
"crates/optimism",
"crates/inspector",

# utility
Expand All @@ -31,7 +31,7 @@ members = [
"examples/database_components",
"examples/uniswap_get_reserves",
"examples/uniswap_v2_usdc_swap",
#"examples/erc20_gas",
"examples/erc20_gas",
#"examples/custom_opcodes",
]
resolver = "2"
Expand Down
40 changes: 40 additions & 0 deletions crates/context/interface/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ pub struct ResultAndState<HaltReasonT: HaltReasonTrait> {
pub state: EvmState,
}

impl<HaltReasonT: HaltReasonTrait> ResultAndState<HaltReasonT> {
/// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
pub fn map_haltreason<F, OHR: HaltReasonTrait>(self, op: F) -> ResultAndState<OHR>
where
F: FnOnce(HaltReasonT) -> OHR,
{
ResultAndState {
result: self.result.map_haltreason(op),
state: self.state,
}
}
}

/// Result of a transaction execution
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -53,6 +66,33 @@ impl<HaltReasonT: HaltReasonTrait> ExecutionResult<HaltReasonT> {
matches!(self, Self::Success { .. })
}

/// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
pub fn map_haltreason<F, OHR: HaltReasonTrait>(self, op: F) -> ExecutionResult<OHR>
where
F: FnOnce(HaltReasonT) -> OHR,
{
match self {
Self::Success {
reason,
gas_used,
gas_refunded,
logs,
output,
} => ExecutionResult::Success {
reason,
gas_used,
gas_refunded,
logs,
output,
},
Self::Revert { gas_used, output } => ExecutionResult::Revert { gas_used, output },
Self::Halt { reason, gas_used } => ExecutionResult::Halt {
reason: op(reason),
gas_used,
},
}
}

/// Returns created address if execution is Create transaction
/// and Contract was created.
pub fn created_address(&self) -> Option<Address> {
Expand Down
3 changes: 2 additions & 1 deletion crates/handler/interface/src/precompile_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub trait PrecompileProvider: Clone {
type Context;
type Output;
type Error;
type Spec: Into<SpecId>;

fn set_spec(&mut self, spec: SpecId);
fn set_spec(&mut self, spec: Self::Spec);

/// Run the precompile.
fn run(
Expand Down
3 changes: 2 additions & 1 deletion crates/handler/interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ impl<ITEM, RES> ItemOrResult<ITEM, RES> {
}

pub type FrameOrResult<FRAME> = ItemOrResult<FRAME, <FRAME as Frame>::FrameResult>;
pub type FrameInitOrResult<FRAME> = ItemOrResult<<FRAME as Frame>::FrameInit, <FRAME as Frame>::FrameResult>;
pub type FrameInitOrResult<FRAME> =
ItemOrResult<<FRAME as Frame>::FrameInit, <FRAME as Frame>::FrameResult>;
62 changes: 32 additions & 30 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use state::Bytecode;
use std::borrow::ToOwned;
use std::{rc::Rc, sync::Arc};

pub struct EthFrame<CTX, ERROR, IW: InterpreterTypes, PRECOMPILES, INSTRUCTIONS> {
_phantom: core::marker::PhantomData<fn() -> (PRECOMPILES, INSTRUCTIONS, CTX, ERROR)>,
pub struct EthFrame<CTX, ERROR, IW: InterpreterTypes, FRAMECTX> {
_phantom: core::marker::PhantomData<(FRAMECTX, CTX, ERROR)>,
data: FrameData,
// TODO : Include this
depth: usize,
Expand All @@ -39,7 +39,7 @@ pub struct EthFrame<CTX, ERROR, IW: InterpreterTypes, PRECOMPILES, INSTRUCTIONS>
pub memory: Rc<RefCell<SharedMemory>>,
}

impl<CTX, IW, ERROR, PRECOMPILES, INSTRUCTIONS> EthFrame<CTX, ERROR, IW, PRECOMPILES, INSTRUCTIONS>
impl<CTX, IW, ERROR, FRAMECTX> EthFrame<CTX, ERROR, IW, FRAMECTX>
where
CTX: JournalGetter,
IW: InterpreterTypes,
Expand All @@ -62,19 +62,25 @@ where
}
}

impl<CTX, ERROR, PRECOMPILES, INSTRUCTIONS>
EthFrame<CTX, ERROR, EthInterpreter<()>, PRECOMPILES, INSTRUCTIONS>
impl<CTX, ERROR, FRAMECTX> EthFrame<CTX, ERROR, EthInterpreter<()>, FRAMECTX>
where
CTX: EthFrameContext,
ERROR: EthFrameError<CTX>,
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR, Output = InterpreterResult>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter<()>, Host = CTX>,
ERROR: From<JournalDBError<CTX>> + From<PrecompileErrors>,
FRAMECTX: PrecompileProviderGetter<
PrecompileProvider: PrecompileProvider<
Context = CTX,
Error = ERROR,
Output = InterpreterResult,
>,
> + InstructionProviderGetter<
InstructionProvider: InstructionProvider<WIRE = EthInterpreter<()>, Host = CTX>,
>,
{
/// Make call frame
#[inline]
pub fn make_call_frame(
context: &mut CTX,
frame_context: &mut FrameContext<PRECOMPILES, INSTRUCTIONS>,
frame_context: &mut FRAMECTX,
depth: usize,
memory: Rc<RefCell<SharedMemory>>,
inputs: &CallInputs,
Expand Down Expand Up @@ -172,7 +178,7 @@ where
call_value: inputs.value.get(),
};

Ok(ItemOrResult::Frame(Self::new(
Ok(ItemOrResult::Item(Self::new(
FrameData::Call(CallFrame {
return_memory_range: inputs.return_memory_offset.clone(),
}),
Expand Down Expand Up @@ -274,7 +280,7 @@ where
call_value: inputs.value,
};

Ok(ItemOrResult::Frame(Self::new(
Ok(ItemOrResult::Item(Self::new(
FrameData::Create(CreateFrame { created_address }),
depth,
Interpreter::new(
Expand Down Expand Up @@ -387,7 +393,7 @@ where
call_value: inputs.value,
};

Ok(ItemOrResult::Frame(Self::new(
Ok(ItemOrResult::Item(Self::new(
FrameData::EOFCreate(EOFCreateFrame { created_address }),
depth,
Interpreter::new(
Expand All @@ -409,7 +415,7 @@ where
frame_init: FrameInput,
memory: Rc<RefCell<SharedMemory>>,
context: &mut CTX,
frame_context: &mut FrameContext<PRECOMPILES, INSTRUCTIONS>,
frame_context: &mut FRAMECTX,
) -> Result<ItemOrResult<Self, FrameResult>, ERROR> {
match frame_init {
FrameInput::Call(inputs) => {
Expand Down Expand Up @@ -459,18 +465,24 @@ impl<PRECOMPILES: PrecompileProvider, INSTRUCTIONS: InstructionProvider> Instruc
}
}

impl<CTX, ERROR, PRECOMPILES, INSTRUCTIONS> Frame
for EthFrame<CTX, ERROR, EthInterpreter<()>, PRECOMPILES, INSTRUCTIONS>
impl<CTX, ERROR, FRAMECTX> Frame for EthFrame<CTX, ERROR, EthInterpreter<()>, FRAMECTX>
where
CTX: EthFrameContext,
ERROR: EthFrameError<CTX>,
PRECOMPILES: PrecompileProvider<Context = CTX, Error = ERROR, Output = InterpreterResult>,
INSTRUCTIONS: InstructionProvider<WIRE = EthInterpreter<()>, Host = CTX>,
ERROR: From<JournalDBError<CTX>> + From<PrecompileErrors>,
FRAMECTX: PrecompileProviderGetter<
PrecompileProvider: PrecompileProvider<
Context = CTX,
Error = ERROR,
Output = InterpreterResult,
>,
> + InstructionProviderGetter<
InstructionProvider: InstructionProvider<WIRE = EthInterpreter<()>, Host = CTX>,
>,
{
type Context = CTX;
type Error = ERROR;
type FrameInit = FrameInput;
type FrameContext = FrameContext<PRECOMPILES, INSTRUCTIONS>;
type FrameContext = FRAMECTX;
type FrameResult = FrameResult;

fn init_first(
Expand Down Expand Up @@ -526,7 +538,7 @@ where
.run(frame_context.instructions().table(), context);

let mut interpreter_result = match next_action {
InterpreterAction::NewFrame(new_frame) => return Ok(ItemOrResult::Frame(new_frame)),
InterpreterAction::NewFrame(new_frame) => return Ok(ItemOrResult::Item(new_frame)),
InterpreterAction::Return { result } => result,
InterpreterAction::None => unreachable!("InterpreterAction::None is not expected"),
};
Expand Down Expand Up @@ -827,13 +839,3 @@ impl<
> EthFrameContext for CTX
{
}

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

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