Skip to content

Commit

Permalink
refactor Kakarot interface (#776)
Browse files Browse the repository at this point in the history
* refactor Kakarot interface

* fix

* fix tests

* remove unused variable
  • Loading branch information
TAdev0 authored Apr 14, 2024
1 parent 94958e3 commit 9f6ea8d
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 55 deletions.
2 changes: 1 addition & 1 deletion crates/contracts/src/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub mod AccountContract {

let kakarot = IKakarotCoreDispatcher { contract_address: kakarot_address };

let native_token = kakarot.native_token();
let native_token = kakarot.get_native_token();
// To internally perform value transfer of the network's native
// token (which conforms to the ERC20 standard), we need to give the
// KakarotCore contract infinite allowance
Expand Down
13 changes: 8 additions & 5 deletions crates/contracts/src/kakarot_core/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait IKakarotCore<TContractState> {
fn set_native_token(ref self: TContractState, native_token: ContractAddress);

/// Gets the native token used by the Kakarot smart contract
fn native_token(self: @TContractState) -> ContractAddress;
fn get_native_token(self: @TContractState) -> ContractAddress;

/// 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,
Expand All @@ -19,9 +19,10 @@ pub trait IKakarotCore<TContractState> {
/// otherwise returns 0
fn address_registry(self: @TContractState, evm_address: EthAddress) -> ContractAddress;


/// Deploys an EOA for a particular EVM address
fn deploy_eoa(ref self: TContractState, evm_address: EthAddress) -> ContractAddress;
fn deploy_externally_owned_account(
ref self: TContractState, evm_address: EthAddress
) -> ContractAddress;

/// View entrypoint into the EVM
/// Performs view calls into the blockchain
Expand Down Expand Up @@ -58,7 +59,7 @@ pub trait IExtendedKakarotCore<TContractState> {
fn set_native_token(ref self: TContractState, native_token: ContractAddress);

/// Gets the native token used by the Kakarot smart contract
fn native_token(self: @TContractState) -> ContractAddress;
fn get_native_token(self: @TContractState) -> ContractAddress;

/// 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,
Expand All @@ -71,7 +72,9 @@ pub trait IExtendedKakarotCore<TContractState> {
fn address_registry(self: @TContractState, evm_address: EthAddress) -> ContractAddress;

/// Deploys an EOA for a particular EVM address
fn deploy_eoa(ref self: TContractState, evm_address: EthAddress) -> ContractAddress;
fn deploy_externally_owned_account(
ref self: TContractState, evm_address: EthAddress
) -> ContractAddress;

/// View entrypoint into the EVM
/// Performs view calls into the blockchain
Expand Down
9 changes: 5 additions & 4 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub mod KakarotCore {
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),
Option::Some(eoa_address) => self.deploy_externally_owned_account(*eoa_address),
Option::None => { break; },
};
}
Expand All @@ -133,7 +133,7 @@ pub mod KakarotCore {
self.Kakarot_native_token_address.write(native_token);
}

fn native_token(self: @ContractState) -> ContractAddress {
fn get_native_token(self: @ContractState) -> ContractAddress {
self.Kakarot_native_token_address.read()
}

Expand All @@ -150,7 +150,9 @@ pub mod KakarotCore {
self.Kakarot_evm_to_starknet_address.read(evm_address)
}

fn deploy_eoa(ref self: ContractState, evm_address: EthAddress) -> ContractAddress {
fn deploy_externally_owned_account(
ref self: ContractState, evm_address: EthAddress
) -> ContractAddress {
starknet_backend::deploy(evm_address).expect('EOA Deployment failed').starknet
}

Expand Down Expand Up @@ -215,7 +217,6 @@ pub mod KakarotCore {
let existing_address = self.Kakarot_evm_to_starknet_address.read(evm_address);
assert(existing_address.is_zero(), 'Account already exists');

let caller = get_caller_address();
let starknet_address = self.compute_starknet_address(evm_address);
//TODO: enable this assertion. Will require changing test runner to snfoundry
// assert!(starknet_address == caller, "Account must be registered by the caller");
Expand Down
2 changes: 1 addition & 1 deletion crates/contracts/src/tests/test_contract_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use evm::tests::test_utils::{ca_address, native_token};

#[test]
fn test_ca_deploy() {
let (_, kakarot_core) = setup_contracts_for_testing();
setup_contracts_for_testing();
let ca_address = deploy_contract_account(ca_address(), Default::default().span());
let contract_account = IAccountDispatcher { contract_address: ca_address.starknet };

Expand Down
18 changes: 8 additions & 10 deletions crates/contracts/src/tests/test_eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod test_external_owned_account {
let (native_token, kakarot_core) = setup_contracts_for_testing();

let evm_address = evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
// pop ownership transfer event
core::starknet::testing::pop_log_raw(eoa);
fund_account_with_native_token(eoa, native_token, 0xfffffffffffffffffffffffffff);
Expand Down Expand Up @@ -93,8 +93,6 @@ mod test_external_owned_account {
let result = eoa_contract.__execute__(array![call]);
assert_eq!(result.len(), 1);

let tx_info = get_tx_info().unbox();

let event = pop_log_debug::<TransactionExecuted>(eoa).unwrap();

assert_eq!(event.response, *result.span()[0]);
Expand Down Expand Up @@ -125,7 +123,7 @@ mod test_external_owned_account {
fn test___validate__fail__caller_not_0() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address = evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_contract_address(other_starknet_address());
Expand All @@ -139,7 +137,7 @@ mod test_external_owned_account {
fn test___validate__fail__call_data_len_not_1() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address = evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_contract_address(contract_address_const::<0>());
Expand All @@ -153,7 +151,7 @@ mod test_external_owned_account {
fn test___validate__fail__to_address_not_kakarot_core() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address = evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

// to reproduce locally:
Expand Down Expand Up @@ -185,7 +183,7 @@ mod test_external_owned_account {
fn test___validate__fail__selector_not_eth_send_transaction() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address = evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_chain_id(chain_id().into());
Expand Down Expand Up @@ -218,7 +216,7 @@ mod test_external_owned_account {
fn test___validate__legacy_transaction() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address: EthAddress = 0xaA36F24f65b5F0f2c642323f3d089A3F0f2845Bf_u256.into();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_chain_id(chain_id().into());
Expand Down Expand Up @@ -253,7 +251,7 @@ mod test_external_owned_account {
fn test___validate__eip_2930_transaction() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address: EthAddress = 0xaA36F24f65b5F0f2c642323f3d089A3F0f2845Bf_u256.into();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_chain_id(chain_id().into());
Expand Down Expand Up @@ -289,7 +287,7 @@ mod test_external_owned_account {
fn test___validate__eip_1559_transaction() {
let (_, kakarot_core) = setup_contracts_for_testing();
let evm_address: EthAddress = 0xaA36F24f65b5F0f2c642323f3d089A3F0f2845Bf_u256.into();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
let eoa_contract = IAccountDispatcher { contract_address: eoa };

set_chain_id(chain_id().into());
Expand Down
24 changes: 13 additions & 11 deletions crates/contracts/src/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use contracts::account_contract::{
IAccountDispatcher, IAccountDispatcherTrait, AccountContract::TEST_CLASS_HASH
};
use contracts::kakarot_core::interface::IKakarotCore;
use contracts::kakarot_core::interface::{
IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait
};
Expand Down Expand Up @@ -56,7 +55,7 @@ fn test_kakarot_core_renounce_ownership() {

#[test]
fn test_kakarot_core_chain_id() {
let (_, kakarot_core) = contract_utils::setup_contracts_for_testing();
contract_utils::setup_contracts_for_testing();

assert(chain_id() == contract_utils::chain_id(), 'wrong chain id');
}
Expand All @@ -65,19 +64,21 @@ fn test_kakarot_core_chain_id() {
fn test_kakarot_core_set_native_token() {
let (native_token, kakarot_core) = contract_utils::setup_contracts_for_testing();

assert(kakarot_core.native_token() == native_token.contract_address, 'wrong native_token');
assert(kakarot_core.get_native_token() == native_token.contract_address, 'wrong native_token');

testing::set_contract_address(test_utils::other_starknet_address());
kakarot_core.set_native_token(contract_address_const::<0xdead>());
assert(
kakarot_core.native_token() == contract_address_const::<0xdead>(), 'wrong new native_token'
kakarot_core.get_native_token() == contract_address_const::<0xdead>(),
'wrong new native_token'
);
}

#[test]
fn test_kakarot_core_deploy_eoa() {
let (_, kakarot_core) = contract_utils::setup_contracts_for_testing();
let eoa_starknet_address = kakarot_core.deploy_eoa(test_utils::evm_address());
let eoa_starknet_address = kakarot_core
.deploy_externally_owned_account(test_utils::evm_address());

let event = contract_utils::pop_log::<
KakarotCore::AccountDeployed
Expand All @@ -95,7 +96,8 @@ fn test_kakarot_core_eoa_mapping() {
'should be uninitialized'
);

let expected_eoa_starknet_address = kakarot_core.deploy_eoa(test_utils::evm_address());
let expected_eoa_starknet_address = kakarot_core
.deploy_externally_owned_account(test_utils::evm_address());

// When
let address = kakarot_core.address_registry(test_utils::evm_address());
Expand All @@ -116,7 +118,7 @@ fn test_kakarot_core_eoa_mapping() {
fn test_kakarot_core_compute_starknet_address() {
let evm_address = test_utils::evm_address();
let (_, kakarot_core) = contract_utils::setup_contracts_for_testing();
let expected_starknet_address = kakarot_core.deploy_eoa(evm_address);
let expected_starknet_address = kakarot_core.deploy_externally_owned_account(evm_address);

let actual_starknet_address = kakarot_core.compute_starknet_address(evm_address);
assert_eq!(actual_starknet_address, expected_starknet_address);
Expand Down Expand Up @@ -145,7 +147,7 @@ fn test_eth_send_transaction_non_deploy_tx() {
let (native_token, kakarot_core) = contract_utils::setup_contracts_for_testing();

let evm_address = test_utils::evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
contract_utils::fund_account_with_native_token(
eoa, native_token, 0xfffffffffffffffffffffffffff
);
Expand Down Expand Up @@ -213,7 +215,7 @@ fn test_eth_call() {
let (_, kakarot_core) = contract_utils::setup_contracts_for_testing();

let evm_address = test_utils::evm_address();
kakarot_core.deploy_eoa(evm_address);
kakarot_core.deploy_externally_owned_account(evm_address);

let account = contract_utils::deploy_contract_account(
test_utils::other_evm_address(), counter_evm_bytecode()
Expand Down Expand Up @@ -241,7 +243,7 @@ fn test_process_transaction() {
let (native_token, kakarot_core) = contract_utils::setup_contracts_for_testing();

let evm_address = test_utils::evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
contract_utils::fund_account_with_native_token(
eoa, native_token, 0xfffffffffffffffffffffffffff
);
Expand Down Expand Up @@ -282,7 +284,7 @@ fn test_eth_send_transaction_deploy_tx() {
let (native_token, kakarot_core) = contract_utils::setup_contracts_for_testing();

let evm_address = test_utils::evm_address();
let eoa = kakarot_core.deploy_eoa(evm_address);
let eoa = kakarot_core.deploy_externally_owned_account(evm_address);
contract_utils::fund_account_with_native_token(
eoa, native_token, 0xfffffffffffffffffffffffffff
);
Expand Down
3 changes: 1 addition & 2 deletions crates/contracts/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn deploy_native_token() -> IERC20CamelDispatcher {
Result::Err(err) => panic(err)
}
}

fn deploy_kakarot_core(
native_token: ContractAddress, mut eoas: Span<EthAddress>
) -> IExtendedKakarotCoreDispatcher {
Expand Down Expand Up @@ -127,7 +128,6 @@ pub(crate) fn deploy_contract_account(evm_address: EthAddress, bytecode: Span<u8
ca_address
}


fn deploy_eoa(eoa_address: EthAddress) -> IAccountDispatcher {
let kakarot_address = get_contract_address();
let calldata: Span<felt252> = array![kakarot_address.into(), eoa_address.into()].span();
Expand All @@ -152,7 +152,6 @@ fn call_transaction(
}
}


fn fund_account_with_native_token(
contract_address: ContractAddress, native_token: IERC20CamelDispatcher, amount: u256,
) {
Expand Down
10 changes: 7 additions & 3 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ mod internals {
return;
}

let account = IAccountDispatcher { contract_address: self.starknet_address() };
let account = IAccountDispatcher {
contract_address: self.get_registered_starknet_address()
};
account.write_bytecode(self.bytecode());
account.set_nonce(*self.nonce);

Expand All @@ -172,7 +174,9 @@ mod internals {
return;
}

let account = IAccountDispatcher { contract_address: self.starknet_address() };
let account = IAccountDispatcher {
contract_address: self.get_registered_starknet_address()
};

account.set_nonce(*self.nonce);
//TODO: handle storage commitment
Expand All @@ -188,7 +192,7 @@ mod internals {
/// Iterates through the list of pending transfer and triggers them
fn transfer_native_token(ref self: State) -> Result<(), EVMError> {
let kakarot_state = KakarotCore::unsafe_new_contract_state();
let native_token = kakarot_state.native_token();
let native_token = kakarot_state.get_native_token();
loop {
match self.transfers.pop_front() {
Option::Some(transfer) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl AddressImpl of AddressTrait {

fn fetch_balance(self: @Address) -> u256 {
let kakarot_state = KakarotCore::unsafe_new_contract_state();
let native_token_address = kakarot_state.native_token();
let native_token_address = kakarot_state.get_native_token();
let native_token = IERC20CamelDispatcher { contract_address: native_token_address };
native_token.balanceOf(*self.starknet)
}
Expand Down
13 changes: 7 additions & 6 deletions crates/evm/src/model/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,13 @@ impl AccountImpl of AccountTrait {
let calldata: Span<felt252> = array![kakarot_address.into(), self.address().evm.into()]
.span();

let (starknet_address, _) = deploy_syscall(
uninitialized_account_class_hash, self.address().evm.into(), calldata, true
)
deploy_syscall(uninitialized_account_class_hash, self.address().evm.into(), calldata, true)
.unwrap_syscall();
}

fn commit_storage(self: @Account, key: u256, value: u256) {
IAccountDispatcher { contract_address: self.starknet_address() }.write_storage(key, value);
IAccountDispatcher { contract_address: self.get_registered_starknet_address() }
.write_storage(key, value);
}


Expand Down Expand Up @@ -199,7 +198,7 @@ impl AccountImpl of AccountTrait {
}

#[inline(always)]
fn starknet_address(self: @Account) -> ContractAddress {
fn get_registered_starknet_address(self: @Account) -> ContractAddress {
*self.address.starknet
}

Expand Down Expand Up @@ -237,7 +236,9 @@ impl AccountImpl of AccountTrait {
/// * `value` - The value to set
#[inline(always)]
fn store_storage(self: @Account, key: u256, value: u256) {
let mut contract_account = IAccountDispatcher { contract_address: self.starknet_address() };
let mut contract_account = IAccountDispatcher {
contract_address: self.get_registered_starknet_address()
};
contract_account.write_storage(key, value);
}

Expand Down
1 change: 0 additions & 1 deletion crates/evm/src/model/eoa.cairo

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn test_gaslimit() {
fn test_exec_selfbalance_eoa() {
// Given
let (native_token, kakarot_core) = setup_contracts_for_testing();
let eoa = kakarot_core.deploy_eoa(evm_address());
let eoa = kakarot_core.deploy_externally_owned_account(evm_address());

fund_account_with_native_token(eoa, native_token, 0x1);

Expand Down
Loading

0 comments on commit 9f6ea8d

Please sign in to comment.