{
transition
}
+ /// Replaces the active [`ForkFilter`] with a newly derived one and returns the resulting
+ /// [`ForkId`].
+ ///
+ /// This is used to adopt a fork schedule that changed at runtime (e.g. an L1-signalled
+ /// upgrade) without restarting the node. The caller is expected to build `fork_filter` from
+ /// the updated chain spec already advanced to the node's current head, so that
+ /// [`ForkFilter::current`] reflects the correct [`ForkId`] immediately.
+ ///
+ /// Every subsequent handshake clones this filter and advertises the returned [`ForkId`], so
+ /// the node's advertised fork identity stays aligned with the rules it now enforces. Existing
+ /// active sessions are not revalidated here; see [`SessionManager::active_sessions`].
+ pub(crate) fn set_fork_filter(&mut self, fork_filter: ForkFilter) -> ForkId {
+ self.fork_filter = fork_filter;
+ self.status.forkid = self.fork_filter.current();
+ self.status.forkid
+ }
+
/// An incoming TCP connection was received. This starts the authentication process to turn this
/// stream into an active peer session.
///
diff --git a/crates/net/p2p/src/sync.rs b/crates/net/p2p/src/sync.rs
index 77b97116d19..6770c5d12a2 100644
--- a/crates/net/p2p/src/sync.rs
+++ b/crates/net/p2p/src/sync.rs
@@ -1,6 +1,6 @@
//! Traits used when interacting with the sync status of the network.
-use alloy_eips::eip2124::Head;
+use alloy_eips::eip2124::{ForkFilter, Head};
use reth_eth_wire_types::BlockRangeUpdate;
/// A type that provides information about whether the node is currently syncing and the network is
@@ -31,6 +31,17 @@ pub trait NetworkSyncUpdater: std::fmt::Debug + Send + Sync + 'static {
/// Updates the advertised block range.
fn update_block_range(&self, update: BlockRangeUpdate);
+
+ /// Replaces the node's active [`ForkFilter`] to adopt a fork schedule that changed at runtime
+ /// (e.g. an L1-signalled upgrade) without a restart.
+ ///
+ /// The caller must build `fork_filter` from the updated chain spec advanced to the node's
+ /// current head, and should do so before the fork's activation timestamp so the node
+ /// announces the upcoming fork ahead of time. Defaults to a no-op for updaters that do not
+ /// track a fork filter.
+ fn set_fork_filter(&self, fork_filter: ForkFilter) {
+ let _ = fork_filter;
+ }
}
/// The state the network is currently in when it comes to synchronization.
diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs
index f9c18dfc00d..fb67012927f 100644
--- a/crates/payload/basic/src/lib.rs
+++ b/crates/payload/basic/src/lib.rs
@@ -416,7 +416,7 @@ where
trace!(target: "payload_builder", id = %self.config.payload_id(), "spawn new payload build task");
let (tx, rx) = oneshot::channel();
let cancel = CancelOnDrop::default();
- let _cancel = cancel.clone();
+ let pending_cancel = cancel.clone();
let guard = self.payload_task_guard.clone();
let payload_config = self.config.clone();
let best_payload = self.best_payload.payload().cloned();
@@ -446,7 +446,7 @@ where
});
});
- self.pending_block = Some(PendingPayload { _cancel, payload: rx });
+ self.pending_block = Some(PendingPayload { cancel: pending_cancel, payload: rx });
}
}
@@ -567,6 +567,10 @@ where
let mut empty_payload = None;
if best_payload.is_none() {
+ if let Some(pending) = maybe_better.as_ref() {
+ pending.cancel.request_finalization();
+ }
+
debug!(target: "payload_builder", id=%self.config.payload_id(), "no best payload yet to resolve, building empty payload");
let args = BuildArguments {
@@ -734,8 +738,8 @@ where
/// A future that resolves to the result of the block building job.
#[derive(Debug)]
pub struct PendingPayload {
- /// The marker to cancel the job on drop
- _cancel: CancelOnDrop,
+ /// Cancels the job on drop and carries cooperative control signals.
+ cancel: CancelOnDrop,
/// The channel to send the result to.
payload: oneshot::Receiver, PayloadBuilderError>>,
}
@@ -746,7 +750,7 @@ impl PendingPayload
{
cancel: CancelOnDrop,
payload: oneshot::Receiver, PayloadBuilderError>>,
) -> Self {
- Self { _cancel: cancel, payload }
+ Self { cancel, payload }
}
}
diff --git a/crates/revm/src/cancelled.rs b/crates/revm/src/cancelled.rs
index e535882355c..591cd3d376d 100644
--- a/crates/revm/src/cancelled.rs
+++ b/crates/revm/src/cancelled.rs
@@ -1,26 +1,53 @@
use alloc::sync::Arc;
-use core::sync::atomic::AtomicBool;
+use core::sync::atomic::{AtomicBool, AtomicU8, Ordering};
-/// A marker that can be used to cancel execution.
+/// Payload building is still in progress.
+const RUNNING: u8 = 0;
+/// Payload building should stop accepting transactions and seal the accumulated work.
+const FINALIZATION_REQUESTED: u8 = 1;
+/// Payload building should stop and discard the accumulated work.
+const CANCELLED: u8 = 2;
+
+/// Cancels execution on drop and supports cooperative finalization.
///
/// If dropped, it will set the `cancelled` flag to true.
///
/// This is most useful when a payload job needs to be cancelled.
#[derive(Default, Clone, Debug)]
-pub struct CancelOnDrop(Arc);
+pub struct CancelOnDrop(Arc);
// === impl CancelOnDrop ===
impl CancelOnDrop {
+ /// Returns true if the current work should be interrupted.
+ pub fn is_interrupted(&self) -> bool {
+ self.0.load(Ordering::Relaxed) != RUNNING
+ }
+
/// Returns true if the job was cancelled.
pub fn is_cancelled(&self) -> bool {
- self.0.load(core::sync::atomic::Ordering::Relaxed)
+ self.0.load(Ordering::Relaxed) == CANCELLED
+ }
+
+ /// Requests that the current work be finalized without cancelling it.
+ pub fn request_finalization(&self) {
+ let _ = self.0.compare_exchange(
+ RUNNING,
+ FINALIZATION_REQUESTED,
+ Ordering::Relaxed,
+ Ordering::Relaxed,
+ );
+ }
+
+ /// Returns true if finalization was requested.
+ pub fn is_finalization_requested(&self) -> bool {
+ self.0.load(Ordering::Relaxed) == FINALIZATION_REQUESTED
}
}
impl Drop for CancelOnDrop {
fn drop(&mut self) {
- self.0.store(true, core::sync::atomic::Ordering::Relaxed);
+ self.0.store(CANCELLED, Ordering::Relaxed);
}
}
@@ -55,6 +82,7 @@ mod tests {
#[test]
fn test_default_cancelled() {
let c = CancelOnDrop::default();
+ assert!(!c.is_interrupted());
assert!(!c.is_cancelled());
}
@@ -122,6 +150,7 @@ mod tests {
drop(cancel);
// The cloned instance should now see the cancelled flag as true
+ assert!(cloned_cancel.is_interrupted());
assert!(cloned_cancel.is_cancelled());
}
@@ -140,8 +169,31 @@ mod tests {
// Drop one clone - this should cancel all instances
drop(clone1);
+ assert!(cancel.is_interrupted());
assert!(cancel.is_cancelled());
assert!(clone2.is_cancelled());
assert!(clone3.is_cancelled());
}
+
+ #[test]
+ fn test_cancel_on_drop_finalization_request() {
+ let cancel = CancelOnDrop::default();
+ let clone = cancel.clone();
+
+ cancel.request_finalization();
+
+ assert!(clone.is_interrupted());
+ assert!(clone.is_finalization_requested());
+ assert!(!clone.is_cancelled());
+
+ drop(cancel);
+
+ assert!(clone.is_cancelled());
+ assert!(!clone.is_finalization_requested());
+
+ clone.request_finalization();
+
+ assert!(clone.is_cancelled());
+ assert!(!clone.is_finalization_requested());
+ }
}
diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs
index 060d2365298..5b4842ce303 100644
--- a/crates/rpc/rpc-eth-api/src/helpers/call.rs
+++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs
@@ -21,8 +21,10 @@ use futures::Future;
use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks};
use reth_errors::{ProviderError, RethError};
use reth_evm::{
- block::BlockExecutor, env::BlockEnvironment, execute::BlockBuilder, ConfigureEvm, Evm,
- EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, TransactionEnvMut, TxEnvFor,
+ block::{BlockExecutor, TxResult},
+ env::BlockEnvironment,
+ execute::BlockBuilder,
+ ConfigureEvm, Evm, EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, TransactionEnvMut, TxEnvFor,
};
use reth_node_api::BlockBody;
use reth_primitives_traits::Recovered;
@@ -717,9 +719,10 @@ pub trait Call:
executor.execute_transaction(block_tx).map_err(Self::Error::from_eth_err)?;
}
- let tx_env = RpcNodeCore::evm_config(&this).tx_env(tx);
-
- let res = executor.evm_mut().transact(tx_env).map_err(Self::Error::from_evm_err)?;
+ let res = executor
+ .execute_transaction_without_commit(tx)
+ .map_err(Self::Error::from_eth_err)?
+ .into_result();
drop(executor);
f(tx_info, res, db)
})
@@ -732,8 +735,11 @@ pub trait Call:
///
/// This executes on a caller provided EVM, so the target transaction can then be run on the
/// same EVM, keeping any block-scoped EVM state intact. The EVM's inspector configuration is
- /// left untouched; see
- /// [`Trace::inspect_transaction_in_block`] to replay without inspection and trace the target.
+ /// left untouched.
+ ///
+ /// This bypasses the block executor, so it misses any chain-specific per-transaction
+ /// settlement the executor applies. To replay a block's own transactions, use
+ /// [`Trace::replay_block_until`] or [`Trace::inspect_transaction_in_block`] instead.
///
/// If the target index is greater than or equal to the iterator length, all transactions are
/// replayed.
diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs
index a2662147624..08fc7d3328a 100644
--- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs
+++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs
@@ -8,13 +8,17 @@ use alloy_rpc_types_eth::{BlockId, TransactionInfo};
use futures::Future;
use reth_errors::RethError;
use reth_evm::{
- block::BlockExecutor, evm::EvmFactoryExt, tracing::TracingCtx, ConfigureEvm, Evm, EvmEnvFor,
- EvmFor, HaltReasonFor, InspectorFor, IntoTxEnv, TxEnvFor,
+ block::{BlockExecutor, BlockExecutorFor, TxResult},
+ ConfigureEvm, Evm, EvmEnvFor, EvmFor, HaltReasonFor, InspectorFor, IntoTxEnv, TxEnvFor,
};
use reth_primitives_traits::{BlockBody, BlockTy, Recovered, RecoveredBlock};
-use reth_rpc_eth_types::cache::db::StateCacheDb;
+use reth_rpc_eth_types::{cache::db::StateCacheDb, EthApiError};
use reth_storage_api::{ProviderBlock, ProviderTx};
-use revm::{context::Block, context_interface::result::ResultAndState};
+use revm::{
+ context::Block,
+ context_interface::result::{ExecutionResult, ResultAndState},
+ state::EvmState,
+};
use revm_inspectors::tracing::{TracingInspector, TracingInspectorConfig};
use std::sync::Arc;
@@ -98,7 +102,7 @@ pub trait Trace: LoadState> + Call {
None => return Ok(None),
Some(res) => res,
};
- let (tx, tx_info) = transaction.split();
+ let (_, tx_info) = transaction.split();
// we need to get the state of the parent block because we're essentially replaying the
// block the transaction is included in
@@ -113,7 +117,6 @@ pub trait Trace: LoadState> + Call {
// returns transactions included in a block
tx_info.index.expect("transaction_and_block only returns block transactions")
as usize,
- tx,
)?;
f(tx_info, inspector, res, db)
})
@@ -124,8 +127,9 @@ pub trait Trace: LoadState> + Call {
/// Replays all transactions before the target transaction index.
///
- /// All transactions before the target transaction are executed and their changes are written to
- /// the _runtime_ db ([`StateCacheDb`]).
+ /// The block's pre-execution changes and all transactions before the target transaction are
+ /// executed through the block executor, and their changes are written to the _runtime_ db
+ /// ([`StateCacheDb`]).
///
/// If the target index is greater than or equal to the block's transaction count, all
/// transactions are replayed.
@@ -135,19 +139,25 @@ pub trait Trace: LoadState> + Call {
block: &RecoveredBlock>,
target_tx_index: usize,
) -> Result<(), Self::Error> {
- self.apply_pre_execution_changes(block, db)?;
+ let mut executor = self
+ .evm_config()
+ .executor_for_block(db, block.sealed_block())
+ .map_err(RethError::other)
+ .map_err(Self::Error::from_eth_err)?;
+ executor.apply_pre_execution_changes().map_err(Self::Error::from_eth_err)?;
- let evm_env = self.evm_env_for_header(block.sealed_block().sealed_header())?;
- let mut evm = self.evm_config().evm_with_env(db, evm_env);
- self.replay_transactions_until_with_evm(
- &mut evm,
- block.transactions_recovered(),
- target_tx_index,
- )
+ for tx in block.transactions_recovered().take(target_tx_index) {
+ executor.execute_transaction(tx).map_err(Self::Error::from_eth_err)?;
+ }
+ Ok(())
}
/// Replays all transactions before the target transaction without inspection, then executes
- /// the target transaction with the configured inspector, all on the given EVM.
+ /// the block's transaction at the target index with the configured inspector, all through the
+ /// same block executor.
+ ///
+ /// The returned [`ResultAndState`] is the executor's, so it includes any chain-specific
+ /// per-transaction settlement. It is not committed to `db`.
#[expect(clippy::type_complexity)]
fn inspect_transaction_in_block<'a>(
&self,
@@ -155,26 +165,68 @@ pub trait Trace: LoadState> + Call {
db: &'a mut StateCacheDb,
inspector: impl InspectorFor,
target_tx_index: usize,
- target_tx_env: impl IntoTxEnv>,
) -> Result<(ResultAndState>, EvmEnvFor), Self::Error> {
- let block_txs = block.transactions_recovered();
-
- self.apply_pre_execution_changes(block, db)?;
-
let evm_env = self.evm_env_for_header(block.sealed_block().sealed_header())?;
- let mut evm = self.evm_config().evm_with_env_and_inspector(db, evm_env, inspector);
-
- evm.disable_inspector();
- self.replay_transactions_until_with_evm(&mut evm, block_txs, target_tx_index)?;
- evm.enable_inspector();
+ let mut executor = self.inspected_block_executor(block, db, evm_env.clone(), inspector)?;
- let res = evm.transact(target_tx_env).map_err(Self::Error::from_evm_err)?;
+ let mut block_txs = block.transactions_recovered();
+ for tx in block_txs.by_ref().take(target_tx_index) {
+ executor.execute_transaction(tx).map_err(Self::Error::from_eth_err)?;
+ }
+ let target_tx = block_txs.next().ok_or_else(|| {
+ Self::Error::from_eth_err(EthApiError::InvalidParams(format!(
+ "tx_index {target_tx_index} out of bounds for block with {} transactions",
+ block.transaction_count()
+ )))
+ })?;
- let (_, evm_env) = evm.finish();
+ executor.evm_mut().enable_inspector();
+ let res = executor
+ .execute_transaction_without_commit(target_tx)
+ .map_err(Self::Error::from_eth_err)?
+ .into_result();
Ok((res, evm_env))
}
+ /// Creates a block executor for `block` whose EVM runs the given inspector, and applies the
+ /// block's pre-execution changes. The inspector is left disabled.
+ ///
+ /// Block transactions must be replayed through the executor rather than its EVM: the executor
+ /// tracks the transaction's position in the block and applies chain-specific per-transaction
+ /// settlement, such as OP Stack SDM refunds, that a bare EVM replay misses.
+ #[expect(clippy::type_complexity)]
+ fn inspected_block_executor<'a, 'db, I>(
+ &'a self,
+ block: &'a RecoveredBlock>,
+ db: &'db mut StateCacheDb,
+ evm_env: EvmEnvFor,
+ inspector: I,
+ ) -> Result<
+ BlockExecutorFor<
+ 'a,
+ ::BlockExecutorFactory,
+ &'db mut StateCacheDb,
+ I,
+ >,
+ Self::Error,
+ >
+ where
+ I: InspectorFor,
+ {
+ let evm_config = self.evm_config();
+ let ctx = evm_config
+ .context_for_block(block.sealed_block())
+ .map_err(RethError::other)
+ .map_err(Self::Error::from_eth_err)?;
+ let mut evm = evm_config.evm_with_env_and_inspector(db, evm_env, inspector);
+ evm.disable_inspector();
+
+ let mut executor = evm_config.create_executor_with_state(evm, ctx);
+ executor.apply_pre_execution_changes().map_err(Self::Error::from_eth_err)?;
+ Ok(executor)
+ }
+
/// Executes all transactions of a block up to a given index.
///
/// If a `highest_index` is given, this will only execute the first `highest_index`
@@ -268,38 +320,64 @@ pub trait Trace: LoadState> + Call {
let block_timestamp = evm_env.block_env.timestamp().saturating_to();
let base_fee = evm_env.block_env.basefee();
- this.apply_pre_execution_changes(&block, &mut db)?;
-
// prepare transactions, we do everything upfront to reduce time spent with open
// state
- let max_transactions = highest_index.map_or_else(
- || block.body().transaction_count(),
- |highest| {
- // we need + 1 because the index is 0-based
- highest as usize + 1
- },
- );
+ let max_transactions = highest_index
+ .map_or_else(
+ || block.body().transaction_count(),
+ |highest| {
+ // we need + 1 because the index is 0-based
+ (highest as usize).saturating_add(1)
+ },
+ )
+ .min(block.body().transaction_count());
- let mut idx = 0;
+ let inspector = inspector_setup();
+ let fused_inspector = inspector.clone();
+ let mut executor =
+ this.inspected_block_executor(&block, &mut db, evm_env, inspector)?;
+ executor.evm_mut().enable_inspector();
- let results = this
- .evm_config()
- .evm_factory()
- .create_tracer(&mut db, evm_env, inspector_setup())
- .try_trace_many(block.transactions_recovered().take(max_transactions), |ctx| {
- let tx_info = TransactionInfo {
- hash: Some(*ctx.tx.tx_hash()),
- index: Some(idx),
- block_hash: Some(block_hash),
- block_number: Some(block_number),
- block_timestamp: Some(block_timestamp),
- base_fee: Some(base_fee),
- };
- idx += 1;
+ let mut results = Vec::with_capacity(max_transactions);
+ let mut transactions =
+ block.transactions_recovered().take(max_transactions).enumerate().peekable();
+ while let Some((idx, tx)) = transactions.next() {
+ let output = executor
+ .execute_transaction_without_commit(tx)
+ .map_err(Self::Error::from_eth_err)?;
+ let ResultAndState { result, state } = output.result();
- f(tx_info, ctx)
- })
- .collect::>()?;
+ let tx_info = TransactionInfo {
+ hash: Some(*tx.tx_hash()),
+ index: Some(idx as u64),
+ block_hash: Some(block_hash),
+ block_number: Some(block_number),
+ block_timestamp: Some(block_timestamp),
+ base_fee: Some(base_fee),
+ };
+ let mut was_fused = false;
+ let (db, inspector, _) = executor.evm_mut().components_mut();
+ results.push(f(
+ tx_info,
+ TracingCtx {
+ tx,
+ result: result.clone(),
+ state,
+ inspector,
+ db,
+ fused_inspector: &fused_inspector,
+ was_fused: &mut was_fused,
+ },
+ )?);
+ if !was_fused {
+ *executor.evm_mut().inspector_mut() = fused_inspector.clone();
+ }
+
+ // the state of the last traced transaction is not needed
+ if transactions.peek().is_some() {
+ executor.commit_transaction(output);
+ }
+ }
Ok(Some(results))
})
@@ -404,3 +482,30 @@ pub trait Trace: LoadState> + Call {
Ok(())
}
}
+
+/// Context for one transaction of a block traced by [`Trace::trace_block_until_with_inspector`].
+#[derive(Debug)]
+pub struct TracingCtx<'a, T, E: Evm> {
+ /// The transaction that was just executed.
+ pub tx: T,
+ /// Result of transaction execution.
+ pub result: ExecutionResult,
+ /// State changes after transaction.
+ pub state: &'a EvmState,
+ /// Inspector state after transaction.
+ pub inspector: &'a mut E::Inspector,
+ /// Database used when executing the transaction, _before_ committing the state changes.
+ pub db: &'a mut E::DB,
+ /// Inspector state before any transaction was executed.
+ fused_inspector: &'a E::Inspector,
+ /// Whether the inspector was already reset by [`TracingCtx::take_inspector`].
+ was_fused: &'a mut bool,
+}
+
+impl> TracingCtx<'_, T, E> {
+ /// Returns the inspector state of this transaction, resetting the inspector for the next one.
+ pub fn take_inspector(&mut self) -> E::Inspector {
+ *self.was_fused = true;
+ core::mem::replace(self.inspector, self.fused_inspector.clone())
+ }
+}
diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs
index 9860eae2fc7..ff01b500508 100644
--- a/crates/rpc/rpc/src/debug.rs
+++ b/crates/rpc/rpc/src/debug.rs
@@ -19,7 +19,11 @@ use parking_lot::RwLock;
use reth_chainspec::{ChainSpecProvider, EthChainSpec, EthereumHardforks};
use reth_engine_primitives::ConsensusEngineEvent;
use reth_errors::RethError;
-use reth_evm::{block::BlockExecutor, execute::Executor, ConfigureEvm, EvmEnvFor};
+use reth_evm::{
+ block::{BlockExecutor, TxResult},
+ execute::Executor,
+ ConfigureEvm, EvmEnvFor,
+};
use reth_primitives_traits::{
Block as BlockTrait, BlockBody, BlockTy, ReceiptWithBloom, RecoveredBlock,
};
@@ -28,7 +32,7 @@ use reth_rpc_api::DebugApiServer;
use reth_rpc_convert::RpcTxReq;
use reth_rpc_eth_api::{
helpers::{EthTransactions, TraceExt},
- FromEthApiError, FromEvmError, RpcConvert, RpcNodeCore,
+ FromEthApiError, RpcConvert, RpcNodeCore,
};
use reth_rpc_eth_types::{EthApiError, StateCacheDb};
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
@@ -122,20 +126,22 @@ where
.spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| {
let mut results = Vec::with_capacity(block.body().transactions().len());
- eth_api.apply_pre_execution_changes(&block, &mut db)?;
-
let block_env = evm_env.block_env.clone();
- let mut transactions = block.transactions_recovered().enumerate().peekable();
let inspector = DebugInspector::new(opts).map_err(Eth::Error::from_eth_err)?;
- let mut evm =
- eth_api.evm_config().evm_with_env_and_inspector(&mut db, evm_env, inspector);
+ let mut executor =
+ eth_api.inspected_block_executor(&block, &mut db, evm_env, inspector)?;
+ executor.evm_mut().enable_inspector();
+
+ let mut transactions = block.transactions_recovered().enumerate().peekable();
while let Some((index, tx)) = transactions.next() {
let tx_env = eth_api.evm_config().tx_env(tx);
- let res = evm.transact(tx_env.clone()).map_err(Eth::Error::from_evm_err)?;
+ let output = executor
+ .execute_transaction_without_commit(tx)
+ .map_err(Eth::Error::from_eth_err)?;
- let (db, inspector, _) = evm.components_mut();
+ let (db, inspector, _) = executor.evm_mut().components_mut();
let result = inspector
.get_result(
Some(TransactionContext {
@@ -145,7 +151,7 @@ where
}),
&tx_env,
&block_env,
- &res,
+ output.result(),
db,
)
.map_err(Eth::Error::from_eth_err)?;
@@ -155,7 +161,7 @@ where
inspector.fuse().map_err(Eth::Error::from_eth_err)?;
// need to apply the state changes of this transaction before executing the
// next transaction
- db.commit(res.state)
+ executor.commit_transaction(output);
}
}
@@ -243,13 +249,8 @@ where
let mut inspector = DebugInspector::new(opts).map_err(Eth::Error::from_eth_err)?;
let tx_env = eth_api.evm_config().tx_env(&tx);
- let (res, evm_env) = eth_api.inspect_transaction_in_block(
- &block,
- &mut db,
- &mut inspector,
- index,
- tx_env.clone(),
- )?;
+ let (res, evm_env) =
+ eth_api.inspect_transaction_in_block(&block, &mut db, &mut inspector, index)?;
let trace = inspector
.get_result(
@@ -704,22 +705,24 @@ where
.recovered_block(block_hash.into())
.await?
.ok_or(EthApiError::HeaderNotFound(block_hash.into()))?;
- let evm_env = self.eth_api().evm_env_for_header(block.sealed_block().sealed_header())?;
self.eth_api()
.spawn_with_state_at_block(block.parent_hash(), move |eth_api, mut db| {
// Enable transition tracking so that merge_transitions works
db.transition_state = Some(Default::default());
- eth_api.apply_pre_execution_changes(&block, &mut db)?;
+ let mut executor = eth_api
+ .evm_config()
+ .executor_for_block(&mut db, block.sealed_block())
+ .map_err(RethError::other)
+ .map_err(Eth::Error::from_eth_err)?;
+ executor.apply_pre_execution_changes().map_err(Eth::Error::from_eth_err)?;
let mut roots = Vec::with_capacity(block.body().transactions().len());
- let mut evm = eth_api.evm_config().evm_with_env(&mut db, evm_env);
for tx in block.transactions_recovered() {
- let tx_env = eth_api.evm_config().tx_env(tx);
- evm.transact_commit(tx_env).map_err(Eth::Error::from_evm_err)?;
+ executor.execute_transaction(tx).map_err(Eth::Error::from_eth_err)?;
- let state = evm.db_mut();
+ let state = executor.evm_mut().db_mut();
// Merge transitions into cumulative bundle_state
state.merge_transitions(BundleRetention::PlainState);
// Compute state root from the accumulated state changes
diff --git a/crates/stages/api/src/pipeline/mod.rs b/crates/stages/api/src/pipeline/mod.rs
index 0c2f3b668d6..36a8a6f3c3d 100644
--- a/crates/stages/api/src/pipeline/mod.rs
+++ b/crates/stages/api/src/pipeline/mod.rs
@@ -585,6 +585,7 @@ impl Pipeline {
target: "sync::pipeline",
stage = %stage_id,
bad_block = %block.block.number,
+ bad_block_hash = %block.block.hash,
"Stage encountered a validation error: {validation_error}"
);
@@ -609,6 +610,7 @@ impl Pipeline {
target: "sync::pipeline",
stage = %stage_id,
bad_block = %block.block.number,
+ bad_block_hash = %block.block.hash,
"Stage encountered an execution error: {execution_error}"
);
}
diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml
index 1ff298889c1..17db3b470e5 100644
--- a/crates/storage/provider/Cargo.toml
+++ b/crates/storage/provider/Cargo.toml
@@ -69,6 +69,9 @@ rayon.workspace = true
rocksdb.workspace = true
+[target.'cfg(unix)'.dependencies]
+libc.workspace = true
+
[dev-dependencies]
reth-db = { workspace = true, features = ["test-utils"] }
reth-primitives-traits = { workspace = true, features = ["arbitrary", "test-utils"] }
diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs
index f29f84a5327..df23ce5fda3 100644
--- a/crates/storage/provider/src/providers/database/provider.rs
+++ b/crates/storage/provider/src/providers/database/provider.rs
@@ -798,7 +798,25 @@ impl DatabaseProvider>();
- let merged_trie = TrieUpdatesSorted::disjointed_merge_batch(&batch, &mask);
+ // Sparse-trie streaming emits node updates, while the serial state-root path,
+ // including fallback, can emit a whole storage-trie wipe. `disjointed_merge_batch`
+ // rejects wipes, so use the ordered merge. Persisting updates shadowed by the
+ // masking suffix is redundant but safe: the in-memory overlay still takes
+ // precedence.
+ //
+ // With a revm release containing bluealloy/revm#3863, post-Cancun selfdestructs
+ // will no longer result in `storage.is_deleted` in serial trie updates. The flag
+ // remains valid `save_blocks` input when processing pre-Cancun historical data.
+ let contains_storage_wipe = batch.iter().chain(&mask).any(|updates| {
+ updates.storage_tries_ref().values().any(|storage| storage.is_deleted)
+ });
+ let merged_trie = if contains_storage_wipe {
+ TrieUpdatesSorted::merge_batch(
+ state_trie_blocks.iter().rev().map(|block| block.trie_updates()),
+ )
+ } else {
+ Arc::new(TrieUpdatesSorted::disjointed_merge_batch(&batch, &mask))
+ };
if !merged_trie.is_empty() {
self.write_trie_updates_sorted(&merged_trie)?;
}
@@ -4858,6 +4876,123 @@ mod tests {
assert_eq!(masked_entries[0].1.nibbles.0, masked_storage_node);
}
+ #[cfg(feature = "partial-persistence")]
+ #[test]
+ fn test_save_blocks_merges_storage_wipe_in_multi_block_batch() {
+ use reth_trie::{
+ updates::{StorageTrieUpdatesSorted, TrieUpdatesSorted},
+ BranchNodeCompact, HashedPostStateSorted,
+ };
+
+ let factory = create_test_provider_factory();
+ factory.set_storage_settings_cache(StorageSettings::v2());
+
+ let mut test_block_builder = TestBlockBuilder::eth().with_state();
+ let genesis = test_block_builder.get_executed_blocks(0..1).next().unwrap();
+ let mut blocks: Vec<_> = test_block_builder.get_executed_blocks(1..4).collect();
+ let address = B256::with_last_byte(1);
+ let storage_path = Nibbles::from_nibbles([0x1, 0x2]);
+
+ let provider_rw = factory.provider_rw().unwrap();
+ save_genesis(&provider_rw, &genesis).unwrap();
+ provider_rw
+ .write_trie_updates_sorted(&TrieUpdatesSorted::new(
+ vec![],
+ B256Map::from_iter([(
+ address,
+ StorageTrieUpdatesSorted {
+ is_deleted: false,
+ storage_nodes: vec![(storage_path, Some(BranchNodeCompact::default()))],
+ },
+ )]),
+ ))
+ .unwrap();
+ provider_rw.commit().unwrap();
+
+ let wipe = TrieUpdatesSorted::new(
+ vec![],
+ B256Map::from_iter([(
+ address,
+ StorageTrieUpdatesSorted { is_deleted: true, storage_nodes: vec![] },
+ )]),
+ );
+ let wipe_block = &blocks[2];
+ blocks[2] = ExecutedBlock::new(
+ Arc::clone(&wipe_block.recovered_block),
+ Arc::clone(&wipe_block.execution_output),
+ ComputedTrieData::new(Arc::new(HashedPostStateSorted::default()), Arc::new(wipe)),
+ );
+
+ let provider_rw = factory.provider_rw().unwrap();
+ let input = SaveBlocksInput::new(blocks, 0, 0, 3, 3);
+ provider_rw.save_blocks(&input).unwrap();
+ provider_rw.commit().unwrap();
+
+ let provider = factory.provider().unwrap();
+ let checkpoint = provider.get_stage_checkpoint(StageId::Finish).unwrap().unwrap();
+ assert_eq!(checkpoint.block_number, 3);
+ let storage_entries = provider
+ .tx_ref()
+ .cursor_dup_read::()
+ .unwrap()
+ .walk_dup(Some(address), None)
+ .unwrap()
+ .collect::, _>>()
+ .unwrap();
+ assert!(storage_entries.is_empty());
+ }
+
+ #[cfg(feature = "partial-persistence")]
+ #[test]
+ fn test_save_blocks_batches_transient_storage_wipe() {
+ use alloy_primitives::map::B256Set;
+ use reth_trie::{updates::TrieUpdates, HashBuilder, HashedPostStateSorted};
+
+ let factory = create_test_provider_factory();
+ factory.set_storage_settings_cache(StorageSettings::v2());
+
+ let mut test_block_builder = TestBlockBuilder::eth().with_state();
+ let genesis = test_block_builder.get_executed_blocks(0..1).next().unwrap();
+ let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..4).collect();
+
+ let provider_rw = factory.provider_rw().unwrap();
+ save_genesis(&provider_rw, &genesis).unwrap();
+ provider_rw.commit().unwrap();
+
+ // A contract created and self-destructed in the same transaction leaves an empty whole-
+ // storage wipe in the trie updates, even though the account never reaches persisted state.
+ let ephemeral_account = B256::with_last_byte(0x57);
+ let hashed_state = HashedPostStateSorted::default();
+ let mut trie_updates = TrieUpdates::default();
+ trie_updates.finalize(
+ HashBuilder::default(),
+ Default::default(),
+ B256Set::from_iter([ephemeral_account]),
+ );
+ let trie_updates = trie_updates.into_sorted();
+ assert!(trie_updates.storage_tries_ref()[&ephemeral_account].is_deleted);
+ let selfdestruct_block = ExecutedBlock::new(
+ Arc::clone(&blocks[2].recovered_block),
+ Arc::clone(&blocks[2].execution_output),
+ ComputedTrieData::new(Arc::new(hashed_state), Arc::new(trie_updates)),
+ );
+
+ let provider_rw = factory.provider_rw().unwrap();
+ let input = SaveBlocksInput::new(
+ vec![blocks[0].clone(), blocks[1].clone(), selfdestruct_block],
+ 0,
+ 0,
+ 3,
+ 3,
+ );
+ provider_rw.save_blocks(&input).unwrap();
+ provider_rw.commit().unwrap();
+
+ let checkpoint =
+ factory.provider().unwrap().get_stage_checkpoint(StageId::Finish).unwrap().unwrap();
+ assert_eq!(checkpoint.block_number, 3);
+ }
+
#[cfg(feature = "partial-persistence")]
#[test]
fn test_save_blocks_partial_cycles_do_not_duplicate_static_file_writes() {
diff --git a/crates/storage/provider/src/providers/rocksdb/provider.rs b/crates/storage/provider/src/providers/rocksdb/provider.rs
index d7198e7c74a..9fae9514ebd 100644
--- a/crates/storage/provider/src/providers/rocksdb/provider.rs
+++ b/crates/storage/provider/src/providers/rocksdb/provider.rs
@@ -115,14 +115,22 @@ const DEFAULT_BLOCK_SIZE: usize = 16 * 1024;
/// Default max background jobs for `RocksDB` compaction and flushing.
const DEFAULT_MAX_BACKGROUND_JOBS: i32 = 6;
-/// Default max open file descriptors for `RocksDB`.
+/// Max open file descriptors for `RocksDB` when the process limit is low.
///
/// Caps the number of SST file handles `RocksDB` keeps open simultaneously.
/// Set to 512 to stay within the common default OS `ulimit -n` of 1024,
/// leaving headroom for MDBX, static files, and other I/O.
-/// `RocksDB` uses an internal table cache and re-opens files on demand,
-/// so this has negligible performance impact on read-heavy workloads.
-const DEFAULT_MAX_OPEN_FILES: i32 = 512;
+const LIMITED_MAX_OPEN_FILES: i32 = 512;
+
+/// Keeps all `RocksDB` files open and avoids table cache lookups.
+const KEEP_ALL_FILES_OPEN: i32 = -1;
+
+/// Minimum process file descriptor limit for keeping all `RocksDB` files open.
+///
+/// Mature archive databases can use tens of thousands of file descriptors. This threshold keeps
+/// unlimited mode for hosts configured for that workload while retaining a bounded table cache on
+/// systems with low limits.
+const HIGH_FILE_DESCRIPTOR_LIMIT: u64 = 128 * 1024;
/// Default bytes per sync for `RocksDB` WAL writes (1 MB).
const DEFAULT_BYTES_PER_SYNC: u64 = 1_048_576;
@@ -234,7 +242,7 @@ impl RocksDBBuilder {
options.set_log_level(log_level);
- options.set_max_open_files(DEFAULT_MAX_OPEN_FILES);
+ options.set_max_open_files(select_max_open_files());
// Delete obsolete WAL files immediately after all column families have flushed.
// Both set to 0 means "delete ASAP, no archival".
@@ -415,7 +423,7 @@ impl RocksDBBuilder {
// Open as secondary instance for catch-up capability.
// Secondary needs max_open_files = -1 to keep all FDs open.
let mut options = options;
- options.set_max_open_files(-1);
+ options.set_max_open_files(KEEP_ALL_FILES_OPEN);
let secondary_path = self
.path
@@ -2879,6 +2887,60 @@ const fn convert_log_level(level: LogLevel) -> rocksdb::LogLevel {
}
}
+/// Selects the `RocksDB` `max_open_files` setting from the current file descriptor limit
+/// balancing performance vs. compatibility.
+///
+/// A mature database can use tens of thousands of file descriptors. With a
+/// finite `max_open_files` value, `RocksDB` performs additional table-cache checks
+/// even when enough capacity is available. The value `-1` keeps all table files
+/// open and avoids this overhead.
+///
+/// During normal startup, Reth raises the soft file descriptor limit to the
+/// system hard limit before it opens `RocksDB`. Therefore, we expect the current
+/// limit to be the hard limit. A survey of modern Linux distributions shows
+/// common default limits of `1024:524288`.
+///
+/// We use `-1` only when the current limit is above a conservative threshold.
+/// This keeps enough file descriptors for other parts of the process. When the
+/// limit is lower or cannot be read, we use a stricter fixed limit.
+fn select_max_open_files() -> i32 {
+ let file_descriptor_limit = current_file_descriptor_limit();
+ let max_open_files = max_open_files_for_limit(file_descriptor_limit);
+
+ if max_open_files == LIMITED_MAX_OPEN_FILES {
+ tracing::warn!(
+ target: "providers::rocksdb",
+ ?file_descriptor_limit,
+ threshold = HIGH_FILE_DESCRIPTOR_LIMIT,
+ max_open_files,
+ "RocksDB will not keep all files open; performance may be reduced"
+ );
+ }
+
+ max_open_files
+}
+
+const fn max_open_files_for_limit(file_descriptor_limit: Option) -> i32 {
+ match file_descriptor_limit {
+ Some(limit) if limit >= HIGH_FILE_DESCRIPTOR_LIMIT => KEEP_ALL_FILES_OPEN,
+ _ => LIMITED_MAX_OPEN_FILES,
+ }
+}
+
+#[cfg(unix)]
+#[allow(clippy::useless_conversion)]
+fn current_file_descriptor_limit() -> Option {
+ let mut limit = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
+ // SAFETY: `limit` points to initialized writable memory for the kernel to populate.
+ let result = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &raw mut limit) };
+ (result == 0).then(|| limit.rlim_cur.into())
+}
+
+#[cfg(not(unix))]
+const fn current_file_descriptor_limit() -> Option {
+ None
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -2895,6 +2957,16 @@ mod tests {
};
use tempfile::TempDir;
+ #[test]
+ fn max_open_files_adapts_to_file_descriptor_limit() {
+ assert_eq!(max_open_files_for_limit(Some(HIGH_FILE_DESCRIPTOR_LIMIT)), KEEP_ALL_FILES_OPEN);
+ assert_eq!(
+ max_open_files_for_limit(Some(HIGH_FILE_DESCRIPTOR_LIMIT - 1)),
+ LIMITED_MAX_OPEN_FILES
+ );
+ assert_eq!(max_open_files_for_limit(None), LIMITED_MAX_OPEN_FILES);
+ }
+
#[test]
fn test_with_default_tables_registers_required_column_families() {
let temp_dir = TempDir::new().unwrap();
diff --git a/docs/vocs/vocs.config.ts b/docs/vocs/vocs.config.ts
index bb8a8fba54b..6aa891ac973 100644
--- a/docs/vocs/vocs.config.ts
+++ b/docs/vocs/vocs.config.ts
@@ -23,7 +23,7 @@ export default defineConfig({
{ text: 'Rustdocs', link: '/docs' },
{ text: 'GitHub', link: 'https://github.com/paradigmxyz/reth' },
{
- text: 'v2.4.1',
+ text: 'v2.5.2',
items: [
{
text: 'Releases',