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 5 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
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 @@ -65,7 +65,7 @@ pub fn trie_root(acc_data: Vec<(H160, Bytes)>) -> B256 {
B256(sec_trie_root::<KeccakHasher, _, _, _>(acc_data).0)
}

#[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 crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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
5 changes: 2 additions & 3 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
};
use alloc::vec::Vec;

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

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::{Bytes, B160, 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 @@ -22,6 +23,7 @@ pub struct CallInputs {
pub is_static: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateInputs {
pub caller: B160,
Expand All @@ -36,7 +38,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 @@ -50,7 +52,7 @@ pub enum CallScheme {
}

/// CallContext of the runtime.
#[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 @@ -78,7 +80,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 {
/// Source address.
Expand All @@ -89,7 +91,7 @@ pub struct Transfer {
pub value: U256,
}

#[derive(Default)]
#[derive(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
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/i256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::primitives::U256;
use core::cmp::Ordering;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(i8)]
pub enum Sign {
// same as `cmp::Ordering`
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod analysis;
mod contract;
pub mod memory;
pub(crate) mod memory;
mod stack;

use crate::primitives::{Bytes, Spec};
Expand All @@ -9,7 +9,7 @@ use crate::{alloc::boxed::Box, opcode::eval, Gas, Host, InstructionResult};
pub use analysis::BytecodeLocked;
pub use contract::Contract;
pub use memory::Memory;
pub use stack::{Stack, STACK_LIMIT};
pub use stack::Stack;

pub const CALL_STACK_LIMIT: u64 = 1024;

Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::{

/// A sequential memory. It uses Rust's `Vec` for internal
/// representation.
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Memory {
data: Vec<u8>,
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use alloc::vec::Vec;
use core::fmt;

/// The EVM stack limit, in number of items.
pub const STACK_LIMIT: usize = 1024;
const STACK_LIMIT: usize = 1024;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it as a pub, it does not hurt


/// EVM stack.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Stack {
data: Vec<U256>,
Expand Down
3 changes: 3 additions & 0 deletions crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![warn(missing_debug_implementations, unreachable_pub)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


extern crate alloc;

Expand Down
10 changes: 7 additions & 3 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![no_std]
#![warn(missing_debug_implementations)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
extern crate alloc;
Expand Down Expand Up @@ -64,7 +67,7 @@ impl Default for Precompiles {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Precompile {
Standard(StandardPrecompileFn),
Env(EnvPrecompileFn),
Expand All @@ -79,6 +82,7 @@ impl fmt::Debug for Precompile {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PrecompileAddress(B160, Precompile);

impl From<PrecompileAddress> for (B160, Precompile) {
Expand All @@ -87,7 +91,7 @@ impl From<PrecompileAddress> for (B160, Precompile) {
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum SpecId {
HOMESTEAD,
BYZANTIUM,
Expand Down
5 changes: 2 additions & 3 deletions crates/precompile/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn};
use alloc::vec::Vec;
use core::cmp::min;

pub const ECRECOVER: PrecompileAddress = PrecompileAddress(
crate::u64_to_b160(1),
Expand Down Expand Up @@ -60,9 +62,6 @@ mod secp256k1 {
}

fn ec_recover_run(i: &[u8], target_gas: u64) -> PrecompileResult {
use alloc::vec::Vec;
use core::cmp::min;

const ECRECOVER_BASE: u64 = 3_000;

if ECRECOVER_BASE > target_gas {
Expand Down
12 changes: 6 additions & 6 deletions crates/primitives/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl<'de> serde::Deserialize<'de> for B160 {

// code optained from: https://docs.rs/impl-serde/0.4.0/impl_serde/
#[cfg(feature = "serde")]
#[allow(unreachable_pub)]
mod serialize {

use alloc::string::String;
use core::{fmt, result::Result};
use serde::{de, Deserializer, Serializer};
Expand Down Expand Up @@ -164,7 +164,7 @@ mod serialize {
}

/// Decoding bytes from hex string error.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum FromHexError {
/// Invalid (non-hex) character encountered.
InvalidHex {
Expand All @@ -179,7 +179,7 @@ mod serialize {
impl std::error::Error for FromHexError {}

impl fmt::Display for FromHexError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::InvalidHex { character, index } => {
write!(fmt, "invalid hex character: {character}, at {index}")
Expand Down Expand Up @@ -245,7 +245,7 @@ mod serialize {
}

/// Expected length of bytes vector.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ExpectedLen<'a> {
/// Exact length in bytes.
Exact(&'a mut [u8]),
Expand All @@ -255,7 +255,7 @@ mod serialize {
}

impl<'a> fmt::Display for ExpectedLen<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ExpectedLen::Exact(ref v) => write!(fmt, "length of {}", v.len() * 2),
ExpectedLen::Between(min, ref v) => {
Expand All @@ -281,7 +281,7 @@ mod serialize {
impl<'a, 'b> de::Visitor<'b> for Visitor<'a> {
type Value = usize;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"a (both 0x-prefixed or not) hex string with {}",
Expand Down
6 changes: 3 additions & 3 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bytes::Bytes;
use core::fmt::Debug;

/// A map of valid `jump` destinations.
#[derive(Clone, Eq, PartialEq, Default)]
#[derive(Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JumpMap(pub Arc<BitVec<u8>>);

Expand Down Expand Up @@ -39,7 +39,7 @@ impl JumpMap {
}

/// State of the [`Bytecode`] analysis.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BytecodeState {
/// No analysis has been performed.
Expand All @@ -50,7 +50,7 @@ pub enum BytecodeState {
Analysed { len: usize, jump_map: JumpMap },
}

#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Bytecode {
#[cfg_attr(feature = "serde", serde(with = "crate::utilities::serde_hex_bytes"))]
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait DatabaseRef {
}

/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);

impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
Expand Down Expand Up @@ -87,6 +88,7 @@ impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
/// Wraps a `dyn DatabaseRef` to provide a [`Database`] implementation.
#[doc(hidden)]
#[deprecated = "use `WrapDatabaseRef` instead"]
#[allow(missing_debug_implementations)]
pub struct RefDBWrapper<'a, E> {
pub db: &'a dyn DatabaseRef<Error = E>,
}
Expand Down
18 changes: 9 additions & 9 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use bytes::Bytes;
use core::cmp::{min, Ordering};

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Env {
pub cfg: CfgEnv,
Expand All @@ -15,7 +15,7 @@ pub struct Env {
}

/// The block environment.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEnv {
/// The number of ancestor blocks of this block (block height).
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct BlockEnv {
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlobExcessGasAndPrice {
pub excess_blob_gas: u64,
Expand All @@ -78,7 +78,7 @@ impl BlobExcessGasAndPrice {
}

#[cfg(feature = "optimism")]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OptimismFields {
pub source_hash: Option<B256>,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl BlockEnv {
}

/// The transaction environment.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxEnv {
/// The caller, author or signer of the transaction.
Expand Down Expand Up @@ -196,7 +196,7 @@ impl TxEnv {
}

/// Transaction destination.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactTo {
/// Simple call to an address.
Expand Down Expand Up @@ -238,7 +238,7 @@ impl TransactTo {
}

/// Create scheme.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CreateScheme {
/// Legacy create scheme of `CREATE`.
Expand All @@ -251,7 +251,7 @@ pub enum CreateScheme {
}

/// EVM configuration.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct CfgEnv {
Expand Down Expand Up @@ -371,7 +371,7 @@ impl CfgEnv {
}

/// What bytecode analysis to perform.
#[derive(Clone, Default, Debug, Eq, PartialEq)]
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnalysisKind {
/// Do not perform bytecode analysis.
Expand Down
Loading