Skip to content

Commit

Permalink
Remove unused config from runtime (#161)
Browse files Browse the repository at this point in the history
* Remove unused config from runtime

* Remove unused lifetime

* Run cargo fmt

* Fix extra lifetime args in runtime/eval

* Run cargo fmt

* Remove unnecessary explict lifetime
  • Loading branch information
sorpaas authored Apr 18, 2023
1 parent 0c75304 commit e7138f7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 64 deletions.
8 changes: 4 additions & 4 deletions runtime/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub enum Control<H: Handler> {
Exit(ExitReason),
}

fn handle_other<H: Handler>(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control<H> {
fn handle_other<H: Handler>(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control<H> {
match handler.other(opcode, &mut state.machine) {
Ok(()) => Control::Continue,
Err(e) => Control::Exit(e.into()),
}
}

pub fn eval<H: Handler>(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control<H> {
pub fn eval<H: Handler>(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control<H> {
match opcode {
Opcode::SHA3 => system::sha3(state),
Opcode::ADDRESS => system::address(state),
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn eval<H: Handler>(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H
}

pub fn finish_create(
runtime: &mut Runtime<'_>,
runtime: &mut Runtime,
reason: ExitReason,
address: Option<H160>,
return_data: Vec<u8>,
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 28 additions & 28 deletions runtime/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloc::vec::Vec;
use primitive_types::{H256, U256};
use sha3::{Digest, Keccak256};

pub fn sha3<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn sha3<H: Handler>(runtime: &mut Runtime) -> Control<H> {
pop_u256!(runtime, from, len);

try_or_fail!(runtime.machine.memory_mut().resize_offset(from, len));
Expand All @@ -26,85 +26,85 @@ pub fn sha3<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
Control::Continue
}

pub fn chainid<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn chainid<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.chain_id());

Control::Continue
}

pub fn address<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn address<H: Handler>(runtime: &mut Runtime) -> Control<H> {
let ret = H256::from(runtime.context.address);
push!(runtime, ret);

Control::Continue
}

pub fn balance<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn balance<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop!(runtime, address);
push_u256!(runtime, handler.balance(address.into()));

Control::Continue
}

pub fn selfbalance<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn selfbalance<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.balance(runtime.context.address));

Control::Continue
}

pub fn origin<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn origin<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
let ret = H256::from(handler.origin());
push!(runtime, ret);

Control::Continue
}

pub fn caller<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn caller<H: Handler>(runtime: &mut Runtime) -> Control<H> {
let ret = H256::from(runtime.context.caller);
push!(runtime, ret);

Control::Continue
}

pub fn callvalue<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn callvalue<H: Handler>(runtime: &mut Runtime) -> Control<H> {
let mut ret = H256::default();
runtime.context.apparent_value.to_big_endian(&mut ret[..]);
push!(runtime, ret);

Control::Continue
}

pub fn gasprice<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn gasprice<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
let mut ret = H256::default();
handler.gas_price().to_big_endian(&mut ret[..]);
push!(runtime, ret);

Control::Continue
}

pub fn base_fee<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn base_fee<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
let mut ret = H256::default();
handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]);
push!(runtime, ret);

Control::Continue
}

pub fn extcodesize<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn extcodesize<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop!(runtime, address);
push_u256!(runtime, handler.code_size(address.into()));

Control::Continue
}

pub fn extcodehash<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn extcodehash<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop!(runtime, address);
push!(runtime, handler.code_hash(address.into()));

Control::Continue
}

pub fn extcodecopy<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn extcodecopy<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop!(runtime, address);
pop_u256!(runtime, memory_offset, code_offset, len);

Expand All @@ -125,14 +125,14 @@ pub fn extcodecopy<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Contro
Control::Continue
}

pub fn returndatasize<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn returndatasize<H: Handler>(runtime: &mut Runtime) -> Control<H> {
let size = U256::from(runtime.return_data_buffer.len());
push_u256!(runtime, size);

Control::Continue
}

pub fn returndatacopy<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
pub fn returndatacopy<H: Handler>(runtime: &mut Runtime) -> Control<H> {
pop_u256!(runtime, memory_offset, data_offset, len);

try_or_fail!(runtime
Expand All @@ -158,39 +158,39 @@ pub fn returndatacopy<H: Handler>(runtime: &mut Runtime<'_>) -> Control<H> {
}
}

pub fn blockhash<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn blockhash<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop_u256!(runtime, number);
push!(runtime, handler.block_hash(number));

Control::Continue
}

pub fn coinbase<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn coinbase<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push!(runtime, handler.block_coinbase().into());
Control::Continue
}

pub fn timestamp<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn timestamp<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.block_timestamp());
Control::Continue
}

pub fn number<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn number<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.block_number());
Control::Continue
}

pub fn difficulty<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn difficulty<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.block_difficulty());
Control::Continue
}

pub fn gaslimit<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn gaslimit<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.block_gas_limit());
Control::Continue
}

pub fn sload<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn sload<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
pop!(runtime, index);
let value = handler.storage(runtime.context.address, index);
push!(runtime, value);
Expand All @@ -204,7 +204,7 @@ pub fn sload<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
Control::Continue
}

