revm_primitives/db/
components.rs

1//! Database that is split on State and BlockHash traits.
2pub mod block_hash;
3pub mod state;
4
5pub use block_hash::{BlockHash, BlockHashRef};
6pub use state::{State, StateRef};
7
8use crate::{
9    db::{Database, DatabaseRef},
10    Account, AccountInfo, Address, Bytecode, HashMap, B256, U256,
11};
12
13use super::DatabaseCommit;
14
15#[derive(Debug)]
16pub struct DatabaseComponents<S, BH> {
17    pub state: S,
18    pub block_hash: BH,
19}
20
21#[derive(Debug)]
22pub enum DatabaseComponentError<SE, BHE> {
23    State(SE),
24    BlockHash(BHE),
25}
26
27impl<S: State, BH: BlockHash> Database for DatabaseComponents<S, BH> {
28    type Error = DatabaseComponentError<S::Error, BH::Error>;
29
30    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
31        self.state.basic(address).map_err(Self::Error::State)
32    }
33
34    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
35        self.state
36            .code_by_hash(code_hash)
37            .map_err(Self::Error::State)
38    }
39
40    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
41        self.state
42            .storage(address, index)
43            .map_err(Self::Error::State)
44    }
45
46    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
47        self.block_hash
48            .block_hash(number)
49            .map_err(Self::Error::BlockHash)
50    }
51}
52
53impl<S: StateRef, BH: BlockHashRef> DatabaseRef for DatabaseComponents<S, BH> {
54    type Error = DatabaseComponentError<S::Error, BH::Error>;
55
56    fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
57        self.state.basic(address).map_err(Self::Error::State)
58    }
59
60    fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
61        self.state
62            .code_by_hash(code_hash)
63            .map_err(Self::Error::State)
64    }
65
66    fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
67        self.state
68            .storage(address, index)
69            .map_err(Self::Error::State)
70    }
71
72    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
73        self.block_hash
74            .block_hash(number)
75            .map_err(Self::Error::BlockHash)
76    }
77}
78
79impl<S: DatabaseCommit, BH: BlockHashRef> DatabaseCommit for DatabaseComponents<S, BH> {
80    fn commit(&mut self, changes: HashMap<Address, Account>) {
81        self.state.commit(changes);
82    }
83}