Skip to content

Commit

Permalink
refactor: use box_snapshot (#597)
Browse files Browse the repository at this point in the history
* refactor: use box_snapshot

* refactor: pass machine by snapshot
  • Loading branch information
enitrat authored Nov 29, 2023
1 parent f992a57 commit 430d8c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 90 deletions.
11 changes: 3 additions & 8 deletions crates/evm/src/context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ impl ExecutionContextImpl of ExecutionContextTrait {
/// * `self` - The `ExecutionContext` instance to read the data from.
#[inline(always)]
fn read_only(self: @ExecutionContext) -> bool {
let read_only = (*self.call_ctx).unbox().read_only();

read_only
self.call_ctx().read_only()
}

#[inline(always)]
Expand Down Expand Up @@ -338,20 +336,17 @@ impl ExecutionContextImpl of ExecutionContextTrait {
*self.program_counter
}

fn origin(ref self: ExecutionContext) -> Address {
fn origin(self: @ExecutionContext) -> Address {
if (self.is_root()) {
return self.call_ctx().caller();
}
// If the current execution context is not root, then it MUST have a parent_context
// We're able to deref the nullable pointer without risk of panic
let mut parent_context = self.parent_ctx.deref();
let mut parent_context = self.parent_ctx.as_snapshot().deref();

// Entering a recursion
let origin = parent_context.origin();

// Recursively reboxing parent contexts
self.parent_ctx = NullableTrait::new(parent_context);

// Return self.call_context().caller() where self is the root context
origin
}
Expand Down
120 changes: 38 additions & 82 deletions crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,8 @@ impl MachineImpl of MachineTrait {
}

#[inline(always)]
fn id(ref self: Machine) -> usize {
let current_execution_ctx = self.current_ctx.unbox();
let id = current_execution_ctx.id();
self.current_ctx = BoxTrait::new(current_execution_ctx);
id
fn id(self: @Machine) -> usize {
self.current_ctx.as_snapshot().unbox().id()
}

/// Sets the current execution context being executed by the machine.
Expand All @@ -112,11 +109,8 @@ impl MachineImpl of MachineTrait {
}

#[inline(always)]
fn pc(ref self: Machine) -> usize {
let current_execution_ctx = self.current_ctx.unbox();
let pc = current_execution_ctx.pc();
self.current_ctx = BoxTrait::new(current_execution_ctx);
pc
fn pc(self: @Machine) -> usize {
self.current_ctx.as_snapshot().unbox().pc()
}

#[inline(always)]
Expand All @@ -134,36 +128,23 @@ impl MachineImpl of MachineTrait {
}

#[inline(always)]
fn reverted(ref self: Machine) -> bool {
let current_execution_ctx = self.current_ctx.unbox();
let reverted = current_execution_ctx.reverted();
self.current_ctx = BoxTrait::new(current_execution_ctx);
reverted
fn reverted(self: @Machine) -> bool {
self.current_ctx.as_snapshot().unbox().reverted()
}

#[inline(always)]
fn stopped(ref self: Machine) -> bool {
let current_execution_ctx = self.current_ctx.unbox();
let stopped = current_execution_ctx.stopped();
self.current_ctx = BoxTrait::new(current_execution_ctx);
stopped
fn stopped(self: @Machine) -> bool {
self.current_ctx.as_snapshot().unbox().stopped()
}

#[inline(always)]
fn status(ref self: Machine) -> Status {
let current_execution_ctx = self.current_ctx.unbox();
let status = current_execution_ctx.status();

self.current_ctx = BoxTrait::new(current_execution_ctx);
status
fn status(self: @Machine) -> Status {
self.current_ctx.as_snapshot().unbox().status()
}

#[inline(always)]
fn call_ctx(ref self: Machine) -> CallContext {
let current_execution_ctx = self.current_ctx.unbox();
let call_ctx = current_execution_ctx.call_ctx.unbox();
self.current_ctx = BoxTrait::new(current_execution_ctx);
call_ctx
fn call_ctx(self: @Machine) -> CallContext {
(*self.current_ctx.as_snapshot().unbox().call_ctx).unbox()
}

/// Returns from the sub context by setting the current context
Expand Down Expand Up @@ -195,11 +176,8 @@ impl MachineImpl of MachineTrait {
}

#[inline(always)]
fn return_data(ref self: Machine) -> Span<u8> {
let current_execution_ctx = self.current_ctx.unbox();
let return_data = current_execution_ctx.return_data;
self.current_ctx = BoxTrait::new(current_execution_ctx);
return_data
fn return_data(self: @Machine) -> Span<u8> {
*self.current_ctx.as_snapshot().unbox().return_data
}

/// Stops the current execution context.
Expand All @@ -212,61 +190,48 @@ impl MachineImpl of MachineTrait {


#[inline(always)]
fn address(ref self: Machine) -> Address {
let current_execution_ctx = self.current_ctx.unbox();
let evm_address = current_execution_ctx.address();
self.current_ctx = BoxTrait::new(current_execution_ctx);
evm_address
fn address(self: @Machine) -> Address {
self.current_ctx.as_snapshot().unbox().address()
}

#[inline(always)]
fn caller(ref self: Machine) -> Address {
let current_call_ctx = self.call_ctx();
current_call_ctx.caller()
fn caller(self: @Machine) -> Address {
self.call_ctx().caller()
}

#[inline(always)]
fn origin(ref self: Machine) -> Address {
let mut current_execution_ctx = self.current_ctx.unbox();
let origin = current_execution_ctx.origin();
self.current_ctx = BoxTrait::new(current_execution_ctx);
origin
fn origin(self: @Machine) -> Address {
self.current_ctx.as_snapshot().unbox().origin()
}

#[inline(always)]
fn read_only(ref self: Machine) -> bool {
let current_call_ctx = self.call_ctx();
current_call_ctx.read_only()
fn read_only(self: @Machine) -> bool {
self.call_ctx().read_only()
}

#[inline(always)]
fn gas_limit(ref self: Machine) -> u128 {
let current_call_ctx = self.call_ctx();
current_call_ctx.gas_limit()
fn gas_limit(self: @Machine) -> u128 {
self.call_ctx().gas_limit()
}

#[inline(always)]
fn gas_price(ref self: Machine) -> u128 {
let current_call_ctx = self.call_ctx();
current_call_ctx.gas_price()
fn gas_price(self: @Machine) -> u128 {
self.call_ctx().gas_price()
}

#[inline(always)]
fn value(ref self: Machine) -> u256 {
let current_call_ctx = self.call_ctx();
current_call_ctx.value()
fn value(self: @Machine) -> u256 {
self.call_ctx().value()
}

#[inline(always)]
fn bytecode(ref self: Machine) -> Span<u8> {
let current_call_ctx = self.call_ctx();
current_call_ctx.bytecode()
fn bytecode(self: @Machine) -> Span<u8> {
self.call_ctx().bytecode()
}

#[inline(always)]
fn calldata(ref self: Machine) -> Span<u8> {
let current_call_ctx = self.call_ctx();
current_call_ctx.calldata()
fn calldata(self: @Machine) -> Span<u8> {
self.call_ctx().calldata()
}


Expand All @@ -288,29 +253,20 @@ impl MachineImpl of MachineTrait {

/// Returns the current execution context type (root, call or create).
#[inline(always)]
fn ctx_type(ref self: Machine) -> ExecutionContextType {
let current_execution_ctx = self.current_ctx.unbox();
let ctx_type = current_execution_ctx.ctx_type();
self.current_ctx = BoxTrait::new(current_execution_ctx);
ctx_type
fn ctx_type(self: @Machine) -> ExecutionContextType {
self.current_ctx.as_snapshot().unbox().ctx_type()
}

/// Returns whether the current execution context is the root context.
#[inline(always)]
fn is_root(ref self: Machine) -> bool {
let current_execution_ctx = self.current_ctx.unbox();
let is_root = current_execution_ctx.is_root();
self.current_ctx = BoxTrait::new(current_execution_ctx);
is_root
fn is_root(self: @Machine) -> bool {
self.current_ctx.as_snapshot().unbox().is_root()
}

/// Returns whether the current execution context is a call context.
#[inline(always)]
fn is_call(ref self: Machine) -> bool {
let current_execution_ctx = self.current_ctx.unbox();
let is_call = current_execution_ctx.is_call();
self.current_ctx = BoxTrait::new(current_execution_ctx);
is_call
fn is_call(self: @Machine) -> bool {
self.current_ctx.as_snapshot().unbox().is_call()
}

/// Sets the `return_data` field of the appropriate execution context,
Expand Down

0 comments on commit 430d8c2

Please sign in to comment.