From bbeec158402bb79134f1b282a7c9ce284da30c41 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 25 Feb 2025 13:29:37 +0800 Subject: [PATCH] save work --- crates/core/src/executor.rs | 157 ++++++++++++++++++++++++----- crates/primitives/src/types/mod.rs | 5 +- 2 files changed, 137 insertions(+), 25 deletions(-) diff --git a/crates/core/src/executor.rs b/crates/core/src/executor.rs index 354177f..a549c1c 100644 --- a/crates/core/src/executor.rs +++ b/crates/core/src/executor.rs @@ -1,21 +1,39 @@ use crate::{database::EvmDatabase, error::VerificationError}; -use reth_evm::execute::{BlockExecutorProvider, Executor}; +use reth_evm::{ + ConfigureEvm, ConfigureEvmEnv, Evm, + execute::{ + BlockExecutionError, BlockExecutionStrategy, BlockExecutionStrategyFactory, + BlockExecutorProvider, BlockValidationError, Executor, + }, +}; +#[cfg(not(feature = "scroll"))] +use reth_evm_ethereum::execute::EthExecutorProvider as ExecutorProvider; use reth_execution_types::BlockExecutionOutput; +#[cfg(feature = "scroll")] +use reth_scroll_evm::ScrollExecutorProvider as ExecutorProvider; +use reth_scroll_evm::{ + BasicScrollReceiptBuilder, ReceiptBuilderCtx, ScrollEvmConfig, ScrollExecutionStrategyFactory, + ScrollReceiptBuilder, +}; use sbv_kv::KeyValueStoreGet; use sbv_primitives::{ - B256, Bytes, RecoveredBlock, + B256, Bytes, RecoveredBlock, U256, chainspec::ChainSpec, - revm::db::CacheDB, - types::reth::{Block, Receipt}, + revm::{ + Database, DatabaseCommit, EvmContext, Inspector, + db::CacheDB, + inspectors::CustomPrintTracer, + interpreter::Interpreter, + primitives::{ExecutionResult, ResultAndState}, + }, + types::{ + consensus::SignedTransaction, + reth::{Block, Receipt, ScrollPrimitives}, + }, }; use sbv_trie::TrieNode; use std::{fmt::Debug, sync::Arc}; -#[cfg(not(feature = "scroll"))] -use reth_evm_ethereum::execute::EthExecutorProvider as ExecutorProvider; -#[cfg(feature = "scroll")] -use reth_scroll_evm::ScrollExecutorProvider as ExecutorProvider; - /// EVM executor that handles the block. #[derive(Debug)] pub struct EvmExecutor<'a, CodeDb, NodesProvider, BlockHashProvider> { @@ -49,23 +67,116 @@ impl< { /// Handle the block with the given witness pub fn execute(self) -> Result, VerificationError> { - #[cfg(not(feature = "scroll"))] - let provider = ExecutorProvider::ethereum(self.chain_spec.clone()); - #[cfg(feature = "scroll")] - let provider = ExecutorProvider::scroll(self.chain_spec.clone()); - - #[allow(clippy::let_and_return)] - let output = measure_duration_millis!( - handle_block_duration_milliseconds, - cycle_track!( - provider.executor(CacheDB::new(self.db)).execute(self.block), - "handle_block" - ) - )?; + // #[cfg(not(feature = "scroll"))] + // let provider = ExecutorProvider::ethereum(self.chain_spec.clone()); + // #[cfg(feature = "scroll")] + // let provider = ExecutorProvider::scroll(self.chain_spec.clone()); + // + // #[allow(clippy::let_and_return)] + // let output = measure_duration_millis!( + // handle_block_duration_milliseconds, + // cycle_track!( + // provider.executor(CacheDB::new(self.db)).execute(self.block), + // "handle_block" + // ) + // )?; + // + // #[cfg(feature = "metrics")] + // sbv_helpers::metrics::REGISTRY.block_counter.inc(); + + Ok(self.execute_raw()?) + } + + fn execute_raw(self) -> Result, BlockExecutionError> { + let receipt_builder = BasicScrollReceiptBuilder::default(); + + let evm_config = ScrollEvmConfig::new(self.chain_spec.clone()); + let strategy_factory = ScrollExecutionStrategyFactory::::new( + evm_config.clone(), + receipt_builder, + ); + let mut strategy = strategy_factory.create_strategy(CacheDB::new(self.db)); + strategy.apply_pre_execution_changes(&self.block)?; + + let mut evm = evm_config.evm_with_env_and_inspector( + strategy.state_mut(), + evm_config.evm_env(&self.block.header()), + Tracer, + ); + + let mut cumulative_gas_used = 0; + let mut receipts = Vec::with_capacity(self.block.body().transactions.len()); + + for (sender, transaction) in self.block.transactions_with_sender() { + let tx_env = evm_config.tx_env(transaction, *sender); + // disable the base fee checks for l1 messages. + evm.context.evm.inner.env.cfg.disable_base_fee = transaction.is_l1_message(); + + // execute the transaction and commit the result to the database + let ResultAndState { result, state } = + evm.transact(tx_env) + .map_err(|err| BlockValidationError::EVM { + hash: transaction.recalculate_hash(), + error: Box::new(err), + })?; + evm.db_mut().commit(state); + + let l1_fee = if transaction.is_l1_message() { + // l1 messages do not get any gas refunded + if let ExecutionResult::Success { gas_refunded, .. } = result { + cumulative_gas_used += gas_refunded + } + + U256::ZERO + } else { + // compute l1 fee for all non-l1 transaction + let l1_block_info = evm.context.evm.inner.l1_block_info.as_ref().unwrap(); + let transaction_rlp_bytes = + evm.context.evm.env.tx.scroll.rlp_bytes.as_ref().unwrap(); + l1_block_info.calculate_tx_l1_cost(transaction_rlp_bytes, evm.handler.cfg.spec_id) + }; + + cumulative_gas_used += result.gas_used(); + + let ctx = ReceiptBuilderCtx { + header: self.block.header(), + tx: transaction, + result, + cumulative_gas_used, + l1_fee, + }; + receipts.push(receipt_builder.build_receipt(ctx)) + } + drop(evm); + + let requests = strategy.apply_post_execution_changes(&self.block, &receipts)?; + let state = strategy.finish(); + + // #[allow(clippy::let_and_return)] + // let output = measure_duration_millis!( + // handle_block_duration_milliseconds, + // cycle_track!( + // provider.executor(CacheDB::new(self.db)).execute(self.block), + // "handle_block" + // ) + // )?; #[cfg(feature = "metrics")] sbv_helpers::metrics::REGISTRY.block_counter.inc(); - Ok(output) + Ok(BlockExecutionOutput { + state, + receipts, + requests, + gas_used: cumulative_gas_used, + }) + } +} + +struct Tracer; + +impl Inspector for Tracer { + fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext) { + panic!("1"); } } diff --git a/crates/primitives/src/types/mod.rs b/crates/primitives/src/types/mod.rs index cd6d841..4788982 100644 --- a/crates/primitives/src/types/mod.rs +++ b/crates/primitives/src/types/mod.rs @@ -29,6 +29,7 @@ pub mod consensus { #[cfg(not(feature = "scroll"))] pub use alloy_consensus::{TxEnvelope, TxType, TypedTransaction}; use reth_primitives_traits::transaction::signature::Signature; + pub use reth_primitives_traits::transaction::signed::SignedTransaction; #[cfg(feature = "scroll")] pub use scroll_alloy_consensus::{ ScrollReceiptEnvelope as ReceiptEnvelope, ScrollTxEnvelope as TxEnvelope, @@ -80,8 +81,8 @@ pub mod reth { pub use reth_primitives::{Block, BlockBody, Receipt, TransactionSigned}; #[cfg(feature = "scroll")] pub use reth_scroll_primitives::{ - ScrollBlock as Block, ScrollBlockBody as BlockBody, ScrollReceipt as Receipt, - ScrollTransactionSigned as TransactionSigned, + ScrollBlock as Block, ScrollBlockBody as BlockBody, ScrollPrimitives, + ScrollReceipt as Receipt, ScrollTransactionSigned as TransactionSigned, }; }