Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod dummy_host;

use crate::primitives::Bytecode;
use crate::{
primitives::{Bytes, Env, B160, B256, U256},
CallInputs, CreateInputs, Gas, InstructionResult, Interpreter, SelfDestructResult,
};
pub use alloc::vec::Vec;
pub use dummy_host::DummyHost;
use alloc::vec::Vec;
pub use dummy::DummyHost;

mod dummy;

/// EVM context host.
pub trait Host {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct DummyHost {
}

impl DummyHost {
/// Create a new dummy host with the given [`Env`].
#[inline]
pub fn new(env: Env) -> Self {
Self {
env,
Expand All @@ -21,17 +23,22 @@ impl DummyHost {
log: Vec::new(),
}
}

/// Clears the storage and logs of the dummy host.
#[inline]
pub fn clear(&mut self) {
self.storage.clear();
self.log.clear();
}
}

impl Host for DummyHost {
#[inline]
fn step(&mut self, _interp: &mut Interpreter) -> InstructionResult {
InstructionResult::Continue
}

#[inline]
fn step_end(
&mut self,
_interp: &mut Interpreter,
Expand All @@ -40,30 +47,37 @@ impl Host for DummyHost {
InstructionResult::Continue
}

#[inline]
fn env(&mut self) -> &mut Env {
&mut self.env
}

#[inline]
fn load_account(&mut self, _address: B160) -> Option<(bool, bool)> {
Some((true, true))
}

#[inline]
fn block_hash(&mut self, _number: U256) -> Option<B256> {
Some(B256::zero())
}

#[inline]
fn balance(&mut self, _address: B160) -> Option<(U256, bool)> {
Some((U256::ZERO, false))
}

#[inline]
fn code(&mut self, _address: B160) -> Option<(Bytecode, bool)> {
Some((Bytecode::default(), false))
}

#[inline]
fn code_hash(&mut self, __address: B160) -> Option<(B256, bool)> {
Some((KECCAK_EMPTY, false))
}

#[inline]
fn sload(&mut self, __address: B160, index: U256) -> Option<(U256, bool)> {
match self.storage.entry(index) {
Entry::Occupied(entry) => Some((*entry.get(), false)),
Expand All @@ -74,6 +88,7 @@ impl Host for DummyHost {
}
}

#[inline]
fn sstore(
&mut self,
_address: B160,
Expand All @@ -91,17 +106,20 @@ impl Host for DummyHost {
Some((U256::ZERO, present, value, is_cold))
}

#[inline]
fn tload(&mut self, _address: B160, index: U256) -> U256 {
self.transient_storage
.get(&index)
.cloned()
.unwrap_or_default()
}

#[inline]
fn tstore(&mut self, _address: B160, index: U256, value: U256) {
self.transient_storage.insert(index, value);
}

#[inline]
fn log(&mut self, address: B160, topics: Vec<B256>, data: Bytes) {
self.log.push(Log {
address,
Expand All @@ -110,17 +128,20 @@ impl Host for DummyHost {
})
}

#[inline]
fn selfdestruct(&mut self, _address: B160, _target: B160) -> Option<SelfDestructResult> {
panic!("Selfdestruct is not supported for this host")
}

#[inline]
fn create(
&mut self,
_inputs: &mut CreateInputs,
) -> (InstructionResult, Option<B160>, Gas, Bytes) {
panic!("Create is not supported for this host")
}

#[inline]
fn call(&mut self, _input: &mut CallInputs) -> (InstructionResult, Gas, Bytes) {
panic!("Call is not supported for this host")
}
Expand Down
35 changes: 27 additions & 8 deletions crates/interpreter/src/instruction_result.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use revm_primitives::{Eval, Halt};
use crate::primitives::{Eval, Halt};

#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InstructionResult {
//success codes
// success codes
Continue = 0x00,
Stop = 0x01,
Return = 0x02,
SelfDestruct = 0x03,

// revert code
// revert codes
Revert = 0x20, // revert opcode
CallTooDeep = 0x21,
OutOfFund = 0x22,
Expand Down Expand Up @@ -41,12 +41,26 @@ pub enum InstructionResult {
/// EIP-3860: Limit and meter initcode. Initcode size limit exceeded.
CreateInitcodeSizeLimit,

// Fatal external error. Returned by database.
/// Fatal external error. Returned by database.
FatalExternalError,
}

impl InstructionResult {
pub fn is_error(&self) -> bool {
/// Returns whether the result is a success.
#[inline]
pub fn is_ok(self) -> bool {
matches!(self, crate::return_ok!())
}

/// Returns whether the result is a revert.
#[inline]
pub fn is_revert(self) -> bool {
matches!(self, crate::return_revert!())
}

/// Returns whether the result is an error.
#[inline]
pub fn is_error(self) -> bool {
matches!(
self,
Self::OutOfGas
Expand Down Expand Up @@ -87,11 +101,13 @@ pub enum SuccessOrHalt {

impl SuccessOrHalt {
/// Returns true if the transaction returned successfully without halts.
pub fn is_success(&self) -> bool {
#[inline]
pub fn is_success(self) -> bool {
matches!(self, SuccessOrHalt::Success(_))
}

/// Returns the [Eval] value if this a successful result
#[inline]
pub fn to_success(self) -> Option<Eval> {
match self {
SuccessOrHalt::Success(eval) => Some(eval),
Expand All @@ -100,16 +116,19 @@ impl SuccessOrHalt {
}

/// Returns true if the transaction reverted.
pub fn is_revert(&self) -> bool {
#[inline]
pub fn is_revert(self) -> bool {
matches!(self, SuccessOrHalt::Revert)
}

/// Returns true if the EVM has experienced an exceptional halt
pub fn is_halt(&self) -> bool {
#[inline]
pub fn is_halt(self) -> bool {
matches!(self, SuccessOrHalt::Halt(_))
}

/// Returns the [Halt] value the EVM has experienced an exceptional halt
#[inline]
pub fn to_halt(self) -> Option<Halt> {
match self {
SuccessOrHalt::Halt(halt) => Some(halt),
Expand Down
1 change: 1 addition & 0 deletions crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[macro_use]
mod macros;

mod arithmetic;
mod bitwise;
mod control;
Expand Down