Skip to content

Commit

Permalink
feat: add more auto_impls to revm traits (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Oct 16, 2023
1 parent 2f78eb9 commit 94642fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
24 changes: 16 additions & 8 deletions crates/primitives/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use components::{
/// EVM database interface.
#[auto_impl(&mut, Box)]
pub trait Database {
/// The database error type.
type Error;

/// Get basic account information.
Expand All @@ -28,22 +29,22 @@ pub trait Database {
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error>;
}

impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
fn from(f: F) -> Self {
WrapDatabaseRef(f)
}
}

/// EVM database commit interface.
#[auto_impl(&mut, Box)]
pub trait DatabaseCommit {
/// Commit changes to the database.
fn commit(&mut self, changes: Map<Address, Account>);
}

/// Same as [Database], but uses immutable references.
#[auto_impl(&, Box, Arc)]
/// EVM database interface.
///
/// Contains the same methods as [`Database`], but with `&self` receivers instead of `&mut self`.
///
/// Use [`WrapDatabaseRef`] to provide [`Database`] implementation for a type
/// that only implements this trait.
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait DatabaseRef {
/// The database error type.
type Error;

/// Get basic account information.
Expand All @@ -63,6 +64,13 @@ pub trait DatabaseRef {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);

impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
#[inline]
fn from(f: F) -> Self {
WrapDatabaseRef(f)
}
}

impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
type Error = T::Error;

Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/db/components/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use alloc::sync::Arc;
use auto_impl::auto_impl;
use core::ops::Deref;

#[auto_impl(& mut, Box)]
#[auto_impl(&mut, Box)]
pub trait BlockHash {
type Error;

/// Get block hash by block number
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error>;
}

#[auto_impl(&, Box, Arc)]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait BlockHashRef {
type Error;

Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/db/components/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::sync::Arc;
use auto_impl::auto_impl;
use core::ops::Deref;

#[auto_impl(& mut, Box)]
#[auto_impl(&mut, Box)]
pub trait State {
type Error;

Expand All @@ -20,7 +20,7 @@ pub trait State {
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
}

#[auto_impl(&, Box, Arc)]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait StateRef {
type Error;

Expand Down
5 changes: 4 additions & 1 deletion crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{db::Database, journaled_state::JournaledState, precompile, Inspector
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use auto_impl::auto_impl;
use core::fmt;
use core::marker::PhantomData;
use revm_interpreter::opcode::make_boxed_instruction_table;
Expand Down Expand Up @@ -89,6 +90,8 @@ struct CallResult {
return_value: Bytes,
}

/// EVM transaction interface.
#[auto_impl(&mut, Box)]
pub trait Transact<DBError> {
/// Run checks that could make transaction fail before call/create.
fn preverify_transaction(&mut self) -> Result<(), EVMError<DBError>>;
Expand All @@ -100,7 +103,7 @@ pub trait Transact<DBError> {
#[inline]
fn transact(&mut self) -> EVMResult<DBError> {
self.preverify_transaction()
.and_then(|_| self.transact_preverified())
.and_then(|()| self.transact_preverified())
}
}

Expand Down

0 comments on commit 94642fa

Please sign in to comment.