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, HashMap, B256, U256};
13use state::{Account, AccountInfo, Bytecode};
14use std::string::String;
15
16#[cfg(feature = "asyncdb")]
17pub mod async_db;
18pub mod empty_db;
19pub mod try_commit;
20
21#[cfg(feature = "asyncdb")]
22pub use async_db::{DatabaseAsync, WrapDatabaseAsync};
23pub use empty_db::{EmptyDB, EmptyDBTyped};
24pub use try_commit::{ArcUpgradeError, TryDatabaseCommit};
25
26pub trait DBErrorMarker {}
28
29impl DBErrorMarker for () {}
31impl DBErrorMarker for Infallible {}
32impl DBErrorMarker for String {}
33
34#[auto_impl(&mut, Box)]
36pub trait Database {
37 type Error: DBErrorMarker + Error;
39
40 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
42
43 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
45
46 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
48
49 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
51}
52
53#[auto_impl(&mut, Box)]
55pub trait DatabaseCommit {
56 fn commit(&mut self, changes: HashMap<Address, Account>);
58}
59
60#[auto_impl(&, &mut, Box, Rc, Arc)]
67pub trait DatabaseRef {
68 type Error: DBErrorMarker + Error;
70
71 fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
73
74 fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
76
77 fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
79
80 fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error>;
82}
83
84#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
86pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);
87
88impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
89 #[inline]
90 fn from(f: F) -> Self {
91 WrapDatabaseRef(f)
92 }
93}
94
95impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
96 type Error = T::Error;
97
98 #[inline]
99 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
100 self.0.basic_ref(address)
101 }
102
103 #[inline]
104 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
105 self.0.code_by_hash_ref(code_hash)
106 }
107
108 #[inline]
109 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
110 self.0.storage_ref(address, index)
111 }
112
113 #[inline]
114 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
115 self.0.block_hash_ref(number)
116 }
117}
118
119impl<T: DatabaseRef + DatabaseCommit> DatabaseCommit for WrapDatabaseRef<T> {
120 #[inline]
121 fn commit(&mut self, changes: HashMap<Address, Account>) {
122 self.0.commit(changes)
123 }
124}