example_database_components/
state.rs

1//! State database component from [`crate::Database`]
2
3use auto_impl::auto_impl;
4use core::ops::Deref;
5use revm::{
6    primitives::{Address, StorageKey, StorageValue, B256},
7    state::{AccountInfo, Bytecode},
8};
9use std::{error::Error as StdError, sync::Arc};
10
11/// Trait for mutable access to state data including accounts, code, and storage.
12/// This is typically used for database implementations that may modify state
13/// or need mutable access for caching purposes.
14#[auto_impl(&mut, Box)]
15pub trait State {
16    /// Error type for state operations
17    type Error: StdError;
18
19    /// Gets basic account information.
20    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
21
22    /// Gets account code by its hash.
23    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
24
25    /// Gets storage value of address at index.
26    fn storage(&mut self, address: Address, index: StorageKey)
27        -> Result<StorageValue, Self::Error>;
28}
29
30/// Trait for immutable access to state data including accounts, code, and storage.
31/// This is typically used for read-only database implementations or when
32/// state data is pre-loaded and doesn't require modification.
33#[auto_impl(&, &mut, Box, Rc, Arc)]
34pub trait StateRef {
35    /// Error type for state operations
36    type Error: StdError;
37
38    /// Gets basic account information.
39    fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
40
41    /// Gets account code by its hash.
42    fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
43
44    /// Gets storage value of address at index.
45    fn storage(&self, address: Address, index: StorageKey) -> Result<StorageValue, Self::Error>;
46}
47
48impl<T> State for &T
49where
50    T: StateRef,
51{
52    type Error = <T as StateRef>::Error;
53
54    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
55        StateRef::basic(*self, address)
56    }
57
58    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
59        StateRef::code_by_hash(*self, code_hash)
60    }
61
62    fn storage(
63        &mut self,
64        address: Address,
65        index: StorageKey,
66    ) -> Result<StorageValue, Self::Error> {
67        StateRef::storage(*self, address, index)
68    }
69}
70
71impl<T> State for Arc<T>
72where
73    T: StateRef,
74{
75    type Error = <T as StateRef>::Error;
76
77    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
78        self.deref().basic(address)
79    }
80
81    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
82        self.deref().code_by_hash(code_hash)
83    }
84
85    fn storage(
86        &mut self,
87        address: Address,
88        index: StorageKey,
89    ) -> Result<StorageValue, Self::Error> {
90        self.deref().storage(address, index)
91    }
92}