Skip to content

Commit

Permalink
feat: add more auto_impls to revm traits (bluealloy#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored and Wodann committed Dec 28, 2023
1 parent c0209bf commit d6fea43
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
25 changes: 17 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,20 +29,21 @@ 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 {
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 @@ -60,6 +62,13 @@ pub trait DatabaseRef {
/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
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 @@ -18,7 +18,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 @@ -13,6 +13,7 @@ use crate::primitives::{
use crate::{db::Database, journaled_state::JournaledState, precompile, Inspector};
use alloc::boxed::Box;
use alloc::vec::Vec;
use auto_impl::auto_impl;
use core::marker::PhantomData;
use revm_interpreter::gas::initial_tx_gas;
use revm_interpreter::MAX_CODE_SIZE;
Expand Down Expand Up @@ -65,6 +66,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 @@ -76,7 +79,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 d6fea43

Please sign in to comment.