Skip to content

Commit

Permalink
Revert "Remove unused config from runtime (rust-ethereum#161)"
Browse files Browse the repository at this point in the history
This reverts commit e7138f7.
  • Loading branch information
vimpunk committed Apr 27, 2023
1 parent e7138f7 commit 75ade29
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 70 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) -> Control<H>
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<H>
}
}

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) -> Control
}
}

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) -> Control<H>
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: &mut
}
}

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, 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 }
}
}
15 changes: 8 additions & 7 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
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<Vec<u8>>,
data: Rc<Vec<u8>>,
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,
}
}

Expand All @@ -149,15 +150,15 @@ impl Runtime {
pub fn step<'a, H: Handler>(
&'a mut self,
handler: &mut H,
) -> Result<(), Capture<ExitReason, Resolve<'a, H>>> {
) -> Result<(), Capture<ExitReason, Resolve<'a, 'config, 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, H>> {
) -> Capture<ExitReason, Resolve<'a, 'config, H>> {
loop {
step!(self, handler, return;)
}
Expand Down
Loading

0 comments on commit 75ade29

Please sign in to comment.