pub struct Interpreter {Show 13 fields
pub instruction_pointer: *const u8,
pub gas: Gas,
pub contract: Contract,
pub instruction_result: InstructionResult,
pub bytecode: Bytes,
pub is_eof: bool,
pub is_eof_init: bool,
pub shared_memory: SharedMemory,
pub stack: Stack,
pub function_stack: FunctionStack,
pub return_data_buffer: Bytes,
pub is_static: bool,
pub next_action: InterpreterAction,
}
Expand description
EVM bytecode interpreter.
Fields§
§instruction_pointer: *const u8
The current instruction pointer.
gas: Gas
The gas state.
contract: Contract
Contract information and invoking data
instruction_result: InstructionResult
The execution control flag. If this is not set to Continue
, the interpreter will stop
execution.
bytecode: Bytes
Currently run Bytecode that instruction result will point to. Bytecode is owned by the contract.
is_eof: bool
Whether we are Interpreting the Ethereum Object Format (EOF) bytecode.
This is local field that is set from contract.is_eof()
.
is_eof_init: bool
Is init flag for eof create
Shared memory.
Note: This field is only set while running the interpreter loop. Otherwise it is taken and replaced with empty shared memory.
stack: Stack
Stack.
function_stack: FunctionStack
EOF function stack.
return_data_buffer: Bytes
The return data buffer for internal calls. It has multi usage:
- It contains the output bytes of call sub call.
- When this interpreter finishes execution it contains the output bytes of this contract.
is_static: bool
Whether the interpreter is in “staticcall” mode, meaning no state changes can happen.
next_action: InterpreterAction
Actions that the EVM should do.
Set inside CALL or CREATE instructions and RETURN or REVERT instructions. Additionally those instructions will set InstructionResult to CallOrCreate/Return/Revert so we know the reason.
Implementations§
Source§impl Interpreter
impl Interpreter
Sourcepub fn set_is_eof_init(&mut self)
pub fn set_is_eof_init(&mut self)
Set is_eof_init to true, this is used to enable RETURNCONTRACT
opcode.
pub fn eof(&self) -> Option<&Arc<Eof>>
Sourcepub fn insert_create_outcome(&mut self, create_outcome: CreateOutcome)
pub fn insert_create_outcome(&mut self, create_outcome: CreateOutcome)
Inserts the output of a create
call into the interpreter.
This function is used after a create
call has been executed. It processes the outcome
of that call and updates the state of the interpreter accordingly.
§Arguments
create_outcome
- ACreateOutcome
struct containing the results of thecreate
call.
§Behavior
The function updates the return_data_buffer
with the data from create_outcome
.
Depending on the InstructionResult
indicated by create_outcome
, it performs one of the following:
Ok
: Pushes the address fromcreate_outcome
to the stack, updates gas costs, and records any gas refunds.Revert
: PushesU256::ZERO
to the stack and updates gas costs.FatalExternalError
: Sets theinstruction_result
toInstructionResult::FatalExternalError
.Default
: PushesU256::ZERO
to the stack.
§Side Effects
- Updates
return_data_buffer
with the data fromcreate_outcome
. - Modifies the stack by pushing values depending on the
InstructionResult
. - Updates gas costs and records refunds in the interpreter’s
gas
field. - May alter
instruction_result
in case of external errors.
pub fn insert_eofcreate_outcome(&mut self, create_outcome: CreateOutcome)
Sourcepub fn insert_call_outcome(
&mut self,
shared_memory: &mut SharedMemory,
call_outcome: CallOutcome,
)
pub fn insert_call_outcome( &mut self, shared_memory: &mut SharedMemory, call_outcome: CallOutcome, )
Inserts the outcome of a call into the virtual machine’s state.
This function takes the result of a call, represented by CallOutcome
,
and updates the virtual machine’s state accordingly. It involves updating
the return data buffer, handling gas accounting, and setting the memory
in shared storage based on the outcome of the call.
§Arguments
shared_memory
- A mutable reference to the shared memory used by the virtual machine.call_outcome
- The outcome of the call to be processed, containing details such as instruction result, gas information, and output data.
§Behavior
The function first copies the output data from the call outcome to the virtual machine’s return data buffer. It then checks the instruction result from the call outcome:
return_ok!()
: Processes successful execution, refunds gas, and updates shared memory.return_revert!()
: Handles a revert by only updating the gas usage and shared memory.InstructionResult::FatalExternalError
: Sets the instruction result to a fatal external error.- Any other result: No specific action is taken.
Sourcepub fn current_opcode(&self) -> u8
pub fn current_opcode(&self) -> u8
Returns the opcode at the current instruction pointer.
Sourcepub fn stack_mut(&mut self) -> &mut Stack
pub fn stack_mut(&mut self) -> &mut Stack
Returns a mutable reference to the interpreter’s stack.
Sourcepub fn program_counter(&self) -> usize
pub fn program_counter(&self) -> usize
Returns the current program counter.
Sourcepub fn take_memory(&mut self) -> SharedMemory
pub fn take_memory(&mut self) -> SharedMemory
Take memory and replace it with empty memory.
Sourcepub fn run<FN, H: Host + ?Sized>(
&mut self,
shared_memory: SharedMemory,
instruction_table: &[FN; 256],
host: &mut H,
) -> InterpreterAction
pub fn run<FN, H: Host + ?Sized>( &mut self, shared_memory: SharedMemory, instruction_table: &[FN; 256], host: &mut H, ) -> InterpreterAction
Executes the interpreter until it returns or stops.
Sourcepub fn resize_memory(&mut self, new_size: usize) -> bool
pub fn resize_memory(&mut self, new_size: usize) -> bool
Resize the memory to the new size. Returns whether the gas was enough to resize the memory.
Trait Implementations§
Source§impl Debug for Interpreter
impl Debug for Interpreter
Source§impl Default for Interpreter
impl Default for Interpreter
Source§impl<'de> Deserialize<'de> for Interpreter
impl<'de> Deserialize<'de> for Interpreter
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Auto Trait Implementations§
impl !Freeze for Interpreter
impl RefUnwindSafe for Interpreter
impl !Send for Interpreter
impl !Sync for Interpreter
impl Unpin for Interpreter
impl UnwindSafe for Interpreter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.