Skip to content

Commit

Permalink
grevm: implement grevm batch executor (#3)
Browse files Browse the repository at this point in the history
* support GrevmBatchExecutor

* implement post execution for grevm executor
  • Loading branch information
nekomoto911 authored Nov 14, 2024
1 parent 52472e6 commit 65d7182
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 111 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ jwttoken/
crates/storage/libmdbx-rs/mdbx-sys/libmdbx/cmake-build-debug

# Rust bug report
rustc-ice-*
rustc-ice-*

# Rust lock file
Cargo.lock
12 changes: 9 additions & 3 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use reth_blockchain_tree_api::{
};
use reth_consensus::{Consensus, ConsensusError, PostExecutionInput};
use reth_db_api::database::Database;
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_evm::execute::{BlockExecutorProvider, Executor, ParallelExecutorProvider};
use reth_execution_errors::BlockExecutionError;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
Expand Down Expand Up @@ -205,11 +205,17 @@ impl AppendableChain {
let provider = BundleStateProvider::new(state_provider, bundle_state_data_provider);

let db = StateProviderDatabase::new(&provider);
let executor = externals.executor_factory.parallel_executor(db);
let block_hash = block.hash();
let block = block.unseal();

let state = executor.execute((&block, U256::MAX).into())?;
let state = if let Some(parallel_provider) =
externals.executor_factory.try_into_parallel_provider()
{
parallel_provider.executor(db).execute((&block, U256::MAX).into())?
} else {
externals.executor_factory.executor(db).execute((&block, U256::MAX).into())?
};

externals.consensus.validate_block_post_execution(
&block,
PostExecutionInput::new(&state.receipts, &state.requests),
Expand Down
13 changes: 10 additions & 3 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use reth_primitives::{
TransactionSigned, Withdrawals, B256, U256,
};
use reth_provider::{BlockReaderIdExt, StateProviderFactory, StateRootProvider};
use reth_revm::database::StateProviderDatabase;
use reth_revm::{database::StateProviderDatabase, db::WrapDatabaseRef};
use reth_transaction_pool::TransactionPool;
use reth_trie::HashedPostState;
use std::{
Expand All @@ -46,7 +46,7 @@ mod task;

pub use crate::client::AutoSealClient;
pub use mode::{FixedBlockTimeMiner, MiningMode, ReadyTransactionMiner};
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_evm::execute::{BlockExecutorProvider, Executor, ParallelExecutorProvider};
pub use task::MiningTask;

/// A consensus implementation intended for local development and testing purposes.
Expand Down Expand Up @@ -376,7 +376,14 @@ impl StorageInner {

// execute the block
let block_execution_output =
executor.parallel_executor(db.clone()).execute((&block, U256::ZERO).into())?;
if let Some(parallel_provider) = executor.try_into_parallel_provider() {
parallel_provider.executor(db.clone()).execute((&block, U256::ZERO).into())?
} else {
executor
.executor(WrapDatabaseRef::from(db.clone()))
.execute((&block, U256::ZERO).into())?
};

let gas_used = block_execution_output.gas_used;
let execution_outcome = ExecutionOutcome::from((block_execution_output, block.number));
let hashed_state = HashedPostState::from_bundle_state(&execution_outcome.state().state);
Expand Down
22 changes: 9 additions & 13 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
dao_fork::{DAO_HARDFORK_BENEFICIARY, DAO_HARDKFORK_ACCOUNTS},
parallel_execute::EthGrevmExecutor,
parallel_execute::GrevmExecutorProvider,
EthEvmConfig,
};
use core::fmt::Display;
Expand All @@ -11,7 +11,7 @@ use reth_ethereum_consensus::validate_block_post_execution;
use reth_evm::{
execute::{
BatchExecutor, BlockExecutionError, BlockExecutionInput, BlockExecutionOutput,
BlockExecutorProvider, BlockValidationError, Executor, ParallelDatabase, ProviderError,
BlockExecutorProvider, BlockValidationError, Executor, ProviderError,
},
system_calls::{
apply_beacon_root_contract_call, apply_blockhashes_contract_call,
Expand Down Expand Up @@ -90,8 +90,7 @@ where
type BatchExecutor<DB: Database<Error: Into<ProviderError> + Display>> =
EthBatchExecutor<EvmConfig, DB>;

type ParallelExecutor<DB: ParallelDatabase<Error: Into<ProviderError> + Display + Clone>> =
EthGrevmExecutor<EvmConfig, DB>;
type ParallelProvider<'a> = GrevmExecutorProvider<'a, EvmConfig>;

fn executor<DB>(&self, db: DB) -> Self::Executor<DB>
where
Expand All @@ -108,20 +107,17 @@ where
EthBatchExecutor { executor, batch_record: BlockBatchRecord::default() }
}

fn parallel_executor<DB>(&self, db: DB) -> Self::ParallelExecutor<DB>
where
DB: ParallelDatabase<Error: Into<ProviderError> + Display + Clone>,
{
EthGrevmExecutor::new(self.chain_spec.clone(), self.evm_config.clone(), db)
fn try_into_parallel_provider<'a>(&self) -> Option<Self::ParallelProvider<'_>> {
Some(GrevmExecutorProvider::new(&self.chain_spec, &self.evm_config))
}
}

/// Helper type for the output of executing a block.
#[derive(Debug, Clone)]
struct EthExecuteOutput {
receipts: Vec<Receipt>,
requests: Vec<Request>,
gas_used: u64,
pub(super) struct EthExecuteOutput {
pub receipts: Vec<Receipt>,
pub requests: Vec<Request>,
pub gas_used: u64,
}

/// Helper container type for EVM with chain spec.
Expand Down
Loading

0 comments on commit 65d7182

Please sign in to comment.