revm_database_interface/
empty_db.rs

1use crate::{DBErrorMarker, Database, DatabaseRef};
2use core::error::Error;
3use core::{convert::Infallible, fmt, marker::PhantomData};
4use primitives::{keccak256, Address, B256, U256};
5use state::{AccountInfo, Bytecode};
6use std::string::ToString;
7
8/// An empty database that always returns default values when queried
9pub type EmptyDB = EmptyDBTyped<Infallible>;
10
11/// An empty database that always returns default values when queried
12///
13/// This is generic over a type which is used as the database error type.
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct EmptyDBTyped<E> {
16    _phantom: PhantomData<E>,
17}
18
19// Don't derive traits, because the type parameter is unused.
20impl<E> Clone for EmptyDBTyped<E> {
21    fn clone(&self) -> Self {
22        *self
23    }
24}
25
26impl<E> Copy for EmptyDBTyped<E> {}
27
28impl<E> Default for EmptyDBTyped<E> {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl<E> fmt::Debug for EmptyDBTyped<E> {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        f.debug_struct("EmptyDB").finish_non_exhaustive()
37    }
38}
39
40impl<E> PartialEq for EmptyDBTyped<E> {
41    fn eq(&self, _: &Self) -> bool {
42        true
43    }
44}
45
46impl<E> Eq for EmptyDBTyped<E> {}
47
48impl<E> EmptyDBTyped<E> {
49    pub fn new() -> Self {
50        Self {
51            _phantom: PhantomData,
52        }
53    }
54}
55
56impl<E: DBErrorMarker + Error> Database for EmptyDBTyped<E> {
57    type Error = E;
58
59    #[inline]
60    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
61        <Self as DatabaseRef>::basic_ref(self, address)
62    }
63
64    #[inline]
65    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
66        <Self as DatabaseRef>::code_by_hash_ref(self, code_hash)
67    }
68
69    #[inline]
70    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
71        <Self as DatabaseRef>::storage_ref(self, address, index)
72    }
73
74    #[inline]
75    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
76        <Self as DatabaseRef>::block_hash_ref(self, number)
77    }
78}
79
80impl<E: DBErrorMarker + Error> DatabaseRef for EmptyDBTyped<E> {
81    type Error = E;
82
83    #[inline]
84    fn basic_ref(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
85        Ok(None)
86    }
87
88    #[inline]
89    fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
90        Ok(Bytecode::default())
91    }
92
93    #[inline]
94    fn storage_ref(&self, _address: Address, _index: U256) -> Result<U256, Self::Error> {
95        Ok(U256::default())
96    }
97
98    #[inline]
99    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
100        Ok(keccak256(number.to_string().as_bytes()))
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use primitives::b256;
108
109    #[test]
110    fn conform_block_hash_calculation() {
111        let db = EmptyDB::new();
112        assert_eq!(
113            db.block_hash_ref(0u64),
114            Ok(b256!(
115                "044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"
116            ))
117        );
118
119        assert_eq!(
120            db.block_hash_ref(1u64),
121            Ok(b256!(
122                "c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6"
123            ))
124        );
125
126        assert_eq!(
127            db.block_hash_ref(100u64),
128            Ok(b256!(
129                "8c18210df0d9514f2d2e5d8ca7c100978219ee80d3968ad850ab5ead208287b3"
130            ))
131        );
132    }
133}