Skip to content

Commit

Permalink
fix: do not store code hash for EOAs
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Nov 27, 2024
1 parent 01a7c41 commit 8efb705
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
4 changes: 0 additions & 4 deletions cairo_zero/backend/starknet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ namespace Internals {
if (starknet_account_exists == 0) {
// Deploy account
Starknet.deploy(self.address.evm);
// Commit the code hash upon deployment
// of a new account, in all cases.
// Retrieved in `fetch_or_create` in the next transaction.
IAccount.set_code_hash(starknet_address, [self.code_hash]);
tempvar syscall_ptr = syscall_ptr;
tempvar pedersen_ptr = pedersen_ptr;
tempvar range_check_ptr = range_check_ptr;
Expand Down
5 changes: 1 addition & 4 deletions cairo_zero/kakarot/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ namespace Account {
tempvar address = new model.Address(starknet=starknet_address, evm=evm_address);
let balance = fetch_balance(address);
assert balance_ptr = new Uint256(balance.low, balance.high);
// empty code hash see https://eips.ethereum.org/EIPS/eip-1052
tempvar code_hash_ptr = new Uint256(
low=Constants.EMPTY_CODE_HASH_LOW, high=Constants.EMPTY_CODE_HASH_HIGH
);
tempvar code_hash_ptr = new Uint256(0, 0);
let account = Account.init(
address=address,
code_len=0,
Expand Down
22 changes: 16 additions & 6 deletions cairo_zero/kakarot/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from starkware.cairo.common.math_cmp import is_not_zero, is_nn
from starkware.cairo.common.uint256 import Uint256, uint256_le

from kakarot.account import Account
from kakarot.constants import Constants

from kakarot.evm import EVM
from kakarot.errors import Errors
Expand Down Expand Up @@ -478,18 +479,27 @@ namespace EnvironmentalInformation {
return evm;
}

let account = State.get_account(evm_address);
let has_code_or_nonce = Account.has_code_or_nonce(account);
let account_exists = has_code_or_nonce + account.balance.low + account.balance.high;
// Relevant cases:
// https://github.com/ethereum/go-ethereum/blob/master/core/vm/instructions.go#L392
if (account_exists == FALSE) {
Stack.push_uint128(0);
let account = State.get_account(evm_address);

// If the account has code, return the code_hash stored in the account storage.
if (account.code_len != 0) {
Stack.push_uint256([account.code_hash]);
return evm;
}

Stack.push_uint256([account.code_hash]);
// If the account has no code but a balance or nonce, return the hash of the empty code hash.
if (account.nonce + account.balance.low + account.balance.high != 0) {
let empty_code_hash = Uint256(
low=Constants.EMPTY_CODE_HASH_LOW, high=Constants.EMPTY_CODE_HASH_HIGH
);
Stack.push_uint256(empty_code_hash);
return evm;
}

// Account is empty (EIP-161), return 0
Stack.push_uint128(0);
return evm;
}
}
2 changes: 1 addition & 1 deletion cairo_zero/tests/src/kakarot/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func test__fetch_original_storage__state_modified{
let starknet_address = Account.compute_starknet_address(evm_address);
tempvar address = new model.Address(starknet_address, evm_address);
let (local code: felt*) = alloc();
tempvar code_hash = new Uint256(Constants.EMPTY_CODE_HASH_LOW, Constants.EMPTY_CODE_HASH_HIGH);
tempvar code_hash = new Uint256(0, 0);
tempvar balance = new Uint256(0, 0);
let account = Account.init(address, 0, code, code_hash, 0, balance);

Expand Down

0 comments on commit 8efb705

Please sign in to comment.