anvil/eth/backend/info.rs
1//! Handler that can get current storage related data
2
3use crate::mem::Backend;
4use alloy_consensus::TxReceipt;
5use alloy_network::{AnyRpcBlock, Network};
6use alloy_primitives::B256;
7use anvil_core::eth::block::Block;
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<N: Network> {
17 backend: Arc<Backend<N>>,
18}
19
20impl<N: Network> StorageInfo<N> {
21 pub(crate) fn new(backend: Arc<Backend<N>>) -> Self {
22 Self { backend }
23 }
24
25 /// Returns the current block
26 pub fn current_block(&self) -> Option<Block> {
27 self.backend.get_block(self.backend.best_number())
28 }
29
30 /// Returns the block with the given hash
31 pub fn block(&self, hash: B256) -> Option<Block> {
32 self.backend.get_block_by_hash(hash)
33 }
34
35 /// Returns the block with the given hash in the format of the ethereum API
36 pub fn eth_block(&self, hash: B256) -> Option<AnyRpcBlock> {
37 let block = self.block(hash)?;
38 Some(self.backend.convert_block(block))
39 }
40}
41
42impl<N: Network> StorageInfo<N>
43where
44 N::ReceiptEnvelope: TxReceipt<Log = alloy_primitives::Log> + Clone,
45{
46 /// Returns the receipts of the current block
47 pub fn current_receipts(&self) -> Option<Vec<N::ReceiptEnvelope>> {
48 self.backend.mined_receipts(self.backend.best_hash())
49 }
50
51 /// Returns the receipts of the block with the given hash
52 pub fn receipts(&self, hash: B256) -> Option<Vec<N::ReceiptEnvelope>> {
53 self.backend.mined_receipts(hash)
54 }
55}
56
57impl<N: Network> fmt::Debug for StorageInfo<N> {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 f.debug_struct("StorageInfo").finish_non_exhaustive()
60 }
61}