diff --git a/runtime/src/eval/mod.rs b/runtime/src/eval/mod.rs index c65370fca..855ff9a9a 100644 --- a/runtime/src/eval/mod.rs +++ b/runtime/src/eval/mod.rs @@ -14,14 +14,14 @@ pub enum Control { Exit(ExitReason), } -fn handle_other(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control { +fn handle_other(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control { match handler.other(opcode, &mut state.machine) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn eval(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control { +pub fn eval(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control { match opcode { Opcode::SHA3 => system::sha3(state), Opcode::ADDRESS => system::address(state), @@ -64,7 +64,7 @@ pub fn eval(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> } pub fn finish_create( - runtime: &mut Runtime, + runtime: &mut Runtime<'_>, reason: ExitReason, address: Option, return_data: Vec, @@ -93,7 +93,7 @@ pub fn finish_create( } pub fn finish_call( - runtime: &mut Runtime, + runtime: &mut Runtime<'_>, out_len: U256, out_offset: U256, reason: ExitReason, diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 574355437..bf6736481 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -7,7 +7,7 @@ use alloc::vec::Vec; use primitive_types::{H256, U256}; use sha3::{Digest, Keccak256}; -pub fn sha3(runtime: &mut Runtime) -> Control { +pub fn sha3(runtime: &mut Runtime<'_>) -> Control { pop_u256!(runtime, from, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(from, len)); @@ -26,47 +26,47 @@ pub fn sha3(runtime: &mut Runtime) -> Control { Control::Continue } -pub fn chainid(runtime: &mut Runtime, handler: &H) -> Control { +pub fn chainid(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.chain_id()); Control::Continue } -pub fn address(runtime: &mut Runtime) -> Control { +pub fn address(runtime: &mut Runtime<'_>) -> Control { let ret = H256::from(runtime.context.address); push!(runtime, ret); Control::Continue } -pub fn balance(runtime: &mut Runtime, handler: &H) -> Control { +pub fn balance(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop!(runtime, address); push_u256!(runtime, handler.balance(address.into())); Control::Continue } -pub fn selfbalance(runtime: &mut Runtime, handler: &H) -> Control { +pub fn selfbalance(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.balance(runtime.context.address)); Control::Continue } -pub fn origin(runtime: &mut Runtime, handler: &H) -> Control { +pub fn origin(runtime: &mut Runtime<'_>, handler: &H) -> Control { let ret = H256::from(handler.origin()); push!(runtime, ret); Control::Continue } -pub fn caller(runtime: &mut Runtime) -> Control { +pub fn caller(runtime: &mut Runtime<'_>) -> Control { let ret = H256::from(runtime.context.caller); push!(runtime, ret); Control::Continue } -pub fn callvalue(runtime: &mut Runtime) -> Control { +pub fn callvalue(runtime: &mut Runtime<'_>) -> Control { let mut ret = H256::default(); runtime.context.apparent_value.to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -74,7 +74,7 @@ pub fn callvalue(runtime: &mut Runtime) -> Control { Control::Continue } -pub fn gasprice(runtime: &mut Runtime, handler: &H) -> Control { +pub fn gasprice(runtime: &mut Runtime<'_>, handler: &H) -> Control { let mut ret = H256::default(); handler.gas_price().to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -82,7 +82,7 @@ pub fn gasprice(runtime: &mut Runtime, handler: &H) -> Control { Control::Continue } -pub fn base_fee(runtime: &mut Runtime, handler: &H) -> Control { +pub fn base_fee(runtime: &mut Runtime<'_>, handler: &H) -> Control { let mut ret = H256::default(); handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -90,21 +90,21 @@ pub fn base_fee(runtime: &mut Runtime, handler: &H) -> Control { Control::Continue } -pub fn extcodesize(runtime: &mut Runtime, handler: &H) -> Control { +pub fn extcodesize(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop!(runtime, address); push_u256!(runtime, handler.code_size(address.into())); Control::Continue } -pub fn extcodehash(runtime: &mut Runtime, handler: &H) -> Control { +pub fn extcodehash(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop!(runtime, address); push!(runtime, handler.code_hash(address.into())); Control::Continue } -pub fn extcodecopy(runtime: &mut Runtime, handler: &H) -> Control { +pub fn extcodecopy(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop!(runtime, address); pop_u256!(runtime, memory_offset, code_offset, len); @@ -125,14 +125,14 @@ pub fn extcodecopy(runtime: &mut Runtime, handler: &H) -> Control Control::Continue } -pub fn returndatasize(runtime: &mut Runtime) -> Control { +pub fn returndatasize(runtime: &mut Runtime<'_>) -> Control { let size = U256::from(runtime.return_data_buffer.len()); push_u256!(runtime, size); Control::Continue } -pub fn returndatacopy(runtime: &mut Runtime) -> Control { +pub fn returndatacopy(runtime: &mut Runtime<'_>) -> Control { pop_u256!(runtime, memory_offset, data_offset, len); try_or_fail!(runtime @@ -158,39 +158,39 @@ pub fn returndatacopy(runtime: &mut Runtime) -> Control { } } -pub fn blockhash(runtime: &mut Runtime, handler: &H) -> Control { +pub fn blockhash(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop_u256!(runtime, number); push!(runtime, handler.block_hash(number)); Control::Continue } -pub fn coinbase(runtime: &mut Runtime, handler: &H) -> Control { +pub fn coinbase(runtime: &mut Runtime<'_>, handler: &H) -> Control { push!(runtime, handler.block_coinbase().into()); Control::Continue } -pub fn timestamp(runtime: &mut Runtime, handler: &H) -> Control { +pub fn timestamp(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.block_timestamp()); Control::Continue } -pub fn number(runtime: &mut Runtime, handler: &H) -> Control { +pub fn number(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.block_number()); Control::Continue } -pub fn difficulty(runtime: &mut Runtime, handler: &H) -> Control { +pub fn difficulty(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.block_difficulty()); Control::Continue } -pub fn gaslimit(runtime: &mut Runtime, handler: &H) -> Control { +pub fn gaslimit(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.block_gas_limit()); Control::Continue } -pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { +pub fn sload(runtime: &mut Runtime<'_>, handler: &H) -> Control { pop!(runtime, index); let value = handler.storage(runtime.context.address, index); push!(runtime, value); @@ -204,7 +204,7 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { Control::Continue } -pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control { +pub fn sstore(runtime: &mut Runtime<'_>, handler: &mut H) -> Control { pop!(runtime, index, value); event!(SStore { @@ -219,13 +219,13 @@ pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control } } -pub fn gas(runtime: &mut Runtime, handler: &H) -> Control { +pub fn gas(runtime: &mut Runtime<'_>, handler: &H) -> Control { push_u256!(runtime, handler.gas_left()); Control::Continue } -pub fn log(runtime: &mut Runtime, n: u8, handler: &mut H) -> Control { +pub fn log(runtime: &mut Runtime<'_>, n: u8, handler: &mut H) -> Control { pop_u256!(runtime, offset, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(offset, len)); @@ -254,7 +254,7 @@ pub fn log(runtime: &mut Runtime, n: u8, handler: &mut H) -> Control } } -pub fn suicide(runtime: &mut Runtime, handler: &mut H) -> Control { +pub fn suicide(runtime: &mut Runtime<'_>, handler: &mut H) -> Control { pop!(runtime, target); match handler.mark_delete(runtime.context.address, target.into()) { @@ -265,7 +265,7 @@ pub fn suicide(runtime: &mut Runtime, handler: &mut H) -> Control Control::Exit(ExitSucceed::Suicided.into()) } -pub fn create(runtime: &mut Runtime, is_create2: bool, handler: &mut H) -> Control { +pub fn create(runtime: &mut Runtime<'_>, is_create2: bool, handler: &mut H) -> Control { runtime.return_data_buffer = Vec::new(); pop_u256!(runtime, value, code_offset, len); @@ -305,7 +305,7 @@ pub fn create(runtime: &mut Runtime, is_create2: bool, handler: &mut } } -pub fn call(runtime: &mut Runtime, scheme: CallScheme, handler: &mut H) -> Control { +pub fn call(runtime: &mut Runtime<'_>, scheme: CallScheme, handler: &mut H) -> Control { runtime.return_data_buffer = Vec::new(); pop_u256!(runtime, gas); diff --git a/runtime/src/interrupt.rs b/runtime/src/interrupt.rs index 188fc5127..136cf9a6a 100644 --- a/runtime/src/interrupt.rs +++ b/runtime/src/interrupt.rs @@ -1,31 +1,31 @@ use crate::{Handler, Runtime}; /// Interrupt resolution. -pub enum Resolve<'a, H: Handler> { +pub enum Resolve<'a, 'config, H: Handler> { /// Create interrupt resolution. - Create(H::CreateInterrupt, ResolveCreate<'a>), + Create(H::CreateInterrupt, ResolveCreate<'a, 'config>), /// Call interrupt resolution. - Call(H::CallInterrupt, ResolveCall<'a>), + Call(H::CallInterrupt, ResolveCall<'a, 'config>), } /// Create interrupt resolution. -pub struct ResolveCreate<'a> { - _runtime: &'a mut Runtime, +pub struct ResolveCreate<'a, 'config> { + _runtime: &'a mut Runtime<'config>, } -impl<'a> ResolveCreate<'a> { - pub(crate) fn new(runtime: &'a mut Runtime) -> Self { +impl<'a, 'config> ResolveCreate<'a, 'config> { + pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self { Self { _runtime: runtime } } } /// Call interrupt resolution. -pub struct ResolveCall<'a> { - _runtime: &'a mut Runtime, +pub struct ResolveCall<'a, 'config> { + _runtime: &'a mut Runtime<'config>, } -impl<'a> ResolveCall<'a> { - pub(crate) fn new(runtime: &'a mut Runtime) -> Self { +impl<'a, 'config> ResolveCall<'a, 'config> { + pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self { Self { _runtime: runtime } } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index a5d3a54b5..7d8bff7c5 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -107,31 +107,32 @@ macro_rules! step { /// EVM runtime. /// /// The runtime wraps an EVM `Machine` with support of return data and context. -pub struct Runtime { +pub struct Runtime<'config> { machine: Machine, status: Result<(), ExitReason>, return_data_buffer: Vec, return_data_len: U256, return_data_offset: U256, context: Context, + _config: &'config Config, } -impl Runtime { +impl<'config> Runtime<'config> { /// Create a new runtime with given code and data. pub fn new( code: Rc>, data: Rc>, context: Context, - stack_limit: usize, - memory_limit: usize, + config: &'config Config, ) -> Self { Self { - machine: Machine::new(code, data, stack_limit, memory_limit), + machine: Machine::new(code, data, config.stack_limit, config.memory_limit), status: Ok(()), return_data_buffer: Vec::new(), return_data_len: U256::zero(), return_data_offset: U256::zero(), context, + _config: config, } } @@ -149,7 +150,7 @@ impl Runtime { pub fn step<'a, H: Handler>( &'a mut self, handler: &mut H, - ) -> Result<(), Capture>> { + ) -> Result<(), Capture>> { step!(self, handler, return Err; Ok) } @@ -157,7 +158,7 @@ impl Runtime { pub fn run<'a, H: Handler>( &'a mut self, handler: &mut H, - ) -> Capture> { + ) -> Capture> { loop { step!(self, handler, return;) } diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index b50e68af2..ad27688e3 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -291,7 +291,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } /// Execute the runtime until it returns. - pub fn execute(&mut self, runtime: &mut Runtime) -> ExitReason { + pub fn execute(&mut self, runtime: &mut Runtime<'config>) -> ExitReason { let mut call_stack = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY); call_stack.push(TaggedRuntime { kind: RuntimeKind::Execute, @@ -302,9 +302,9 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } /// Execute using Runtimes on the call_stack until it returns. - fn execute_with_call_stack( + fn execute_with_call_stack<'borrow>( &mut self, - call_stack: &mut Vec, + call_stack: &mut Vec>, ) -> (ExitReason, Option, Vec) { // This `interrupt_runtime` is used to pass the runtime obtained from the // `Capture::Trap` branch in the match below back to the top of the call stack. @@ -664,7 +664,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> init_code: Vec, target_gas: Option, take_l64: bool, - ) -> Capture<(ExitReason, Option, Vec), StackExecutorCreateInterrupt<'static>> { + ) -> Capture<(ExitReason, Option, Vec), StackExecutorCreateInterrupt<'config>> { macro_rules! try_or_fail { ( $e:expr ) => { match $e { @@ -764,8 +764,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Rc::new(init_code), Rc::new(Vec::new()), context, - self.config.stack_limit, - self.config.memory_limit, + self.config, ); Capture::Trap(StackExecutorCreateInterrupt(TaggedRuntime { @@ -785,7 +784,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> take_l64: bool, take_stipend: bool, context: Context, - ) -> Capture<(ExitReason, Vec), StackExecutorCallInterrupt<'static>> { + ) -> Capture<(ExitReason, Vec), StackExecutorCallInterrupt<'config>> { macro_rules! try_or_fail { ( $e:expr ) => { match $e { @@ -893,13 +892,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }; } - let runtime = Runtime::new( - Rc::new(code), - Rc::new(input), - context, - self.config.stack_limit, - self.config.memory_limit, - ); + let runtime = Runtime::new(Rc::new(code), Rc::new(input), context, self.config); Capture::Trap(StackExecutorCallInterrupt(TaggedRuntime { kind: RuntimeKind::Call(code_address), @@ -1008,15 +1001,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } } -pub struct StackExecutorCallInterrupt<'borrow>(TaggedRuntime<'borrow>); -pub struct StackExecutorCreateInterrupt<'borrow>(TaggedRuntime<'borrow>); +pub struct StackExecutorCallInterrupt<'config>(TaggedRuntime<'config, 'config>); +pub struct StackExecutorCreateInterrupt<'config>(TaggedRuntime<'config, 'config>); impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler for StackExecutor<'config, 'precompiles, S, P> { - type CreateInterrupt = StackExecutorCreateInterrupt<'static>; + type CreateInterrupt = StackExecutorCreateInterrupt<'config>; type CreateFeedback = Infallible; - type CallInterrupt = StackExecutorCallInterrupt<'static>; + type CallInterrupt = StackExecutorCallInterrupt<'config>; type CallFeedback = Infallible; fn balance(&self, address: H160) -> U256 { diff --git a/src/executor/stack/tagged_runtime.rs b/src/executor/stack/tagged_runtime.rs index c0399f8e4..9af9fa827 100644 --- a/src/executor/stack/tagged_runtime.rs +++ b/src/executor/stack/tagged_runtime.rs @@ -5,9 +5,9 @@ use crate::maybe_borrowed::MaybeBorrowed; use crate::Runtime; use primitive_types::H160; -pub struct TaggedRuntime<'borrow> { +pub struct TaggedRuntime<'config, 'borrow> { pub kind: RuntimeKind, - pub inner: MaybeBorrowed<'borrow, Runtime>, + pub inner: MaybeBorrowed<'borrow, Runtime<'config>>, } #[derive(Debug, Clone, Copy, PartialEq, Eq)]