From 5d0bd00cf7bda8749f23133a4e76d166aaf25b86 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 21 Nov 2023 17:34:27 +0100 Subject: [PATCH 1/2] init --- crates/contracts/src/kakarot_core/kakarot.cairo | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 3bfca48be..f1664b8af 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -19,6 +19,7 @@ mod KakarotCore { use contracts::components::ownable::{ownable_component}; use contracts::components::upgradeable::{IUpgradeable, upgradeable_component}; use contracts::contract_account::{IContractAccountDispatcher, IContractAccountDispatcherTrait}; + use contracts::eoa::{IExternallyOwnedAccountDispatcher, IExternallyOwnedAccountDispatcherTrait}; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::interface; use core::starknet::SyscallResultTrait; @@ -32,8 +33,8 @@ mod KakarotCore { use evm::model::{ExecutionResult, Address, AddressTrait}; use evm::state::StateTrait; use starknet::{ - EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address, - get_caller_address, deploy_syscall + EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address, deploy_syscall, + get_caller_address }; use super::{INVOKE_ETH_CALL_FORBIDDEN}; use super::{StoredAccountType}; From 102ceefd656b22e15e4f68309ad142e12d1e6dd7 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 21 Nov 2023 17:43:16 +0100 Subject: [PATCH 2/2] fix: greg's bug --- .../contracts/src/kakarot_core/kakarot.cairo | 83 +++++++------------ .../src/tests/test_kakarot_core.cairo | 25 +++--- 2 files changed, 43 insertions(+), 65 deletions(-) diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index f1664b8af..730e781ce 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -255,17 +255,7 @@ mod KakarotCore { let from = Address { evm: from, starknet: self.compute_starknet_address(from) }; - let to = match to { - Option::Some(to) => { - let target_starknet_address = self.compute_starknet_address(to); - Option::Some(Address { evm: to, starknet: target_starknet_address }) - }, - Option::None => Option::None - }; - - let result = KakarotInternal::handle_execute( - :from, :to, :gas_limit, :gas_price, :value, :data - ); + let result = self.handle_call(:from, :to, :gas_limit, :gas_price, :value, :data); match result { Result::Ok(result) => result.return_data, // TODO: Return the error message as Bytes in the response @@ -296,17 +286,7 @@ mod KakarotCore { .expect('Fetching EOA failed'); assert(caller_account_type == AccountType::EOA, 'Caller is not an EOA'); - let to = match to { - Option::Some(to) => { - let target_starknet_address = self.compute_starknet_address(to); - Option::Some(Address { evm: to, starknet: target_starknet_address }) - }, - Option::None(_) => Option::None(()) - }; - - let mut result = KakarotInternal::handle_execute( - :from, :to, :gas_limit, :gas_price, :value, :data - ); + let mut result = self.handle_call(:from, :to, :gas_limit, :gas_price, :value, :data); match result { Result::Ok(result) => { let mut state = result.state; @@ -358,14 +338,32 @@ mod KakarotCore { } } - mod KakarotInternal { - use evm::errors::EVMError; - use evm::execution::execute; - use evm::model::{ExecutionResult, Address, AccountTrait}; + #[generate_trait] + impl KakarotCoreInternalImpl of KakarotCoreInternal { + fn is_view(self: @ContractState) -> bool { + let tx_info = get_tx_info().unbox(); + + // If the account that originated the transaction is not zero, this means we + // are in an invoke transaction instead of a call; therefore, `eth_call` is being wrongly called + // For invoke transactions, `eth_send_transaction` must be used + if !tx_info.account_contract_address.is_zero() { + return false; + } + true + } - fn handle_execute( + /// Maps an EVM address to a Starknet address + /// Triggerred when deployment of an EOA or CA is successful + fn set_address_registry( + ref self: ContractState, evm_address: EthAddress, account: StoredAccountType + ) { + self.address_registry.write(evm_address, account); + } + + fn handle_call( + self: @ContractState, from: Address, - to: Option
, + to: Option, gas_limit: u128, gas_price: u128, value: u256, @@ -374,7 +372,11 @@ mod KakarotCore { match to { //TODO we can optimize this by doing this one step later, when we load the account from the state. This way we can avoid loading the account bytecode twice. Option::Some(to) => { - let bytecode = AccountTrait::fetch_or_create(to.evm)?.code; + let bytecode = AccountTrait::fetch_or_create(to)?.code; + + let target_starknet_address = self.compute_starknet_address(to); + let to = Address { evm: to, starknet: target_starknet_address }; + let execution_result = execute( from, to, @@ -397,27 +399,4 @@ mod KakarotCore { } } } - - #[generate_trait] - impl KakarotCoreInternalImpl of KakarotCoreInternal { - fn is_view(self: @ContractState) -> bool { - let tx_info = get_tx_info().unbox(); - - // If the account that originated the transaction is not zero, this means we - // are in an invoke transaction instead of a call; therefore, `eth_call` is being wrongly called - // For invoke transactions, `eth_send_transaction` must be used - if !tx_info.account_contract_address.is_zero() { - return false; - } - true - } - - /// Maps an EVM address to a Starknet address - /// Triggerred when deployment of an EOA or CA is successful - fn set_address_registry( - ref self: ContractState, evm_address: EthAddress, account: StoredAccountType - ) { - self.address_registry.write(evm_address, account); - } - } } diff --git a/crates/contracts/src/tests/test_kakarot_core.cairo b/crates/contracts/src/tests/test_kakarot_core.cairo index ffca63d2c..42abb57e5 100644 --- a/crates/contracts/src/tests/test_kakarot_core.cairo +++ b/crates/contracts/src/tests/test_kakarot_core.cairo @@ -5,8 +5,7 @@ use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait; use contracts::kakarot_core::interface::IKakarotCore; use contracts::kakarot_core::kakarot::StoredAccountType; use contracts::kakarot_core::{ - interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore, - KakarotCore::{KakarotCoreInternal, KakarotInternal}, + interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore, KakarotCore::{KakarotCoreInternal}, }; use contracts::tests::test_data::counter_evm_bytecode; use contracts::tests::test_upgradeable::{ @@ -337,8 +336,7 @@ fn test_handle_call() { ) .unwrap(); - let to = test_utils::other_evm_address(); - let to = Option::Some(Address { evm: to, starknet: kakarot_core.compute_starknet_address(to) }); + let to = Option::Some(test_utils::other_evm_address()); let gas_limit = test_utils::gas_limit(); let gas_price = test_utils::gas_price(); let value = 0; @@ -346,15 +344,16 @@ fn test_handle_call() { let data = array![0x6d, 0x4c, 0xe6, 0x3c].span(); // When - - let result = KakarotInternal::handle_execute( - from: Address { evm: evm_address, starknet: eoa }, - :to, - :gas_limit, - :gas_price, - :value, - :data - ) + let mut kakarot_core = KakarotCore::unsafe_new_contract_state(); + let result = kakarot_core + .handle_call( + from: Address { evm: evm_address, starknet: eoa }, + :to, + :gas_limit, + :gas_price, + :value, + :data + ) .expect('handle_call failed'); let return_data = result.return_data;