From 94642faca89f3cdbd3d34cccb6bbfe13a1baaf47 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:37:45 +0200 Subject: [PATCH] feat: add more `auto_impl`s to revm traits (#799) --- crates/primitives/src/db.rs | 24 ++++++++++++------- .../src/db/components/block_hash.rs | 4 ++-- crates/primitives/src/db/components/state.rs | 4 ++-- crates/revm/src/evm_impl.rs | 5 +++- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/crates/primitives/src/db.rs b/crates/primitives/src/db.rs index 1e4d6c1e98..6ce54028c2 100644 --- a/crates/primitives/src/db.rs +++ b/crates/primitives/src/db.rs @@ -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. @@ -28,12 +29,6 @@ pub trait Database { fn block_hash(&mut self, number: U256) -> Result; } -impl From for WrapDatabaseRef { - fn from(f: F) -> Self { - WrapDatabaseRef(f) - } -} - /// EVM database commit interface. #[auto_impl(&mut, Box)] pub trait DatabaseCommit { @@ -41,9 +36,15 @@ pub trait DatabaseCommit { fn commit(&mut self, changes: Map); } -/// 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. @@ -63,6 +64,13 @@ pub trait DatabaseRef { #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct WrapDatabaseRef(pub T); +impl From for WrapDatabaseRef { + #[inline] + fn from(f: F) -> Self { + WrapDatabaseRef(f) + } +} + impl Database for WrapDatabaseRef { type Error = T::Error; diff --git a/crates/primitives/src/db/components/block_hash.rs b/crates/primitives/src/db/components/block_hash.rs index f5b6959003..300f4d6dc8 100644 --- a/crates/primitives/src/db/components/block_hash.rs +++ b/crates/primitives/src/db/components/block_hash.rs @@ -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 BlockHash { type Error; @@ -14,7 +14,7 @@ pub trait BlockHash { fn block_hash(&mut self, number: U256) -> Result; } -#[auto_impl(&, Box, Arc)] +#[auto_impl(&, &mut, Box, Rc, Arc)] pub trait BlockHashRef { type Error; diff --git a/crates/primitives/src/db/components/state.rs b/crates/primitives/src/db/components/state.rs index 6b7ed2941c..63b0fd5c26 100644 --- a/crates/primitives/src/db/components/state.rs +++ b/crates/primitives/src/db/components/state.rs @@ -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; @@ -20,7 +20,7 @@ pub trait State { fn storage(&mut self, address: Address, index: U256) -> Result; } -#[auto_impl(&, Box, Arc)] +#[auto_impl(&, &mut, Box, Rc, Arc)] pub trait StateRef { type Error; diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 486ecbfe17..95e4a31ad7 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -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; @@ -89,6 +90,8 @@ struct CallResult { return_value: Bytes, } +/// EVM transaction interface. +#[auto_impl(&mut, Box)] pub trait Transact { /// Run checks that could make transaction fail before call/create. fn preverify_transaction(&mut self) -> Result<(), EVMError>; @@ -100,7 +103,7 @@ pub trait Transact { #[inline] fn transact(&mut self) -> EVMResult { self.preverify_transaction() - .and_then(|_| self.transact_preverified()) + .and_then(|()| self.transact_preverified()) } }