anvil/eth/backend/
info.rs

1//! Handler that can get current storage related data
2
3use crate::mem::Backend;
4use alloy_network::AnyRpcBlock;
5use alloy_primitives::B256;
6use anvil_core::eth::block::Block;
7use foundry_primitives::FoundryReceiptEnvelope;
8use std::{fmt, sync::Arc};
9
10/// A type that can fetch data related to the ethereum storage.
11///
12/// This is simply a wrapper type for the [`Backend`] but exposes a limited set of functions to
13/// fetch ethereum storage related data
14// TODO(mattsee): once we have multiple Backend types, this should be turned into a trait
15#[derive(Clone)]
16pub struct StorageInfo {
17    backend: Arc<Backend>,
18}
19
20impl StorageInfo {
21    pub(crate) fn new(backend: Arc<Backend>) -> Self {
22        Self { backend }
23    }
24
25    /// Returns the receipts of the current block
26    pub fn current_receipts(&self) -> Option<Vec<FoundryReceiptEnvelope>> {
27        self.backend.mined_receipts(self.backend.best_hash())
28    }
29
30    /// Returns the current block
31    pub fn current_block(&self) -> Option<Block> {
32        self.backend.get_block(self.backend.best_number())
33    }
34
35    /// Returns the receipts of the block with the given hash
36    pub fn receipts(&self, hash: B256) -> Option<Vec<FoundryReceiptEnvelope>> {
37        self.backend.mined_receipts(hash)
38    }
39
40    /// Returns the block with the given hash
41    pub fn block(&self, hash: B256) -> Option<Block> {
42        self.backend.get_block_by_hash(hash)
43    }
44
45    /// Returns the block with the given hash in the format of the ethereum API
46    pub fn eth_block(&self, hash: B256) -> Option<AnyRpcBlock> {
47        let block = self.block(hash)?;
48        Some(self.backend.convert_block(block))
49    }
50}
51
52impl fmt::Debug for StorageInfo {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.debug_struct("StorageInfo").finish_non_exhaustive()
55    }
56}