Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set code hash in pre state #731

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ef-testing/src/evm_sequencer/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub mod kkrt_account {
_evm_address: &Address,
_code: &Bytes,
_nonce: U256,
_balance: U256,
_evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
Ok(Self {
Expand Down
25 changes: 24 additions & 1 deletion crates/ef-testing/src/evm_sequencer/account/v0.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use blockifier::abi::{abi_utils::get_storage_var_address, sierra_types::next_storage_key};
use reth_primitives::alloy_primitives::keccak256;
use reth_primitives::KECCAK_EMPTY;
use reth_primitives::{Address, U256};

use revm_interpreter::analysis::to_analysed;
use revm_primitives::{Bytecode, Bytes};
use starknet_api::{core::Nonce, state::StorageKey, StarknetApiError};
use starknet_crypto::Felt;

use super::{pack_byte_array_to_starkfelt_array, KakarotAccount};
use crate::evm_sequencer::constants::storage_variables::{
ACCOUNT_BYTECODE_LEN, ACCOUNT_EVM_ADDRESS, ACCOUNT_IS_INITIALIZED,
ACCOUNT_BYTECODE_LEN, ACCOUNT_CODE_HASH, ACCOUNT_EVM_ADDRESS, ACCOUNT_IS_INITIALIZED,
ACCOUNT_JUMPDESTS_INITIALIZED, ACCOUNT_NONCE, ACCOUNT_STORAGE, ACCOUNT_VALID_JUMPDESTS,
};
use crate::evm_sequencer::{types::felt::FeltSequencer, utils::split_u256};
Expand All @@ -18,6 +21,7 @@ impl KakarotAccount {
evm_address: &Address,
code: &Bytes,
nonce: U256,
balance: U256,
evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
let nonce = Felt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
Expand Down Expand Up @@ -47,6 +51,25 @@ impl KakarotAccount {
.collect();
storage.append(&mut bytecode_storage);

// Initialize the code hash var
let account_is_empty =
code.is_empty() && nonce == Felt::from(0) && balance == U256::from(0);
let code_hash = if account_is_empty {
U256::from(0)
} else if code.is_empty() {
U256::from_be_slice(KECCAK_EMPTY.as_slice())
} else {
U256::from_be_slice(keccak256(code).as_slice())
};

let code_hash_values = split_u256(code_hash);
let code_hash_low_key = get_storage_var_address(ACCOUNT_CODE_HASH, &[]);
let code_hash_high_key = next_storage_key(&code_hash_low_key)?;
storage.append(&mut vec![
(code_hash_low_key, Felt::from(code_hash_values[0])),
(code_hash_high_key, Felt::from(code_hash_values[1])),
]);
obatirou marked this conversation as resolved.
Show resolved Hide resolved

// Initialize the bytecode jumpdests.
let bytecode = to_analysed(Bytecode::new_raw(code.clone()));
let valid_jumpdests: Vec<usize> = match bytecode {
Expand Down
1 change: 1 addition & 0 deletions crates/ef-testing/src/evm_sequencer/account/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl KakarotAccount {
evm_address: &Address,
code: &Bytes,
nonce: U256,
_balance: U256,
evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
let nonce = Felt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
Expand Down
1 change: 1 addition & 0 deletions crates/ef-testing/src/evm_sequencer/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub mod storage_variables {
pub const ACCOUNT_VALID_JUMPDESTS : &str = "Account_valid_jumpdests";
pub const ACCOUNT_JUMPDESTS_INITIALIZED : &str = "Account_jumpdests_initialized";
pub const ACCOUNT_PUBLIC_KEY: &str = "Account_public_key";
pub const ACCOUNT_CODE_HASH: &str = "Account_code_hash";

pub const KAKAROT_COINBASE: &str = "Kakarot_coinbase";
pub const KAKAROT_BASE_FEE: &str = "Kakarot_base_fee";
Expand Down
4 changes: 3 additions & 1 deletion crates/ef-testing/src/evm_sequencer/evm_state/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,12 @@ mod tests {
&TEST_CONTRACT_ADDRESS,
&contract_bytecode,
contract_nonce,
U256::ZERO,
&[],
)
.unwrap();
let eoa = KakarotAccount::new(&PUBLIC_KEY, &Bytes::default(), eoa_nonce, &[]).unwrap();
let eoa = KakarotAccount::new(&PUBLIC_KEY, &Bytes::default(), eoa_nonce, U256::ZERO, &[])
.unwrap();
sequencer.setup_account(contract).unwrap();
sequencer.setup_account(eoa).unwrap();
let execution_result = sequencer.execute_transaction(transaction);
Expand Down
14 changes: 11 additions & 3 deletions crates/ef-testing/src/evm_sequencer/evm_state/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,14 @@ mod tests {
]);

// When
let account =
KakarotAccount::new(&TEST_CONTRACT_ADDRESS, &bytecode, U256::ZERO, &[]).unwrap();
let account = KakarotAccount::new(
&TEST_CONTRACT_ADDRESS,
&bytecode,
U256::ZERO,
U256::ZERO,
&[],
)
.unwrap();
sequencer.setup_account(account).unwrap();

// Then
Expand Down Expand Up @@ -438,10 +444,12 @@ mod tests {
&TEST_CONTRACT_ADDRESS,
&contract_bytecode,
contract_nonce,
U256::ZERO,
&[],
)
.unwrap();
let eoa = KakarotAccount::new(&PUBLIC_KEY, &Bytes::default(), eoa_nonce, &[]).unwrap();
let eoa = KakarotAccount::new(&PUBLIC_KEY, &Bytes::default(), eoa_nonce, U256::ZERO, &[])
.unwrap();
sequencer.setup_account(contract).unwrap();
sequencer.setup_account(eoa).unwrap();
let execution_result = sequencer.execute_transaction(transaction);
Expand Down
1 change: 1 addition & 0 deletions crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl BlockchainTestCase {
address,
&account.code,
account.nonce,
account.balance,
&account.storage.clone().into_iter().collect::<Vec<_>>()[..],
)?;
sequencer.setup_account(kakarot_account)?;
Expand Down
Loading