revm_primitives/db/components/
state.rs

1//! State database component from [`crate::db::Database`]
2//! it is used inside [`crate::db::DatabaseComponents`]
3
4use crate::{AccountInfo, Address, Bytecode, B256, U256};
5use auto_impl::auto_impl;
6use core::ops::Deref;
7use std::sync::Arc;
8
9#[auto_impl(&mut, Box)]
10pub trait State {
11    type Error;
12
13    /// Get basic account information.
14    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
15
16    /// Get account code by its hash
17    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
18
19    /// Get storage value of address at index.
20    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
21}
22
23#[auto_impl(&, &mut, Box, Rc, Arc)]
24pub trait StateRef {
25    type Error;
26
27    /// Get basic account information.
28    fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
29
30    /// Get account code by its hash
31    fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
32
33    /// Get storage value of address at index.
34    fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
35}
36
37impl<T> State for &T
38where
39    T: StateRef,
40{
41    type Error = <T as StateRef>::Error;
42
43    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
44        StateRef::basic(*self, address)
45    }
46
47    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
48        StateRef::code_by_hash(*self, code_hash)
49    }
50
51    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
52        StateRef::storage(*self, address, index)
53    }
54}
55
56impl<T> State for Arc<T>
57where
58    T: StateRef,
59{
60    type Error = <T as StateRef>::Error;
61
62    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
63        self.deref().basic(address)
64    }
65
66    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
67        self.deref().code_by_hash(code_hash)
68    }
69
70    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
71        self.deref().storage(address, index)
72    }
73}