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