revm_database_interface/
lib.rs

1//! Optimism-specific constants, types, and helpers.
2#![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
26/// Database error marker is needed to implement From conversion for Error type.
27pub trait DBErrorMarker {}
28
29/// Implement marker for `()`.
30impl DBErrorMarker for () {}
31impl DBErrorMarker for Infallible {}
32impl DBErrorMarker for String {}
33
34/// EVM database interface.
35#[auto_impl(&mut, Box)]
36pub trait Database {
37    /// The database error type.
38    type Error: DBErrorMarker + Error;
39
40    /// Gets basic account information.
41    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
42
43    /// Gets account code by its hash.
44    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
45
46    /// Gets storage value of address at index.
47    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
48
49    /// Gets block hash by block number.
50    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
51}
52
53/// EVM database commit interface.
54#[auto_impl(&mut, Box)]
55pub trait DatabaseCommit {
56    /// Commit changes to the database.
57    fn commit(&mut self, changes: HashMap<Address, Account>);
58}
59
60/// EVM database interface.
61///
62/// Contains the same methods as [`Database`], but with `&self` receivers instead of `&mut self`.
63///
64/// Use [`WrapDatabaseRef`] to provide [`Database`] implementation for a type
65/// that only implements this trait.
66#[auto_impl(&, &mut, Box, Rc, Arc)]
67pub trait DatabaseRef {
68    /// The database error type.
69    type Error: DBErrorMarker + Error;
70
71    /// Gets basic account information.
72    fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
73
74    /// Gets account code by its hash.
75    fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
76
77    /// Gets storage value of address at index.
78    fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
79
80    /// Gets block hash by block number.
81    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error>;
82}
83
84/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
85#[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}