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 3 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::HashMap;

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: HashMap<U256, (u64, H256)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be BtreeMap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. but curious, why is this important? or is it just for visual consistency?

(a bit more nitpicky, but perhaps the whole Backend::active_snapshots be a btree from the start, to avoid conversion here, and perhaps to slightly optimize its cleanup?

Copy link
Member

@mattsse mattsse Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or is it just for visual consistency?

just for that yes

but perhaps the whole Backend::active_snapshots

yeah that'd be easier, but since we only need this for this rpc call, should be fine to collect

}

/// 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().await;

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
4 changes: 4 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ impl Backend {
Ok(self.db.write().await.revert(id))
}

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

/// 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