use crate::{Account, AccountInfo, Address, Bytecode, HashMap, B256, U256};
use auto_impl::auto_impl;
pub mod components;
pub use components::{
BlockHash, BlockHashRef, DatabaseComponentError, DatabaseComponents, State, StateRef,
};
#[auto_impl(&mut, Box)]
pub trait Database {
type Error;
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error>;
}
#[auto_impl(&mut, Box)]
pub trait DatabaseCommit {
fn commit(&mut self, changes: HashMap<Address, Account>);
}
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait DatabaseRef {
type Error;
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error>;
}
#[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)
}
}
pub trait DatabaseWithDebugError: Database
where
<Self as Database>::Error: std::fmt::Debug + std::fmt::Display,
{
}
impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
type Error = T::Error;
#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.0.basic_ref(address)
}
#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.0.code_by_hash_ref(code_hash)
}
#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.0.storage_ref(address, index)
}
#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.0.block_hash_ref(number)
}
}
impl<T: DatabaseRef + DatabaseCommit> DatabaseCommit for WrapDatabaseRef<T> {
#[inline]
fn commit(&mut self, changes: HashMap<Address, Account>) {
self.0.commit(changes)
}
}
#[doc(hidden)]
#[deprecated = "use `WrapDatabaseRef` instead"]
pub struct RefDBWrapper<'a, E> {
pub db: &'a dyn DatabaseRef<Error = E>,
}
#[allow(deprecated)]
impl<'a, E> RefDBWrapper<'a, E> {
#[inline]
pub fn new(db: &'a dyn DatabaseRef<Error = E>) -> Self {
Self { db }
}
}
#[allow(deprecated)]
impl<'a, E> Database for RefDBWrapper<'a, E> {
type Error = E;
#[inline]
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
self.db.basic_ref(address)
}
#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.db.code_by_hash_ref(code_hash)
}
#[inline]
fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
self.db.storage_ref(address, index)
}
#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.db.block_hash_ref(number)
}
}