Skip to content

Commit

Permalink
Put chain first in Pevm::execute
Browse files Browse the repository at this point in the history
  • Loading branch information
i1i1 committed Dec 25, 2024
1 parent c94e854 commit 6200a50
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bins/fetch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Execute the block and track the pre-state in the RPC storage.
Pevm::default()
.execute(&storage, &chain, &block, NonZeroUsize::MIN, true)
.execute(&chain, &storage, &block, NonZeroUsize::MIN, true)
.map_err(|err| format!("Failed to execute block: {:?}", err))?;

let block_dir = format!("data/blocks/{}", block.header.number);
Expand Down
4 changes: 2 additions & 2 deletions crates/pevm/benches/gigagas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn bench(c: &mut Criterion, name: &str, storage: InMemoryStorage, txs: Vec<T
group.bench_function("Sequential", |b| {
b.iter(|| {
execute_revm_sequential(
black_box(&storage),
black_box(&chain),
black_box(&storage),
black_box(spec_id),
black_box(block_env.clone()),
black_box(txs.clone()),
Expand All @@ -55,8 +55,8 @@ pub fn bench(c: &mut Criterion, name: &str, storage: InMemoryStorage, txs: Vec<T
group.bench_function("Parallel", |b| {
b.iter(|| {
pevm.execute_revm_parallel(
black_box(&storage),
black_box(&chain),
black_box(&storage),
black_box(spec_id),
black_box(block_env.clone()),
black_box(txs.clone()),
Expand Down
4 changes: 2 additions & 2 deletions crates/pevm/benches/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("Sequential", |b| {
b.iter(|| {
pevm.execute(
black_box(&storage),
black_box(&chain),
black_box(&storage),
black_box(&block),
black_box(concurrency_level),
black_box(true),
Expand All @@ -63,8 +63,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
group.bench_function("Parallel", |b| {
b.iter(|| {
pevm.execute(
black_box(&storage),
black_box(&chain),
black_box(&storage),
black_box(&block),
black_box(concurrency_level),
black_box(false),
Expand Down
28 changes: 18 additions & 10 deletions crates/pevm/src/pevm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ pub struct Pevm {
impl Pevm {
/// Execute an Alloy block, which is becoming the "standard" format in Rust.
/// TODO: Better error handling.
pub fn execute<S: Storage + Send + Sync, C: PevmChain + Send + Sync>(
pub fn execute<S, C>(
&mut self,
storage: &S,
chain: &C,
storage: &S,
// We assume the block is still needed afterwards like in most Reth cases
// so take in a reference and only copy values when needed. We may want
// to use a [`std::borrow::Cow`] to build [`BlockEnv`] and [`TxEnv`] without
Expand All @@ -115,7 +115,11 @@ impl Pevm {
block: &Block<C::Transaction>,
concurrency_level: NonZeroUsize,
force_sequential: bool,
) -> PevmResult<C> {
) -> PevmResult<C>
where
C: PevmChain + Send + Sync,
S: Storage + Send + Sync,
{
let spec_id = chain
.get_block_spec(&block.header)
.map_err(PevmError::BlockSpecError)?;
Expand All @@ -133,11 +137,11 @@ impl Pevm {
|| tx_envs.len() < concurrency_level.into()
|| block.header.gas_used < 4_000_000
{
execute_revm_sequential(storage, chain, spec_id, block_env, tx_envs)
execute_revm_sequential(chain, storage, spec_id, block_env, tx_envs)
} else {
self.execute_revm_parallel(
storage,
chain,
storage,
spec_id,
block_env,
tx_envs,
Expand All @@ -149,15 +153,19 @@ impl Pevm {
/// Execute an REVM block.
// Ideally everyone would go through the [Alloy] interface. This one is currently
// useful for testing, and for users that are heavily tied to Revm like Reth.
pub fn execute_revm_parallel<S: Storage + Send + Sync, C: PevmChain + Send + Sync>(
pub fn execute_revm_parallel<S, C>(
&mut self,
storage: &S,
chain: &C,
storage: &S,
spec_id: SpecId,
block_env: BlockEnv,
txs: Vec<TxEnv>,
concurrency_level: NonZeroUsize,
) -> PevmResult<C> {
) -> PevmResult<C>
where
C: PevmChain + Send + Sync,
S: Storage + Send + Sync,
{
if txs.is_empty() {
return Ok(Vec::new());
}
Expand Down Expand Up @@ -213,7 +221,7 @@ impl Pevm {
match abort_reason {
AbortReason::FallbackToSequential => {
self.dropper.drop((mv_memory, scheduler, Vec::new()));
return execute_revm_sequential(storage, chain, spec_id, block_env, txs);
return execute_revm_sequential(chain, storage, spec_id, block_env, txs);
}
AbortReason::ExecutionError(err) => {
self.dropper.drop((mv_memory, scheduler, txs));
Expand Down Expand Up @@ -422,8 +430,8 @@ fn try_validate(
// Useful for falling back for (small) blocks with many dependencies.
// TODO: Use this for a long chain of sequential transactions even in parallel mode.
pub fn execute_revm_sequential<S: Storage, C: PevmChain>(
storage: &S,
chain: &C,
storage: &S,
spec_id: SpecId,
block_env: BlockEnv,
txs: Vec<TxEnv>,
Expand Down
8 changes: 4 additions & 4 deletions crates/pevm/tests/common/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ where
let concurrency_level = thread::available_parallelism().unwrap_or(NonZeroUsize::MIN);
assert_eq!(
pevm::execute_revm_sequential(
&storage,
chain,
&storage,
SpecId::LATEST,
BlockEnv::default(),
txs.clone(),
),
Pevm::default().execute_revm_parallel(
&storage,
chain,
&storage,
SpecId::LATEST,
BlockEnv::default(),
txs,
Expand All @@ -61,8 +61,8 @@ pub fn test_execute_alloy<C, S>(
{
let concurrency_level = thread::available_parallelism().unwrap_or(NonZeroUsize::MIN);
let mut pevm = Pevm::default();
let sequential_result = pevm.execute(storage, chain, &block, concurrency_level, true);
let parallel_result = pevm.execute(storage, chain, &block, concurrency_level, false);
let sequential_result = pevm.execute(chain, storage, &block, concurrency_level, true);
let parallel_result = pevm.execute(chain, storage, &block, concurrency_level, false);
assert!(sequential_result.is_ok());
assert_eq!(&sequential_result, &parallel_result);

Expand Down
2 changes: 1 addition & 1 deletion crates/pevm/tests/ethereum/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ fn run_test_unit(path: &Path, unit: TestUnit) {
match (
test.expect_exception.as_deref(),
Pevm::default().execute_revm_parallel(
&InMemoryStorage::new(chain_state.clone(), Arc::new(bytecodes), Default::default()),
&PevmEthereum::mainnet(),
&InMemoryStorage::new(chain_state.clone(), Arc::new(bytecodes), Default::default()),
spec_name.to_spec_id(),
build_block_env(&unit.env),
vec![tx_env.unwrap()],
Expand Down

0 comments on commit 6200a50

Please sign in to comment.