Skip to content

[DO NOT MERGE] [VIEW ONLY] trace reth #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: feat/euclid-v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 134 additions & 23 deletions crates/core/src/executor.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -49,23 +67,116 @@ impl<
{
/// Handle the block with the given witness
pub fn execute(self) -> Result<BlockExecutionOutput<Receipt>, 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<BlockExecutionOutput<Receipt>, BlockExecutionError> {
let receipt_builder = BasicScrollReceiptBuilder::default();

let evm_config = ScrollEvmConfig::new(self.chain_spec.clone());
let strategy_factory = ScrollExecutionStrategyFactory::<ScrollPrimitives, _>::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<DB: Database> Inspector<DB> for Tracer {
fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) {
panic!("1");
}
}
5 changes: 3 additions & 2 deletions crates/primitives/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};
}

Expand Down