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: u64) -> 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: u64) -> 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)
}
}
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: u64) -> 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)
}
}