Skip to content
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/pevm/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub enum CalculateReceiptRootError {

/// Custom behaviours for different chains & networks
pub trait PevmChain: Debug {
/// The network type
type Network: alloy_provider::Network<BlockResponse: Into<alloy_rpc_types_eth::Block<Self::Transaction>>>;

/// The transaction type
type Transaction: Debug + Clone + PartialEq;

Expand Down
1 change: 1 addition & 0 deletions crates/pevm/src/chain/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn get_ethereum_gas_price(tx: &TxEnvelope) -> Result<U256, EthereumTransactionPa
}

impl PevmChain for PevmEthereum {
type Network = alloy_provider::network::Ethereum;
type Transaction = alloy_rpc_types_eth::Transaction;
type Envelope = TxEnvelope;
type BlockSpecError = std::convert::Infallible;
Expand Down
1 change: 1 addition & 0 deletions crates/pevm/src/chain/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn get_optimism_fields(
}

impl PevmChain for PevmOptimism {
type Network = op_alloy_network::Optimism;
type Transaction = op_alloy_rpc_types::Transaction;
type Envelope = OpTxEnvelope;
type BlockSpecError = OptimismBlockSpecError;
Expand Down
126 changes: 60 additions & 66 deletions crates/pevm/tests/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,81 @@ use pevm::chain::PevmEthereum;

pub mod common;

#[tokio::test(flavor = "multi_thread")]
#[cfg(feature = "rpc-storage")]
async fn mainnet_blocks_from_rpc() {
fn get_rpc_url(env_var: &str, default_url: &str) -> reqwest::Url {
match std::env::var(env_var) {
// 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(default_url).unwrap(),
}
}

#[cfg(feature = "rpc-storage")]
async fn test_blocks_from_rpc<C>(chain: C, url: reqwest::Url, block_numbers: &[u64])
where
C: pevm::chain::PevmChain + PartialEq + Send + Sync,
{
use alloy_provider::{Provider, ProviderBuilder};
use alloy_rpc_types_eth::{BlockId, BlockTransactionsKind};
use pevm::chain::PevmChain;

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-mainnet.public.blastapi.io").unwrap(),
};
let provider = ProviderBuilder::new().network::<C::Network>().on_http(url);

// First block under 50 transactions of each EVM-spec-changing fork
for block_number in [
46147, // FRONTIER
1150000, // HOMESTEAD
// TODO: Enable these when CI is less flaky.
// 2463002, // TANGERINE
// 2675000, // SPURIOUS_DRAGON
// 4370003, // BYZANTIUM
// 7280003, // PETERSBURG
// 9069001, // ISTANBUL
// 12244002, // BERLIN
// 12965034, // LONDON
// 15537395, // MERGE
// 17035010, // SHANGHAI
// 19426587, // CANCUN
] {
let provider = ProviderBuilder::new().on_http(rpc_url.clone());
for &block_number in block_numbers {
let block = provider
.get_block(BlockId::number(block_number), BlockTransactionsKind::Full)
.await
.unwrap()
.unwrap();
let chain = PevmEthereum::mainnet();
.unwrap()
.into();
let spec_id = chain.get_block_spec(&block.header).unwrap();
let rpc_storage =
pevm::RpcStorage::new(provider, spec_id, BlockId::number(block_number - 1));
pevm::RpcStorage::new(provider.clone(), 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() {
test_blocks_from_rpc(
PevmEthereum::mainnet(),
get_rpc_url("ETHEREUM_RPC_URL", "https://eth-mainnet.public.blastapi.io"),
&[
46147, // FRONTIER
1150000, // HOMESTEAD
// TODO: Enable these when CI is less flaky.
// 2463002, // TANGERINE
// 2675000, // SPURIOUS_DRAGON
// 4370003, // BYZANTIUM
// 7280003, // PETERSBURG
// 9069001, // ISTANBUL
// 12244002, // BERLIN
// 12965034, // LONDON
// 15537395, // MERGE
// 17035010, // SHANGHAI
// 19426587, // CANCUN
],
)
.await;
}

#[tokio::test(flavor = "multi_thread")]
#[cfg(all(feature = "rpc-storage", feature = "optimism"))]
async fn optimism_mainnet_blocks_from_rpc() {
test_blocks_from_rpc(
pevm::chain::PevmOptimism::mainnet(),
get_rpc_url("OPTIMISM_RPC_URL", "https://mainnet.optimism.io"),
&[
114874075, // CANYON (https://specs.optimism.io/protocol/canyon/overview.html)
// TODO: doesn't pass `Err(ExecutionError("Database(InvalidNonce(0))"))`
// 117874236, // ECOTONE (https://specs.optimism.io/protocol/ecotone/overview.html)
// 122874325, // FJORD (https://specs.optimism.io/protocol/fjord/overview.html)
// 125874340, // GRANITE (https://specs.optimism.io/protocol/granite/overview.html)
],
)
.await;
}

#[test]
fn mainnet_blocks_from_disk() {
common::for_each_block_from_disk(|block, storage| {
Expand All @@ -57,41 +89,3 @@ fn mainnet_blocks_from_disk() {
}
});
}

#[tokio::test(flavor = "multi_thread")]
#[cfg(all(feature = "rpc-storage", feature = "optimism"))]
async fn optimism_mainnet_blocks_from_rpc() {
use alloy_provider::{Provider, ProviderBuilder};
use alloy_rpc_types_eth::{BlockId, 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(),
_ => reqwest::Url::parse("https://mainnet.optimism.io").unwrap(),
};

// First block under 50 transactions of each EVM-spec-changing fork
for block_number in [
114874075, // CANYON (https://specs.optimism.io/protocol/canyon/overview.html)
// TODO: doesn't pass `Err(ExecutionError("Database(InvalidNonce(0))"))`
// 117874236, // ECOTONE (https://specs.optimism.io/protocol/ecotone/overview.html)
// 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);
}
}