Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove all dyn for opcode that reduce dyn trait cost #660

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H
opcode::SAR => bitwise::sar::<S>(interp, host),
opcode::KECCAK256 => system::calculate_keccak256(interp, host),
opcode::ADDRESS => system::address(interp, host),
opcode::BALANCE => host::balance::<S>(interp, host),
opcode::SELFBALANCE => host::selfbalance::<S>(interp, host),
opcode::BALANCE => host::balance::<H, S>(interp, host),
opcode::SELFBALANCE => host::selfbalance::<H, S>(interp, host),
opcode::CODESIZE => system::codesize(interp, host),
opcode::CODECOPY => system::codecopy(interp, host),
opcode::CALLDATALOAD => system::calldataload(interp, host),
Expand Down Expand Up @@ -144,40 +144,40 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H
opcode::RETURN => control::ret(interp, host),
opcode::REVERT => control::revert::<S>(interp, host),
opcode::INVALID => return_invalid(interp, host),
opcode::BASEFEE => host_env::basefee::<S>(interp, host),
opcode::ORIGIN => host_env::origin(interp, host),
opcode::CALLER => system::caller(interp, host),
opcode::BASEFEE => host_env::basefee::<H, S>(interp, host),
opcode::ORIGIN => host_env::origin::<H>(interp, host),
opcode::CALLER => system::caller::<H>(interp, host),
opcode::CALLVALUE => system::callvalue(interp, host),
opcode::GASPRICE => host_env::gasprice(interp, host),
opcode::EXTCODESIZE => host::extcodesize::<S>(interp, host),
opcode::EXTCODEHASH => host::extcodehash::<S>(interp, host),
opcode::EXTCODECOPY => host::extcodecopy::<S>(interp, host),
opcode::RETURNDATASIZE => system::returndatasize::<S>(interp, host),
opcode::RETURNDATACOPY => system::returndatacopy::<S>(interp, host),
opcode::BLOCKHASH => host::blockhash(interp, host),
opcode::EXTCODESIZE => host::extcodesize::<H, S>(interp, host),
opcode::EXTCODEHASH => host::extcodehash::<H, S>(interp, host),
opcode::EXTCODECOPY => host::extcodecopy::<H, S>(interp, host),
opcode::RETURNDATASIZE => system::returndatasize::<H, S>(interp, host),
opcode::RETURNDATACOPY => system::returndatacopy::<H, S>(interp, host),
opcode::BLOCKHASH => host::blockhash::<H>(interp, host),
opcode::COINBASE => host_env::coinbase(interp, host),
opcode::TIMESTAMP => host_env::timestamp(interp, host),
opcode::NUMBER => host_env::number(interp, host),
opcode::DIFFICULTY => host_env::difficulty::<H, S>(interp, host),
opcode::GASLIMIT => host_env::gaslimit(interp, host),
opcode::SLOAD => host::sload::<S>(interp, host),
opcode::SSTORE => host::sstore::<S>(interp, host),
opcode::SLOAD => host::sload::<H, S>(interp, host),
opcode::SSTORE => host::sstore::<H, S>(interp, host),
opcode::TSTORE => host::tstore::<H, S>(interp, host),
opcode::TLOAD => host::tload::<H, S>(interp, host),
opcode::GAS => system::gas(interp, host),
opcode::LOG0 => host::log::<0>(interp, host),
opcode::LOG1 => host::log::<1>(interp, host),
opcode::LOG2 => host::log::<2>(interp, host),
opcode::LOG3 => host::log::<3>(interp, host),
opcode::LOG4 => host::log::<4>(interp, host),
opcode::SELFDESTRUCT => host::selfdestruct::<S>(interp, host),
opcode::CREATE => host::create::<false, S>(interp, host), //check
opcode::CREATE2 => host::create::<true, S>(interp, host), //check
opcode::CALL => host::call::<S>(interp, host), //check
opcode::CALLCODE => host::call_code::<S>(interp, host), //check
opcode::DELEGATECALL => host::delegate_call::<S>(interp, host), //check
opcode::STATICCALL => host::static_call::<S>(interp, host), //check
opcode::CHAINID => host_env::chainid::<S>(interp, host),
opcode::LOG0 => host::log::<H, 0>(interp, host),
opcode::LOG1 => host::log::<H, 1>(interp, host),
opcode::LOG2 => host::log::<H, 2>(interp, host),
opcode::LOG3 => host::log::<H, 3>(interp, host),
opcode::LOG4 => host::log::<H, 4>(interp, host),
opcode::SELFDESTRUCT => host::selfdestruct::<H, S>(interp, host),
opcode::CREATE => host::create::<H, false, S>(interp, host), //check
opcode::CREATE2 => host::create::<H, true, S>(interp, host), //check
opcode::CALL => host::call::<H, S>(interp, host), //check
opcode::CALLCODE => host::call_code::<H, S>(interp, host), //check
opcode::DELEGATECALL => host::delegate_call::<H, S>(interp, host), //check
opcode::STATICCALL => host::static_call::<H, S>(interp, host), //check
opcode::CHAINID => host_env::chainid::<H, S>(interp, host),
opcode::MCOPY => memory::mcopy::<S>(interp, host),
_ => return_not_found(interp, host),
}
Expand Down
56 changes: 28 additions & 28 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use core::cmp::min;
use revm_primitives::BLOCK_HASH_HISTORY;

