revm_database_interface/
lib.rs1#![cfg_attr(not(test), warn(unused_crate_dependencies))]
3#![cfg_attr(not(feature = "std"), no_std)]
4
5#[cfg(not(feature = "std"))]
6extern crate alloc as std;
7
8use core::convert::Infallible;
9
10use auto_impl::auto_impl;
11use core::error::Error;
12use primitives::{address, Address, HashMap, StorageKey, StorageValue, B256, U256};
13use state::{Account, AccountInfo, Bytecode};
14use std::string::String;
15
16pub const FFADDRESS: Address = address!("0xffffffffffffffffffffffffffffffffffffffff");
18pub const BENCH_TARGET: Address = FFADDRESS;
20pub const BENCH_TARGET_BALANCE: U256 = U256::from_limbs([10_000_000_000_000_000, 0, 0, 0]);
22pub const EEADDRESS: Address = address!("0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
24pub const BENCH_CALLER: Address = EEADDRESS;
26pub const BENCH_CALLER_BALANCE: U256 = U256::from_limbs([10_000_000_000_000_000, 0, 0, 0]);
28
29#[cfg(feature = "asyncdb")]
30pub mod async_db;
31pub mod either;
32pub mod empty_db;
33pub mod try_commit;
34
35#[cfg(feature = "asyncdb")]
36pub use async_db::{DatabaseAsync, WrapDatabaseAsync};
37pub use empty_db::{EmptyDB, EmptyDBTyped};
38pub use try_commit::{ArcUpgradeError, TryDatabaseCommit};
39
40pub trait DBErrorMarker {}
42
43impl DBErrorMarker for () {}
45impl DBErrorMarker for Infallible {}
46impl DBErrorMarker for String {}
47
48#[auto_impl(&mut, Box)]
50pub trait Database {
51 type Error: DBErrorMarker + Error;
53
54 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
56
57 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
59
60 fn storage(&mut self, address: Address, index: StorageKey)
62 -> Result<StorageValue, Self::Error>;
63
64 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
66}
67
68#[auto_impl(&mut, Box)]
70pub trait DatabaseCommit {
71 fn commit(&mut self, changes: HashMap<Address, Account>);
73}
74
75#[auto_impl(&, &mut, Box, Rc, Arc)]
82pub trait DatabaseRef {
83 type Error: DBErrorMarker + Error;
85
86 fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
88
89 fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
91
92 fn storage_ref(&self, address: Address, index: StorageKey)
94 -> Result<StorageValue, Self::Error>;
95
96 fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error>;
98}
99
100#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
102pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);
103
104impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
105 #[inline]
106 fn from(f: F) -> Self {
107 WrapDatabaseRef(f)
108 }
109}
110
111impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
112 type Error = T::Error;
113
114 #[inline]
115 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
116 self.0.basic_ref(address)
117 }
118
119 #[inline]
120 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
121 self.0.code_by_hash_ref(code_hash)
122 }
123
124 #[inline]
125 fn storage(
126 &mut self,
127 address: Address,
128 index: StorageKey,
129 ) -> Result<StorageValue, Self::Error> {
130 self.0.storage_ref(address, index)
131 }
132
133 #[inline]
134 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
135 self.0.block_hash_ref(number)
136 }
137}
138
139impl<T: DatabaseRef + DatabaseCommit> DatabaseCommit for WrapDatabaseRef<T> {
140 #[inline]
141 fn commit(&mut self, changes: HashMap<Address, Account>) {
142 self.0.commit(changes)
143 }
144}