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

feat: derive more traits #745

Merged
merged 9 commits into from
Oct 11, 2023
Merged
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
6 changes: 3 additions & 3 deletions bins/revme/src/cli_env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use revm::primitives::{Address, Bytes, Env, TransactTo, U256};
use structopt::StructOpt;

#[derive(StructOpt, Clone, Debug)]
#[derive(StructOpt, Clone, Debug, PartialEq, Eq)]
pub struct CliEnv {
#[structopt(flatten)]
block: CliEnvBlock,
Expand Down Expand Up @@ -51,7 +51,7 @@ impl From<CliEnv> for Env {
}
}

#[derive(StructOpt, Clone, Debug)]
#[derive(StructOpt, Clone, Debug, PartialEq, Eq)]
pub struct CliEnvBlock {
#[structopt(long = "env.block.gas_limit")]
pub block_gas_limit: Option<u64>,
Expand All @@ -71,7 +71,7 @@ pub struct CliEnvBlock {
pub basefee: Option<u64>,
}

#[derive(StructOpt, Clone, Debug)]
#[derive(StructOpt, Clone, Debug, PartialEq, Eq)]
pub struct CliEnvTx {
/// Caller or Author or tx signer
#[structopt(long = "env.tx.caller")]
Expand Down
2 changes: 1 addition & 1 deletion bins/revme/src/statetest/merkle_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
sec_trie_root::<KeccakHasher, _, _, _>(input)
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeccakHasher;

impl Hasher for KeccakHasher {
Expand Down
2 changes: 1 addition & 1 deletion bins/revme/src/statetest/models/spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use revm::primitives::SpecId;
use serde::Deserialize;

#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Ord, Deserialize)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Hash)]
pub enum SpecName {
Frontier,
FrontierToHomesteadAt5,
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use calc::*;
pub use constants::*;

/// Represents the state of gas during execution.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Gas {
/// The initial gas limit.
limit: u64,
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ mod dummy;
/// EVM context host.
pub trait Host {
/// Called before the interpreter executes an instruction.
fn step(&mut self, interpreter: &mut Interpreter) -> InstructionResult;
fn step(&mut self, interpreter: &mut Interpreter<'_>) -> InstructionResult;

/// Called after the interpreter executes an instruction.
fn step_end(
&mut self,
interpreter: &mut Interpreter,
interpreter: &mut Interpreter<'_>,
ret: InstructionResult,
) -> InstructionResult;

Expand Down
9 changes: 4 additions & 5 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use alloc::vec::Vec;

/// A dummy [Host] implementation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DummyHost {
pub env: Env,
pub storage: HashMap<U256, U256>,
Expand All @@ -20,9 +21,7 @@ impl DummyHost {
pub fn new(env: Env) -> Self {
Self {
env,
storage: HashMap::new(),
transient_storage: Default::default(),
log: Vec::new(),
..Default::default()
}
}

Expand All @@ -36,14 +35,14 @@ impl DummyHost {

impl Host for DummyHost {
#[inline]
fn step(&mut self, _interp: &mut Interpreter) -> InstructionResult {
fn step(&mut self, _interp: &mut Interpreter<'_>) -> InstructionResult {
InstructionResult::Continue
}

#[inline]
fn step_end(
&mut self,
_interp: &mut Interpreter,
_interp: &mut Interpreter<'_>,
_ret: InstructionResult,
) -> InstructionResult {
InstructionResult::Continue
Expand Down
10 changes: 6 additions & 4 deletions crates/interpreter/src/inner_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use crate::primitives::CreateScheme;
use crate::primitives::{Address, Bytes, U256};

/// Inputs for a call.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallInputs {
/// The target of the call.
Expand All @@ -19,6 +20,7 @@ pub struct CallInputs {
}

/// Inputs for a create call.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateInputs {
/// Caller address of the EVM.
Expand All @@ -34,7 +36,7 @@ pub struct CreateInputs {
}

/// Call schemes.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CallScheme {
/// `CALL`
Expand All @@ -48,7 +50,7 @@ pub enum CallScheme {
}

/// Context of a runtime call.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallContext {
/// Execution address.
Expand Down Expand Up @@ -76,7 +78,7 @@ impl Default for CallContext {
}

/// Transfer from source to target, with given value.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Transfer {
/// The source address.
Expand All @@ -88,7 +90,7 @@ pub struct Transfer {
}

/// Result of a call that resulted in a self destruct.
#[derive(Default)]
#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelfDestructResult {
pub had_value: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instruction_result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::primitives::{Eval, Halt};

#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InstructionResult {
// success codes
Expand Down Expand Up @@ -89,7 +89,7 @@ impl InstructionResult {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum SuccessOrHalt {
Success(Eval),
Revert,
Expand Down
22 changes: 11 additions & 11 deletions crates/interpreter/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,67 @@ use crate::{
Host, InstructionResult, Interpreter,
};

pub fn wrapped_add<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn wrapped_add<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_add(*op2);
}

pub fn wrapping_mul<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn wrapping_mul<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_mul(*op2);
}

pub fn wrapping_sub<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn wrapping_sub<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.wrapping_sub(*op2);
}

pub fn div<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn div<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = op1.wrapping_div(*op2);
}
}

pub fn sdiv<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn sdiv<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = i256_div(op1, *op2);
}

pub fn rem<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn rem<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = op1.wrapping_rem(*op2);
}
}

pub fn smod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn smod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = i256_mod(op1, *op2)
}
}

pub fn addmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn addmod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::MID);
pop_top!(interpreter, op1, op2, op3);
*op3 = op1.add_mod(op2, *op3)
}

pub fn mulmod<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn mulmod<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::MID);
pop_top!(interpreter, op1, op2, op3);
*op3 = op1.mul_mod(op2, *op3)
}

pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
pop_top!(interpreter, op1, op2);
gas_or_fail!(interpreter, gas::exp_cost::<SPEC>(*op2));
*op2 = op1.pow(*op2);
Expand All @@ -86,7 +86,7 @@ pub fn exp<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
/// `y | !mask` where `|` is the bitwise `OR` and `!` is bitwise negation. Similarly, if
/// `b == 0` then the yellow paper says the output should start with all zeros, then end with
/// bits from `b`; this is equal to `y & mask` where `&` is bitwise `AND`.
pub fn signextend<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn signextend<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if op1 < U256::from(32) {
Expand Down
28 changes: 14 additions & 14 deletions crates/interpreter/src/instructions/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,67 @@ use crate::{
};
use core::cmp::Ordering;

pub fn lt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn lt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 < *op2);
}

pub fn gt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn gt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 > *op2);
}

pub fn slt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn slt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Less);
}

pub fn sgt<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn sgt<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(i256_cmp(&op1, op2) == Ordering::Greater);
}

pub fn eq<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn eq<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = U256::from(op1 == *op2);
}

pub fn iszero<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn iszero<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1);
*op1 = U256::from(*op1 == U256::ZERO);
}

pub fn bitand<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn bitand<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 & *op2;
}

pub fn bitor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn bitor<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 | *op2;
}

pub fn bitxor<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn bitxor<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 = op1 ^ *op2;
}

pub fn not<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn not<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1);
*op1 = !*op1;
}

pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn byte<H: Host>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);

Expand All @@ -80,23 +80,23 @@ pub fn byte<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn shl<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 <<= as_usize_saturated!(op1);
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn shr<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
*op2 >>= as_usize_saturated!(op1);
}

/// EIP-145: Bitwise shifting instructions in EVM
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H) {
pub fn sar<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, _host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
gas!(interpreter, gas::VERYLOW);
pop_top!(interpreter, op1, op2);
Expand Down
Loading
Loading