From 79b1d6efdfd013304eea293add27284df7b3e432 Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Tue, 7 Jan 2025 00:12:27 +0530 Subject: [PATCH 1/2] add snapshot_data.rs --- crates/pevm/tests/snapshot_data.rs | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 crates/pevm/tests/snapshot_data.rs diff --git a/crates/pevm/tests/snapshot_data.rs b/crates/pevm/tests/snapshot_data.rs new file mode 100644 index 00000000..8965597d --- /dev/null +++ b/crates/pevm/tests/snapshot_data.rs @@ -0,0 +1,36 @@ +//! Test if block hashes are equal + +#[tokio::test(flavor = "multi_thread")] +async fn block_hashes_equal() { + use std::fs::File; + use std::collections::BTreeMap; + use alloy_primitives::B256; + use alloy_provider::{Provider, ProviderBuilder}; + use alloy_rpc_types_eth::{BlockNumberOrTag, BlockTransactionsKind}; + + let block_hashes = match File::open("../../data/block_hashes.bincode") { + Ok(compressed_file) => bincode::deserialize_from::<_, BTreeMap>(compressed_file).unwrap(), + Err(_) => BTreeMap::new(), + }; + + let rpc_url = match std::env::var("ETHEREUM_RPC_URL") { + // The empty check is for GitHub Actions where the variable is set with an empty string when unset!? + Ok(value) if !value.is_empty() => value.parse().unwrap(), + _ => reqwest::Url::parse("https://eth.public-rpc.com").unwrap(), + }; + + let provider = ProviderBuilder::new().on_http(rpc_url.clone()); + + for (block_number, hash) in block_hashes { + let block = provider + .get_block_by_number(BlockNumberOrTag::Number(block_number), BlockTransactionsKind::Hashes) + .await + .unwrap() + .unwrap(); + + let block_hash = block.header.hash; + + assert!(block_hash == hash); + } + +} \ No newline at end of file From 7cb6692db11232eff964a8996fc43ffa55f33e3e Mon Sep 17 00:00:00 2001 From: Levi0804 Date: Tue, 7 Jan 2025 17:48:06 +0530 Subject: [PATCH 2/2] address review comments --- crates/pevm/tests/snapshot_data.rs | 40 ++++++++++++++---------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/crates/pevm/tests/snapshot_data.rs b/crates/pevm/tests/snapshot_data.rs index 8965597d..b07dc0ea 100644 --- a/crates/pevm/tests/snapshot_data.rs +++ b/crates/pevm/tests/snapshot_data.rs @@ -1,36 +1,34 @@ -//! Test if block hashes are equal +//! Test if snapshotted mainnet data is correct -#[tokio::test(flavor = "multi_thread")] -async fn block_hashes_equal() { - use std::fs::File; - use std::collections::BTreeMap; - use alloy_primitives::B256; - use alloy_provider::{Provider, ProviderBuilder}; - use alloy_rpc_types_eth::{BlockNumberOrTag, BlockTransactionsKind}; +use alloy_primitives::B256; +use alloy_provider::{Provider, ProviderBuilder}; +use alloy_rpc_types_eth::{BlockNumberOrTag, BlockTransactionsKind}; +use std::collections::BTreeMap; +use std::fs::File; + +#[tokio::test] +async fn snapshotted_mainnet_block_hashes() { + let file = File::open("../../data/block_hashes.bincode").unwrap(); + let block_hashes = bincode::deserialize_from::<_, BTreeMap>(file).unwrap(); - let block_hashes = match File::open("../../data/block_hashes.bincode") { - Ok(compressed_file) => bincode::deserialize_from::<_, BTreeMap>(compressed_file).unwrap(), - Err(_) => BTreeMap::new(), - }; - let rpc_url = match std::env::var("ETHEREUM_RPC_URL") { // The empty check is for GitHub Actions where the variable is set with an empty string when unset!? Ok(value) if !value.is_empty() => value.parse().unwrap(), _ => reqwest::Url::parse("https://eth.public-rpc.com").unwrap(), }; - let provider = ProviderBuilder::new().on_http(rpc_url.clone()); + let provider = ProviderBuilder::new().on_http(rpc_url); - for (block_number, hash) in block_hashes { + for (block_number, snapshotted_hash) in block_hashes { let block = provider - .get_block_by_number(BlockNumberOrTag::Number(block_number), BlockTransactionsKind::Hashes) + .get_block_by_number( + BlockNumberOrTag::Number(block_number), + BlockTransactionsKind::Hashes, + ) .await .unwrap() .unwrap(); - let block_hash = block.header.hash; - - assert!(block_hash == hash); + assert_eq!(snapshotted_hash, block.header.hash); } - -} \ No newline at end of file +}