From 430d8c2216a6101dda4d11c7bb679702caa89312 Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:51:56 +0100 Subject: [PATCH] refactor: use box_snapshot (#597) * refactor: use box_snapshot * refactor: pass machine by snapshot --- crates/evm/src/context.cairo | 11 +--- crates/evm/src/machine.cairo | 120 +++++++++++------------------------ 2 files changed, 41 insertions(+), 90 deletions(-) diff --git a/crates/evm/src/context.cairo b/crates/evm/src/context.cairo index 7c7d8c37f..cc632a34f 100644 --- a/crates/evm/src/context.cairo +++ b/crates/evm/src/context.cairo @@ -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)] @@ -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 } diff --git a/crates/evm/src/machine.cairo b/crates/evm/src/machine.cairo index cd811fa38..97c898595 100644 --- a/crates/evm/src/machine.cairo +++ b/crates/evm/src/machine.cairo @@ -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. @@ -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)] @@ -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 @@ -195,11 +176,8 @@ impl MachineImpl of MachineTrait { } #[inline(always)] - fn return_data(ref self: Machine) -> Span { - 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 { + *self.current_ctx.as_snapshot().unbox().return_data } /// Stops the current execution context. @@ -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 { - let current_call_ctx = self.call_ctx(); - current_call_ctx.bytecode() + fn bytecode(self: @Machine) -> Span { + self.call_ctx().bytecode() } #[inline(always)] - fn calldata(ref self: Machine) -> Span { - let current_call_ctx = self.call_ctx(); - current_call_ctx.calldata() + fn calldata(self: @Machine) -> Span { + self.call_ctx().calldata() } @@ -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,