Skip to content

Commit

Permalink
Refactored access-list warm for transact_create functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Oct 15, 2024
1 parent dc4e443 commit d80b2a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
2 changes: 1 addition & 1 deletion benches/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn run_loop_contract() {
.unwrap(),
// hex::decode("0f14a4060000000000000000000000000000000000000000000000000000000000002ee0").unwrap(),
u64::MAX,
Vec::new(),
&[],
);
}

Expand Down
6 changes: 3 additions & 3 deletions evm-tests/jsontests/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ fn test_run(
);
executor.state_mut().withdraw(caller, total_fee).unwrap();

let access_list = transaction
let access_list: Vec<(H160, Vec<H256>)> = transaction
.access_list
.into_iter()
.map(|(address, keys)| (address.0, keys.into_iter().map(|k| k.0).collect()))
Expand All @@ -1178,7 +1178,7 @@ fn test_run(
value,
data,
gas_limit,
access_list,
&access_list,
);
assert_call_exit_exception(&state.expect_exception);
}
Expand All @@ -1191,7 +1191,7 @@ fn test_run(
value,
code,
gas_limit,
access_list,
&access_list,
);
if check_create_exit_reason(
&reason.0,
Expand Down
76 changes: 31 additions & 45 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,14 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
value: U256,
init_code: Vec<u8>,
gas_limit: u64,
access_list: Vec<(H160, Vec<H256>)>, // See EIP-2930
access_list: &[(H160, Vec<H256>)], // See EIP-2930
) -> (ExitReason, Vec<u8>) {
if self.nonce(caller) >= U64_MAX {
return (ExitError::MaxNonce.into(), Vec::new());
}

let address = self.create_address(CreateScheme::Legacy { caller });

event!(TransactCreate {
caller,
value,
init_code: &init_code,
gas_limit,
address,
address: self.create_address(CreateScheme::Legacy { caller }),
});

if let Some(limit) = self.config.max_initcode_size {
Expand All @@ -540,12 +534,12 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
}
}

if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) {
if let Err(e) = self.record_create_transaction_cost(&init_code, access_list) {
return emit_exit!(e.into(), Vec::new());
}

// Initialize initial addresses for EIP-2929
self.initialize_addresses(caller, address, access_list);
self.initialize_addresses(&[caller], access_list);

match self.create_inner(
caller,
Expand Down Expand Up @@ -574,24 +568,22 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
value: U256,
init_code: Vec<u8>,
gas_limit: u64,
access_list: Vec<(H160, Vec<H256>)>, // See EIP-2930
access_list: &[(H160, Vec<H256>)], // See EIP-2930
) -> (ExitReason, Vec<u8>) {
let address = self.create_address(CreateScheme::Fixed(address));

event!(TransactCreate {
caller,
value,
init_code: &init_code,
gas_limit,
address
address: self.create_address(CreateScheme::Fixed(address)),
});

if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) {
return emit_exit!(e.into(), Vec::new());
}

// Initialize initial addresses for EIP-2929
self.initialize_addresses(caller, address, access_list);
self.initialize_addresses(&[caller], access_list);

match self.create_inner(
caller,
Expand Down Expand Up @@ -619,36 +611,35 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
init_code: Vec<u8>,
salt: H256,
gas_limit: u64,
access_list: Vec<(H160, Vec<H256>)>, // See EIP-2930
access_list: &[(H160, Vec<H256>)], // See EIP-2930
) -> (ExitReason, Vec<u8>) {
if let Some(limit) = self.config.max_initcode_size {
if init_code.len() > limit {
self.state.metadata_mut().gasometer.fail();
return emit_exit!(ExitError::CreateContractLimit.into(), Vec::new());
}
}

let code_hash = H256::from_slice(Keccak256::digest(&init_code).as_slice());
let address = self.create_address(CreateScheme::Create2 {
caller,
code_hash,
salt,
});
event!(TransactCreate2 {
caller,
value,
init_code: &init_code,
salt,
gas_limit,
address,
address: self.create_address(CreateScheme::Create2 {
caller,
code_hash,
salt,
}),
});

if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) {
if let Some(limit) = self.config.max_initcode_size {
if init_code.len() > limit {
self.state.metadata_mut().gasometer.fail();
return emit_exit!(ExitError::CreateContractLimit.into(), Vec::new());
}
}

if let Err(e) = self.record_create_transaction_cost(&init_code, access_list) {
return emit_exit!(e.into(), Vec::new());
}

// Initialize initial addresses for EIP-2929
self.initialize_addresses(caller, address, access_list);
self.initialize_addresses(&[caller], access_list);

match self.create_inner(
caller,
Expand Down Expand Up @@ -685,7 +676,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
value: U256,
data: Vec<u8>,
gas_limit: u64,
access_list: Vec<(H160, Vec<H256>)>,
access_list: &[(H160, Vec<H256>)],
) -> (ExitReason, Vec<u8>) {
event!(TransactCall {
caller,
Expand All @@ -699,15 +690,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
return (ExitError::MaxNonce.into(), Vec::new());
}

let transaction_cost = gasometer::call_transaction_cost(&data, &access_list);
let transaction_cost = gasometer::call_transaction_cost(&data, access_list);
let gasometer = &mut self.state.metadata_mut().gasometer;
match gasometer.record_transaction(transaction_cost) {
Ok(()) => (),
Err(e) => return emit_exit!(e.into(), Vec::new()),
}

// Initialize initial addresses for EIP-2929
self.initialize_addresses(caller, address, access_list);
self.initialize_addresses(&[caller, address], access_list);

if let Err(e) = self.state.inc_nonce(caller) {
return (e.into(), Vec::new());
Expand Down Expand Up @@ -801,33 +792,28 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet>
}
}

pub fn initialize_with_access_list(&mut self, access_list: Vec<(H160, Vec<H256>)>) {
pub fn initialize_with_access_list(&mut self, access_list: &[(H160, Vec<H256>)]) {
let addresses = access_list.iter().map(|a| a.0);
self.state.metadata_mut().access_addresses(addresses);

let storage_keys = access_list
.into_iter()
.flat_map(|(address, keys)| keys.into_iter().map(move |key| (address, key)));
.iter()
.flat_map(|(address, keys)| keys.iter().map(move |key| (*address, *key)));
self.state.metadata_mut().access_storages(storage_keys);
}

fn initialize_addresses(
&mut self,
caller: H160,
address: H160,
access_list: Vec<(H160, Vec<H256>)>,
) {
fn initialize_addresses(&mut self, addresses: &[H160], access_list: &[(H160, Vec<H256>)]) {
if self.config.increase_state_access_gas {
if self.config.warm_coinbase_address {
// Warm coinbase address for EIP-3651
let coinbase = self.block_coinbase();
self.state
.metadata_mut()
.access_addresses([caller, address, coinbase].iter().copied());
.access_addresses(addresses.iter().copied().chain(Some(coinbase)));
} else {
self.state
.metadata_mut()
.access_addresses([caller, address].iter().copied());
.access_addresses(addresses.iter().copied());
};

self.initialize_with_access_list(access_list);
Expand Down

0 comments on commit d80b2a7

Please sign in to comment.