diff --git a/benches/mainnet.rs b/benches/mainnet.rs index d01c52c1..56118dae 100644 --- a/benches/mainnet.rs +++ b/benches/mainnet.rs @@ -11,7 +11,8 @@ use std::{ }; use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use pevm::chain::PevmEthereum; +use libmdbx::DatabaseOptions; +use pevm::{chain::PevmEthereum, OnDiskStorage}; // Better project structure #[path = "../tests/common/mod.rs"] @@ -29,7 +30,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { // with many dependencies. .min(NonZeroUsize::new(8).unwrap()); - common::for_each_block_from_disk(|block, in_memory_storage, on_disk_storage| { + common::for_each_block_from_disk(|block, in_memory_storage, mdbx_dir| { let mut group = c.benchmark_group(format!( "Block {}({} txs, {} gas)", block.header.number.unwrap(), @@ -62,6 +63,14 @@ pub fn criterion_benchmark(c: &mut Criterion) { b.iter_custom(|iters| { let mut total_duration = Duration::ZERO; for _i in 0..iters { + let on_disk_storage = OnDiskStorage::open( + mdbx_dir.clone(), + DatabaseOptions { + max_tables: Some(16), + ..DatabaseOptions::default() + }, + ) + .unwrap(); let start = Instant::now(); pevm::execute( black_box(&on_disk_storage), @@ -72,7 +81,6 @@ pub fn criterion_benchmark(c: &mut Criterion) { ) .unwrap(); total_duration += start.elapsed(); - on_disk_storage.clear_cache(); } total_duration }) @@ -82,6 +90,14 @@ pub fn criterion_benchmark(c: &mut Criterion) { b.iter_custom(|iters| { let mut total_duration = Duration::ZERO; for _i in 0..iters { + let on_disk_storage = OnDiskStorage::open( + mdbx_dir.clone(), + DatabaseOptions { + max_tables: Some(16), + ..DatabaseOptions::default() + }, + ) + .unwrap(); let start = Instant::now(); pevm::execute( black_box(&on_disk_storage), @@ -92,7 +108,6 @@ pub fn criterion_benchmark(c: &mut Criterion) { ) .unwrap(); total_duration += start.elapsed(); - on_disk_storage.clear_cache(); } total_duration }) diff --git a/src/storage/on_disk.rs b/src/storage/on_disk.rs index c963fb6f..96a13c95 100644 --- a/src/storage/on_disk.rs +++ b/src/storage/on_disk.rs @@ -48,13 +48,6 @@ impl OnDiskStorage { } } } - - /// Clear cache - pub fn clear_cache(&self) { - self.cache_accounts.clear(); - self.cache_bytecodes.clear(); - self.cache_block_hashes.clear(); - } } impl Storage for OnDiskStorage { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 7d7da04a..65f23b8e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -2,13 +2,13 @@ use std::{ collections::HashMap, fs::{self, File}, io::BufReader, + path::PathBuf, }; use ahash::AHashMap; use alloy_primitives::{Address, Bloom, Bytes, B256, U256}; use alloy_rpc_types::{Block, Header}; -use libmdbx::DatabaseOptions; -use pevm::{Bytecodes, EvmAccount, InMemoryStorage, OnDiskStorage}; +use pevm::{Bytecodes, EvmAccount, InMemoryStorage}; pub mod runner; pub use runner::{assert_execution_result, mock_account, test_execute_alloy, test_execute_revm}; @@ -49,7 +49,7 @@ pub static MOCK_ALLOY_BLOCK_HEADER: Header = Header { pub const RAW_TRANSFER_GAS_LIMIT: u64 = 21_000; // TODO: Put somewhere better? -pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage, OnDiskStorage)) { +pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage, &PathBuf)) { // Parse bytecodes let bytecodes: Bytecodes = bincode::deserialize_from(BufReader::new( File::open("data/bytecodes.bincode").unwrap(), @@ -83,10 +83,10 @@ pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage, }) .unwrap_or_default(); - let mut db_dir = std::env::temp_dir(); - db_dir.push(block_number); + let mut mdbx_dir = std::env::temp_dir(); + mdbx_dir.push(block_number); mdbx::create_db_dir( - &db_dir, + &mdbx_dir, bytecodes.iter(), accounts.iter(), block_hashes.iter(), @@ -95,15 +95,8 @@ pub fn for_each_block_from_disk(mut handler: impl FnMut(Block, InMemoryStorage, handler( block, InMemoryStorage::new(accounts, Some(&bytecodes), block_hashes), - OnDiskStorage::open( - &db_dir, - DatabaseOptions { - max_tables: Some(16), - ..DatabaseOptions::default() - }, - ) - .unwrap(), + &mdbx_dir, ); - std::fs::remove_dir_all(db_dir).unwrap(); + std::fs::remove_dir_all(mdbx_dir).unwrap(); } } diff --git a/tests/mainnet.rs b/tests/mainnet.rs index 390e8e94..bd52721e 100644 --- a/tests/mainnet.rs +++ b/tests/mainnet.rs @@ -8,13 +8,14 @@ use alloy_consensus::constants::KECCAK_EMPTY; use alloy_primitives::{Address, B256}; use alloy_provider::{Provider, ProviderBuilder}; use alloy_rpc_types::{BlockId, BlockTransactionsKind}; +use libmdbx::DatabaseOptions; use reqwest::Url; use revm::db::CacheDB; use tokio::runtime::Runtime; use pevm::{ chain::{PevmChain, PevmEthereum}, - EvmAccount, EvmCode, RpcStorage, StorageWrapper, + EvmAccount, EvmCode, OnDiskStorage, RpcStorage, StorageWrapper, }; pub mod common; @@ -103,7 +104,7 @@ fn mainnet_blocks_from_rpc() { #[test] fn mainnet_blocks_from_disk() { - common::for_each_block_from_disk(|block, in_memory_storage, on_disk_storage| { + common::for_each_block_from_disk(|block, in_memory_storage, mdbx_dir| { // 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. for _ in 0..3 { @@ -113,13 +114,20 @@ fn mainnet_blocks_from_disk() { block.clone(), true, ); + let on_disk_storage = OnDiskStorage::open( + mdbx_dir.clone(), + DatabaseOptions { + max_tables: Some(16), + ..DatabaseOptions::default() + }, + ) + .unwrap(); common::test_execute_alloy( &on_disk_storage, &PevmEthereum::mainnet(), block.clone(), true, ); - on_disk_storage.clear_cache(); } }); }