Skip to content

Commit

Permalink
add code_size/code_hash fn in StackState trait ref: rust-ethereum/evm…
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed Jan 12, 2024
1 parent 5655b15 commit 2e362fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
12 changes: 5 additions & 7 deletions modules/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use crate::{
runner::{
state::{Accessed, CustomStackState, StackExecutor, StackState as StackStateT, StackSubstateMetadata},
state::{Accessed, StackExecutor, StackState as StackStateT, StackSubstateMetadata},
Runner as RunnerT, RunnerExtended,
},
AccountStorages, BalanceOf, CallInfo, Config, CreateInfo, Error, ExecutionInfo, Pallet, STORAGE_SIZE,
Expand Down Expand Up @@ -911,14 +911,12 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config> for SubstrateStackState
self.substate
.recursive_is_cold(&|a: &Accessed| a.accessed_storage.contains(&(address, key)))
}
}

impl<'vicinity, 'config, T: Config> CustomStackState for SubstrateStackState<'vicinity, 'config, T> {
fn code_hash_at_address(&self, address: H160) -> H256 {
Pallet::<T>::code_hash_at_address(&address)
fn code_size(&self, address: H160) -> U256 {
Pallet::<T>::code_size_at_address(&address)
}

fn code_size_at_address(&self, address: H160) -> U256 {
Pallet::<T>::code_size_at_address(&address)
fn code_hash(&self, address: H160) -> H256 {
Pallet::<T>::code_hash_at_address(&address)
}
}
27 changes: 19 additions & 8 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,7 @@ impl<'config> StackSubstateMetadata<'config> {
}
}

pub trait CustomStackState {
fn code_hash_at_address(&self, address: H160) -> H256;
fn code_size_at_address(&self, address: H160) -> U256;
}

pub trait StackState<'config>: Backend + CustomStackState {
pub trait StackState<'config>: Backend {
fn metadata(&self) -> &StackSubstateMetadata<'config>;
fn metadata_mut(&mut self) -> &mut StackSubstateMetadata<'config>;

Expand All @@ -336,6 +331,22 @@ pub trait StackState<'config>: Backend + CustomStackState {
fn transfer(&mut self, transfer: Transfer) -> Result<(), ExitError>;
fn reset_balance(&mut self, address: H160);
fn touch(&mut self, address: H160);

/// Fetch the code size of an address.
/// Provide a default implementation by fetching the code, but
/// can be customized to use a more performant approach that don't need to
/// fetch the code.
fn code_size(&self, address: H160) -> U256 {
U256::from(self.code(address).len())
}

/// Fetch the code hash of an address.
/// Provide a default implementation by fetching the code, but
/// can be customized to use a more performant approach that don't need to
/// fetch the code.
fn code_hash(&self, address: H160) -> H256 {
H256::from_slice(Keccak256::digest(self.code(address)).as_slice())
}
}

/// Data returned by a precompile on success.
Expand Down Expand Up @@ -1136,15 +1147,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
}

fn code_size(&self, address: H160) -> U256 {
self.state.code_size_at_address(address)
self.state.code_size(address)
}

fn code_hash(&self, address: H160) -> H256 {
if !self.exists(address) {
return H256::default();
}

self.state.code_hash_at_address(address)
self.state.code_hash(address)
}

fn code(&self, address: H160) -> Vec<u8> {
Expand Down

0 comments on commit 2e362fc

Please sign in to comment.