Skip to content

Commit cd8d014

Browse files
refactor(anvil): make StorageInfo<N: Network> generic (#13749)
1 parent 9d24901 commit cd8d014

6 files changed

Lines changed: 32 additions & 24 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/anvil/src/eth/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ impl EthApi {
871871
}
872872

873873
/// Returns a new accessor for certain storage elements
874-
pub fn storage_info(&self) -> StorageInfo {
874+
pub fn storage_info(&self) -> StorageInfo<FoundryNetwork> {
875875
StorageInfo::new(Arc::clone(&self.backend))
876876
}
877877

crates/anvil/src/eth/backend/info.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Handler that can get current storage related data
22
33
use crate::mem::Backend;
4-
use alloy_network::AnyRpcBlock;
4+
use alloy_network::{AnyRpcBlock, Network};
55
use alloy_primitives::B256;
66
use anvil_core::eth::block::Block;
77
use foundry_primitives::{FoundryNetwork, FoundryReceiptEnvelope};
@@ -13,30 +13,20 @@ use std::{fmt, sync::Arc};
1313
/// fetch ethereum storage related data
1414
// TODO(mattsee): once we have multiple Backend types, this should be turned into a trait
1515
#[derive(Clone)]
16-
pub struct StorageInfo {
17-
backend: Arc<Backend<FoundryNetwork>>,
16+
pub struct StorageInfo<N: Network> {
17+
backend: Arc<Backend<N>>,
1818
}
1919

20-
impl StorageInfo {
21-
pub(crate) fn new(backend: Arc<Backend<FoundryNetwork>>) -> Self {
20+
impl<N: Network> StorageInfo<N> {
21+
pub(crate) fn new(backend: Arc<Backend<N>>) -> Self {
2222
Self { backend }
2323
}
2424

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-
3025
/// Returns the current block
3126
pub fn current_block(&self) -> Option<Block> {
3227
self.backend.get_block(self.backend.best_number())
3328
}
3429

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-
4030
/// Returns the block with the given hash
4131
pub fn block(&self, hash: B256) -> Option<Block> {
4232
self.backend.get_block_by_hash(hash)
@@ -49,7 +39,21 @@ impl StorageInfo {
4939
}
5040
}
5141

52-
impl fmt::Debug for StorageInfo {
42+
impl StorageInfo<FoundryNetwork> {
43+
// TODO: receipts methods require N::ReceiptEnvelope generalization
44+
45+
/// Returns the receipts of the current block
46+
pub fn current_receipts(&self) -> Option<Vec<FoundryReceiptEnvelope>> {
47+
self.backend.mined_receipts(self.backend.best_hash())
48+
}
49+
50+
/// Returns the receipts of the block with the given hash
51+
pub fn receipts(&self, hash: B256) -> Option<Vec<FoundryReceiptEnvelope>> {
52+
self.backend.mined_receipts(hash)
53+
}
54+
}
55+
56+
impl<N: Network> fmt::Debug for StorageInfo<N> {
5357
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5458
f.debug_struct("StorageInfo").finish_non_exhaustive()
5559
}

crates/anvil/src/eth/fees.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::eth::{
1717
backend::{info::StorageInfo, notifications::NewBlockNotifications},
1818
error::BlockchainError,
1919
};
20+
use foundry_primitives::FoundryNetwork;
2021

2122
/// Maximum number of entries in the fee history cache
2223
pub const MAX_FEE_HISTORY_CACHE_SIZE: u64 = 2048u64;
@@ -199,15 +200,15 @@ pub struct FeeHistoryService {
199200
/// number of items to consider
200201
fee_history_limit: u64,
201202
/// a type that can fetch ethereum-storage data
202-
storage_info: StorageInfo,
203+
storage_info: StorageInfo<FoundryNetwork>,
203204
}
204205

205206
impl FeeHistoryService {
206207
pub fn new(
207208
blob_params: BlobParams,
208209
new_blocks: NewBlockNotifications,
209210
cache: FeeHistoryCache,
210-
storage_info: StorageInfo,
211+
storage_info: StorageInfo<FoundryNetwork>,
211212
) -> Self {
212213
Self {
213214
blob_params,

crates/anvil/src/filter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use anvil_rpc::{
1111
error::{ErrorCode, RpcError},
1212
response::ResponseResult,
1313
};
14+
use foundry_primitives::FoundryNetwork;
1415
use futures::{Stream, StreamExt, channel::mpsc::Receiver};
1516
use std::{
1617
pin::Pin,
@@ -158,7 +159,7 @@ pub struct LogsFilter {
158159
/// listener for new blocks
159160
pub blocks: NewBlockNotifications,
160161
/// accessor for block storage
161-
pub storage: StorageInfo,
162+
pub storage: StorageInfo<FoundryNetwork>,
162163
/// matcher with all provided filter params
163164
pub filter: FilteredParams,
164165
/// existing logs that matched the filter when the listener was installed

crates/anvil/src/pubsub.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use alloy_primitives::{B256, TxHash};
88
use alloy_rpc_types::{FilteredParams, Log, Transaction, pubsub::SubscriptionResult};
99
use anvil_core::eth::{block::Block, subscription::SubscriptionId};
1010
use anvil_rpc::{request::Version, response::ResponseResult};
11+
use foundry_primitives::FoundryNetwork;
1112
use futures::{Stream, StreamExt, channel::mpsc::Receiver, ready};
1213
use serde::Serialize;
1314
use std::{
@@ -21,7 +22,7 @@ use tokio::sync::mpsc::UnboundedReceiver;
2122
#[derive(Debug)]
2223
pub struct LogsSubscription {
2324
pub blocks: NewBlockNotifications,
24-
pub storage: StorageInfo,
25+
pub storage: StorageInfo<FoundryNetwork>,
2526
pub filter: FilteredParams,
2627
pub queued: VecDeque<Log>,
2728
pub id: SubscriptionId,
@@ -87,7 +88,7 @@ pub struct EthSubscriptionParams {
8788
#[derive(Debug)]
8889
pub enum EthSubscription {
8990
Logs(Box<LogsSubscription>),
90-
Header(NewBlockNotifications, StorageInfo, SubscriptionId),
91+
Header(NewBlockNotifications, StorageInfo<FoundryNetwork>, SubscriptionId),
9192
PendingTransactions(Receiver<TxHash>, SubscriptionId),
9293
FullPendingTransactions(UnboundedReceiver<AnyRpcTransaction>, SubscriptionId),
9394
}

0 commit comments

Comments
 (0)