From 8b5d7a7115d2dd15bf4fa853b2fa49a9753d83df Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Tue, 3 Oct 2023 11:07:08 +0700 Subject: [PATCH 1/6] feat: implement exec_caller --- .../environmental_information.cairo | 3 ++- .../test_comparison_operations.cairo | 2 +- .../test_environment_information.cairo | 21 ++++++++++++++++++- .../test_stop_and_arithmetic_operations.cairo | 2 +- crates/evm/src/tests/test_stack.cairo | 4 ++-- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index be0f29044..a34ce6d0c 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -7,6 +7,7 @@ use evm::stack::StackTrait; use integer::u32_overflowing_add; use utils::helpers::{load_word}; use utils::traits::{EthAddressIntoU256}; +use starknet::EthAddress; #[generate_trait] impl EnvironmentInformationImpl of EnvironmentInformationTrait { @@ -35,7 +36,7 @@ impl EnvironmentInformationImpl of EnvironmentInformationTrait { /// Get caller address. /// # Specification: https://www.evm.codes/#33?fork=shanghai fn exec_caller(ref self: Machine) -> Result<(), EVMError> { - Result::Ok(()) + self.stack.push(self.caller().into()) } /// 0x34 - CALLVALUE 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 cafc5edd0..0cd836ca4 100644 --- a/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo @@ -1687,7 +1687,7 @@ fn assert_sar(a: u256, b: u256, expected: u256) { #[test] #[available_gas(20000000)] -fn test__exec_or__should_pop_0_and_1_and_push_0xCD__when_0_is_0x89_and_1_is_0xC5() { +fn test_exec_or__should_pop_0_and_1_and_push_0xCD__when_0_is_0x89_and_1_is_0xC5() { //Given let mut machine = setup_machine(); machine.stack.push(0x89); 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 44ebe8529..48503ac15 100644 --- a/crates/evm/src/tests/test_instructions/test_environment_information.cairo +++ b/crates/evm/src/tests/test_instructions/test_environment_information.cairo @@ -42,13 +42,32 @@ fn test_address_nested_call() { // A (EOA) -(calls)-> B (smart contract) -(calls // ref: https://github.com/kkrt-labs/kakarot-ssj/issues/183 } + +// ************************************************************************* +// 0x33: CALLER +// ************************************************************************* +#[test] +#[available_gas(5000000)] +fn test_caller() { + // Given + let mut machine = setup_machine(); + + // When + machine.exec_caller(); + + // Then + assert(machine.stack.len() == 1, 'stack should have one element'); + assert(machine.stack.peek().unwrap() == 'evm_address'.into(), 'should be evm_address'); +} + + // ************************************************************************* // 0x34: CALLVALUE // ************************************************************************* #[test] #[available_gas(1200000)] -fn test__exec_callvalue() { +fn test_exec_callvalue() { // Given let mut machine = setup_machine(); 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 b85554b2e..fd8d25e2a 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 @@ -171,7 +171,7 @@ fn test_exec_sdiv_pos() { #[test] #[available_gas(20000000)] -fn test__exec_sdiv_neg() { +fn test_exec_sdiv_neg() { // Given let mut machine = setup_machine(); machine.stack.push(BoundedInt::max()).unwrap(); diff --git a/crates/evm/src/tests/test_stack.cairo b/crates/evm/src/tests/test_stack.cairo index 4473cd79c..2bbbdd339 100644 --- a/crates/evm/src/tests/test_stack.cairo +++ b/crates/evm/src/tests/test_stack.cairo @@ -16,7 +16,7 @@ fn test_stack_new_should_return_empty_stack() { #[test] #[available_gas(400000)] -fn test__empty__should_return_if_stack_is_empty() { +fn test_empty__should_return_if_stack_is_empty() { // Given let mut stack = StackTrait::new(); @@ -31,7 +31,7 @@ fn test__empty__should_return_if_stack_is_empty() { #[test] #[available_gas(350000)] -fn test__len__should_return_the_length_of_the_stack() { +fn test_len__should_return_the_length_of_the_stack() { // Given let mut stack = StackTrait::new(); From 54e6952d22e586bae838a4a1d25c0d947e632373 Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Tue, 3 Oct 2023 14:40:46 +0700 Subject: [PATCH 2/6] chore: fmt --- crates/evm/src/instructions/environmental_information.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index a34ce6d0c..d1b5161dc 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -5,9 +5,9 @@ use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::MemoryTrait; use evm::stack::StackTrait; use integer::u32_overflowing_add; +use starknet::EthAddress; use utils::helpers::{load_word}; use utils::traits::{EthAddressIntoU256}; -use starknet::EthAddress; #[generate_trait] impl EnvironmentInformationImpl of EnvironmentInformationTrait { From 247a92d480ad65bc602f412ead6f65764303dbf5 Mon Sep 17 00:00:00 2001 From: Elias Tazartes <66871571+Eikix@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:25:15 +0700 Subject: [PATCH 3/6] Update crates/evm/src/tests/test_instructions/test_comparison_operations.cairo Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> --- .../tests/test_instructions/test_comparison_operations.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0cd836ca4..a245782b4 100644 --- a/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_comparison_operations.cairo @@ -1687,7 +1687,7 @@ fn assert_sar(a: u256, b: u256, expected: u256) { #[test] #[available_gas(20000000)] -fn test_exec_or__should_pop_0_and_1_and_push_0xCD__when_0_is_0x89_and_1_is_0xC5() { +fn test_exec_or_should_pop_0_and_1_and_push_0xCD_when_0_is_0x89_and_1_is_0xC5() { //Given let mut machine = setup_machine(); machine.stack.push(0x89); From 6701016604f02351e8d561f2b1ab52087b368328 Mon Sep 17 00:00:00 2001 From: Elias Tazartes <66871571+Eikix@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:25:22 +0700 Subject: [PATCH 4/6] Update crates/evm/src/tests/test_stack.cairo Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> --- crates/evm/src/tests/test_stack.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm/src/tests/test_stack.cairo b/crates/evm/src/tests/test_stack.cairo index 2bbbdd339..fc885b5b9 100644 --- a/crates/evm/src/tests/test_stack.cairo +++ b/crates/evm/src/tests/test_stack.cairo @@ -16,7 +16,7 @@ fn test_stack_new_should_return_empty_stack() { #[test] #[available_gas(400000)] -fn test_empty__should_return_if_stack_is_empty() { +fn test_empty_should_return_if_stack_is_empty() { // Given let mut stack = StackTrait::new(); From 269cd0e9cc1f2d9f39fde5a071b10a97602bef8d Mon Sep 17 00:00:00 2001 From: Elias Tazartes <66871571+Eikix@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:25:29 +0700 Subject: [PATCH 5/6] Update crates/evm/src/tests/test_stack.cairo Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> --- crates/evm/src/tests/test_stack.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm/src/tests/test_stack.cairo b/crates/evm/src/tests/test_stack.cairo index fc885b5b9..74312b0bd 100644 --- a/crates/evm/src/tests/test_stack.cairo +++ b/crates/evm/src/tests/test_stack.cairo @@ -31,7 +31,7 @@ fn test_empty_should_return_if_stack_is_empty() { #[test] #[available_gas(350000)] -fn test_len__should_return_the_length_of_the_stack() { +fn test_len_should_return_the_length_of_the_stack() { // Given let mut stack = StackTrait::new(); From ad0c6a5aa2bc4615c6073281fe21fc873000bc9f Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Tue, 3 Oct 2023 17:27:18 +0700 Subject: [PATCH 6/6] fix: fix commnets --- crates/evm/src/instructions/environmental_information.cairo | 1 - .../tests/test_instructions/test_environment_information.cairo | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/evm/src/instructions/environmental_information.cairo b/crates/evm/src/instructions/environmental_information.cairo index d1b5161dc..0b3fb9ed2 100644 --- a/crates/evm/src/instructions/environmental_information.cairo +++ b/crates/evm/src/instructions/environmental_information.cairo @@ -5,7 +5,6 @@ use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::memory::MemoryTrait; use evm::stack::StackTrait; use integer::u32_overflowing_add; -use starknet::EthAddress; use utils::helpers::{load_word}; use utils::traits::{EthAddressIntoU256}; 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 48503ac15..b1765bfd8 100644 --- a/crates/evm/src/tests/test_instructions/test_environment_information.cairo +++ b/crates/evm/src/tests/test_instructions/test_environment_information.cairo @@ -57,7 +57,7 @@ fn test_caller() { // Then assert(machine.stack.len() == 1, 'stack should have one element'); - assert(machine.stack.peek().unwrap() == 'evm_address'.into(), 'should be evm_address'); + assert(machine.stack.peek().unwrap() == evm_address().into(), 'should be evm_address'); }