From fcbb3f30941bf3c7ef951654ec9268498d6a186c Mon Sep 17 00:00:00 2001 From: msaug Date: Mon, 2 Oct 2023 17:10:00 +0700 Subject: [PATCH 1/6] chore: improve cairodoc, refactor: clean code, feat: set_active_execution_ctx --- .tool-versions | 2 +- Scarb.toml | 4 +- crates/evm/src/context.cairo | 6 +-- crates/evm/src/errors.cairo | 3 ++ crates/evm/src/execution.cairo | 3 +- crates/evm/src/helpers.cairo | 12 +++-- .../src/instructions/block_information.cairo | 2 +- .../environmental_information.cairo | 3 +- .../src/instructions/memory_operations.cairo | 11 ++--- .../src/instructions/push_operations.cairo | 4 +- crates/evm/src/instructions/sha3.cairo | 1 - .../stop_and_arithmetic_operations.cairo | 2 +- .../src/instructions/system_operations.cairo | 3 +- crates/evm/src/interpreter.cairo | 49 +++++++------------ crates/evm/src/machine.cairo | 39 +++++++++------ crates/evm/src/memory.cairo | 10 ++-- crates/evm/src/stack.cairo | 9 ++-- .../test_environment_information.cairo | 2 +- .../test_memory_operations.cairo | 3 +- .../test_stop_and_arithmetic_operations.cairo | 2 +- .../test_system_operations.cairo | 2 +- crates/evm/src/tests/test_machine.cairo | 2 +- 22 files changed, 85 insertions(+), 89 deletions(-) diff --git a/.tool-versions b/.tool-versions index 2175a337d..54917fa62 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -scarb 0.7.0 +scarb 2.3.0-rc0 diff --git a/Scarb.toml b/Scarb.toml index 71a2765c0..a78d0e8b2 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -4,13 +4,13 @@ members = ["crates/*"] [workspace.package] description = "Kakarot is an (zk)-Ethereum Virtual Machine implementation written in Cairo." documentation = "https://www.kakarot.org/" -cairo-version = "2.2.0" +cairo-version = "2.3.0-rc0" version = "0.1.0" readme = "README.md" repository = "https://github.com/kkrt-labs/kakarot-ssj/" license-file = "LICENSE" [workspace.dependencies] -starknet = "2.2.0" +starknet = "2.3.0-rc0" [[workspace.tool.starknet-contract]] diff --git a/crates/evm/src/context.cairo b/crates/evm/src/context.cairo index 52c00a6eb..b61eb820a 100644 --- a/crates/evm/src/context.cairo +++ b/crates/evm/src/context.cairo @@ -148,6 +148,7 @@ impl DefaultBoxExecutionContext of Default> { /// `ExecutionContext` implementation. + #[generate_trait] impl ExecutionContextImpl of ExecutionContextTrait { /// Create a new execution context instance. @@ -314,8 +315,3 @@ impl ExecutionContextImpl of ExecutionContextTrait { *self.program_counter } } - - -/// The execution summary. -#[derive(Drop, Copy)] -struct ExecutionSummary {} diff --git a/crates/evm/src/errors.cairo b/crates/evm/src/errors.cairo index 27d50881d..2b7fa20d0 100644 --- a/crates/evm/src/errors.cairo +++ b/crates/evm/src/errors.cairo @@ -22,6 +22,7 @@ enum EVMError { ReturnDataError: felt252, JumpError: felt252, NotImplemented, + UnknownOpcode: u8 } @@ -34,6 +35,8 @@ impl EVMErrorIntoU256 of Into { EVMError::ReturnDataError(error_message) => error_message.into(), EVMError::JumpError(error_message) => error_message.into(), EVMError::NotImplemented => 'NotImplemented'.into(), + // TODO: refactor with dynamic strings once supported + EVMError::UnknownOpcode => 'UnknownOpcode'.into() } } } diff --git a/crates/evm/src/execution.cairo b/crates/evm/src/execution.cairo index 530be3d12..e5e8fa0be 100644 --- a/crates/evm/src/execution.cairo +++ b/crates/evm/src/execution.cairo @@ -1,5 +1,4 @@ -use starknet::{ContractAddress, EthAddress}; -use evm::context::{CallContext, ExecutionContext, ExecutionSummary, ExecutionContextTrait}; +use evm::context::{CallContext, ExecutionContext}; use evm::interpreter::EVMInterpreterTrait; use evm::machine::Machine; diff --git a/crates/evm/src/helpers.cairo b/crates/evm/src/helpers.cairo index 2e372dbaa..8fe7d40b9 100644 --- a/crates/evm/src/helpers.cairo +++ b/crates/evm/src/helpers.cairo @@ -1,8 +1,14 @@ use evm::errors::{EVMError, TYPE_CONVERSION_ERROR}; -// Try converting u256 to u32 -impl U256IntoResultU32 of Into> { - fn into(self: u256) -> Result { +trait TryIntoResult { + fn try_into_result(self: T) -> Result; +} + +impl U256TryIntoResultU32 of TryIntoResult { + /// Converts a u256 into a Result + /// If the u256 is larger than MAX_U32, it returns an error. + /// Otherwise, it returns the casted value. + fn try_into_result(self: u256) -> Result { match self.try_into() { Option::Some(value) => Result::Ok(value), Option::None(_) => Result::Err(EVMError::TypeConversionError(TYPE_CONVERSION_ERROR)) diff --git a/crates/evm/src/instructions/block_information.cairo b/crates/evm/src/instructions/block_information.cairo index 101eec83c..504c24a7e 100644 --- a/crates/evm/src/instructions/block_information.cairo +++ b/crates/evm/src/instructions/block_information.cairo @@ -7,7 +7,7 @@ use starknet::info::{get_block_number, get_block_timestamp}; use evm::stack::StackTrait; use evm::errors::EVMError; use utils::constants::CHAIN_ID; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; #[generate_trait] impl BlockInformation of BlockInformationTrait { diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index 0e30f136b..a63d1e947 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -2,8 +2,7 @@ use evm::stack::StackTrait; use evm::context::ExecutionContextTrait; use evm::errors::{EVMError, RETURNDATA_OUT_OF_BOUNDS_ERROR}; -use evm::helpers::U256IntoResultU32; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use utils::helpers::{load_word}; use utils::traits::{EthAddressIntoU256}; use evm::memory::MemoryTrait; diff --git a/crates/evm/src/instructions/memory_operations.cairo b/crates/evm/src/instructions/memory_operations.cairo index 4d1421009..70a0ec5a7 100644 --- a/crates/evm/src/instructions/memory_operations.cairo +++ b/crates/evm/src/instructions/memory_operations.cairo @@ -1,9 +1,8 @@ //! Stack Memory Storage and Flow Operations. -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::{EVMError, INVALID_DESTINATION}; use evm::stack::StackTrait; use evm::memory::MemoryTrait; -use evm::helpers::U256IntoResultU32; #[generate_trait] impl MemoryOperation of MemoryOperationTrait { @@ -59,7 +58,7 @@ impl MemoryOperation of MemoryOperationTrait { // TODO: Currently this doesn't check that byte is actually `JUMPDEST` // and not `0x5B` that is a part of PUSHN instruction - // + // // That can be done by storing all valid jump locations during contract deployment // which would also simplify the logic because we would be just checking if idx is // present in that list @@ -71,9 +70,7 @@ impl MemoryOperation of MemoryOperationTrait { return Result::Err(EVMError::JumpError(INVALID_DESTINATION)); } }, - Option::None => { - return Result::Err(EVMError::JumpError(INVALID_DESTINATION)); - } + Option::None => { return Result::Err(EVMError::JumpError(INVALID_DESTINATION)); } } self.set_pc(index); Result::Ok(()) @@ -104,7 +101,7 @@ impl MemoryOperation of MemoryOperationTrait { /// 0x5b - JUMPDEST operation /// Serves as a check that JUMP or JUMPI was executed correctly. /// # Specification: https://www.evm.codes/#5b?fork=shanghai - /// + /// /// This doesn't have any affect on execution state, so we don't have /// to do anything here. It's a NO-OP. fn exec_jumpdest(ref self: Machine) -> Result<(), EVMError> { diff --git a/crates/evm/src/instructions/push_operations.cairo b/crates/evm/src/instructions/push_operations.cairo index 64630f8a2..93acfdf1c 100644 --- a/crates/evm/src/instructions/push_operations.cairo +++ b/crates/evm/src/instructions/push_operations.cairo @@ -1,12 +1,12 @@ //! Push Operations. // Internal imports -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::EVMError; use evm::stack::StackTrait; mod internal { - use evm::machine::{Machine, MachineCurrentContext}; + use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::EVMError; use evm::stack::StackTrait; use utils::helpers::load_word; diff --git a/crates/evm/src/instructions/sha3.cairo b/crates/evm/src/instructions/sha3.cairo index 68791bf3a..8b8be3261 100644 --- a/crates/evm/src/instructions/sha3.cairo +++ b/crates/evm/src/instructions/sha3.cairo @@ -5,7 +5,6 @@ use evm::machine::Machine; use evm::stack::StackTrait; use evm::memory::MemoryTrait; use evm::errors::EVMError; -use evm::helpers::U256IntoResultU32; use keccak::{cairo_keccak, u128_split}; use utils::helpers::{ArrayExtensionTrait, U256Trait}; diff --git a/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo b/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo index 58218f5a5..aa6a5d812 100644 --- a/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo +++ b/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo @@ -4,7 +4,7 @@ use integer::{ u256_overflowing_add, u256_overflow_sub, u256_overflow_mul, u256_safe_divmod, u512_safe_div_rem_by_u256, u256_try_as_non_zero }; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::stack::StackTrait; use utils::math::{Exponentiation, WrappingExponentiation, u256_wide_add}; use evm::errors::EVMError; diff --git a/crates/evm/src/instructions/system_operations.cairo b/crates/evm/src/instructions/system_operations.cairo index 1e3f23220..35601a969 100644 --- a/crates/evm/src/instructions/system_operations.cairo +++ b/crates/evm/src/instructions/system_operations.cairo @@ -5,11 +5,10 @@ use traits::TryInto; use box::BoxTrait; // Internal imports -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::MemoryTrait; use evm::stack::StackTrait; use evm::errors::EVMError; -use evm::helpers::U256IntoResultU32; #[generate_trait] impl SystemOperations of SystemOperationsTrait { diff --git a/crates/evm/src/interpreter.cairo b/crates/evm/src/interpreter.cairo index 50c6e412a..1f1de741c 100644 --- a/crates/evm/src/interpreter.cairo +++ b/crates/evm/src/interpreter.cairo @@ -1,41 +1,35 @@ /// System imports. /// Internal imports. -// TODO remove destruct imports when no longer required -use evm::context::{ExecutionSummary, ExecutionContext, ExecutionContextTrait, CallContextTrait,}; +use evm::context::{CallContextTrait,}; use utils::{helpers::u256_to_bytes_array}; use evm::errors::{EVMError, PC_OUT_OF_BOUNDS}; use evm::instructions::{ - duplication_operations, environmental_information, ExchangeOperationsTrait, logging_operations, - memory_operations, sha3, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait, + ExchangeOperationsTrait, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait, SystemOperationsTrait, BlockInformationTrait, DuplicationOperationsTrait, - EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationTrait + EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationTrait, logging_operations }; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; - -/// EVM instructions as defined in the Yellow Paper and the EIPs. #[derive(Drop, Copy)] struct EVMInterpreter {} trait EVMInterpreterTrait { - /// Create a new instance of the EVM instructions. + /// Create a new instance of the EVM Interpreter. fn new() -> EVMInterpreter; /// Execute the EVM bytecode. fn run(ref self: EVMInterpreter, ref machine: Machine); - /// Decode the current opcode and execute associated function. + /// Decodes the opcode at `pc` and executes the associated function. fn decode_and_execute(ref self: EVMInterpreter, ref machine: Machine) -> Result<(), EVMError>; } impl EVMInterpreterImpl of EVMInterpreterTrait { - /// Create a new instance of the EVM instructions. #[inline(always)] fn new() -> EVMInterpreter { EVMInterpreter {} } - /// Execute the EVM bytecode. fn run(ref self: EVMInterpreter, ref machine: Machine) { // Decode and execute the current opcode. let result = self.decode_and_execute(ref machine); @@ -61,7 +55,6 @@ impl EVMInterpreterImpl of EVMInterpreterTrait { } } - /// Decode the current opcode and execute associated function. fn decode_and_execute(ref self: EVMInterpreter, ref machine: Machine) -> Result<(), EVMError> { // Retrieve the current program counter. let pc = machine.pc(); @@ -613,53 +606,45 @@ impl EVMInterpreterImpl of EVMInterpreterTrait { } if opcode == 240 { // CREATE - machine.exec_create(); + return machine.exec_create(); } if opcode == 241 { // CALL - machine.exec_call(); + return machine.exec_call(); } if opcode == 242 { // CALLCODE - machine.exec_callcode(); + return machine.exec_callcode(); } if opcode == 243 { // RETURN - machine.exec_return(); + return machine.exec_return(); } if opcode == 244 { // DELEGATECALL - machine.exec_delegatecall(); + return machine.exec_delegatecall(); } if opcode == 245 { // CREATE2 - machine.exec_create2(); + return machine.exec_create2(); } if opcode == 250 { // STATICCALL - machine.exec_staticcall(); + return machine.exec_staticcall(); } if opcode == 253 { // REVERT - machine.exec_revert(); + return machine.exec_revert(); } if opcode == 254 { // INVALID - machine.exec_invalid(); + return machine.exec_invalid(); } if opcode == 255 { // SELFDESTRUCT - machine.exec_selfdestruct(); + return machine.exec_selfdestruct(); } // Unknown opcode - unknown_opcode(opcode); - Result::Ok(()) + return Result::Err(EVMError::UnknownOpcode(opcode)); } } - -/// This function is called when an unknown opcode is encountered. -/// # Arguments -/// * `opcode` - The unknown opcode -/// # TODO -/// * Implement this function and revert execution. -fn unknown_opcode(opcode: u8) {} diff --git a/crates/evm/src/machine.cairo b/crates/evm/src/machine.cairo index 37a3d606d..41c94df01 100644 --- a/crates/evm/src/machine.cairo +++ b/crates/evm/src/machine.cairo @@ -3,11 +3,16 @@ use evm::{ ExecutionContext, ExecutionContextTrait, DefaultBoxExecutionContext, CallContext, CallContextTrait, Status, Event }, - stack::Stack, memory::Memory + stack::{Stack, StackTrait}, memory::{Memory, MemoryTrait} }; use starknet::{EthAddress, ContractAddress}; +/// The Journal tracks the changes applied to storage during the execution of a transaction. +/// Local changes tracks the changes applied inside a single execution context. +/// Global changes tracks the changes applied in the entire transaction. +/// Upon exiting an execution context, local changes must be finalized into global changes +/// Upon exiting the transaction, global changes must be finalized into storage updates. #[derive(Destruct, Default)] struct Journal { local_changes: Felt252Dict, @@ -54,8 +59,19 @@ impl DefaultMachine of Default { /// 1. We're not able to use @Machine as an argument for getters, as the ExecutionContext struct does not derive the Copy trait. /// 2. We must use a box reference to the current context, as the changes made during execution must be applied /// to only one ExecutionContext struct instance. Using a pointer ensures we never duplicate structs and thus changes. + #[generate_trait] -impl MachineCurrentContextImpl of MachineCurrentContext { +impl MachineCurrentContextImpl of MachineCurrentContextTrait { + /// Sets the current execution context being executed by the machine. + /// This is an implementation-specific concept that is used + /// to divide a unique Stack/Memory simulated by a dict into + /// multiple sub-structures relative to a single context. + #[inline(always)] + fn set_active_execution_ctx(ref self: Machine, id: usize) { + self.memory.set_active_segment(id); + self.stack.set_active_segment(id); + } + #[inline(always)] fn pc(ref self: Machine) -> usize { let current_execution_ctx = self.current_context.unbox(); @@ -202,16 +218,12 @@ impl MachineCurrentContextImpl of MachineCurrentContext { current_call_ctx.calldata() } - // ************************************************************************* - // ExecutionContext methods - // ************************************************************************* - - /// Reads and return data from bytecode. - /// The program counter is incremented accordingly. - /// + /// Reads and returns `size` elements from bytecode starting from the current value + /// `pc`. /// # Arguments /// - /// * `self` - The `ExecutionContext` instance to read the data from. + /// * `self` - The `Machine` instance to read the data from. + /// * The current execution context is handled implicitly by the Machine. /// * `len` - The length of the data to read from the bytecode. #[inline(always)] fn read_code(ref self: Machine, len: usize) -> Span { @@ -223,6 +235,8 @@ impl MachineCurrentContextImpl of MachineCurrentContext { } + /// Returns whether the current execution context is the root context. + /// The root is always the first context to be executed, and thus has id 0. #[inline(always)] fn is_root(ref self: Machine) -> bool { let current_execution_ctx = self.current_context.unbox(); @@ -231,11 +245,6 @@ impl MachineCurrentContextImpl of MachineCurrentContext { is_root } - // TODO: Implement print_debug - /// Debug print the execution context. - #[inline(always)] - fn print_debug(ref self: Machine) {} - #[inline(always)] fn set_return_data(ref self: Machine, value: Array) { let mut current_execution_ctx = self.current_context.unbox(); diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index 5e0687af3..1d7c17b4e 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -45,13 +45,15 @@ impl MemoryImpl of MemoryTrait { Memory { active_segment: 0, items: Default::default(), bytes_len: Default::default() } } - /// Initializes a new `Memory` instance with a specific active segment + /// Sets the current active segment for the `Memory` instance. + /// Active segment are implementation-specific concepts that reflect + /// the execution context being currently executed. #[inline(always)] fn set_active_segment(ref self: Memory, active_segment: usize) { self.active_segment = active_segment; } - /// Return size of the memory. + /// Returns the size of the memory. #[inline(always)] fn size(ref self: Memory) -> usize { self.bytes_len.get(self.active_segment.into()) @@ -77,7 +79,7 @@ impl MemoryImpl of MemoryTrait { self.bytes_len.insert(self.active_segment(), cmp::max(new_min_bytes_len, self.size())); - // Compute actual offset in the dict, given active_segment of Memory (current Execution Context id) + // Compute actual offset in the dict, given active_segment of Memory (current Execution Context id) // And Memory Segment Size let internal_offset = self.compute_active_segment_offset(offset); @@ -116,7 +118,7 @@ impl MemoryImpl of MemoryTrait { let new_min_bytes_len = helpers::ceil_bytes_len_to_next_32_bytes_word(offset + 1); self.bytes_len.insert(self.active_segment(), cmp::max(new_min_bytes_len, self.size())); - // Compute actual offset in Memory, given active_segment of Memory (current Execution Context id) + // Compute actual offset in Memory, given active_segment of Memory (current Execution Context id) // And Memory Segment Size let internal_offset = self.compute_active_segment_offset(offset); diff --git a/crates/evm/src/stack.cairo b/crates/evm/src/stack.cairo index 67dbfa925..a6c859903 100644 --- a/crates/evm/src/stack.cairo +++ b/crates/evm/src/stack.cairo @@ -20,7 +20,7 @@ use utils::constants; use debug::PrintTrait; use nullable::{nullable_from_box, NullableTrait}; use evm::errors::{EVMError, STACK_OVERFLOW, STACK_UNDERFLOW}; -use evm::helpers::U256IntoResultU32; +use evm::helpers::U256TryIntoResultU32; use starknet::EthAddress; use utils::i256::i256; @@ -57,6 +57,9 @@ impl StackImpl of StackTrait { } #[inline(always)] + /// Sets the current active segment for the `Stack` instance. + /// Active segment are implementation-specific concepts that reflect + /// the execution context being currently executed. fn set_active_segment(ref self: Stack, active_segment: usize) { self.active_segment = active_segment; } @@ -85,7 +88,7 @@ impl StackImpl of StackTrait { Result::Ok(()) } - /// Pops the top item off the stack. + /// Pops the top item off the stack. /// /// # Errors /// @@ -113,7 +116,7 @@ impl StackImpl of StackTrait { #[inline(always)] fn pop_usize(ref self: Stack) -> Result { let item: u256 = self.pop()?; - let item: usize = Into::>::into(item)?; + let item: usize = item.try_into_result()?; Result::Ok(item) } diff --git a/crates/evm/src/tests/test_instructions/test_environment_information.cairo b/crates/evm/src/tests/test_instructions/test_environment_information.cairo index cd0af3c33..e0a428909 100644 --- a/crates/evm/src/tests/test_instructions/test_environment_information.cairo +++ b/crates/evm/src/tests/test_instructions/test_environment_information.cairo @@ -9,7 +9,7 @@ use evm::stack::StackTrait; use starknet::EthAddressIntoFelt252; use utils::traits::{EthAddressIntoU256}; use evm::errors::{EVMError, TYPE_CONVERSION_ERROR, RETURNDATA_OUT_OF_BOUNDS_ERROR}; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use utils::helpers::{ u256_to_bytes_array, load_word, ArrayExtension, ArrayExtensionTrait, SpanExtension, SpanExtensionTrait diff --git a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo index c3a36a6d2..f1421efbf 100644 --- a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo @@ -9,9 +9,8 @@ use starknet::EthAddressIntoFelt252; use utils::helpers::{u256_to_bytes_array}; use utils::traits::{EthAddressIntoU256}; use evm::errors::{EVMError, STACK_UNDERFLOW, INVALID_DESTINATION}; -use evm::helpers::U256IntoResultU32; use integer::BoundedInt; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo b/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo index 1a244788e..8c76f8b1d 100644 --- a/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo @@ -4,7 +4,7 @@ use evm::stack::StackTrait; use evm::context::ExecutionContextTrait; use integer::BoundedInt; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_system_operations.cairo b/crates/evm/src/tests/test_instructions/test_system_operations.cairo index 7b7ed95aa..53eeb4b6e 100644 --- a/crates/evm/src/tests/test_instructions/test_system_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_system_operations.cairo @@ -5,7 +5,7 @@ use evm::instructions::MemoryOperationTrait; use evm::context::{ExecutionContext, ExecutionContextTrait,}; use utils::helpers::load_word; use traits::Into; -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] #[available_gas(20000000)] diff --git a/crates/evm/src/tests/test_machine.cairo b/crates/evm/src/tests/test_machine.cairo index b6b0e30d1..c98fae9d5 100644 --- a/crates/evm/src/tests/test_machine.cairo +++ b/crates/evm/src/tests/test_machine.cairo @@ -1,4 +1,4 @@ -use evm::machine::{Machine, MachineCurrentContext}; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::context::CallContextTrait; use evm::tests::test_utils::{ evm_address, setup_machine_with_bytecode, setup_machine, starknet_address From 270b9a7e21e6d1313b3380f97f11200ebcb23c1a Mon Sep 17 00:00:00 2001 From: msaug Date: Mon, 2 Oct 2023 17:23:55 +0700 Subject: [PATCH 2/6] chore: order imports --- Scarb.toml | 3 ++ crates/evm/Scarb.toml | 3 ++ crates/evm/src/context.cairo | 8 ++--- crates/evm/src/instructions.cairo | 28 ++++++---------- .../src/instructions/block_information.cairo | 9 +++--- .../instructions/comparison_operations.cairo | 10 +++--- .../instructions/duplication_operations.cairo | 2 +- .../environmental_information.cairo | 6 ++-- .../instructions/exchange_operations.cairo | 3 +- .../src/instructions/memory_operations.cairo | 4 +-- .../src/instructions/push_operations.cairo | 5 ++- crates/evm/src/instructions/sha3.cairo | 10 +++--- .../stop_and_arithmetic_operations.cairo | 9 +++--- .../src/instructions/system_operations.cairo | 6 +--- crates/evm/src/interpreter.cairo | 2 +- crates/evm/src/lib.cairo | 32 +++++++++---------- crates/evm/src/memory.cairo | 4 +-- crates/evm/src/stack.cairo | 8 ++--- crates/evm/src/tests.cairo | 14 ++++---- .../src/tests/test_execution_context.cairo | 15 +++------ crates/evm/src/tests/test_instructions.cairo | 10 +++--- .../test_comparison_operations.cairo | 5 ++- .../test_duplication_operations.cairo | 6 ++-- .../test_environment_information.cairo | 10 +++--- .../test_exchange_operations.cairo | 5 ++- .../test_memory_operations.cairo | 10 +++--- .../test_push_operations.cairo | 3 +- .../tests/test_instructions/test_sha3.cairo | 7 ++-- .../test_stop_and_arithmetic_operations.cairo | 6 ++-- .../test_system_operations.cairo | 7 ++-- crates/evm/src/tests/test_kakarot.cairo | 4 +-- crates/evm/src/tests/test_machine.cairo | 2 +- crates/evm/src/tests/test_memory.cairo | 2 +- crates/evm/src/tests/test_stack.cairo | 14 +++----- crates/evm/src/tests/test_utils.cairo | 4 +-- crates/utils/src/helpers.cairo | 4 +-- 36 files changed, 124 insertions(+), 156 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index a78d0e8b2..d089247ae 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -14,3 +14,6 @@ license-file = "LICENSE" starknet = "2.3.0-rc0" [[workspace.tool.starknet-contract]] + +[workspace.tool.fmt] +sort-module-level-items = true diff --git a/crates/evm/Scarb.toml b/crates/evm/Scarb.toml index 87bd3a1fd..4406e05b1 100644 --- a/crates/evm/Scarb.toml +++ b/crates/evm/Scarb.toml @@ -7,3 +7,6 @@ version = "0.1.0" [dependencies] starknet.workspace = true utils = { path = "../utils" } + +[tool.fmt] +sort-module-level-items = true diff --git a/crates/evm/src/context.cairo b/crates/evm/src/context.cairo index b61eb820a..2b835e2ab 100644 --- a/crates/evm/src/context.cairo +++ b/crates/evm/src/context.cairo @@ -1,11 +1,11 @@ -use evm::stack::{Stack, StackTrait}; +use debug::PrintTrait; use evm::memory::{Memory, MemoryTrait}; use evm::model::Event; -use debug::PrintTrait; +use evm::stack::{Stack, StackTrait}; +use starknet::get_caller_address; +use starknet::{EthAddress, ContractAddress}; use utils::helpers::{ArrayExtension, ArrayExtensionTrait}; use utils::traits::{SpanDefault, EthAddressDefault, ContractAddressDefault}; -use starknet::{EthAddress, ContractAddress}; -use starknet::get_caller_address; #[derive(Drop, Default, Copy, PartialEq)] enum Status { diff --git a/crates/evm/src/instructions.cairo b/crates/evm/src/instructions.cairo index ef51bb420..c4fd50788 100644 --- a/crates/evm/src/instructions.cairo +++ b/crates/evm/src/instructions.cairo @@ -1,32 +1,24 @@ /// Sub modules. mod block_information; -use block_information::BlockInformationTrait; - mod comparison_operations; -use comparison_operations::ComparisonAndBitwiseOperationsTrait; mod duplication_operations; -use duplication_operations::DuplicationOperationsTrait; - mod environmental_information; -use environmental_information::EnvironmentInformationTrait; - mod exchange_operations; -use exchange_operations::ExchangeOperationsTrait; - mod logging_operations; - mod memory_operations; -use memory_operations::MemoryOperationTrait; - mod push_operations; -use push_operations::PushOperationsTrait; - mod sha3; -use sha3::Sha3Trait; - mod stop_and_arithmetic_operations; -use stop_and_arithmetic_operations::StopAndArithmeticOperationsTrait; - mod system_operations; + +use block_information::BlockInformationTrait; +use comparison_operations::ComparisonAndBitwiseOperationsTrait; +use duplication_operations::DuplicationOperationsTrait; +use environmental_information::EnvironmentInformationTrait; +use exchange_operations::ExchangeOperationsTrait; +use memory_operations::MemoryOperationTrait; +use push_operations::PushOperationsTrait; +use sha3::Sha3Trait; +use stop_and_arithmetic_operations::StopAndArithmeticOperationsTrait; use system_operations::SystemOperationsTrait; diff --git a/crates/evm/src/instructions/block_information.cairo b/crates/evm/src/instructions/block_information.cairo index 504c24a7e..b1ea71a6b 100644 --- a/crates/evm/src/instructions/block_information.cairo +++ b/crates/evm/src/instructions/block_information.cairo @@ -1,13 +1,12 @@ //! Block Information. +use evm::errors::EVMError; +use evm::machine::{Machine, MachineCurrentContextTrait}; +use evm::stack::StackTrait; + // Corelib imports use starknet::info::{get_block_number, get_block_timestamp}; - -// Internal imports -use evm::stack::StackTrait; -use evm::errors::EVMError; use utils::constants::CHAIN_ID; -use evm::machine::{Machine, MachineCurrentContextTrait}; #[generate_trait] impl BlockInformation of BlockInformationTrait { diff --git a/crates/evm/src/instructions/comparison_operations.cairo b/crates/evm/src/instructions/comparison_operations.cairo index 226710361..09b0baa06 100644 --- a/crates/evm/src/instructions/comparison_operations.cairo +++ b/crates/evm/src/instructions/comparison_operations.cairo @@ -1,13 +1,13 @@ +use evm::errors::EVMError; +use evm::errors::STACK_UNDERFLOW; // Internal imports use evm::machine::Machine; use evm::stack::StackTrait; -use evm::errors::STACK_UNDERFLOW; -use evm::errors::EVMError; -use utils::math::{Exponentiation, Bitshift, WrappingBitshift}; +use integer::BoundedInt; use utils::constants::{POW_2_127}; -use utils::traits::BoolIntoNumeric; use utils::i256::i256; -use integer::BoundedInt; +use utils::math::{Exponentiation, Bitshift, WrappingBitshift}; +use utils::traits::BoolIntoNumeric; #[generate_trait] impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait { diff --git a/crates/evm/src/instructions/duplication_operations.cairo b/crates/evm/src/instructions/duplication_operations.cairo index d94fdf434..a9084ba02 100644 --- a/crates/evm/src/instructions/duplication_operations.cairo +++ b/crates/evm/src/instructions/duplication_operations.cairo @@ -6,9 +6,9 @@ use evm::machine::Machine; mod internal { use evm::context::{ExecutionContext, ExecutionContextTrait,}; - use evm::stack::StackTrait; use evm::errors::EVMError; use evm::machine::Machine; + use evm::stack::StackTrait; /// Generic DUP operation #[inline(always)] diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index a63d1e947..be0f29044 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -1,12 +1,12 @@ //! Environmental Information. -use evm::stack::StackTrait; use evm::context::ExecutionContextTrait; use evm::errors::{EVMError, RETURNDATA_OUT_OF_BOUNDS_ERROR}; use evm::machine::{Machine, MachineCurrentContextTrait}; -use utils::helpers::{load_word}; -use utils::traits::{EthAddressIntoU256}; use evm::memory::MemoryTrait; +use evm::stack::StackTrait; use integer::u32_overflowing_add; +use utils::helpers::{load_word}; +use utils::traits::{EthAddressIntoU256}; #[generate_trait] impl EnvironmentInformationImpl of EnvironmentInformationTrait { diff --git a/crates/evm/src/instructions/exchange_operations.cairo b/crates/evm/src/instructions/exchange_operations.cairo index 1816adad3..48859c752 100644 --- a/crates/evm/src/instructions/exchange_operations.cairo +++ b/crates/evm/src/instructions/exchange_operations.cairo @@ -1,9 +1,8 @@ //! Exchange Operations. -// Internal imports +use evm::errors::EVMError; use evm::machine::Machine; use evm::stack::StackTrait; -use evm::errors::EVMError; use utils::helpers::load_word; #[generate_trait] diff --git a/crates/evm/src/instructions/memory_operations.cairo b/crates/evm/src/instructions/memory_operations.cairo index 70a0ec5a7..c48493a79 100644 --- a/crates/evm/src/instructions/memory_operations.cairo +++ b/crates/evm/src/instructions/memory_operations.cairo @@ -1,8 +1,8 @@ //! Stack Memory Storage and Flow Operations. -use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::{EVMError, INVALID_DESTINATION}; -use evm::stack::StackTrait; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::MemoryTrait; +use evm::stack::StackTrait; #[generate_trait] impl MemoryOperation of MemoryOperationTrait { diff --git a/crates/evm/src/instructions/push_operations.cairo b/crates/evm/src/instructions/push_operations.cairo index 93acfdf1c..c06e6bfb0 100644 --- a/crates/evm/src/instructions/push_operations.cairo +++ b/crates/evm/src/instructions/push_operations.cairo @@ -1,13 +1,12 @@ //! Push Operations. -// Internal imports -use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::EVMError; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::stack::StackTrait; mod internal { - use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::errors::EVMError; + use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::stack::StackTrait; use utils::helpers::load_word; diff --git a/crates/evm/src/instructions/sha3.cairo b/crates/evm/src/instructions/sha3.cairo index 8b8be3261..20ee5aa28 100644 --- a/crates/evm/src/instructions/sha3.cairo +++ b/crates/evm/src/instructions/sha3.cairo @@ -1,15 +1,13 @@ //! SHA3. +use evm::errors::EVMError; // Internal imports use evm::machine::Machine; -use evm::stack::StackTrait; use evm::memory::MemoryTrait; -use evm::errors::EVMError; +use evm::stack::StackTrait; use keccak::{cairo_keccak, u128_split}; use utils::helpers::{ArrayExtensionTrait, U256Trait}; -use array::ArrayTrait; - #[generate_trait] impl Sha3Impl of Sha3Trait { /// SHA3 operation : Hashes n bytes in memory at a given offset in memory @@ -55,10 +53,10 @@ impl Sha3Impl of Sha3Trait { mod internal { - use evm::stack::StackTrait; + use evm::machine::Machine; use evm::memory::MemoryTrait; + use evm::stack::StackTrait; use utils::helpers::U256Trait; - use evm::machine::Machine; /// Computes how many words are read from the memory /// and how many words must be filled with zeroes diff --git a/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo b/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo index aa6a5d812..21d3e3a89 100644 --- a/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo +++ b/crates/evm/src/instructions/stop_and_arithmetic_operations.cairo @@ -1,14 +1,15 @@ //! Stop and Arithmetic Operations. +use evm::errors::EVMError; +use evm::machine::{Machine, MachineCurrentContextTrait}; +use evm::stack::StackTrait; + use integer::{ u256_overflowing_add, u256_overflow_sub, u256_overflow_mul, u256_safe_divmod, u512_safe_div_rem_by_u256, u256_try_as_non_zero }; -use evm::machine::{Machine, MachineCurrentContextTrait}; -use evm::stack::StackTrait; -use utils::math::{Exponentiation, WrappingExponentiation, u256_wide_add}; -use evm::errors::EVMError; use utils::i256::i256; +use utils::math::{Exponentiation, WrappingExponentiation, u256_wide_add}; #[generate_trait] impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait { diff --git a/crates/evm/src/instructions/system_operations.cairo b/crates/evm/src/instructions/system_operations.cairo index 35601a969..e56dc8bc6 100644 --- a/crates/evm/src/instructions/system_operations.cairo +++ b/crates/evm/src/instructions/system_operations.cairo @@ -1,14 +1,10 @@ //! System operations. -// Corelib imports -use traits::TryInto; use box::BoxTrait; - -// Internal imports +use evm::errors::EVMError; use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::MemoryTrait; use evm::stack::StackTrait; -use evm::errors::EVMError; #[generate_trait] impl SystemOperations of SystemOperationsTrait { diff --git a/crates/evm/src/interpreter.cairo b/crates/evm/src/interpreter.cairo index 1f1de741c..c08c1ccaa 100644 --- a/crates/evm/src/interpreter.cairo +++ b/crates/evm/src/interpreter.cairo @@ -2,7 +2,6 @@ /// Internal imports. use evm::context::{CallContextTrait,}; -use utils::{helpers::u256_to_bytes_array}; use evm::errors::{EVMError, PC_OUT_OF_BOUNDS}; use evm::instructions::{ ExchangeOperationsTrait, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait, @@ -10,6 +9,7 @@ use evm::instructions::{ EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationTrait, logging_operations }; use evm::machine::{Machine, MachineCurrentContextTrait}; +use utils::{helpers::u256_to_bytes_array}; #[derive(Drop, Copy)] struct EVMInterpreter {} diff --git a/crates/evm/src/lib.cairo b/crates/evm/src/lib.cairo index 0274c4304..bbe052589 100644 --- a/crates/evm/src/lib.cairo +++ b/crates/evm/src/lib.cairo @@ -1,32 +1,32 @@ +// Context module +mod context; + +// Errors module +mod errors; + // Kakarot main module mod execution; -// Memory module -mod memory; +// Helpers module +mod helpers; -// Stack module -mod stack; +// instructions module +mod instructions; // interpreter module mod interpreter; -// instructions module -mod instructions; +// Machine module +mod machine; -// Context module -mod context; +// Memory module +mod memory; // Data Models module mod model; -// Errors module -mod errors; - -// Helpers module -mod helpers; - -// Machine module -mod machine; +// Stack module +mod stack; // tests #[cfg(test)] diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index 1d7c17b4e..5e564a765 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -1,3 +1,5 @@ +use cmp::{max}; +use debug::PrintTrait; use integer::{ u32_safe_divmod, u32_as_non_zero, u128_safe_divmod, u128_as_non_zero, u256_as_non_zero }; @@ -5,12 +7,10 @@ use utils::constants::{ POW_2_0, POW_2_8, POW_2_16, POW_2_24, POW_2_32, POW_2_40, POW_2_48, POW_2_56, POW_2_64, POW_2_72, POW_2_80, POW_2_88, POW_2_96, POW_2_104, POW_2_112, POW_2_120, POW_256_16 }; -use cmp::{max}; use utils::{ helpers, helpers::SpanExtensionTrait, helpers::ArrayExtensionTrait, math::Exponentiation, math::WrappingExponentiation, math::Bitshift }; -use debug::PrintTrait; // 2**17 const MEMORY_SEGMENT_SIZE: usize = 131072; diff --git a/crates/evm/src/stack.cairo b/crates/evm/src/stack.cairo index a6c859903..05175e707 100644 --- a/crates/evm/src/stack.cairo +++ b/crates/evm/src/stack.cairo @@ -13,15 +13,13 @@ //! let value = stack.pop()?; //! ``` - -// Core lib imports - -use utils::constants; use debug::PrintTrait; -use nullable::{nullable_from_box, NullableTrait}; use evm::errors::{EVMError, STACK_OVERFLOW, STACK_UNDERFLOW}; use evm::helpers::U256TryIntoResultU32; +use nullable::{nullable_from_box, NullableTrait}; use starknet::EthAddress; + +use utils::constants; use utils::i256::i256; diff --git a/crates/evm/src/tests.cairo b/crates/evm/src/tests.cairo index 256228ef3..be17947a7 100644 --- a/crates/evm/src/tests.cairo +++ b/crates/evm/src/tests.cairo @@ -1,20 +1,20 @@ #[cfg(test)] -mod test_kakarot; +mod test_execution_context; #[cfg(test)] -mod test_stack; +mod test_instructions; #[cfg(test)] -mod test_memory; +mod test_kakarot; #[cfg(test)] -mod test_utils; +mod test_machine; #[cfg(test)] -mod test_execution_context; +mod test_memory; #[cfg(test)] -mod test_instructions; +mod test_stack; #[cfg(test)] -mod test_machine; +mod test_utils; diff --git a/crates/evm/src/tests/test_execution_context.cairo b/crates/evm/src/tests/test_execution_context.cairo index 0732b37eb..caa4faf89 100644 --- a/crates/evm/src/tests/test_execution_context.cairo +++ b/crates/evm/src/tests/test_execution_context.cairo @@ -1,22 +1,17 @@ -// Imports, may need adjustments based on actual dependencies and modules - -use debug::PrintTrait; -use traits::PartialEq; - -use starknet::{EthAddress, ContractAddress}; -use starknet::testing::{set_contract_address, set_caller_address}; use core::nullable::{NullableTrait, null}; - +use debug::PrintTrait; +use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait}; use evm::memory::{Memory, MemoryTrait}; use evm::model::Event; use evm::stack::{Stack, StackTrait}; -use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait}; -//TODO remove import once merged in corelib use evm::tests::test_utils::{setup_call_context, setup_execution_context, CallContextPartialEq}; use evm::tests::test_utils; +use starknet::testing::{set_contract_address, set_caller_address}; +use starknet::{EthAddress, ContractAddress}; use test_utils::{callvalue, evm_address}; +use traits::PartialEq; // TODO remove once no longer required (see https://github.com/starkware-libs/cairo/issues/3863) #[inline(never)] diff --git a/crates/evm/src/tests/test_instructions.cairo b/crates/evm/src/tests/test_instructions.cairo index 9c3c5b109..d435460a1 100644 --- a/crates/evm/src/tests/test_instructions.cairo +++ b/crates/evm/src/tests/test_instructions.cairo @@ -1,10 +1,10 @@ -mod test_stop_and_arithmetic_operations; +mod test_block_information; mod test_comparison_operations; mod test_duplication_operations; -mod test_block_information; mod test_environment_information; -mod test_push_operations; -mod test_memory_operations; mod test_exchange_operations; -mod test_system_operations; +mod test_memory_operations; +mod test_push_operations; mod test_sha3; +mod test_stop_and_arithmetic_operations; +mod test_system_operations; diff --git a/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo b/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo index cd8a41dc4..cafc5edd0 100644 --- a/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo @@ -1,8 +1,7 @@ +use debug::PrintTrait; use evm::instructions::ComparisonAndBitwiseOperationsTrait; -use evm::tests::test_utils::setup_machine; use evm::stack::StackTrait; - -use debug::PrintTrait; +use evm::tests::test_utils::setup_machine; use integer::BoundedInt; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_duplication_operations.cairo b/crates/evm/src/tests/test_instructions/test_duplication_operations.cairo index f2287de61..163b625a5 100644 --- a/crates/evm/src/tests/test_instructions/test_duplication_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_duplication_operations.cairo @@ -1,10 +1,8 @@ +use debug::PrintTrait; use evm::instructions::DuplicationOperationsTrait; use evm::stack::Stack; -use evm::tests::test_utils::setup_machine; use evm::stack::StackTrait; - - -use debug::PrintTrait; +use evm::tests::test_utils::setup_machine; use integer::BoundedInt; diff --git a/crates/evm/src/tests/test_instructions/test_environment_information.cairo b/crates/evm/src/tests/test_instructions/test_environment_information.cairo index e0a428909..44ebe8529 100644 --- a/crates/evm/src/tests/test_instructions/test_environment_information.cairo +++ b/crates/evm/src/tests/test_instructions/test_environment_information.cairo @@ -1,20 +1,20 @@ use array::{ArrayTrait}; +use evm::errors::{EVMError, TYPE_CONVERSION_ERROR, RETURNDATA_OUT_OF_BOUNDS_ERROR}; use evm::instructions::EnvironmentInformationTrait; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::{InternalMemoryTrait, MemoryTrait}; +use evm::stack::StackTrait; use evm::tests::test_utils::{ setup_machine, setup_machine_with_calldata, setup_machine_with_bytecode, evm_address, callvalue }; -use evm::stack::StackTrait; +use integer::u32_overflowing_add; use starknet::EthAddressIntoFelt252; -use utils::traits::{EthAddressIntoU256}; -use evm::errors::{EVMError, TYPE_CONVERSION_ERROR, RETURNDATA_OUT_OF_BOUNDS_ERROR}; -use evm::machine::{Machine, MachineCurrentContextTrait}; use utils::helpers::{ u256_to_bytes_array, load_word, ArrayExtension, ArrayExtensionTrait, SpanExtension, SpanExtensionTrait }; -use integer::u32_overflowing_add; +use utils::traits::{EthAddressIntoU256}; // ************************************************************************* // 0x30: ADDRESS diff --git a/crates/evm/src/tests/test_instructions/test_exchange_operations.cairo b/crates/evm/src/tests/test_instructions/test_exchange_operations.cairo index 54c568ba1..48efae00c 100644 --- a/crates/evm/src/tests/test_instructions/test_exchange_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_exchange_operations.cairo @@ -1,10 +1,9 @@ -use evm::stack::StackTrait; -use evm::tests::test_utils::setup_machine; use array::ArrayTrait; use evm::context::ExecutionContextTrait; use evm::instructions::exchange_operations::ExchangeOperationsTrait; -use core::result::ResultTrait; use evm::machine::Machine; +use evm::stack::StackTrait; +use evm::tests::test_utils::setup_machine; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo index f1421efbf..18522573d 100644 --- a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo @@ -1,16 +1,16 @@ +use evm::errors::{EVMError, STACK_UNDERFLOW, INVALID_DESTINATION}; use evm::instructions::{MemoryOperationTrait, EnvironmentInformationTrait}; +use evm::machine::{Machine, MachineCurrentContextTrait}; +use evm::memory::{InternalMemoryTrait, MemoryTrait}; +use evm::stack::StackTrait; use evm::tests::test_utils::{ setup_machine, setup_machine_with_bytecode, setup_machine_with_calldata, evm_address, callvalue }; -use evm::stack::StackTrait; -use evm::memory::{InternalMemoryTrait, MemoryTrait}; +use integer::BoundedInt; use starknet::EthAddressIntoFelt252; use utils::helpers::{u256_to_bytes_array}; use utils::traits::{EthAddressIntoU256}; -use evm::errors::{EVMError, STACK_UNDERFLOW, INVALID_DESTINATION}; -use integer::BoundedInt; -use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_push_operations.cairo b/crates/evm/src/tests/test_instructions/test_push_operations.cairo index 08cf5d855..5fa1322fd 100644 --- a/crates/evm/src/tests/test_instructions/test_push_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_push_operations.cairo @@ -1,9 +1,8 @@ +use evm::context::ExecutionContextTrait; use evm::instructions::PushOperationsTrait; use evm::stack::StackTrait; use evm::tests::test_utils::setup_machine_with_bytecode; -use evm::context::ExecutionContextTrait; - fn get_n_0xFF(mut n: u8) -> Span { let mut array: Array = ArrayTrait::new(); loop { diff --git a/crates/evm/src/tests/test_instructions/test_sha3.cairo b/crates/evm/src/tests/test_instructions/test_sha3.cairo index d3c6d14fe..41e1b5ada 100644 --- a/crates/evm/src/tests/test_instructions/test_sha3.cairo +++ b/crates/evm/src/tests/test_instructions/test_sha3.cairo @@ -1,11 +1,10 @@ +use evm::context::{ExecutionContext, ExecutionContextTrait,}; +use evm::errors::{EVMError, TYPE_CONVERSION_ERROR}; use evm::instructions::Sha3Trait; use evm::instructions::sha3::internal; -use evm::tests::test_utils::setup_machine; -use evm::context::{ExecutionContext, ExecutionContextTrait,}; use evm::memory::{InternalMemoryTrait, MemoryTrait}; use evm::stack::StackTrait; -use option::OptionTrait; -use evm::errors::{EVMError, TYPE_CONVERSION_ERROR}; +use evm::tests::test_utils::setup_machine; #[test] #[available_gas(20000000)] diff --git a/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo b/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo index 8c76f8b1d..b85554b2e 100644 --- a/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_stop_and_arithmetic_operations.cairo @@ -1,10 +1,10 @@ -use evm::tests::test_utils::setup_machine; +use evm::context::ExecutionContextTrait; use evm::instructions::StopAndArithmeticOperationsTrait; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::stack::StackTrait; -use evm::context::ExecutionContextTrait; +use evm::tests::test_utils::setup_machine; use integer::BoundedInt; -use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] diff --git a/crates/evm/src/tests/test_instructions/test_system_operations.cairo b/crates/evm/src/tests/test_instructions/test_system_operations.cairo index 53eeb4b6e..5d8167615 100644 --- a/crates/evm/src/tests/test_instructions/test_system_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_system_operations.cairo @@ -1,11 +1,10 @@ +use evm::context::{ExecutionContext, ExecutionContextTrait,}; +use evm::instructions::MemoryOperationTrait; use evm::instructions::SystemOperationsTrait; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::stack::StackTrait; use evm::tests::test_utils::setup_machine; -use evm::instructions::MemoryOperationTrait; -use evm::context::{ExecutionContext, ExecutionContextTrait,}; use utils::helpers::load_word; -use traits::Into; -use evm::machine::{Machine, MachineCurrentContextTrait}; #[test] #[available_gas(20000000)] diff --git a/crates/evm/src/tests/test_kakarot.cairo b/crates/evm/src/tests/test_kakarot.cairo index 9cd6e96e6..b42391a17 100644 --- a/crates/evm/src/tests/test_kakarot.cairo +++ b/crates/evm/src/tests/test_kakarot.cairo @@ -1,4 +1,4 @@ -use evm::execution; -use evm::stack::StackTrait; use evm::context::CallContext; use evm::errors; +use evm::execution; +use evm::stack::StackTrait; diff --git a/crates/evm/src/tests/test_machine.cairo b/crates/evm/src/tests/test_machine.cairo index c98fae9d5..89e14e9f3 100644 --- a/crates/evm/src/tests/test_machine.cairo +++ b/crates/evm/src/tests/test_machine.cairo @@ -1,5 +1,5 @@ -use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::context::CallContextTrait; +use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::tests::test_utils::{ evm_address, setup_machine_with_bytecode, setup_machine, starknet_address }; diff --git a/crates/evm/src/tests/test_memory.cairo b/crates/evm/src/tests/test_memory.cairo index b5b8ec366..253a77ef8 100644 --- a/crates/evm/src/tests/test_memory.cairo +++ b/crates/evm/src/tests/test_memory.cairo @@ -1,10 +1,10 @@ use core::dict::Felt252DictTrait; use evm::memory::{MemoryTrait, InternalMemoryTrait, MemoryPrintTrait}; +use integer::BoundedInt; use utils::{ math::Exponentiation, math::WrappingExponentiation, helpers, helpers::SpanExtensionTrait }; use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120}; -use integer::BoundedInt; mod internal { use evm::memory::{MemoryTrait, InternalMemoryTrait, MemoryPrintTrait}; diff --git a/crates/evm/src/tests/test_stack.cairo b/crates/evm/src/tests/test_stack.cairo index 1786dbcf6..4473cd79c 100644 --- a/crates/evm/src/tests/test_stack.cairo +++ b/crates/evm/src/tests/test_stack.cairo @@ -46,12 +46,11 @@ fn test__len__should_return_the_length_of_the_stack() { #[cfg(test)] mod push { + use evm::errors::{EVMError, STACK_OVERFLOW}; use super::StackTrait; use super::constants; - use evm::errors::{EVMError, STACK_OVERFLOW}; - #[test] #[available_gas(600000)] fn test_should_add_an_element_to_the_stack() { @@ -142,10 +141,8 @@ mod push { #[cfg(test)] mod pop { - use super::StackTrait; - - use evm::errors::{EVMError, STACK_UNDERFLOW}; + use super::StackTrait; #[test] #[available_gas(950000)] @@ -275,10 +272,8 @@ mod pop { #[cfg(test)] mod peek { - use super::StackTrait; - - use evm::errors::{EVMError, STACK_UNDERFLOW}; + use super::StackTrait; #[test] #[available_gas(800000)] @@ -401,9 +396,8 @@ mod peek { #[cfg(test)] mod swap { - use super::StackTrait; - use evm::errors::{EVMError, STACK_UNDERFLOW}; + use super::StackTrait; #[test] #[available_gas(4000000)] diff --git a/crates/evm/src/tests/test_utils.cairo b/crates/evm/src/tests/test_utils.cairo index 3fc14da01..c79b432c3 100644 --- a/crates/evm/src/tests/test_utils.cairo +++ b/crates/evm/src/tests/test_utils.cairo @@ -1,7 +1,7 @@ -use starknet::{contract_address_try_from_felt252, ContractAddress, EthAddress}; +use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait,}; use evm::machine::Machine; -use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait,}; +use starknet::{contract_address_try_from_felt252, ContractAddress, EthAddress}; fn starknet_address() -> ContractAddress { 'starknet_address'.try_into().unwrap() diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 406a8194a..09468e64e 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -448,9 +448,7 @@ impl ArrayExtension, impl TDrop: Drop> of ArrayExtensi loop { match arr2.pop_front() { Option::Some(elem) => self.append(*elem), - Option::None => { - break; - } + Option::None => { break; } }; } } From 1332bec64df20657ba57015f1a830ce24e89d630 Mon Sep 17 00:00:00 2001 From: msaug Date: Mon, 2 Oct 2023 17:30:48 +0700 Subject: [PATCH 3/6] chore: bump scarb CI --- .github/workflows/gas_reports.yml | 2 +- .github/workflows/gas_snapshot.yml | 2 +- .github/workflows/test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gas_reports.yml b/.github/workflows/gas_reports.yml index 07ecf9f27..6f443b411 100644 --- a/.github/workflows/gas_reports.yml +++ b/.github/workflows/gas_reports.yml @@ -26,7 +26,7 @@ jobs: - name: Set up Scarb uses: software-mansion/setup-scarb@v1 with: - scarb-version: 0.7.0 + scarb-version: 2.3.0-rc0 - name: Run compare_snapshot script id: run-script diff --git a/.github/workflows/gas_snapshot.yml b/.github/workflows/gas_snapshot.yml index 1c03898e3..cadbd2214 100644 --- a/.github/workflows/gas_snapshot.yml +++ b/.github/workflows/gas_snapshot.yml @@ -22,7 +22,7 @@ jobs: - name: Set up Scarb uses: software-mansion/setup-scarb@v1 with: - scarb-version: 0.7.0 + scarb-version: 2.3.0-rc0 - name: Generate gas snapshot run: python scripts/gen_snapshot.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d233fe144..c0d2afd61 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: - uses: actions/checkout@v3 - uses: software-mansion/setup-scarb@v1 with: - scarb-version: 0.7.0 + scarb-version: 2.3.0-rc0 - run: scarb fmt --check - run: scarb build - run: scarb test From 7ebe35fb51c559d7c1d70508c334b61167d1cf5f Mon Sep 17 00:00:00 2001 From: msaug Date: Tue, 3 Oct 2023 10:14:03 +0700 Subject: [PATCH 4/6] feat: set_active_context --- crates/evm/src/context.cairo | 8 ++++---- crates/evm/src/machine.cairo | 9 +++++---- docs/general/execution_context.md | 2 +- docs/general/machine.md | 5 ++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/evm/src/context.cairo b/crates/evm/src/context.cairo index 2b835e2ab..b9b442c29 100644 --- a/crates/evm/src/context.cairo +++ b/crates/evm/src/context.cairo @@ -125,7 +125,7 @@ impl DefaultBoxCallContext of Default> { /// Stores all data relevant to the current execution context. #[derive(Drop, Default)] struct ExecutionContext { - context_id: usize, + id: usize, evm_address: EthAddress, starknet_address: ContractAddress, program_counter: u32, @@ -154,7 +154,7 @@ impl ExecutionContextImpl of ExecutionContextTrait { /// Create a new execution context instance. #[inline(always)] fn new( - context_id: usize, + id: usize, evm_address: EthAddress, starknet_address: ContractAddress, call_context: CallContext, @@ -163,7 +163,7 @@ impl ExecutionContextImpl of ExecutionContextTrait { return_data: Array, ) -> ExecutionContext { ExecutionContext { - context_id, + id, evm_address, starknet_address, program_counter: Default::default(), @@ -288,7 +288,7 @@ impl ExecutionContextImpl of ExecutionContextTrait { #[inline(always)] fn is_root(self: @ExecutionContext) -> bool { - *self.context_id == 0 + *self.id == 0 } // TODO: Implement print_debug diff --git a/crates/evm/src/machine.cairo b/crates/evm/src/machine.cairo index 41c94df01..bccfbc1c3 100644 --- a/crates/evm/src/machine.cairo +++ b/crates/evm/src/machine.cairo @@ -67,9 +67,10 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait { /// to divide a unique Stack/Memory simulated by a dict into /// multiple sub-structures relative to a single context. #[inline(always)] - fn set_active_execution_ctx(ref self: Machine, id: usize) { - self.memory.set_active_segment(id); - self.stack.set_active_segment(id); + fn set_active_execution_ctx(ref self: Machine, ctx: ExecutionContext) { + self.memory.set_active_segment(ctx.id); + self.stack.set_active_segment(ctx.id); + self.current_context = BoxTrait::new(ctx); } #[inline(always)] @@ -240,7 +241,7 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait { #[inline(always)] fn is_root(ref self: Machine) -> bool { let current_execution_ctx = self.current_context.unbox(); - let is_root = current_execution_ctx.context_id == 0; + let is_root = current_execution_ctx.id == 0; self.current_context = BoxTrait::new(current_execution_ctx); is_root } diff --git a/docs/general/execution_context.md b/docs/general/execution_context.md index b04e77932..83ee7d92e 100644 --- a/docs/general/execution_context.md +++ b/docs/general/execution_context.md @@ -16,7 +16,7 @@ fields ```mermaid classDiagram class ExecutionContext{ - context_id: usize, + id: usize, evm_address: EthAddress, starknet_address: ContractAddress, program_counter: u32, diff --git a/docs/general/machine.md b/docs/general/machine.md index e50bca8d6..4fdcdc36a 100644 --- a/docs/general/machine.md +++ b/docs/general/machine.md @@ -37,8 +37,7 @@ To overcome the problem stated above, we have come up with the following design: context tree until the desired execution context is reached. The machine also stores a pointer to the current execution context. - The execution context tree is initialized with a single root execution - context, which has no parent and no child. It has `context_id` field equal - to 0. + context, which has no parent and no child. It has `id` field equal to 0. The following diagram describes the model of the Kakarot Machine. @@ -65,7 +64,7 @@ classDiagram } class ExecutionContext{ - context_id: usize, + id: usize, evm_address: EthAddress, starknet_address: ContractAddress, program_counter: u32, From 0cd8ce9b89e0499ad6888b60e2b660c29c0f7272 Mon Sep 17 00:00:00 2001 From: msaug Date: Tue, 3 Oct 2023 10:20:51 +0700 Subject: [PATCH 5/6] chore: scarb fmt --- crates/evm/src/tests/test_memory.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm/src/tests/test_memory.cairo b/crates/evm/src/tests/test_memory.cairo index 253a77ef8..a8d2818cd 100644 --- a/crates/evm/src/tests/test_memory.cairo +++ b/crates/evm/src/tests/test_memory.cairo @@ -1,10 +1,10 @@ use core::dict::Felt252DictTrait; use evm::memory::{MemoryTrait, InternalMemoryTrait, MemoryPrintTrait}; use integer::BoundedInt; +use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120}; use utils::{ math::Exponentiation, math::WrappingExponentiation, helpers, helpers::SpanExtensionTrait }; -use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120}; mod internal { use evm::memory::{MemoryTrait, InternalMemoryTrait, MemoryPrintTrait}; From 8435dbf00e11f219bbf28fdbeb97dd57e40216b7 Mon Sep 17 00:00:00 2001 From: msaug Date: Tue, 3 Oct 2023 10:32:28 +0700 Subject: [PATCH 6/6] tests: current context switching --- crates/evm/src/machine.cairo | 2 +- crates/evm/src/tests/test_machine.cairo | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/evm/src/machine.cairo b/crates/evm/src/machine.cairo index bccfbc1c3..b1a1014c7 100644 --- a/crates/evm/src/machine.cairo +++ b/crates/evm/src/machine.cairo @@ -67,7 +67,7 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait { /// to divide a unique Stack/Memory simulated by a dict into /// multiple sub-structures relative to a single context. #[inline(always)] - fn set_active_execution_ctx(ref self: Machine, ctx: ExecutionContext) { + fn set_current_context(ref self: Machine, ctx: ExecutionContext) { self.memory.set_active_segment(ctx.id); self.stack.set_active_segment(ctx.id); self.current_context = BoxTrait::new(ctx); diff --git a/crates/evm/src/tests/test_machine.cairo b/crates/evm/src/tests/test_machine.cairo index 89e14e9f3..50841f94b 100644 --- a/crates/evm/src/tests/test_machine.cairo +++ b/crates/evm/src/tests/test_machine.cairo @@ -1,7 +1,8 @@ use evm::context::CallContextTrait; use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::tests::test_utils::{ - evm_address, setup_machine_with_bytecode, setup_machine, starknet_address + evm_address, setup_machine_with_bytecode, setup_machine, starknet_address, + setup_execution_context }; @@ -16,6 +17,28 @@ fn test_machine_default() { assert(!machine.stopped(), 'ctx should not be stopped'); } +#[test] +#[available_gas(20000000)] +fn test_set_current_context() { + let mut machine: Machine = Default::default(); + + let first_ctx = machine.current_context.unbox(); + assert(first_ctx.id == 0, 'wrong first id'); + // We need to re-box the context into the machine, otherwise we have a "Variable Moved" error. + machine.current_context = BoxTrait::new(first_ctx); + assert(machine.stack.active_segment == 0, 'wrong initial stack segment'); + assert(machine.memory.active_segment == 0, 'wrong initial memory segment'); + + // Create another context with id=1 + let mut second_ctx = setup_execution_context(); + second_ctx.id = 1; + + machine.set_current_context(second_ctx); + assert(machine.stack.active_segment == 1, 'wrong updated stack segment'); + assert(machine.memory.active_segment == 1, 'wrong updated stack segment'); + assert(machine.current_context.unbox().id == 1, 'wrong updated id'); +} + #[test] #[available_gas(20000000)] fn test_set_pc() {