Skip to content

Commit

Permalink
feat: add extra constructor args (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Apr 13, 2024
1 parent a4aaabd commit 94958e3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 44 deletions.
6 changes: 0 additions & 6 deletions crates/contracts/src/kakarot_core/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ pub trait IKakarotCore<TContractState> {
/// 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.
Expand Down Expand Up @@ -63,9 +60,6 @@ pub trait IExtendedKakarotCore<TContractState> {
/// 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.
Expand Down
20 changes: 8 additions & 12 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<EthAddress>,
) {
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),
Expand All @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions crates/contracts/src/tests/test_eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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),);
Expand Down Expand Up @@ -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());
Expand Down
18 changes: 9 additions & 9 deletions crates/contracts/src/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand All @@ -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));
Expand Down Expand Up @@ -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));

Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions crates/contracts/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,11 +98,12 @@ fn deploy_kakarot_core(
native_token: ContractAddress, mut eoas: Span<EthAddress>
) -> IExtendedKakarotCoreDispatcher {
let mut calldata: Array<felt252> = 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);
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -54,23 +60,21 @@ fn get_bytecode(evm_address: EthAddress) -> Span<u8> {
}

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
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(),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down

0 comments on commit 94958e3

Please sign in to comment.