revm_primitives/
db.rs
1use crate::{Account, AccountInfo, Address, Bytecode, HashMap, B256, U256};
2use auto_impl::auto_impl;
3
4pub mod components;
5pub use components::{
6 BlockHash, BlockHashRef, DatabaseComponentError, DatabaseComponents, State, StateRef,
7};
8
9#[auto_impl(&mut, Box)]
11pub trait Database {
12 type Error;
14
15 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
17
18 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
20
21 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
23
24 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
26}
27
28#[auto_impl(&mut, Box)]
30pub trait DatabaseCommit {
31 fn commit(&mut self, changes: HashMap<Address, Account>);
33}
34
35#[auto_impl(&, &mut, Box, Rc, Arc)]
42pub trait DatabaseRef {
43 type Error;
45
46 fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
48
49 fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
51
52 fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
54
55 fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error>;
57}
58
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
61pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);
62
63impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
64 #[inline]
65 fn from(f: F) -> Self {
66 WrapDatabaseRef(f)
67 }
68}
69
70impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
71 type Error = T::Error;
72
73 #[inline]
74 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
75 self.0.basic_ref(address)
76 }
77
78 #[inline]
79 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
80 self.0.code_by_hash_ref(code_hash)
81 }
82
83 #[inline]
84 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
85 self.0.storage_ref(address, index)
86 }
87
88 #[inline]
89 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
90 self.0.block_hash_ref(number)
91 }
92}
93
94impl<T: DatabaseRef + DatabaseCommit> DatabaseCommit for WrapDatabaseRef<T> {
95 #[inline]
96 fn commit(&mut self, changes: HashMap<Address, Account>) {
97 self.0.commit(changes)
98 }
99}