pub fn balance<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn balance<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some((balance, is_cold)) = host.balance(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
Expand All @@ -31,7 +31,7 @@ pub fn balance<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
push!(interpreter, balance);
}

pub fn selfbalance<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn selfbalance<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
// EIP-1884: Repricing for trie-size-dependent opcodes
check!(interpreter, SPEC::enabled(ISTANBUL));
gas!(interpreter, gas::LOW);
Expand All @@ -42,7 +42,7 @@ pub fn selfbalance<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Hos
push!(interpreter, balance);
}

pub fn extcodesize<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn extcodesize<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some((code, is_cold)) = host.code(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
Expand All @@ -66,7 +66,7 @@ pub fn extcodesize<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Hos
push!(interpreter, U256::from(code.len()));
}

pub fn extcodehash<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn extcodehash<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, SPEC::enabled(CONSTANTINOPLE)); // EIP-1052: EXTCODEHASH opcode
pop_address!(interpreter, address);
let Some((code_hash, is_cold)) = host.code_hash(address) else {
Expand All @@ -90,7 +90,7 @@ pub fn extcodehash<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Hos
push_b256!(interpreter, code_hash);
}

pub fn extcodecopy<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn extcodecopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
pop!(interpreter, memory_offset, code_offset, len_u256);

Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn extcodecopy<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Hos
.set_data(memory_offset, code_offset, len, code.bytes());
}

pub fn blockhash(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn blockhash<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BLOCKHASH);
pop_top!(interpreter, number);

Expand All @@ -140,7 +140,7 @@ pub fn blockhash(interpreter: &mut Interpreter, host: &mut dyn Host) {
*number = U256::ZERO;
}

pub fn sload<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn sload<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop!(interpreter, index);

let Some((value, is_cold)) = host.sload(interpreter.contract.address, index) else {
Expand All @@ -151,7 +151,7 @@ pub fn sload<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
push!(interpreter, value);
}

pub fn sstore<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn sstore<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);

pop!(interpreter, index, value);
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn tload<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
*index = host.tload(interpreter.contract.address, *index);
}

pub fn log<const N: u8>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn log<H: Host, const N: u8>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);

pop!(interpreter, offset, len);
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn log<const N: u8>(interpreter: &mut Interpreter, host: &mut dyn Host) {
host.log(interpreter.contract.address, topics, data);
}

pub fn selfdestruct<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn selfdestruct<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check_staticcall!(interpreter);
pop_address!(interpreter, target);

Expand All @@ -239,9 +239,9 @@ pub fn selfdestruct<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Ho
interpreter.instruction_result = InstructionResult::SelfDestruct;
}

pub fn prepare_create_inputs<const IS_CREATE2: bool, SPEC: Spec>(
pub fn prepare_create_inputs<H: Host, const IS_CREATE2: bool, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut dyn Host,
host: &mut H,
create_inputs: &mut Option<Box<CreateInputs>>,
) {
check_staticcall!(interpreter);
Expand Down Expand Up @@ -309,12 +309,12 @@ pub fn prepare_create_inputs<const IS_CREATE2: bool, SPEC: Spec>(
}));
}

pub fn create<const IS_CREATE2: bool, SPEC: Spec>(
pub fn create<H: Host, const IS_CREATE2: bool, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut dyn Host,
host: &mut H,
) {
let mut create_input: Option<Box<CreateInputs>> = None;
prepare_create_inputs::<IS_CREATE2, SPEC>(interpreter, host, &mut create_input);
prepare_create_inputs::<H, IS_CREATE2, SPEC>(interpreter, host, &mut create_input);

let Some(mut create_input) = create_input else {
return;
Expand Down Expand Up @@ -352,26 +352,26 @@ pub fn create<const IS_CREATE2: bool, SPEC: Spec>(
}
}

pub fn call<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
call_inner::<SPEC>(interpreter, CallScheme::Call, host);
pub fn call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
call_inner::<H, SPEC>(interpreter, CallScheme::Call, host);
}

pub fn call_code<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
call_inner::<SPEC>(interpreter, CallScheme::CallCode, host);
pub fn call_code<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
call_inner::<H, SPEC>(interpreter, CallScheme::CallCode, host);
}

pub fn delegate_call<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
call_inner::<SPEC>(interpreter, CallScheme::DelegateCall, host);
pub fn delegate_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
call_inner::<H, SPEC>(interpreter, CallScheme::DelegateCall, host);
}

pub fn static_call<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
call_inner::<SPEC>(interpreter, CallScheme::StaticCall, host);
pub fn static_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
call_inner::<H, SPEC>(interpreter, CallScheme::StaticCall, host);
}

