From 8f4d9c41a10c891ff4b4e367f6137e7037ce5be4 Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Thu, 3 Nov 2022 12:30:19 +0000 Subject: [PATCH] Add customizable code_size/code_hash fn in StackState trait --- src/executor/stack/executor.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index 516f218a..2e564d9d 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -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. @@ -970,7 +986,7 @@ 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 { @@ -978,7 +994,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler 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 {