Skip to content

Commit

Permalink
fix: do not reuse OnDiskStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
kien-rise committed Aug 12, 2024
1 parent 730fba0 commit 845f3cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
23 changes: 19 additions & 4 deletions benches/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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(),
Expand Down Expand Up @@ -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),
Expand All @@ -72,7 +81,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
)
.unwrap();
total_duration += start.elapsed();
on_disk_storage.clear_cache();
}
total_duration
})
Expand All @@ -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),
Expand All @@ -92,7 +108,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
)
.unwrap();
total_duration += start.elapsed();
on_disk_storage.clear_cache();
}
total_duration
})
Expand Down
7 changes: 0 additions & 7 deletions src/storage/on_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 8 additions & 15 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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();
}
}
14 changes: 11 additions & 3 deletions tests/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
});
}

0 comments on commit 845f3cd

Please sign in to comment.