fn prepare_call_inputs<SPEC: Spec>(
fn prepare_call_inputs<H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
scheme: CallScheme,
host: &mut dyn Host,
host: &mut H,
result_len: &mut usize,
result_offset: &mut usize,
result_call_inputs: &mut Option<Box<CallInputs>>,
Expand Down Expand Up @@ -511,10 +511,10 @@ fn prepare_call_inputs<SPEC: Spec>(
}));
}

pub fn call_inner<SPEC: Spec>(
pub fn call_inner<H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
scheme: CallScheme,
host: &mut dyn Host,
host: &mut H,
) {
match scheme {
CallScheme::DelegateCall => check!(interpreter, SPEC::enabled(HOMESTEAD)), // EIP-7: DELEGATECALL
Expand All @@ -526,7 +526,7 @@ pub fn call_inner<SPEC: Spec>(
let mut out_offset: usize = 0;
let mut out_len: usize = 0;
let mut call_input: Option<Box<CallInputs>> = None;
prepare_call_inputs::<SPEC>(
prepare_call_inputs::<H, SPEC>(
interpreter,
scheme,
host,
Expand Down
16 changes: 8 additions & 8 deletions crates/interpreter/src/instructions/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ use crate::{
gas, interpreter::Interpreter, primitives::Spec, primitives::SpecId::*, Host, InstructionResult,
};

pub fn chainid<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn chainid<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
// EIP-1344: ChainID opcode
check!(interpreter, SPEC::enabled(ISTANBUL));
gas!(interpreter, gas::BASE);
push!(interpreter, host.env().cfg.chain_id);
}

pub fn coinbase(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn coinbase<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push_b256!(interpreter, host.env().block.coinbase.into());
}

pub fn timestamp(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn timestamp<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, host.env().block.timestamp);
}

pub fn number(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn number<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, host.env().block.number);
}
Expand All @@ -33,24 +33,24 @@ pub fn difficulty<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut
}
}

pub fn gaslimit(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn gaslimit<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, host.env().block.gas_limit);
}

pub fn gasprice(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn gasprice<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, host.env().effective_gas_price());
}

pub fn basefee<SPEC: Spec>(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn basefee<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
// EIP-3198: BASEFEE opcode
check!(interpreter, SPEC::enabled(LONDON));
push!(interpreter, host.env().block.basefee);
}

pub fn origin(interpreter: &mut Interpreter, host: &mut dyn Host) {
pub fn origin<H: Host>(interpreter: &mut Interpreter, host: &mut H) {
gas!(interpreter, gas::BASE);
push_b256!(interpreter, host.env().tx.caller.into());
}
22 changes: 11 additions & 11 deletions crates/interpreter/src/instructions/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ pub fn calculate_keccak256(interpreter: &mut Interpreter, _host: &mut dyn Host)
push_b256!(interpreter, hash);
}

pub fn address(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn address<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push_b256!(interpreter, B256::from(interpreter.contract.address));
}

pub fn caller(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn caller<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push_b256!(interpreter, B256::from(interpreter.contract.caller));
}

pub fn codesize(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn codesize<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, U256::from(interpreter.contract.bytecode.len()));
}

pub fn codecopy(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn codecopy<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pop!(interpreter, memory_offset, code_offset, len);
let len = as_usize_or_fail!(interpreter, len, InstructionResult::InvalidOperandOOG);
gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64));
Expand All @@ -60,7 +60,7 @@ pub fn codecopy(interpreter: &mut Interpreter, _host: &mut dyn Host) {
);
}

pub fn calldataload(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn calldataload<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop!(interpreter, index);
let index = as_usize_saturated!(index);
Expand All @@ -77,17 +77,17 @@ pub fn calldataload(interpreter: &mut Interpreter, _host: &mut dyn Host) {
push_b256!(interpreter, load);
}

pub fn calldatasize(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn calldatasize<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, U256::from(interpreter.contract.input.len()));
}

pub fn callvalue(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn callvalue<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, interpreter.contract.value);
}

pub fn calldatacopy(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn calldatacopy<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pop!(interpreter, memory_offset, data_offset, len);
let len = as_usize_or_fail!(interpreter, len, InstructionResult::InvalidOperandOOG);
gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64));
Expand All @@ -108,7 +108,7 @@ pub fn calldatacopy(interpreter: &mut Interpreter, _host: &mut dyn Host) {
.set_data(memory_offset, data_offset, len, &interpreter.contract.input);
}

pub fn returndatasize<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn returndatasize<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
// EIP-211: New opcodes: RETURNDATASIZE and RETURNDATACOPY
check!(interpreter, SPEC::enabled(BYZANTIUM));
Expand All @@ -118,7 +118,7 @@ pub fn returndatasize<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn
);
}

pub fn returndatacopy<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn returndatacopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
// EIP-211: New opcodes: RETURNDATASIZE and RETURNDATACOPY
check!(interpreter, SPEC::enabled(BYZANTIUM));
pop!(interpreter, memory_offset, offset, len);
Expand All @@ -144,7 +144,7 @@ pub fn returndatacopy<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn
}
}

pub fn gas(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn gas<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::BASE);
push!(interpreter, U256::from(interpreter.gas.remaining()));
}