Skip to content

Commit

Permalink
feat:remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
varun-doshi committed Dec 26, 2024
1 parent 8b164ab commit 5a45aa7
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions crates/pevm/tests/mainnet.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
//! Test with mainnet blocks
use pevm::chain::PevmEthereum;
use reqwest::Url;

pub mod common;

#[tokio::test(flavor = "multi_thread")]
enum Chain {
Eth,

Check failure on line 7 in crates/pevm/tests/mainnet.rs

View workflow job for this annotation

GitHub Actions / ci

variant `Eth` is never constructed
Op,
}
#[cfg(feature = "rpc-storage")]
async fn mainnet_blocks_from_rpc() {
async fn blocks_from_rpc(rpc_url: &Url, block_number: u64, chain_name: Chain) {
use alloy_provider::{Provider, ProviderBuilder};
use alloy_rpc_types_eth::{BlockId, BlockTransactionsKind};
use pevm::chain::PevmChain;


match chain_name {
Chain::Eth => {
use pevm::chain::PevmEthereum;
let provider = ProviderBuilder::new().on_http(rpc_url.clone());
let chain = PevmEthereum::mainnet();

let block = provider
.get_block(BlockId::number(block_number), BlockTransactionsKind::Full)
.await
.unwrap()
.unwrap();
let spec_id = chain.get_block_spec(&block.header).unwrap();
let rpc_storage =
pevm::RpcStorage::new(provider, spec_id, BlockId::number(block_number - 1));
common::test_execute_alloy(&chain, &rpc_storage, block, true);
}
#[cfg(feature = "optimism")]
Chain::Op => {
use pevm::chain::PevmOptimism;
let provider = ProviderBuilder::new()
.network::<op_alloy_network::Optimism>()
.on_http(rpc_url.clone());
let chain = PevmOptimism::mainnet();

let block = provider
.get_block(BlockId::number(block_number), BlockTransactionsKind::Full)
.await
.unwrap()
.unwrap();
let spec_id = chain.get_block_spec(&block.header).unwrap();
let rpc_storage =
pevm::RpcStorage::new(provider, spec_id, BlockId::number(block_number - 1));
common::test_execute_alloy(&chain, &rpc_storage, block, true);
}
};
}

#[tokio::test(flavor = "multi_thread")]
#[cfg(feature = "rpc-storage")]
async fn mainnet_blocks_from_rpc() {

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!?
Expand All @@ -33,22 +77,16 @@ async fn mainnet_blocks_from_rpc() {
// 17035010, // SHANGHAI
// 19426587, // CANCUN
] {
let provider = ProviderBuilder::new().on_http(rpc_url.clone());
let block = provider
.get_block(BlockId::number(block_number), BlockTransactionsKind::Full)
.await
.unwrap()
.unwrap();
let chain = PevmEthereum::mainnet();
let spec_id = chain.get_block_spec(&block.header).unwrap();
let rpc_storage =
pevm::RpcStorage::new(provider, spec_id, BlockId::number(block_number - 1));
common::test_execute_alloy(&chain, &rpc_storage, block, true);

blocks_from_rpc(&rpc_url,block_number,Chain::Op).await;

}
}

#[test]
fn mainnet_blocks_from_disk() {
use pevm::chain::PevmEthereum;

common::for_each_block_from_disk(|block, storage| {
// Run several times to try catching a race condition if there is any.
// 1000~2000 is a better choice for local testing after major changes.
Expand All @@ -63,7 +101,6 @@ fn mainnet_blocks_from_disk() {
async fn optimism_mainnet_blocks_from_rpc() {
use alloy_provider::{Provider, ProviderBuilder};

Check failure on line 102 in crates/pevm/tests/mainnet.rs

View workflow job for this annotation

GitHub Actions / ci

unused imports: `ProviderBuilder` and `Provider`
use alloy_rpc_types_eth::{BlockId, BlockTransactionsKind};

Check failure on line 103 in crates/pevm/tests/mainnet.rs

View workflow job for this annotation

GitHub Actions / ci

unused imports: `BlockId` and `BlockTransactionsKind`
use pevm::chain::{PevmChain, PevmOptimism};

let rpc_url = match std::env::var("OPTIMISM_RPC_URL") {
Ok(value) if !value.is_empty() => value.parse().unwrap(),
Expand All @@ -78,20 +115,6 @@ async fn optimism_mainnet_blocks_from_rpc() {
// 122874325, // FJORD (https://specs.optimism.io/protocol/fjord/overview.html)
// 125874340, // GRANITE (https://specs.optimism.io/protocol/granite/overview.html)
] {
let provider = ProviderBuilder::new()
.network::<op_alloy_network::Optimism>()
.on_http(rpc_url.clone());
let block = provider
.get_block(BlockId::number(block_number), BlockTransactionsKind::Full)
.await
.unwrap()
.unwrap();

let chain = PevmOptimism::mainnet();
let spec_id = chain.get_block_spec(&block.header).unwrap();

let rpc_storage =
pevm::RpcStorage::new(provider, spec_id, BlockId::number(block_number - 1));
common::test_execute_alloy(&chain, &rpc_storage, block, true);
blocks_from_rpc(&rpc_url,block_number,Chain::Op).await;
}
}

0 comments on commit 5a45aa7

Please sign in to comment.