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, StorageKey, StorageValue, B256};
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(
71        &mut self,
72        address: Address,
73        index: StorageKey,
74    ) -> Result<StorageValue, Self::Error> {
75        <Self as DatabaseRef>::storage_ref(self, address, index)
76    }
77
78    #[inline]
79    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
80        <Self as DatabaseRef>::block_hash_ref(self, number)
81    }
82}
83
84impl<E: DBErrorMarker + Error> DatabaseRef for EmptyDBTyped<E> {
85    type Error = E;
86
87    #[inline]
88    fn basic_ref(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
89        Ok(None)
90    }
91
92    #[inline]
93    fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
94        Ok(Bytecode::default())
95    }
96
97    #[inline]
98    fn storage_ref(
99        &self,
100        _address: Address,
101        _index: StorageKey,
102    ) -> Result<StorageValue, Self::Error> {
103        Ok(StorageValue::default())
104    }
105
106    #[inline]
107    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
108        Ok(keccak256(number.to_string().as_bytes()))
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115    use primitives::b256;
116
117    #[test]
118    fn conform_block_hash_calculation() {
119        let db = EmptyDB::new();
120        assert_eq!(
121            db.block_hash_ref(0u64),
122            Ok(b256!(
123                "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"
124            ))
125        );
126
127        assert_eq!(
128            db.block_hash_ref(1u64),
129            Ok(b256!(
130                "0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6"
131            ))
132        );
133
134        assert_eq!(
135            db.block_hash_ref(100u64),
136            Ok(b256!(
137                "0x8c18210df0d9514f2d2e5d8ca7c100978219ee80d3968ad850ab5ead208287b3"
138            ))
139        );
140    }
141}