Skip to content

Commit

Permalink
update BlockExecutionInput struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Nov 1, 2024
1 parent ee0c132 commit 639f4be
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
EthExecutorProvider::ethereum(provider_factory.chain_spec()).executor(db);

let block_execution_output =
executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?;
executor.execute((&mut block_with_senders.clone().unseal(), U256::MAX).into())?;
let execution_outcome =
ExecutionOutcome::from((block_execution_output, block.number));
debug!(target: "reth::cli", ?execution_outcome, "Executed block");
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
provider.header_td_by_number(merkle_block_number)?.unwrap_or_default();
let block_execution_output = executor.execute(
(
&block
&mut block
.clone()
.unseal()
.with_recovered_senders()
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
provider_rw.static_file_provider().clone(),
),
));
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
executor.execute_and_verify_one((&mut sealed_block.clone().unseal(), td).into())?;
let execution_outcome = executor.finalize();

let mut storage_writer = UnifiedStorageWriter::from_database(&provider_rw);
Expand Down
4 changes: 2 additions & 2 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ impl AppendableChain {
let db = StateProviderDatabase::new(&provider);
let executor = externals.executor_factory.executor(db);
let block_hash = block.hash();
let block = block.unseal();
let mut block = block.unseal();

let state = executor.execute((&block, U256::MAX).into())?;
let state = executor.execute((&mut block, U256::MAX).into())?;
externals.consensus.validate_block_post_execution(
&block,
PostExecutionInput::new(&state.receipts, &state.requests),
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl StorageInner {
&chain_spec,
);

let block = Block {
let mut block = Block {
header,
body: BlockBody {
transactions,
Expand All @@ -388,7 +388,7 @@ impl StorageInner {

// execute the block
let block_execution_output =
executor.executor(&mut db).execute((&block, U256::ZERO).into())?;
executor.executor(&mut db).execute((&mut 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
4 changes: 2 additions & 2 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2174,10 +2174,10 @@ where
let block_number = block.number;
let block_hash = block.hash();
let sealed_block = Arc::new(block.block.clone());
let block = block.unseal();
let mut block = block.unseal();

let exec_time = Instant::now();
let output = self.metrics.executor.execute_metered(executor, (&block, U256::MAX).into())?;
let output = self.metrics.executor.execute_metered(executor, (&mut block, U256::MAX).into())?;

trace!(target: "engine::tree", elapsed=?exec_time.elapsed(), ?block_number, "Executed block");
if let Err(err) = self.consensus.validate_block_post_execution(
Expand Down
8 changes: 4 additions & 4 deletions crates/ethereum/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ where
///
/// Returns an error if the block could not be executed or failed verification.
fn execute(mut self, input: Self::Input<'_>) -> Result<Self::Output, Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let EthExecuteOutput { receipts, requests, gas_used } =
self.execute_without_verification(block, total_difficulty)?;

Expand All @@ -380,7 +380,7 @@ where
where
F: FnMut(&State<DB>),
{
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let EthExecuteOutput { receipts, requests, gas_used } =
self.execute_without_verification(block, total_difficulty)?;

Expand All @@ -398,7 +398,7 @@ where
where
F: OnStateHook,
{
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let EthExecuteOutput { receipts, requests, gas_used } = self
.execute_without_verification_with_state_hook(
block,
Expand Down Expand Up @@ -442,7 +442,7 @@ where
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;

if self.batch_record.first_block().is_none() {
self.batch_record.set_first_block(block.number);
Expand Down
28 changes: 23 additions & 5 deletions crates/evm/execution-types/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@ use revm::db::BundleState;
#[derive(Debug)]
pub struct BlockExecutionInput<'a, Block> {
/// The block to execute.
pub block: &'a Block,
pub block: &'a mut Block,
/// The total difficulty of the block.
pub total_difficulty: U256,
/// Enable anchor transaction.
pub enable_anchor: bool,
/// Enable skip invalid transaction.
pub enable_skip: bool,
/// Enable build transaction lists.
pub enable_build: bool,
/// Max compressed bytes.
pub max_bytes_per_tx_list: u64,
/// Max length of transactions list.
pub max_transactions_lists: u64,
}

impl<'a, Block> BlockExecutionInput<'a, Block> {
/// Creates a new input.
pub const fn new(block: &'a Block, total_difficulty: U256) -> Self {
Self { block, total_difficulty }
pub fn new(block: &'a mut Block, total_difficulty: U256) -> Self {
Self {
block,
total_difficulty,
enable_anchor: false,
enable_skip: false,
enable_build: false,
max_bytes_per_tx_list: 0,
max_transactions_lists: 0,
}
}
}

impl<'a, Block> From<(&'a Block, U256)> for BlockExecutionInput<'a, Block> {
fn from((block, total_difficulty): (&'a Block, U256)) -> Self {
impl<'a, Block> From<(&'a mut Block, U256)> for BlockExecutionInput<'a, Block> {
fn from((block, total_difficulty): (&'a mut Block, U256)) -> Self {
Self::new(block, total_difficulty)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,6 @@ mod tests {
let provider = TestExecutorProvider;
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
let executor = provider.executor(db);
let _ = executor.execute(BlockExecutionInput::new(&Default::default(), U256::ZERO));
let _ = executor.execute(BlockExecutionInput::new(&mut Default::default(), U256::ZERO));
}
}
12 changes: 10 additions & 2 deletions crates/evm/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ExecutorMetrics {
Error = Error,
>,
{
let output = self.metered(input.block, || {
let output = self.metered(&input.block.clone(), || {
executor.execute_with_state_closure(input, |state: &revm::db::State<DB>| {
// Update the metrics for the number of accounts, storage slots and bytecodes
// loaded
Expand Down Expand Up @@ -120,6 +120,14 @@ impl ExecutorMetrics {
where
F: FnOnce(BlockExecutionInput<'_, BlockWithSenders>) -> R,
{
self.metered(input.block, || f(input))
self.metered(input.block, || f(BlockExecutionInput {
block: &mut input.block.clone(),
total_difficulty: Default::default(),
enable_anchor: false,
enable_skip: false,
enable_build: false,
max_bytes_per_tx_list: 0,
max_transactions_lists: 0,
}))
}
}
8 changes: 4 additions & 4 deletions crates/exex/exex/src/backfill/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ where
// Unseal the block for execution
let (block, senders) = block.into_components();
let (unsealed_header, hash) = block.header.split();
let block =
let mut block =
Block { header: unsealed_header, body: block.body }.with_senders_unchecked(senders);

executor.execute_and_verify_one((&block, td).into())?;
executor.execute_and_verify_one((&mut block, td).into())?;
execution_duration += execute_start.elapsed();

// TODO(alexey): report gas metrics using `block.header.gas_used`
Expand Down Expand Up @@ -196,7 +196,7 @@ where
.ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?;

// Fetch the block with senders for execution.
let block_with_senders = self
let mut block_with_senders = self
.provider
.block_with_senders(block_number.into(), TransactionVariant::WithHash)?
.ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?;
Expand All @@ -208,7 +208,7 @@ where

trace!(target: "exex::backfill", number = block_number, txs = block_with_senders.block.body.transactions.len(), "Executing block");

let block_execution_output = executor.execute((&block_with_senders, td).into())?;
let block_execution_output = executor.execute((&mut block_with_senders, td).into())?;

Ok((block_with_senders, block_execution_output))
}
Expand Down
18 changes: 13 additions & 5 deletions crates/exex/exex/src/backfill/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn chain_spec(address: Address) -> Arc<ChainSpec> {
pub(crate) fn execute_block_and_commit_to_database<N>(
provider_factory: &ProviderFactory<N>,
chain_spec: Arc<ChainSpec>,
block: &BlockWithSenders,
block: &mut BlockWithSenders,
) -> eyre::Result<BlockExecutionOutput<Receipt>>
where
N: ProviderNodeTypes,
Expand All @@ -68,7 +68,15 @@ where
provider.tx_ref(),
provider.static_file_provider(),
)))
.execute(BlockExecutionInput { block, total_difficulty: U256::ZERO })?;
.execute(BlockExecutionInput {
block,
total_difficulty: U256::ZERO,
enable_anchor: false,
enable_skip: false,
enable_build: false,
max_bytes_per_tx_list: 0,
max_transactions_lists: 0,
})?;
block_execution_output.state.reverts.sort();

// Convert the block execution output to an execution outcome for committing to the database
Expand Down Expand Up @@ -167,12 +175,12 @@ pub(crate) fn blocks_and_execution_outputs<N>(
where
N: ProviderNodeTypes,
{
let (block1, block2) = blocks(chain_spec.clone(), key_pair)?;
let (mut block1, mut block2) = blocks(chain_spec.clone(), key_pair)?;

let block_output1 =
execute_block_and_commit_to_database(&provider_factory, chain_spec.clone(), &block1)?;
execute_block_and_commit_to_database(&provider_factory, chain_spec.clone(), &mut block1)?;
let block_output2 =
execute_block_and_commit_to_database(&provider_factory, chain_spec, &block2)?;
execute_block_and_commit_to_database(&provider_factory, chain_spec, &mut block2)?;

let block1 = block1.seal_slow();
let block2 = block2.seal_slow();
Expand Down
8 changes: 4 additions & 4 deletions crates/optimism/evm/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ where
///
/// State changes are committed to the database.
fn execute(mut self, input: Self::Input<'_>) -> Result<Self::Output, Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let (receipts, gas_used) = self.execute_without_verification(block, total_difficulty)?;

// NOTE: we need to merge keep the reverts for the bundle retention
Expand All @@ -389,7 +389,7 @@ where
where
F: FnMut(&State<DB>),
{
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let (receipts, gas_used) = self.execute_without_verification(block, total_difficulty)?;

// NOTE: we need to merge keep the reverts for the bundle retention
Expand All @@ -412,7 +412,7 @@ where
where
F: OnStateHook,
{
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;
let (receipts, gas_used) = self.execute_without_verification_with_state_hook(
block,
total_difficulty,
Expand Down Expand Up @@ -464,7 +464,7 @@ where
type Error = BlockExecutionError;

fn execute_and_verify_one(&mut self, input: Self::Input<'_>) -> Result<(), Self::Error> {
let BlockExecutionInput { block, total_difficulty } = input;
let BlockExecutionInput { block, total_difficulty, .. } = input;

if self.batch_record.first_block().is_none() {
self.batch_record.set_first_block(block.number);
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ where

let _ = block_executor
.execute_with_state_closure(
(&block.clone().unseal(), block.difficulty).into(),
(&mut block.clone().unseal(), block.difficulty).into(),
|statedb| {
codes = statedb
.cache
Expand Down
4 changes: 2 additions & 2 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ where
// Execute the block
let execute_start = Instant::now();

self.metrics.metered_one((&block, td).into(), |input| {
let sealed = block.header.clone().seal_slow();
self.metrics.metered_one((&mut block.clone(), td).into(), |input| {
let sealed = block.clone().header.clone().seal_slow();
let (header, seal) = sealed.into_parts();

executor.execute_and_verify_one(input).map_err(|error| StageError::Block {
Expand Down

0 comments on commit 639f4be

Please sign in to comment.