Skip to content

Commit

Permalink
chore: impl EmptyDB traits manually
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 6, 2023
1 parent db02975 commit cc7fd5e
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions crates/revm/src/db/emptydb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{convert::Infallible, marker::PhantomData};
use core::{convert::Infallible, fmt, marker::PhantomData};
use revm_interpreter::primitives::{
db::{Database, DatabaseRef},
AccountInfo, Bytecode, B160, B256, U256,
Expand All @@ -10,21 +10,40 @@ pub type EmptyDB = EmptyDBTyped<Infallible>;
/// An empty database that always returns default values when queried.
///
/// This is generic over a type which is used as the database error type.
#[derive(Debug, Clone)]
pub struct EmptyDBTyped<E> {
_phantom: PhantomData<E>,
}

// Don't derive it because it doesn't need `E: Default`.
// Don't derive traits, because the type parameter is unused.
impl<E> Clone for EmptyDBTyped<E> {
fn clone(&self) -> Self {
*self
}
}

impl<E> Copy for EmptyDBTyped<E> {}

impl<E> Default for EmptyDBTyped<E> {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}

impl<E> fmt::Debug for EmptyDBTyped<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EmptyDB").finish_non_exhaustive()
}
}

impl<E> PartialEq for EmptyDBTyped<E> {
fn eq(&self, _: &Self) -> bool {
true
}
}

impl<E> Eq for EmptyDBTyped<E> {}

impl<E> EmptyDBTyped<E> {
#[inline(always)]
pub fn new() -> Self {
Self {
_phantom: PhantomData,
Expand All @@ -33,7 +52,6 @@ impl<E> EmptyDBTyped<E> {

#[doc(hidden)]
#[deprecated = "use `new` instead"]
#[inline(always)]
pub fn new_keccak_block_hash() -> Self {
Self::new()
}
Expand All @@ -42,22 +60,22 @@ impl<E> EmptyDBTyped<E> {
impl<E> Database for EmptyDBTyped<E> {
type Error = E;

#[inline(always)]
#[inline]
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> {
<Self as DatabaseRef>::basic(self, address)
}

#[inline(always)]
#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
<Self as DatabaseRef>::code_by_hash(self, code_hash)
}

#[inline(always)]
#[inline]
fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> {
<Self as DatabaseRef>::storage(self, address, index)
}

#[inline(always)]
#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
<Self as DatabaseRef>::block_hash(self, number)
}
Expand All @@ -66,22 +84,22 @@ impl<E> Database for EmptyDBTyped<E> {
impl<E> DatabaseRef for EmptyDBTyped<E> {
type Error = E;

#[inline(always)]
#[inline]
fn basic(&self, _address: B160) -> Result<Option<AccountInfo>, Self::Error> {
Ok(None)
}

#[inline(always)]
#[inline]
fn code_by_hash(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
Ok(Bytecode::new())
}

#[inline(always)]
#[inline]
fn storage(&self, _address: B160, _index: U256) -> Result<U256, Self::Error> {
Ok(U256::default())
}

#[inline(always)]
#[inline]
fn block_hash(&self, number: U256) -> Result<B256, Self::Error> {
Ok(number.to_be_bytes().into())
}
Expand Down

0 comments on commit cc7fd5e

Please sign in to comment.