example_database_components/
state.rs1use auto_impl::auto_impl;
4use core::ops::Deref;
5use revm::{
6 primitives::{Address, B256, U256},
7 state::{AccountInfo, Bytecode},
8};
9use std::{error::Error as StdError, sync::Arc};
10
11#[auto_impl(&mut, Box)]
12pub trait State {
13 type Error: StdError;
14
15 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
17
18 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
20
21 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error>;
23}
24
25#[auto_impl(&, &mut, Box, Rc, Arc)]
26pub trait StateRef {
27 type Error: StdError;
28
29 fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error>;
31
32 fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
34
35 fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error>;
37}
38
39impl<T> State for &T
40where
41 T: StateRef,
42{
43 type Error = <T as StateRef>::Error;
44
45 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
46 StateRef::basic(*self, address)
47 }
48
49 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
50 StateRef::code_by_hash(*self, code_hash)
51 }
52
53 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
54 StateRef::storage(*self, address, index)
55 }
56}
57
58impl<T> State for Arc<T>
59where
60 T: StateRef,
61{
62 type Error = <T as StateRef>::Error;
63
64 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
65 self.deref().basic(address)
66 }
67
68 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
69 self.deref().code_by_hash(code_hash)
70 }
71
72 fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
73 self.deref().storage(address, index)
74 }
75}