Skip to main content

ethrex_vm/
db.rs

1use crate::EvmError;
2use dyn_clone::DynClone;
3use ethrex_common::{
4    Address, H256, U256,
5    types::{AccountState, ChainConfig, Code, CodeMetadata},
6};
7
8pub trait VmDatabase: Send + Sync + DynClone {
9    fn get_account_state(&self, address: Address) -> Result<Option<AccountState>, EvmError>;
10    fn get_storage_slot(&self, address: Address, key: H256) -> Result<Option<U256>, EvmError>;
11    fn get_block_hash(&self, block_number: u64) -> Result<H256, EvmError>;
12    fn get_chain_config(&self) -> Result<ChainConfig, EvmError>;
13    fn get_account_code(&self, code_hash: H256) -> Result<Code, EvmError>;
14    fn get_code_metadata(&self, code_hash: H256) -> Result<CodeMetadata, EvmError>;
15
16    /// Batch account-state lookup. Default impl loops `get_account_state`.
17    /// Backends that can amortize per-key cost (e.g. rocksdb `multi_get_cf` on
18    /// the flat key-value table) should override this.
19    fn get_account_states_batch(
20        &self,
21        addresses: &[Address],
22    ) -> Result<Vec<Option<AccountState>>, EvmError> {
23        addresses
24            .iter()
25            .map(|a| self.get_account_state(*a))
26            .collect()
27    }
28
29    /// Batch storage-slot lookup. Default impl loops `get_storage_slot`.
30    /// Backends that can amortize per-key cost (e.g. rocksdb `multi_get_cf` on
31    /// the flat key-value table) should override this.
32    fn get_storage_slots_batch(
33        &self,
34        keys: &[(Address, H256)],
35    ) -> Result<Vec<Option<U256>>, EvmError> {
36        keys.iter()
37            .map(|&(addr, key)| self.get_storage_slot(addr, key))
38            .collect()
39    }
40}
41
42dyn_clone::clone_trait_object!(VmDatabase);
43
44pub type DynVmDatabase = Box<dyn VmDatabase + Send + Sync + 'static>;