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

feat: Remove duplication in test/mainnet.rs #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@
// 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 @@ -61,9 +99,8 @@
#[tokio::test(flavor = "multi_thread")]
#[cfg(all(feature = "rpc-storage", feature = "optimism"))]
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 @@
// 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;
}
}
Loading