diff --git a/crates/contracts/src/kakarot_core/interface.cairo b/crates/contracts/src/kakarot_core/interface.cairo index c65041807..0e0fa10cc 100644 --- a/crates/contracts/src/kakarot_core/interface.cairo +++ b/crates/contracts/src/kakarot_core/interface.cairo @@ -9,9 +9,6 @@ pub trait IKakarotCore { /// Gets the native token used by the Kakarot smart contract fn native_token(self: @TContractState) -> ContractAddress; - /// Get the chain id - fn chain_id(self: @TContractState) -> u128; - /// Deterministically computes a Starknet address for an given EVM address /// The address is computed as the Starknet address corresponding to the deployment of an EOA, /// Using its EVM address as salt, and KakarotCore as deployer. @@ -63,9 +60,6 @@ pub trait IExtendedKakarotCore { /// Gets the native token used by the Kakarot smart contract fn native_token(self: @TContractState) -> ContractAddress; - /// Get the chain id - fn chain_id(self: @TContractState) -> u128; - /// Deterministically computes a Starknet address for an given EVM address /// The address is computed as the Starknet address corresponding to the deployment of an EOA, /// Using its EVM address as salt, and KakarotCore as deployer. diff --git a/crates/contracts/src/kakarot_core/kakarot.cairo b/crates/contracts/src/kakarot_core/kakarot.cairo index d7a895583..c0bdfb1aa 100644 --- a/crates/contracts/src/kakarot_core/kakarot.cairo +++ b/crates/contracts/src/kakarot_core/kakarot.cairo @@ -60,7 +60,6 @@ pub mod KakarotCore { Kakarot_base_fee: u128, Kakarot_prev_randao: u256, Kakarot_block_gas_limit: u128, - chain_id: u128, // Components #[substorage(v0)] ownable: ownable_component::Storage, @@ -105,19 +104,20 @@ pub mod KakarotCore { #[constructor] fn constructor( ref self: ContractState, + owner: ContractAddress, native_token: ContractAddress, - uninitialized_account_class_hash: ClassHash, account_contract_class_hash: ClassHash, - owner: ContractAddress, - chain_id: u128, + uninitialized_account_class_hash: ClassHash, + coinbase: EthAddress, + block_gas_limit: u128, mut eoas_to_deploy: Span, ) { + self.ownable.initializer(owner); self.Kakarot_native_token_address.write(native_token); - self.Kakarot_uninitialized_account_class_hash.write(uninitialized_account_class_hash); self.Kakarot_account_contract_class_hash.write(account_contract_class_hash); - self.ownable.initializer(owner); - self.chain_id.write(chain_id); - + self.Kakarot_uninitialized_account_class_hash.write(uninitialized_account_class_hash); + self.Kakarot_coinbase.write(coinbase); + self.Kakarot_block_gas_limit.write(block_gas_limit); loop { match eoas_to_deploy.pop_front() { Option::Some(eoa_address) => self.deploy_eoa(*eoa_address), @@ -137,10 +137,6 @@ pub mod KakarotCore { self.Kakarot_native_token_address.read() } - fn chain_id(self: @ContractState) -> u128 { - self.chain_id.read() - } - fn compute_starknet_address( self: @ContractState, evm_address: EthAddress ) -> ContractAddress { diff --git a/crates/contracts/src/tests/test_eoa.cairo b/crates/contracts/src/tests/test_eoa.cairo index a01988153..833a98676 100644 --- a/crates/contracts/src/tests/test_eoa.cairo +++ b/crates/contracts/src/tests/test_eoa.cairo @@ -72,9 +72,7 @@ mod test_external_owned_account { let data_get_tx = array![0x6d, 0x4c, 0xe6, 0x3c].span(); // check counter value is 0 before doing inc - let tx = call_transaction( - kakarot_core.chain_id(), Option::Some(other_evm_address()), data_get_tx - ); + let tx = call_transaction(chain_id(), Option::Some(other_evm_address()), data_get_tx); let (_, return_data) = kakarot_core .eth_call(origin: evm_address, tx: EthereumTransaction::LegacyTransaction(tx),); @@ -104,9 +102,7 @@ mod test_external_owned_account { assert_ne!(event.gas_used, 0); // check counter value has increased - let tx = call_transaction( - kakarot_core.chain_id(), Option::Some(other_evm_address()), data_get_tx - ); + let tx = call_transaction(chain_id(), Option::Some(other_evm_address()), data_get_tx); let (_, return_data) = kakarot_core .eth_call(origin: evm_address, tx: EthereumTransaction::LegacyTransaction(tx),); assert_eq!(return_data, u256_to_bytes_array(1).span()); diff --git a/crates/contracts/src/tests/test_kakarot_core.cairo b/crates/contracts/src/tests/test_kakarot_core.cairo index db0db205a..52cb99bf5 100644 --- a/crates/contracts/src/tests/test_kakarot_core.cairo +++ b/crates/contracts/src/tests/test_kakarot_core.cairo @@ -21,7 +21,7 @@ use core::option::OptionTrait; use core::traits::TryInto; use evm::model::{Address}; -use evm::tests::test_utils::sequencer_evm_address; +use evm::tests::test_utils::{sequencer_evm_address, chain_id}; use evm::tests::test_utils; use starknet::{testing, contract_address_const, ContractAddress, EthAddress, ClassHash}; use utils::eth_transaction::{EthereumTransaction, EthereumTransactionTrait, LegacyTransaction}; @@ -58,7 +58,7 @@ fn test_kakarot_core_renounce_ownership() { fn test_kakarot_core_chain_id() { let (_, kakarot_core) = contract_utils::setup_contracts_for_testing(); - assert(kakarot_core.chain_id() == contract_utils::chain_id(), 'wrong chain id'); + assert(chain_id() == contract_utils::chain_id(), 'wrong chain id'); } #[test] @@ -163,7 +163,7 @@ fn test_eth_send_transaction_non_deploy_tx() { // check counter value is 0 before doing inc let tx = contract_utils::call_transaction( - kakarot_core.chain_id(), Option::Some(counter_address), data_get_tx + chain_id(), Option::Some(counter_address), data_get_tx ); let (_, return_data) = kakarot_core .eth_call(origin: evm_address, tx: EthereumTransaction::LegacyTransaction(tx)); @@ -177,7 +177,7 @@ fn test_eth_send_transaction_non_deploy_tx() { testing::set_contract_address(eoa); let tx = LegacyTransaction { - chain_id: kakarot_core.chain_id(), + chain_id: chain_id(), nonce: 0, destination: Option::Some(counter_address), amount: value, @@ -196,7 +196,7 @@ fn test_eth_send_transaction_non_deploy_tx() { // check counter value is 1 let tx = contract_utils::call_transaction( - kakarot_core.chain_id(), Option::Some(counter_address), data_get_tx + chain_id(), Option::Some(counter_address), data_get_tx ); let (_, _) = kakarot_core .eth_call(origin: evm_address, tx: EthereumTransaction::LegacyTransaction(tx)); @@ -226,7 +226,7 @@ fn test_eth_call() { let calldata = array![0x6d, 0x4c, 0xe6, 0x3c].span(); // When - let tx = contract_utils::call_transaction(kakarot_core.chain_id(), to, calldata); + let tx = contract_utils::call_transaction(chain_id(), to, calldata); let (success, return_data) = kakarot_core .eth_call(origin: evm_address, tx: EthereumTransaction::LegacyTransaction(tx)); @@ -245,7 +245,7 @@ fn test_process_transaction() { contract_utils::fund_account_with_native_token( eoa, native_token, 0xfffffffffffffffffffffffffff ); - let chain_id = kakarot_core.chain_id(); + let chain_id = chain_id(); let _account = contract_utils::deploy_contract_account( test_utils::other_evm_address(), counter_evm_bytecode() @@ -294,7 +294,7 @@ fn test_eth_send_transaction_deploy_tx() { // When // Set the contract address to the EOA address, so that the caller of the `eth_send_transaction` is an eoa let tx = LegacyTransaction { - chain_id: kakarot_core.chain_id(), + chain_id: chain_id(), nonce: 0, destination: Option::None, amount: value, @@ -325,7 +325,7 @@ fn test_eth_send_transaction_deploy_tx() { // No need to set address back to eoa, as eth_call doesn't use the caller address. let tx = LegacyTransaction { - chain_id: kakarot_core.chain_id(), + chain_id: chain_id(), nonce: 0, destination: to, amount: value, diff --git a/crates/contracts/src/tests/test_utils.cairo b/crates/contracts/src/tests/test_utils.cairo index 99da8e275..ddc92881d 100644 --- a/crates/contracts/src/tests/test_utils.cairo +++ b/crates/contracts/src/tests/test_utils.cairo @@ -17,6 +17,7 @@ use starknet::{ testing, contract_address_const, EthAddress, ContractAddress, deploy_syscall, get_contract_address }; +use utils::constants::BLOCK_GAS_LIMIT; use utils::eth_transaction::LegacyTransaction; /// Pop the earliest unpopped logged event for the contract as the requested type @@ -97,11 +98,12 @@ fn deploy_kakarot_core( native_token: ContractAddress, mut eoas: Span ) -> IExtendedKakarotCoreDispatcher { let mut calldata: Array = array![ + other_starknet_address().into(), native_token.into(), - UninitializedAccount::TEST_CLASS_HASH.try_into().unwrap(), AccountContract::TEST_CLASS_HASH.try_into().unwrap(), - other_starknet_address().into(), - chain_id().into(), + UninitializedAccount::TEST_CLASS_HASH.try_into().unwrap(), + 'coinbase', + BLOCK_GAS_LIMIT.into(), ]; Serde::serialize(@eoas, ref calldata); @@ -167,6 +169,7 @@ pub(crate) fn setup_contracts_for_testing() -> ( let kakarot_core = deploy_kakarot_core( native_token.contract_address, array![sequencer_evm_address()].span() ); + // We drop the first event of Kakarot Core, as it is the initializer from Ownable, // triggered in the constructor. drop_event(kakarot_core.contract_address); diff --git a/crates/evm/src/backend/starknet_backend.cairo b/crates/evm/src/backend/starknet_backend.cairo index 874185c82..fb675636a 100644 --- a/crates/evm/src/backend/starknet_backend.cairo +++ b/crates/evm/src/backend/starknet_backend.cairo @@ -1,5 +1,11 @@ use contracts::account_contract::{IAccountDispatcher, IAccountDispatcherTrait}; -use contracts::kakarot_core::{KakarotCore, KakarotCore::KakarotCoreImpl}; +use contracts::kakarot_core::{ + KakarotCore, KakarotCore::KakarotCoreImpl, + KakarotCore::{ + Kakarot_prev_randaoContractMemberStateTrait, Kakarot_coinbaseContractMemberStateTrait, + Kakarot_block_gas_limitContractMemberStateTrait, Kakarot_base_feeContractMemberStateTrait + } +}; use core::num::traits::zero::Zero; use evm::errors::{ensure, EVMError, EOA_EXISTS}; use evm::model::{Address, AddressTrait, Environment}; @@ -54,11 +60,8 @@ fn get_bytecode(evm_address: EthAddress) -> Span { } fn get_env(origin: EthAddress, gas_price: u128) -> Environment { + let kakarot_state = KakarotCore::unsafe_new_contract_state(); let block_info = get_block_info().unbox(); - //TODO(optimization): the coinbase account is deployed from a specific `evm_address` which is specified upon deployment - // and specific to the chain. Rather than reading from a contract, we could directly use this constant. - let coinbase = IAccountDispatcher { contract_address: block_info.sequencer_address } - .get_evm_address(); // tx.gas_price and env.gas_price have the same values here // - this is not always true in EVM transactions @@ -66,11 +69,12 @@ fn get_env(origin: EthAddress, gas_price: u128) -> Environment { origin: origin, gas_price, chain_id: get_tx_info().unbox().chain_id.try_into().unwrap(), - prevrandao: 0, + prevrandao: kakarot_state.Kakarot_prev_randao.read(), block_number: block_info.block_number, - block_timestamp: block_info.block_timestamp, block_gas_limit: constants::BLOCK_GAS_LIMIT, - coinbase, + block_timestamp: block_info.block_timestamp, + coinbase: kakarot_state.Kakarot_coinbase.read(), + base_fee: kakarot_state.Kakarot_base_fee.read(), state: Default::default(), } } diff --git a/crates/evm/src/model.cairo b/crates/evm/src/model.cairo index 9384736f1..473b8535e 100644 --- a/crates/evm/src/model.cairo +++ b/crates/evm/src/model.cairo @@ -24,6 +24,7 @@ struct Environment { block_gas_limit: u128, block_timestamp: u64, coinbase: EthAddress, + base_fee: u128, state: State } #[derive(Copy, Drop, Default, PartialEq, Debug)] diff --git a/crates/evm/src/tests/test_utils.cairo b/crates/evm/src/tests/test_utils.cairo index 2d0774cc2..41beba7bd 100644 --- a/crates/evm/src/tests/test_utils.cairo +++ b/crates/evm/src/tests/test_utils.cairo @@ -212,6 +212,7 @@ fn preset_environment() -> Environment { block_timestamp: block_info.block_timestamp, block_gas_limit: constants::BLOCK_GAS_LIMIT, coinbase: coinbase(), + base_fee: 0, state: Default::default(), } }