diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index 5b902f359..cc9ae5960 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -122,6 +122,8 @@ mod KakarotCore { new_class_hash: ClassHash, } + // TODO: add ability to pass Span, which should be deployed along with Kakarot + // this can be done once https://github.com/starkware-libs/cairo/issues/4488 is resolved #[constructor] fn constructor( ref self: ContractState, @@ -188,7 +190,6 @@ mod KakarotCore { } } - fn contract_account_nonce(self: @ContractState, evm_address: EthAddress) -> u64 { let ca_address = ContractAccountTrait::at(evm_address) .expect('Fetching CA failed') diff --git a/crates/contracts/src/tests/test_eoa.cairo b/crates/contracts/src/tests/test_eoa.cairo index f7386601f..518387e07 100644 --- a/crates/contracts/src/tests/test_eoa.cairo +++ b/crates/contracts/src/tests/test_eoa.cairo @@ -24,6 +24,7 @@ mod test_external_owned_account { use core::array::SpanTrait; use core::starknet::account::{Call, AccountContractDispatcher, AccountContractDispatcherTrait}; + use evm::model::{Address, AddressTrait, ContractAccountTrait}; use evm::tests::test_utils::{ kakarot_address, evm_address, other_evm_address, other_starknet_address, eoa_address, diff --git a/crates/contracts/src/tests/test_kakarot_core.cairo b/crates/contracts/src/tests/test_kakarot_core.cairo index b7faf03d1..4167715f2 100644 --- a/crates/contracts/src/tests/test_kakarot_core.cairo +++ b/crates/contracts/src/tests/test_kakarot_core.cairo @@ -1,8 +1,14 @@ use contracts::contract_account::ContractAccount::TEST_CLASS_HASH as ContractAccountTestClassHash; +use contracts::contract_account::ContractAccount; use contracts::contract_account::{IContractAccountDispatcher, IContractAccountDispatcherTrait}; -use contracts::eoa::ExternallyOwnedAccount; -use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait; +use contracts::eoa::{ + ExternallyOwnedAccount, IExternallyOwnedAccountDispatcher, + IExternallyOwnedAccountDispatcherTrait +}; use contracts::kakarot_core::interface::IKakarotCore; +use contracts::kakarot_core::interface::{ + IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait +}; use contracts::kakarot_core::kakarot::StoredAccountType; use contracts::kakarot_core::{ interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore, KakarotCore::{KakarotCoreInternal}, @@ -14,6 +20,9 @@ use contracts::tests::test_upgradeable::{ }; use contracts::tests::test_utils as contract_utils; use contracts::uninitialized_account::UninitializedAccount; +use core::option::OptionTrait; + + use core::traits::TryInto; use evm::machine::Status; use evm::model::contract_account::ContractAccountTrait; @@ -468,4 +477,3 @@ fn test_eoa_class_hash() { assert(event.old_class_hash == class_hash, 'wrong old hash'); assert(event.new_class_hash == kakarot_core.eoa_class_hash(), 'wrong new hash'); } - diff --git a/crates/evm/src/instructions/block_information.cairo b/crates/evm/src/instructions/block_information.cairo index d47b6d3c9..1cfc1ad3a 100644 --- a/crates/evm/src/instructions/block_information.cairo +++ b/crates/evm/src/instructions/block_information.cairo @@ -1,3 +1,4 @@ +use contracts::eoa::{IExternallyOwnedAccountDispatcher, IExternallyOwnedAccountDispatcherTrait}; //! Block Information. use contracts::kakarot_core::{KakarotCore, IKakarotCore}; @@ -54,11 +55,11 @@ impl BlockInformation of BlockInformationTrait { let execution_info = get_execution_info_syscall() .map_err(EVMError::SyscallFailed(EXECUTION_INFO_SYSCALL_FAILED))? .unbox(); - let coinbase: EthAddress = execution_info - .block_info - .unbox() - .sequencer_address - .try_into_result()?; + + let coinbase = execution_info.block_info.unbox().sequencer_address; + let eoa = IExternallyOwnedAccountDispatcher { contract_address: coinbase }; + + let coinbase = eoa.evm_address(); self.stack.push(coinbase.into()) } diff --git a/crates/evm/src/model/eoa.cairo b/crates/evm/src/model/eoa.cairo index 3e7519915..6cef071ca 100644 --- a/crates/evm/src/model/eoa.cairo +++ b/crates/evm/src/model/eoa.cairo @@ -5,6 +5,7 @@ use contracts::kakarot_core::{IKakarotCore, KakarotCore, KakarotCore::KakarotCor use contracts::uninitialized_account::{ IUninitializedAccountDispatcher, IUninitializedAccountDispatcherTrait }; + use evm::errors::{EVMError, CONTRACT_SYSCALL_FAILED, EOA_EXISTS}; use evm::model::{Address, AddressTrait}; use starknet::{EthAddress, ContractAddress, get_contract_address, deploy_syscall}; @@ -29,6 +30,7 @@ impl EOAImpl of EOATrait { let calldata: Span = array![kakarot_address.into(), evm_address.into()].span(); let maybe_address = deploy_syscall(account_class_hash, evm_address.into(), calldata, false); + // Panic with err as syscall failure can't be caught, so we can't manage // the error match maybe_address { diff --git a/crates/evm/src/tests/test_instructions/test_block_information.cairo b/crates/evm/src/tests/test_instructions/test_block_information.cairo index be8b6dc99..388b6069e 100644 --- a/crates/evm/src/tests/test_instructions/test_block_information.cairo +++ b/crates/evm/src/tests/test_instructions/test_block_information.cairo @@ -3,14 +3,20 @@ use contracts::kakarot_core::interface::{ }; use contracts::tests::test_utils::{ - setup_contracts_for_testing, fund_account_with_native_token, deploy_contract_account + setup_contracts_for_testing, fund_account_with_native_token, deploy_contract_account, }; +use core::clone::Clone; +use core::result::ResultTrait; +use core::traits::TryInto; use evm::instructions::BlockInformationTrait; use evm::model::contract_account::ContractAccountTrait; use evm::stack::StackTrait; use evm::tests::test_utils::{evm_address, MachineBuilderTestTrait}; use openzeppelin::token::erc20::interface::IERC20CamelDispatcherTrait; -use starknet::testing::{set_block_timestamp, set_block_number, set_contract_address}; +use starknet::testing::{ + set_block_timestamp, set_block_number, set_contract_address, set_sequencer_address, + ContractAddress +}; /// 0x40 - BLOCKHASH #[test] @@ -22,7 +28,7 @@ fn test_exec_blockhash_below_bounds() { // When machine.stack.push(243).unwrap(); - machine.exec_blockhash(); + machine.exec_blockhash().unwrap(); // Then assert(machine.stack.peek().unwrap() == 0, 'stack top should be 0'); @@ -37,7 +43,7 @@ fn test_exec_blockhash_above_bounds() { // When machine.stack.push(491).unwrap(); - machine.exec_blockhash(); + machine.exec_blockhash().unwrap(); // Then assert(machine.stack.peek().unwrap() == 0, 'stack top should be 0'); @@ -72,7 +78,7 @@ fn test_block_timestamp_set_to_1692873993() { // If not set the default timestamp is 0. set_block_timestamp(1692873993); // When - machine.exec_timestamp(); + machine.exec_timestamp().unwrap(); // Then assert(machine.stack.len() == 1, 'stack should have one element'); @@ -86,7 +92,7 @@ fn test_block_number_set_to_32() { // If not set the default block number is 0. set_block_number(32); // When - machine.exec_number(); + machine.exec_number().unwrap(); // Then assert(machine.stack.len() == 1, 'stack should have one element'); @@ -98,7 +104,7 @@ fn test_gaslimit() { // Given let mut machine = MachineBuilderTestTrait::new_with_presets().build(); // When - machine.exec_gaslimit(); + machine.exec_gaslimit().unwrap(); // Then assert(machine.stack.len() == 1, 'stack should have one element'); @@ -122,7 +128,7 @@ fn test_exec_selfbalance_eoa() { // When set_contract_address(kakarot_core.contract_address); - machine.exec_selfbalance(); + machine.exec_selfbalance().unwrap(); // Then assert(machine.stack.peek().unwrap() == native_token.balanceOf(eoa), 'wrong balance'); @@ -138,7 +144,7 @@ fn test_exec_selfbalance_zero() { // When set_contract_address(kakarot_core.contract_address); - machine.exec_selfbalance(); + machine.exec_selfbalance().unwrap(); // Then assert(machine.stack.peek().unwrap() == 0x00, 'wrong balance'); @@ -155,7 +161,7 @@ fn test_exec_selfbalance_contract_account() { // When set_contract_address(kakarot_core.contract_address); - machine.exec_selfbalance(); + machine.exec_selfbalance().unwrap(); // Then assert(machine.stack.peek().unwrap() == 0x1, 'wrong balance'); @@ -167,7 +173,7 @@ fn test_basefee() { // Given let mut machine = MachineBuilderTestTrait::new_with_presets().build(); // When - machine.exec_basefee(); + machine.exec_basefee().unwrap(); // Then assert(machine.stack.len() == 1, 'stack should have one element'); @@ -188,7 +194,7 @@ fn test_chainid_should_push_chain_id_to_stack() { .into(); // When - machine.exec_chainid(); + machine.exec_chainid().unwrap(); // Then let result = machine.stack.peek().unwrap(); @@ -208,3 +214,26 @@ fn test_randao_should_push_zero_to_stack() { let result = machine.stack.peek().unwrap(); assert(result == 0x00, 'stack top should be zero'); } + +// ************************************************************************* +// 0x41: COINBASE +// ************************************************************************* +#[test] +#[available_gas(5000000)] +fn test_exec_coinbase() { + // Given + let (native_token, kakarot_core) = setup_contracts_for_testing(); + let sequencer_eoa = kakarot_core.deploy_eoa(evm_address()); + + // And + let mut machine = MachineBuilderTestTrait::new_with_presets().build(); + + // When + set_sequencer_address(sequencer_eoa); + machine.exec_coinbase().unwrap(); + + let sequencer_address = machine.stack.pop().unwrap(); + + // Then + assert(evm_address().address.into() == sequencer_address, 'wrong sequencer_address'); +}