Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds snapshot map to anvil metadata #6364

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/anvil/core/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use ethers_core::types::{TxHash, H256, U256, U64};
use revm::primitives::SpecId;

Expand Down Expand Up @@ -217,6 +219,7 @@ pub struct AnvilMetadata {
pub latest_block_number: u64,
pub latest_block_hash: H256,
pub forked_network: Option<ForkedNetwork>,
pub snapshots: BTreeMap<U256, (u64, H256)>,
}

/// Information about the forked network.
Expand Down
8 changes: 5 additions & 3 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,18 +1708,20 @@ impl EthApi {
pub async fn anvil_metadata(&self) -> Result<AnvilMetadata> {
node_info!("anvil_metadata");
let fork_config = self.backend.get_fork();
let snapshots = self.backend.list_snapshots();

Ok(AnvilMetadata {
client_version: CLIENT_VERSION,
chain_id: self.backend.chain_id().try_into().unwrap_or(u64::MAX),
chain_id: self.backend.chain_id(),
latest_block_hash: self.backend.best_hash(),
latest_block_number: self.backend.best_number().as_u64(),
instance_id: *self.instance_id.read(),
forked_network: fork_config.map(|cfg| ForkedNetwork {
chain_id: cfg.chain_id().into(),
fork_block_number: cfg.block_number().into(),
chain_id: cfg.chain_id(),
fork_block_number: cfg.block_number(),
fork_block_hash: cfg.block_hash(),
}),
snapshots,
})
}

Expand Down
6 changes: 5 additions & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use futures::channel::mpsc::{unbounded, UnboundedSender};
use hash_db::HashDB;
use parking_lot::{Mutex, RwLock};
use std::{
collections::HashMap,
collections::{BTreeMap, HashMap},
io::{Read, Write},
ops::Deref,
sync::Arc,
Expand Down Expand Up @@ -676,6 +676,10 @@ impl Backend {
Ok(self.db.write().await.revert(id))
}

pub fn list_snapshots(&self) -> BTreeMap<U256, (u64, H256)> {
self.active_snapshots.lock().clone().into_iter().collect()
}

/// Get the current state.
pub async fn serialized_state(&self) -> Result<SerializableState, BlockchainError> {
let state = self.db.read().await.dump_state()?;
Expand Down
2 changes: 2 additions & 0 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ async fn can_get_metadata() {
client_version: CLIENT_VERSION,
instance_id: api.instance_id(),
forked_network: None,
snapshots: Default::default(),
};

assert_eq!(metadata, expected_metadata);
Expand Down Expand Up @@ -501,6 +502,7 @@ async fn can_get_metadata_on_fork() {
fork_block_number: block_number,
fork_block_hash: block.hash.unwrap(),
}),
snapshots: Default::default(),
};

assert_eq!(metadata, expected_metadata);
Expand Down
Loading