-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathinfo.rs
More file actions
61 lines (52 loc) · 1.92 KB
/
info.rs
File metadata and controls
61 lines (52 loc) · 1.92 KB
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
56
57
58
59
60
61
//! Handler that can get current storage related data
use crate::mem::Backend;
use alloy_consensus::TxReceipt;
use alloy_network::{AnyRpcBlock, Network};
use alloy_primitives::B256;
use anvil_core::eth::block::Block;
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<N: Network> {
backend: Arc<Backend<N>>,
}
impl<N: Network> StorageInfo<N> {
pub(crate) fn new(backend: Arc<Backend<N>>) -> Self {
Self { backend }
}
/// Returns the current block
pub fn current_block(&self) -> Option<Block> {
self.backend.get_block(self.backend.best_number())
}
/// 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<N: Network> StorageInfo<N>
where
N::ReceiptEnvelope: TxReceipt<Log = alloy_primitives::Log> + Clone,
{
/// Returns the receipts of the current block
pub fn current_receipts(&self) -> Option<Vec<N::ReceiptEnvelope>> {
self.backend.mined_receipts(self.backend.best_hash())
}
/// Returns the receipts of the block with the given hash
pub fn receipts(&self, hash: B256) -> Option<Vec<N::ReceiptEnvelope>> {
self.backend.mined_receipts(hash)
}
}
impl<N: Network> fmt::Debug for StorageInfo<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StorageInfo").finish_non_exhaustive()
}
}