diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 51fa57fbc..0876f496a 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -33,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}; @@ -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 @@ -298,17 +288,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; @@ -360,14 +340,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, @@ -376,7 +374,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, @@ -399,27 +401,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 5fb0f4cb4..7b8d2537a 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_execute() { ) .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_execute() { 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;