pub fn sstore<H: Handler>(runtime: &mut Runtime<'_>, handler: &mut H) -> Control<H> {
pub fn sstore<H: Handler>(runtime: &mut Runtime, handler: &mut H) -> Control<H> {
pop!(runtime, index, value);

event!(SStore {
Expand All @@ -219,13 +219,13 @@ pub fn sstore<H: Handler>(runtime: &mut Runtime<'_>, handler: &mut H) -> Control
}
}

pub fn gas<H: Handler>(runtime: &mut Runtime<'_>, handler: &H) -> Control<H> {
pub fn gas<H: Handler>(runtime: &mut Runtime, handler: &H) -> Control<H> {
push_u256!(runtime, handler.gas_left());

Control::Continue
}

pub fn log<H: Handler>(runtime: &mut Runtime<'_>, n: u8, handler: &mut H) -> Control<H> {
pub fn log<H: Handler>(runtime: &mut Runtime, n: u8, handler: &mut H) -> Control<H> {
pop_u256!(runtime, offset, len);

try_or_fail!(runtime.machine.memory_mut().resize_offset(offset, len));
Expand Down Expand Up @@ -254,7 +254,7 @@ pub fn log<H: Handler>(runtime: &mut Runtime<'_>, n: u8, handler: &mut H) -> Con
}
}

pub fn suicide<H: Handler>(runtime: &mut Runtime<'_>, handler: &mut H) -> Control<H> {
pub fn suicide<H: Handler>(runtime: &mut Runtime, handler: &mut H) -> Control<H> {
pop!(runtime, target);

match handler.mark_delete(runtime.context.address, target.into()) {
Expand All @@ -265,7 +265,7 @@ pub fn suicide<H: Handler>(runtime: &mut Runtime<'_>, handler: &mut H) -> Contro
Control::Exit(ExitSucceed::Suicided.into())
}

pub fn create<H: Handler>(runtime: &mut Runtime<'_>, is_create2: bool, handler: &mut H) -> Control<H> {
pub fn create<H: Handler>(runtime: &mut Runtime, is_create2: bool, handler: &mut H) -> Control<H> {
runtime.return_data_buffer = Vec::new();

pop_u256!(runtime, value, code_offset, len);
Expand Down Expand Up @@ -305,7 +305,7 @@ pub fn create<H: Handler>(runtime: &mut Runtime<'_>, is_create2: bool, handler:
}
}

pub fn call<H: Handler>(runtime: &mut Runtime<'_>, scheme: CallScheme, handler: &mut H) -> Control<H> {
pub fn call<H: Handler>(runtime: &mut Runtime, scheme: CallScheme, handler: &mut H) -> Control<H> {
runtime.return_data_buffer = Vec::new();

pop_u256!(runtime, gas);
Expand Down
22 changes: 11 additions & 11 deletions runtime/src/interrupt.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use crate::{Handler, Runtime};

/// Interrupt resolution.
pub enum Resolve<'a, 'config, H: Handler> {
pub enum Resolve<'a, H: Handler> {
/// Create interrupt resolution.
Create(H::CreateInterrupt, ResolveCreate<'a, 'config>),
Create(H::CreateInterrupt, ResolveCreate<'a>),
/// Call interrupt resolution.
Call(H::CallInterrupt, ResolveCall<'a, 'config>),
Call(H::CallInterrupt, ResolveCall<'a>),
}

/// Create interrupt resolution.
pub struct ResolveCreate<'a, 'config> {
_runtime: &'a mut Runtime<'config>,
pub struct ResolveCreate<'a> {
_runtime: &'a mut Runtime,
}

impl<'a, 'config> ResolveCreate<'a, 'config> {
pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self {
impl<'a> ResolveCreate<'a> {
pub(crate) fn new(runtime: &'a mut Runtime) -> Self {
Self { _runtime: runtime }
}
}

/// Call interrupt resolution.
pub struct ResolveCall<'a, 'config> {
_runtime: &'a mut Runtime<'config>,
pub struct ResolveCall<'a> {
_runtime: &'a mut Runtime,
}

impl<'a, 'config> ResolveCall<'a, 'config> {
pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self {
impl<'a> ResolveCall<'a> {
pub(crate) fn new(runtime: &'a mut Runtime) -> Self {
Self { _runtime: runtime }
}
}
15 changes: 7 additions & 8 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,31 @@ macro_rules! step {
/// EVM runtime.
///
/// The runtime wraps an EVM `Machine` with support of return data and context.
pub struct Runtime<'config> {
pub struct Runtime {
machine: Machine,
status: Result<(), ExitReason>,
return_data_buffer: Vec<u8>,
return_data_len: U256,
return_data_offset: U256,
context: Context,
_config: &'config Config,
}

impl<'config> Runtime<'config> {
impl Runtime {
/// Create a new runtime with given code and data.
pub fn new(
code: Rc<Vec<u8>>,
data: Rc<Vec<u8>>,
context: Context,
config: &'config Config,
stack_limit: usize,
memory_limit: usize,
) -> Self {
Self {
machine: Machine::new(code, data, config.stack_limit, config.memory_limit),
machine: Machine::new(code, data, stack_limit, memory_limit),
status: Ok(()),
return_data_buffer: Vec::new(),
return_data_len: U256::zero(),
return_data_offset: U256::zero(),
context,
_config: config,
}
}

Expand All @@ -150,15 +149,15 @@ impl<'config> Runtime<'config> {
pub fn step<'a, H: Handler>(
&'a mut self,
handler: &mut H,
) -> Result<(), Capture<ExitReason, Resolve<'a, 'config, H>>> {
) -> Result<(), Capture<ExitReason, Resolve<'a, H>>> {
step!(self, handler, return Err; Ok)
}

/// Loop stepping the runtime until it stops.
pub fn run<'a, H: Handler>(
&'a mut self,
handler: &mut H,
) -> Capture<ExitReason, Resolve<'a, 'config, H>> {
) -> Capture<ExitReason, Resolve<'a, H>> {
loop {
step!(self, handler, return;)
}
Expand Down
Loading

0 comments on commit e7138f7

Please sign in to comment.