Skip to content

Commit

Permalink
Add customizable code_size/code_hash fn in StackState trait (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocryk authored Nov 23, 2022
1 parent 6274e6d commit 775c477
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/executor/stack/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ pub trait StackState<'config>: Backend {
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 @@ -970,15 +986,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler
}

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

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

H256::from_slice(Keccak256::digest(&self.state.code(address)).as_slice())
self.state.code_hash(address)
}

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

0 comments on commit 775c477

Please sign in to comment.