From 95a9e4a10d5d566ca51c1249daf22b665bc2acd0 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 5 Nov 2023 08:11:11 +0100 Subject: [PATCH 1/2] Generic state for runtime opcodes --- gasometer/src/lib.rs | 5 +- gasometer/src/standard.rs | 9 ++- interpreter/src/eval/mod.rs | 124 ++++++++++++++++----------------- interpreter/src/eval/system.rs | 81 ++++++++++----------- 4 files changed, 109 insertions(+), 110 deletions(-) diff --git a/gasometer/src/lib.rs b/gasometer/src/lib.rs index 5e5589e62..fd47488ef 100644 --- a/gasometer/src/lib.rs +++ b/gasometer/src/lib.rs @@ -29,10 +29,9 @@ impl Gas for u64 {} impl Gas for U256 {} #[derive(Clone, Copy)] -pub enum MergeStrategy { +pub enum GasometerMergeStrategy { Commit, Revert, - Discard, } pub trait Gasometer: Sized { @@ -47,5 +46,5 @@ pub trait Gasometer: Sized { is_static: bool, ) -> Result<(Self, usize), ExitError>; fn gas(&self) -> Self::Gas; - fn merge(&mut self, other: Self, strategy: MergeStrategy); + fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy); } diff --git a/gasometer/src/standard.rs b/gasometer/src/standard.rs index b6b9bb075..20591d1de 100644 --- a/gasometer/src/standard.rs +++ b/gasometer/src/standard.rs @@ -1,4 +1,4 @@ -use crate::{consts, costs, Config, Gasometer, MergeStrategy}; +use crate::{consts, costs, Config, Gasometer, GasometerMergeStrategy}; use core::cmp::max; use evm_interpreter::{ExitError, ExitException, Handler, Machine, Opcode, RuntimeState, Stack}; use primitive_types::{H160, H256, U256}; @@ -86,16 +86,15 @@ impl<'config, H: Handler> Gasometer for StandardGasometer<'conf self.gas_limit - self.memory_gas - self.used_gas } - fn merge(&mut self, other: Self, strategy: MergeStrategy) { + fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy) { match strategy { - MergeStrategy::Commit => { + GasometerMergeStrategy::Commit => { self.used_gas -= Gasometer::::gas(&other); self.refunded_gas += other.refunded_gas; } - MergeStrategy::Revert => { + GasometerMergeStrategy::Revert => { self.used_gas -= Gasometer::::gas(&other); } - MergeStrategy::Discard => (), } } } diff --git a/interpreter/src/eval/mod.rs b/interpreter/src/eval/mod.rs index 40a7c9bc9..d01676c24 100644 --- a/interpreter/src/eval/mod.rs +++ b/interpreter/src/eval/mod.rs @@ -179,9 +179,9 @@ impl Etable { } } -impl Etable { +impl, H: Handler> Etable { /// Runtime Etable. - pub const fn runtime() -> Etable { + pub const fn runtime() -> Etable { let mut table = Self::core(); table.0[Opcode::SHA3.as_usize()] = eval_sha3 as _; @@ -1217,8 +1217,8 @@ fn eval_unknown( Control::Exit(ExitException::InvalidOpcode(opcode).into()) } -fn eval_sha3( - machine: &mut Machine, +fn eval_sha3, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1226,8 +1226,8 @@ fn eval_sha3( self::system::sha3(machine) } -fn eval_address( - machine: &mut Machine, +fn eval_address, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1235,8 +1235,8 @@ fn eval_address( self::system::address(machine) } -fn eval_balance( - machine: &mut Machine, +fn eval_balance, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1244,8 +1244,8 @@ fn eval_balance( self::system::balance(machine, handle) } -fn eval_selfbalance( - machine: &mut Machine, +fn eval_selfbalance, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1253,8 +1253,8 @@ fn eval_selfbalance( self::system::selfbalance(machine, handle) } -fn eval_origin( - machine: &mut Machine, +fn eval_origin, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1262,8 +1262,8 @@ fn eval_origin( self::system::origin(machine, handle) } -fn eval_caller( - machine: &mut Machine, +fn eval_caller, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1271,8 +1271,8 @@ fn eval_caller( self::system::caller(machine) } -fn eval_callvalue( - machine: &mut Machine, +fn eval_callvalue, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1280,8 +1280,8 @@ fn eval_callvalue( self::system::callvalue(machine) } -fn eval_gasprice( - machine: &mut Machine, +fn eval_gasprice, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1289,8 +1289,8 @@ fn eval_gasprice( self::system::gasprice(machine, handle) } -fn eval_extcodesize( - machine: &mut Machine, +fn eval_extcodesize, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1298,8 +1298,8 @@ fn eval_extcodesize( self::system::extcodesize(machine, handle) } -fn eval_extcodehash( - machine: &mut Machine, +fn eval_extcodehash, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1307,8 +1307,8 @@ fn eval_extcodehash( self::system::extcodehash(machine, handle) } -fn eval_extcodecopy( - machine: &mut Machine, +fn eval_extcodecopy, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1316,8 +1316,8 @@ fn eval_extcodecopy( self::system::extcodecopy(machine, handle) } -fn eval_returndatasize( - machine: &mut Machine, +fn eval_returndatasize, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1325,8 +1325,8 @@ fn eval_returndatasize( self::system::returndatasize(machine) } -fn eval_returndatacopy( - machine: &mut Machine, +fn eval_returndatacopy, H: Handler>( + machine: &mut Machine, _handle: &mut H, _opcode: Opcode, _position: usize, @@ -1334,8 +1334,8 @@ fn eval_returndatacopy( self::system::returndatacopy(machine) } -fn eval_blockhash( - machine: &mut Machine, +fn eval_blockhash, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1343,8 +1343,8 @@ fn eval_blockhash( self::system::blockhash(machine, handle) } -fn eval_coinbase( - machine: &mut Machine, +fn eval_coinbase, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1352,8 +1352,8 @@ fn eval_coinbase( self::system::coinbase(machine, handle) } -fn eval_timestamp( - machine: &mut Machine, +fn eval_timestamp, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1361,8 +1361,8 @@ fn eval_timestamp( self::system::timestamp(machine, handle) } -fn eval_number( - machine: &mut Machine, +fn eval_number, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1370,8 +1370,8 @@ fn eval_number( self::system::number(machine, handle) } -fn eval_difficulty( - machine: &mut Machine, +fn eval_difficulty, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1379,8 +1379,8 @@ fn eval_difficulty( self::system::prevrandao(machine, handle) } -fn eval_gaslimit( - machine: &mut Machine, +fn eval_gaslimit, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1388,8 +1388,8 @@ fn eval_gaslimit( self::system::gaslimit(machine, handle) } -fn eval_sload( - machine: &mut Machine, +fn eval_sload, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1397,8 +1397,8 @@ fn eval_sload( self::system::sload(machine, handle) } -fn eval_sstore( - machine: &mut Machine, +fn eval_sstore, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1406,8 +1406,8 @@ fn eval_sstore( self::system::sstore(machine, handle) } -fn eval_gas( - machine: &mut Machine, +fn eval_gas, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1415,8 +1415,8 @@ fn eval_gas( self::system::gas(machine, handle) } -fn eval_log0( - machine: &mut Machine, +fn eval_log0, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1424,8 +1424,8 @@ fn eval_log0( self::system::log(machine, 0, handle) } -fn eval_log1( - machine: &mut Machine, +fn eval_log1, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1433,8 +1433,8 @@ fn eval_log1( self::system::log(machine, 1, handle) } -fn eval_log2( - machine: &mut Machine, +fn eval_log2, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1442,8 +1442,8 @@ fn eval_log2( self::system::log(machine, 2, handle) } -fn eval_log3( - machine: &mut Machine, +fn eval_log3, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1451,8 +1451,8 @@ fn eval_log3( self::system::log(machine, 3, handle) } -fn eval_log4( - machine: &mut Machine, +fn eval_log4, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1460,8 +1460,8 @@ fn eval_log4( self::system::log(machine, 4, handle) } -fn eval_suicide( - machine: &mut Machine, +fn eval_suicide, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1469,8 +1469,8 @@ fn eval_suicide( self::system::suicide(machine, handle) } -fn eval_chainid( - machine: &mut Machine, +fn eval_chainid, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, @@ -1478,8 +1478,8 @@ fn eval_chainid( self::system::chainid(machine, handle) } -fn eval_basefee( - machine: &mut Machine, +fn eval_basefee, H: Handler>( + machine: &mut Machine, handle: &mut H, _opcode: Opcode, _position: usize, diff --git a/interpreter/src/eval/system.rs b/interpreter/src/eval/system.rs index 0d92adccb..3b470b1fa 100644 --- a/interpreter/src/eval/system.rs +++ b/interpreter/src/eval/system.rs @@ -1,10 +1,10 @@ use super::Control; -use crate::{ExitException, ExitFatal, ExitSucceed, Handler, RuntimeMachine}; +use crate::{ExitException, ExitFatal, ExitSucceed, Handler, Machine, RuntimeState}; use alloc::vec::Vec; use primitive_types::{H256, U256}; use sha3::{Digest, Keccak256}; -pub fn sha3(machine: &mut RuntimeMachine) -> Control { +pub fn sha3>(machine: &mut Machine) -> Control { pop_u256!(machine, from, len); try_or_fail!(machine.memory.resize_offset(from, len)); @@ -23,20 +23,20 @@ pub fn sha3(machine: &mut RuntimeMachine) -> Control { Control::Continue } -pub fn chainid(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn chainid, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.chain_id()); Control::Continue } -pub fn address(machine: &mut RuntimeMachine) -> Control { - let ret = H256::from(machine.state.context.address); +pub fn address>(machine: &mut Machine) -> Control { + let ret = H256::from(machine.state.as_ref().context.address); push!(machine, ret); Control::Continue } -pub fn balance(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn balance, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); push_u256!(machine, handler.balance(address.into())); @@ -44,30 +44,31 @@ pub fn balance(machine: &mut RuntimeMachine, handler: &mut H) -> Con Control::Continue } -pub fn selfbalance(machine: &mut RuntimeMachine, handler: &H) -> Control { - push_u256!(machine, handler.balance(machine.state.context.address)); +pub fn selfbalance, H: Handler>(machine: &mut Machine, handler: &H) -> Control { + push_u256!(machine, handler.balance(machine.state.as_ref().context.address)); Control::Continue } -pub fn origin(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn origin, H: Handler>(machine: &mut Machine, handler: &H) -> Control { let ret = H256::from(handler.origin()); push!(machine, ret); Control::Continue } -pub fn caller(machine: &mut RuntimeMachine) -> Control { - let ret = H256::from(machine.state.context.caller); +pub fn caller>(machine: &mut Machine) -> Control { + let ret = H256::from(machine.state.as_ref().context.caller); push!(machine, ret); Control::Continue } -pub fn callvalue(machine: &mut RuntimeMachine) -> Control { +pub fn callvalue>(machine: &mut Machine) -> Control { let mut ret = H256::default(); machine .state + .as_ref() .context .apparent_value .to_big_endian(&mut ret[..]); @@ -76,7 +77,7 @@ pub fn callvalue(machine: &mut RuntimeMachine) -> Control { Control::Continue } -pub fn gasprice(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn gasprice, H: Handler>(machine: &mut Machine, handler: &H) -> Control { let mut ret = H256::default(); handler.gas_price().to_big_endian(&mut ret[..]); push!(machine, ret); @@ -84,7 +85,7 @@ pub fn gasprice(machine: &mut RuntimeMachine, handler: &H) -> Contro Control::Continue } -pub fn basefee(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn basefee, H: Handler>(machine: &mut Machine, handler: &H) -> Control { let mut ret = H256::default(); handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]); push!(machine, ret); @@ -92,7 +93,7 @@ pub fn basefee(machine: &mut RuntimeMachine, handler: &H) -> Control Control::Continue } -pub fn extcodesize(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn extcodesize, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); let code_size = handler.code_size(address.into()); @@ -101,7 +102,7 @@ pub fn extcodesize(machine: &mut RuntimeMachine, handler: &mut H) -> Control::Continue } -pub fn extcodehash(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn extcodehash, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); let code_hash = handler.code_hash(address.into()); @@ -110,7 +111,7 @@ pub fn extcodehash(machine: &mut RuntimeMachine, handler: &mut H) -> Control::Continue } -pub fn extcodecopy(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn extcodecopy, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, address); pop_u256!(machine, memory_offset, code_offset, len); @@ -129,20 +130,20 @@ pub fn extcodecopy(machine: &mut RuntimeMachine, handler: &mut H) -> Control::Continue } -pub fn returndatasize(machine: &mut RuntimeMachine) -> Control { - let size = U256::from(machine.state.retbuf.len()); +pub fn returndatasize>(machine: &mut Machine) -> Control { + let size = U256::from(machine.state.as_ref().retbuf.len()); push_u256!(machine, size); Control::Continue } -pub fn returndatacopy(machine: &mut RuntimeMachine) -> Control { +pub fn returndatacopy>(machine: &mut Machine) -> Control { pop_u256!(machine, memory_offset, data_offset, len); try_or_fail!(machine.memory.resize_offset(memory_offset, len)); if data_offset .checked_add(len) - .map(|l| l > U256::from(machine.state.retbuf.len())) + .map(|l| l > U256::from(machine.state.as_ref().retbuf.len())) .unwrap_or(true) { return Control::Exit(ExitException::OutOfOffset.into()); @@ -150,41 +151,41 @@ pub fn returndatacopy(machine: &mut RuntimeMachine) -> Control { match machine .memory - .copy_large(memory_offset, data_offset, len, &machine.state.retbuf) + .copy_large(memory_offset, data_offset, len, &machine.state.as_ref().retbuf) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn blockhash(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn blockhash, H: Handler>(machine: &mut Machine, handler: &H) -> Control { pop_u256!(machine, number); push!(machine, handler.block_hash(number)); Control::Continue } -pub fn coinbase(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn coinbase, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push!(machine, handler.block_coinbase().into()); Control::Continue } -pub fn timestamp(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn timestamp, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.block_timestamp()); Control::Continue } -pub fn number(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn number, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.block_number()); Control::Continue } -pub fn difficulty(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn difficulty, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.block_difficulty()); Control::Continue } -pub fn prevrandao(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn prevrandao, H: Handler>(machine: &mut Machine, handler: &H) -> Control { if let Some(rand) = handler.block_randomness() { push!(machine, rand); Control::Continue @@ -193,37 +194,37 @@ pub fn prevrandao(machine: &mut RuntimeMachine, handler: &H) -> Cont } } -pub fn gaslimit(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn gaslimit, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.block_gas_limit()); Control::Continue } -pub fn sload(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn sload, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, index); - try_or_fail!(handler.mark_hot(machine.state.context.address, Some(index))); - let value = handler.storage(machine.state.context.address, index); + try_or_fail!(handler.mark_hot(machine.state.as_ref().context.address, Some(index))); + let value = handler.storage(machine.state.as_ref().context.address, index); push!(machine, value); Control::Continue } -pub fn sstore(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn sstore, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, index, value); - try_or_fail!(handler.mark_hot(machine.state.context.address, Some(index))); + try_or_fail!(handler.mark_hot(machine.state.as_ref().context.address, Some(index))); - match handler.set_storage(machine.state.context.address, index, value) { + match handler.set_storage(machine.state.as_ref().context.address, index, value) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn gas(machine: &mut RuntimeMachine, handler: &H) -> Control { +pub fn gas, H: Handler>(machine: &mut Machine, handler: &H) -> Control { push_u256!(machine, handler.gas_left()); Control::Continue } -pub fn log(machine: &mut RuntimeMachine, n: u8, handler: &mut H) -> Control { +pub fn log, H: Handler>(machine: &mut Machine, n: u8, handler: &mut H) -> Control { pop_u256!(machine, offset, len); try_or_fail!(machine.memory.resize_offset(offset, len)); @@ -246,16 +247,16 @@ pub fn log(machine: &mut RuntimeMachine, n: u8, handler: &mut H) -> } } - match handler.log(machine.state.context.address, topics, data) { + match handler.log(machine.state.as_ref().context.address, topics, data) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn suicide(machine: &mut RuntimeMachine, handler: &mut H) -> Control { +pub fn suicide, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { pop!(machine, target); - match handler.mark_delete(machine.state.context.address, target.into()) { + match handler.mark_delete(machine.state.as_ref().context.address, target.into()) { Ok(()) => (), Err(e) => return Control::Exit(e.into()), } From eaa8f8737ae865798a31338a1762f37135cad76f Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 5 Nov 2023 08:21:04 +0100 Subject: [PATCH 2/2] Add gasometer extra_check --- gasometer/src/lib.rs | 3 +- gasometer/src/standard.rs | 22 +++++- interpreter/src/eval/system.rs | 118 +++++++++++++++++++++++++-------- 3 files changed, 115 insertions(+), 28 deletions(-) diff --git a/gasometer/src/lib.rs b/gasometer/src/lib.rs index fd47488ef..84f378140 100644 --- a/gasometer/src/lib.rs +++ b/gasometer/src/lib.rs @@ -39,12 +39,13 @@ pub trait Gasometer: Sized { type Config; fn new(gas_limit: Self::Gas, machine: &Machine, config: Self::Config) -> Self; - fn record( + fn record_stepn( self, machine: &Machine, handler: &H, is_static: bool, ) -> Result<(Self, usize), ExitError>; + fn record_codedeposit(self, len: usize) -> Result; fn gas(&self) -> Self::Gas; fn merge(&mut self, other: Self, strategy: GasometerMergeStrategy); } diff --git a/gasometer/src/standard.rs b/gasometer/src/standard.rs index 20591d1de..7c4ba6b1d 100644 --- a/gasometer/src/standard.rs +++ b/gasometer/src/standard.rs @@ -42,7 +42,7 @@ impl<'config, H: Handler> Gasometer for StandardGasometer<'conf } } - fn record( + fn record_stepn( mut self, machine: &Machine, handler: &H, @@ -77,11 +77,20 @@ impl<'config, H: Handler> Gasometer for StandardGasometer<'conf self.memory_gas = max(self.memory_gas, memory_cost); } } + + let after_gas = Gasometer::::gas(&self); + gas.extra_check(after_gas, self.config)?; } Ok((self, 1)) } + fn record_codedeposit(mut self, len: usize) -> Result { + let cost = len as u64 * consts::G_CODEDEPOSIT; + self = self.record_cost(cost)?; + Ok(self) + } + fn gas(&self) -> u64 { self.gas_limit - self.memory_gas - self.used_gas } @@ -572,6 +581,17 @@ impl GasCost { _ => 0, } } + + /// Extra check of the cost. + pub fn extra_check(&self, after_gas: u64, config: &Config) -> Result<(), ExitException> { + match *self { + GasCost::Call { gas, .. } => costs::call_extra_check(gas, after_gas, config), + GasCost::CallCode { gas, .. } => costs::call_extra_check(gas, after_gas, config), + GasCost::DelegateCall { gas, .. } => costs::call_extra_check(gas, after_gas, config), + GasCost::StaticCall { gas, .. } => costs::call_extra_check(gas, after_gas, config), + _ => Ok(()), + } + } } /// Memory cost. diff --git a/interpreter/src/eval/system.rs b/interpreter/src/eval/system.rs index 3b470b1fa..18d108c7c 100644 --- a/interpreter/src/eval/system.rs +++ b/interpreter/src/eval/system.rs @@ -23,7 +23,10 @@ pub fn sha3>(machine: &mut Machine) -> Control { Control::Continue } -pub fn chainid, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn chainid, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push_u256!(machine, handler.chain_id()); Control::Continue @@ -36,7 +39,10 @@ pub fn address>(machine: &mut Machine) -> Control { Control::Continue } -pub fn balance, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn balance, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); push_u256!(machine, handler.balance(address.into())); @@ -44,13 +50,22 @@ pub fn balance, H: Handler>(machine: &mut Machine, han Control::Continue } -pub fn selfbalance, H: Handler>(machine: &mut Machine, handler: &H) -> Control { - push_u256!(machine, handler.balance(machine.state.as_ref().context.address)); +pub fn selfbalance, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { + push_u256!( + machine, + handler.balance(machine.state.as_ref().context.address) + ); Control::Continue } -pub fn origin, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn origin, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { let ret = H256::from(handler.origin()); push!(machine, ret); @@ -68,7 +83,7 @@ pub fn callvalue>(machine: &mut Machine) -> Control { let mut ret = H256::default(); machine .state - .as_ref() + .as_ref() .context .apparent_value .to_big_endian(&mut ret[..]); @@ -77,7 +92,10 @@ pub fn callvalue>(machine: &mut Machine) -> Control { Control::Continue } -pub fn gasprice, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn gasprice, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { let mut ret = H256::default(); handler.gas_price().to_big_endian(&mut ret[..]); push!(machine, ret); @@ -85,7 +103,10 @@ pub fn gasprice, H: Handler>(machine: &mut Machine, ha Control::Continue } -pub fn basefee, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn basefee, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { let mut ret = H256::default(); handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]); push!(machine, ret); @@ -93,7 +114,10 @@ pub fn basefee, H: Handler>(machine: &mut Machine, han Control::Continue } -pub fn extcodesize, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn extcodesize, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); let code_size = handler.code_size(address.into()); @@ -102,7 +126,10 @@ pub fn extcodesize, H: Handler>(machine: &mut Machine, Control::Continue } -pub fn extcodehash, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn extcodehash, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, address); try_or_fail!(handler.mark_hot(address.into(), None)); let code_hash = handler.code_hash(address.into()); @@ -111,7 +138,10 @@ pub fn extcodehash, H: Handler>(machine: &mut Machine, Control::Continue } -pub fn extcodecopy, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn extcodecopy, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, address); pop_u256!(machine, memory_offset, code_offset, len); @@ -149,43 +179,63 @@ pub fn returndatacopy>(machine: &mut Machine) -> Contr return Control::Exit(ExitException::OutOfOffset.into()); } - match machine - .memory - .copy_large(memory_offset, data_offset, len, &machine.state.as_ref().retbuf) - { + match machine.memory.copy_large( + memory_offset, + data_offset, + len, + &machine.state.as_ref().retbuf, + ) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn blockhash, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn blockhash, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { pop_u256!(machine, number); push!(machine, handler.block_hash(number)); Control::Continue } -pub fn coinbase, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn coinbase, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push!(machine, handler.block_coinbase().into()); Control::Continue } -pub fn timestamp, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn timestamp, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push_u256!(machine, handler.block_timestamp()); Control::Continue } -pub fn number, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn number, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push_u256!(machine, handler.block_number()); Control::Continue } -pub fn difficulty, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn difficulty, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push_u256!(machine, handler.block_difficulty()); Control::Continue } -pub fn prevrandao, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn prevrandao, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { if let Some(rand) = handler.block_randomness() { push!(machine, rand); Control::Continue @@ -194,12 +244,18 @@ pub fn prevrandao, H: Handler>(machine: &mut Machine, } } -pub fn gaslimit, H: Handler>(machine: &mut Machine, handler: &H) -> Control { +pub fn gaslimit, H: Handler>( + machine: &mut Machine, + handler: &H, +) -> Control { push_u256!(machine, handler.block_gas_limit()); Control::Continue } -pub fn sload, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn sload, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, index); try_or_fail!(handler.mark_hot(machine.state.as_ref().context.address, Some(index))); let value = handler.storage(machine.state.as_ref().context.address, index); @@ -208,7 +264,10 @@ pub fn sload, H: Handler>(machine: &mut Machine, handl Control::Continue } -pub fn sstore, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn sstore, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, index, value); try_or_fail!(handler.mark_hot(machine.state.as_ref().context.address, Some(index))); @@ -224,7 +283,11 @@ pub fn gas, H: Handler>(machine: &mut Machine, handler Control::Continue } -pub fn log, H: Handler>(machine: &mut Machine, n: u8, handler: &mut H) -> Control { +pub fn log, H: Handler>( + machine: &mut Machine, + n: u8, + handler: &mut H, +) -> Control { pop_u256!(machine, offset, len); try_or_fail!(machine.memory.resize_offset(offset, len)); @@ -253,7 +316,10 @@ pub fn log, H: Handler>(machine: &mut Machine, n: u8, } } -pub fn suicide, H: Handler>(machine: &mut Machine, handler: &mut H) -> Control { +pub fn suicide, H: Handler>( + machine: &mut Machine, + handler: &mut H, +) -> Control { pop!(machine, target); match handler.mark_delete(machine.state.as_ref().context.address, target.into()) {