diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 3bb0d757e4f..ad7a5254471 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -14097,8 +14097,10 @@ dependencies = [ "reth-trie-common", "reth-trie-db", "revm", + "rstest", "serde", "serde_json", + "thiserror 2.0.18", "tokio", "url", ] diff --git a/rust/op-reth/crates/cli/src/lib.rs b/rust/op-reth/crates/cli/src/lib.rs index c4c1f00eed1..e4af24c5040 100644 --- a/rust/op-reth/crates/cli/src/lib.rs +++ b/rust/op-reth/crates/cli/src/lib.rs @@ -32,10 +32,12 @@ use reth_cli_runner::CliRunner; use reth_db::DatabaseEnv; use reth_node_builder::{NodeBuilder, WithLaunchContext}; use reth_node_core::{ - args::{DefaultEngineValues, LogArgs, TraceArgs}, + args::{LogArgs, TraceArgs}, version::version_metadata, }; -use reth_optimism_node::args::RollupArgs; +use reth_optimism_node::{ + args::RollupArgs, init_op_engine_defaults, state_masking::STATE_MASKING_UNSUPPORTED, +}; // This allows us to manually enable node metrics features, required for proper jemalloc metric // reporting @@ -152,11 +154,19 @@ const BAL_ARGS_UNSUPPORTED_HELP: &str = "Block access list options are not suppo /// /// op-reth runs with state masking off: it defaults the flag to 0, hides it, and rejects any /// nonzero value from the command line or `RETH_ENGINE_NUM_STATE_MASKING_BLOCKS`. Unlike -/// `DENIED_ARGS`, an explicit 0 stays accepted. See [`STATE_MASKING_UNSUPPORTED_HELP`]. +/// `DENIED_ARGS`, an explicit 0 stays accepted. See [`state_masking_unsupported_help`]. const STATE_MASKING_ARG: &str = "num_state_masking_blocks"; -/// Startup error for a nonzero state masking window. -const STATE_MASKING_UNSUPPORTED_HELP: &str = "--engine.num-state-masking-blocks must be 0 on op-reth.\n\nState masking leaves the on-disk state trie behind the persisted tip. After an unclean shutdown\nwith masking on, op-reth releases before reth v2.6.0 cannot open the datadir, and upstream\nstill has open bugs in the masked-trie code paths."; +/// Startup error for a nonzero state masking window: [`STATE_MASKING_UNSUPPORTED`] plus the +/// reason. +fn state_masking_unsupported_help() -> String { + format!( + "{STATE_MASKING_UNSUPPORTED}.\n\nState masking leaves the on-disk state trie behind the \ + persisted tip. After an unclean shutdown\nwith masking on, op-reth releases before reth \ + v2.6.0 cannot open the datadir, and upstream\nstill has open bugs in the masked-trie code \ + paths." + ) +} impl Cli { /// Parses only the default CLI arguments, rejecting `DENIED_ARGS` @@ -183,9 +193,10 @@ where /// Returns the clap command with all `DENIED_ARGS` and the state masking flag hidden from help /// output. pub fn command_with_denied_args_hidden() -> clap::Command { - // Default state masking to off before clap reads the global engine defaults. If another - // caller initialized them first, the post-parse check still rejects a nonzero value. - let _ = DefaultEngineValues::default().with_num_state_masking_blocks(0).try_init(); + // Must run before clap reads the global engine defaults. + if let Err(err) = init_op_engine_defaults() { + eprintln!("Warning: {err}"); + } ::command().mut_subcommands(hide_denied_args) } @@ -219,9 +230,8 @@ where if let Some(node) = matches.subcommand_matches("node") && node.get_one::(STATE_MASKING_ARG).is_some_and(|blocks| *blocks != 0) { - return Err( - cmd.error(clap::error::ErrorKind::ValueValidation, STATE_MASKING_UNSUPPORTED_HELP) - ); + return Err(cmd + .error(clap::error::ErrorKind::ValueValidation, state_masking_unsupported_help())); } ::from_arg_matches_mut(&mut matches) } @@ -273,7 +283,7 @@ mod test { use clap::Parser; use reth_cli_commands::{NodeCommand, node::NoArgs}; use reth_optimism_chainspec::{OP_DEV, OP_MAINNET}; - use reth_optimism_node::args::RollupArgs; + use reth_optimism_node::{args::RollupArgs, init_op_engine_defaults}; #[test] fn deny_minimal_on_parse() { @@ -413,6 +423,7 @@ mod test { #[test] fn parse_dev() { + init_op_engine_defaults().unwrap(); let cmd = NodeCommand::::parse_from(["op-reth", "--dev"]); let chain = OP_DEV.clone(); assert_eq!(cmd.chain.chain, chain.chain); @@ -431,6 +442,7 @@ mod test { #[test] fn parse_node() { + init_op_engine_defaults().unwrap(); let cmd = Cli::::parse_from([ "op-reth", "node", diff --git a/rust/op-reth/crates/node/Cargo.toml b/rust/op-reth/crates/node/Cargo.toml index 405c23e29b3..8122e361d0c 100644 --- a/rust/op-reth/crates/node/Cargo.toml +++ b/rust/op-reth/crates/node/Cargo.toml @@ -30,6 +30,7 @@ reth-tasks.workspace = true reth-trie-common.workspace = true reth-node-core.workspace = true reth-rpc-engine-api.workspace = true +reth-storage-overlay.workspace = true reth-rpc-api.workspace = true reth-rpc-builder.workspace = true @@ -81,6 +82,7 @@ tokio.workspace = true clap.workspace = true serde.workspace = true eyre.workspace = true +thiserror.workspace = true url.workspace = true futures-util.workspace = true @@ -101,7 +103,6 @@ reth-rpc.workspace = true reth-rpc-eth-api.workspace = true reth-rpc-eth-types.workspace = true reth-stages-types.workspace = true -reth-storage-overlay.workspace = true reth-trie-db.workspace = true alloy-network.workspace = true @@ -117,6 +118,7 @@ metrics-util = { workspace = true, features = ["debugging"] } # Fatal-signal handler in tests/it/crash_backtrace.rs. libc.workspace = true op-alloy-network.workspace = true +rstest.workspace = true [features] default = ["reth-codec"] diff --git a/rust/op-reth/crates/node/src/lib.rs b/rust/op-reth/crates/node/src/lib.rs index 0c4f0c0e502..a7449cb2d92 100644 --- a/rust/op-reth/crates/node/src/lib.rs +++ b/rust/op-reth/crates/node/src/lib.rs @@ -29,6 +29,10 @@ pub use head_metrics::{OpChainHeadMetrics, maintain_chain_head_metrics}; pub mod node; pub use node::*; +/// State masking defaults and the guard that rejects a masked engine config. +pub mod state_masking; +pub use state_masking::init_op_engine_defaults; + /// OP payload-service configuration. pub mod payload_service; pub use payload_service::OpPayloadServiceBuilder; diff --git a/rust/op-reth/crates/node/src/node.rs b/rust/op-reth/crates/node/src/node.rs index 23f7f2bd057..78f1cbcbdb9 100644 --- a/rust/op-reth/crates/node/src/node.rs +++ b/rust/op-reth/crates/node/src/node.rs @@ -6,6 +6,7 @@ use crate::{ derived_payload_service::DerivedPayloadServiceBuilder, engine::OpEngineValidator, payload_service::OpPayloadServiceBuilder, + state_masking::{NoStateMaskingValidatorBuilder, ensure_no_state_masking}, txpool::{OpCustomTransactionPool, OpTransactionValidator}, }; use alloy_consensus::BlockHeader; @@ -411,17 +412,12 @@ impl OpNode { /// Launches an OP node, optionally installing proof history, then waits for it to exit. /// -/// Fails if the engine config enables state masking, which op-reth does not support. +/// Fails before opening the datadir if the engine config enables state masking. pub async fn launch_node( builder: WithLaunchContext>, node: OpNode, ) -> eyre::Result<(), ErrReport> { - // op-reth's CLI already defaults masking to 0 and rejects nonzero values; this catches - // callers that build a `NodeConfig` without that CLI. - eyre::ensure!( - builder.config().engine.num_state_masking_blocks() == 0, - "--engine.num-state-masking-blocks must be 0 on op-reth: state masking is not supported" - ); + ensure_no_state_masking(builder.config().engine.num_state_masking_blocks())?; let builder = if node.args.proofs_history { // Defaults to `/historical-proofs` when not supplied — see @@ -1082,10 +1078,12 @@ where EVB: EngineValidatorBuilder, RpcMiddleware: Send, { - type ValidatorBuilder = EVB; + type ValidatorBuilder = NoStateMaskingValidatorBuilder; fn engine_validator_builder(&self) -> Self::ValidatorBuilder { - EngineValidatorAddOn::engine_validator_builder(&self.rpc_add_ons) + NoStateMaskingValidatorBuilder::new(EngineValidatorAddOn::engine_validator_builder( + &self.rpc_add_ons, + )) } } diff --git a/rust/op-reth/crates/node/src/state_masking.rs b/rust/op-reth/crates/node/src/state_masking.rs new file mode 100644 index 00000000000..14a1d576ffd --- /dev/null +++ b/rust/op-reth/crates/node/src/state_masking.rs @@ -0,0 +1,97 @@ +//! op-reth runs with state masking off: it defaults the window to 0 where it owns the engine +//! defaults and rejects a nonzero window where it can only observe the config it is launched with. + +use reth_node_api::{AddOnsContext, FullNodeComponents, PrimitivesTy, TreeConfig}; +use reth_node_builder::rpc::EngineValidatorBuilder; +use reth_node_core::args::{DefaultEngineValues, EngineArgs}; +use reth_storage_overlay::OverlayManager; +use reth_tracing::tracing::info; + +/// Startup error for a nonzero effective state masking window. +pub const STATE_MASKING_UNSUPPORTED: &str = + "--engine.num-state-masking-blocks must be 0 on op-reth: state masking is not supported"; + +/// The engine defaults were locked with a nonzero state masking window before +/// [`init_op_engine_defaults`] ran, so every `EngineArgs` built from them masks state. +#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)] +#[error( + "engine defaults were locked with --engine.num-state-masking-blocks {num_state_masking_blocks} \ + before op-reth could default state masking off; a node launched with this default is rejected" +)] +pub struct EngineDefaultsLockedMasked { + /// The locked default window. + pub num_state_masking_blocks: u64, +} + +/// Defaults `--engine.num-state-masking-blocks` to 0 for every [`EngineArgs`] built from here on. +/// +/// The engine defaults are a process-global that locks on first use, so this must run before the +/// first `EngineArgs` or `NodeConfig` is built. A default locked earlier is kept and reported +/// when it masks; any node launched with it is rejected. +pub fn init_op_engine_defaults() -> Result<(), EngineDefaultsLockedMasked> { + if DefaultEngineValues::default().with_num_state_masking_blocks(0).try_init().is_ok() { + return Ok(()); + } + match EngineArgs::default().num_state_masking_blocks() { + 0 => Ok(()), + num_state_masking_blocks => Err(EngineDefaultsLockedMasked { num_state_masking_blocks }), + } +} + +/// Rejects a nonzero state masking window; op-reth does not support state masking. +pub fn ensure_no_state_masking(num_state_masking_blocks: u64) -> eyre::Result<()> { + eyre::ensure!(num_state_masking_blocks == 0, STATE_MASKING_UNSUPPORTED); + Ok(()) +} + +/// Builds the engine tree validator with `inner`, rejecting a [`TreeConfig`] that masks state. +#[derive(Clone, Debug)] +pub struct NoStateMaskingValidatorBuilder { + inner: EVB, +} + +impl NoStateMaskingValidatorBuilder { + /// Wraps `inner`. + pub const fn new(inner: EVB) -> Self { + Self { inner } + } +} + +impl EngineValidatorBuilder for NoStateMaskingValidatorBuilder +where + N: FullNodeComponents, + EVB: EngineValidatorBuilder, +{ + type EngineValidator = EVB::EngineValidator; + + async fn build_tree_validator( + self, + ctx: &AddOnsContext<'_, N>, + tree_config: TreeConfig, + overlay_manager: OverlayManager>, + ) -> eyre::Result { + ensure_no_state_masking(tree_config.num_state_masking_blocks())?; + info!(target: "reth::cli", "Engine tree config has state masking off"); + self.inner.build_tree_validator(ctx, tree_config, overlay_manager).await + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rstest::rstest; + + #[rstest] + #[case::masking_off(0, true)] + #[case::masking_on(30, false)] + fn state_masking_guard(#[case] num_state_masking_blocks: u64, #[case] accepted: bool) { + assert_eq!(ensure_no_state_masking(num_state_masking_blocks).is_ok(), accepted); + } + + #[test] + fn init_op_engine_defaults_turns_state_masking_off() { + init_op_engine_defaults().unwrap(); + + assert_eq!(EngineArgs::default().num_state_masking_blocks(), 0); + } +} diff --git a/rust/op-reth/crates/node/src/utils.rs b/rust/op-reth/crates/node/src/utils.rs index bb504309731..e06c39b0010 100644 --- a/rust/op-reth/crates/node/src/utils.rs +++ b/rust/op-reth/crates/node/src/utils.rs @@ -1,14 +1,16 @@ -use crate::{OpBuiltPayload, OpNode as OtherOpNode}; +use crate::{OpBuiltPayload, OpNode as OtherOpNode, init_op_engine_defaults}; use alloy_genesis::Genesis; use alloy_primitives::{Address, B256}; use op_alloy_rpc_types_engine::OpPayloadAttributes; use reth_e2e_test_utils::{ NodeHelperType, TmpDB, transaction::TransactionTestContext, wallet::Wallet, }; -use reth_node_api::NodeTypesWithDBAdapter; -use reth_optimism_chainspec::OpChainSpecBuilder; +use reth_node_api::{NodeTypesWithDBAdapter, TreeConfig}; +use reth_node_core::node_config::NodeConfig; +use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder}; use reth_optimism_payload_builder::OpPayloadAttrs; use reth_provider::providers::BlockchainProvider; +use reth_tracing::tracing::warn; use std::sync::Arc; use tokio::sync::Mutex; @@ -16,6 +18,30 @@ use tokio::sync::Mutex; pub(crate) type OpNode = NodeHelperType>>; +/// Puts the op-reth engine defaults in effect, warning when another `NodeConfig` locked them +/// masked first. +fn init_op_engine_defaults_or_warn() { + if let Err(err) = init_op_engine_defaults() { + warn!(target: "reth::cli", %err, "Engine defaults are masked"); + } +} + +/// Builds a [`NodeConfig`] for `chain_spec` on unused ports with the op-reth engine defaults in +/// effect, so a node launched from it passes the state masking guard. +pub fn node_config(chain_spec: impl Into>) -> NodeConfig { + init_op_engine_defaults_or_warn(); + NodeConfig::new(chain_spec.into()).with_unused_ports() +} + +/// The default [`TreeConfig`] with state masking off, which the state masking guard accepts. +/// +/// Also puts the op-reth engine defaults in effect, so a `NodeConfig` built after this call +/// runs unmasked. +pub fn unmasked_tree_config() -> TreeConfig { + init_op_engine_defaults_or_warn(); + TreeConfig::default().with_num_state_masking_blocks(0) +} + /// Creates the initial setup with `num_nodes` of the node config, started and connected. pub async fn setup(num_nodes: usize) -> eyre::Result<(Vec, Wallet)> { let genesis: Genesis = @@ -26,7 +52,7 @@ pub async fn setup(num_nodes: usize) -> eyre::Result<(Vec, Wallet)> { OpChainSpecBuilder::optimism_sepolia().genesis(genesis).ecotone_activated().build(), ), false, - Default::default(), + unmasked_tree_config(), optimism_payload_attributes, ) .await diff --git a/rust/op-reth/crates/node/tests/e2e-testsuite/testsuite.rs b/rust/op-reth/crates/node/tests/e2e-testsuite/testsuite.rs index 49d3ba3e8ac..ce7250ccaf1 100644 --- a/rust/op-reth/crates/node/tests/e2e-testsuite/testsuite.rs +++ b/rust/op-reth/crates/node/tests/e2e-testsuite/testsuite.rs @@ -7,7 +7,9 @@ use reth_e2e_test_utils::testsuite::{ setup::{NetworkSetup, Setup}, }; use reth_optimism_chainspec::{OP_MAINNET, OpChainSpecBuilder}; -use reth_optimism_node::{OpEngineTypes, OpNode, payload::OpPayloadAttrs}; +use reth_optimism_node::{ + OpEngineTypes, OpNode, payload::OpPayloadAttrs, utils::unmasked_tree_config, +}; use std::sync::Arc; #[tokio::test] @@ -22,7 +24,8 @@ async fn test_testsuite_op_assert_mine_block() -> Result<()> { .build() .into(), )) - .with_network(NetworkSetup::single_node()); + .with_network(NetworkSetup::single_node()) + .with_tree_config(unmasked_tree_config()); let test = TestBuilder::new().with_setup(setup).with_action(AssertMineBlock::::new( @@ -69,7 +72,8 @@ async fn test_testsuite_op_assert_mine_block_isthmus_activated() -> Result<()> { .build() .into(), )) - .with_network(NetworkSetup::single_node()); + .with_network(NetworkSetup::single_node()) + .with_tree_config(unmasked_tree_config()); let test = TestBuilder::new().with_setup(setup).with_action(AssertMineBlock::::new( diff --git a/rust/op-reth/crates/node/tests/it/builder.rs b/rust/op-reth/crates/node/tests/it/builder.rs index 0613b85d6c2..5320120f333 100644 --- a/rust/op-reth/crates/node/tests/it/builder.rs +++ b/rust/op-reth/crates/node/tests/it/builder.rs @@ -14,12 +14,13 @@ use reth_db::test_utils::create_test_rw_db; use reth_evm::{Database, Evm, EvmEnv, EvmFactory, precompiles::PrecompilesMap}; use reth_node_api::{FullNodeComponents, NodeTypesWithDBAdapter}; use reth_node_builder::{ - BuilderContext, FullNodeTypes, Node, NodeBuilder, NodeConfig, NodeTypes, - components::ExecutorBuilder, + BuilderContext, FullNodeTypes, Node, NodeBuilder, NodeTypes, components::ExecutorBuilder, }; use reth_optimism_chainspec::{OP_MAINNET, OP_SEPOLIA, OpChainSpec}; use reth_optimism_evm::{OpBlockExecutorFactory, OpEvm, OpEvmFactory, OpRethReceiptBuilder, OpTx}; -use reth_optimism_node::{OpEvmConfig, OpExecutorBuilder, OpNode, args::RollupArgs}; +use reth_optimism_node::{ + OpEvmConfig, OpExecutorBuilder, OpNode, args::RollupArgs, utils::node_config, +}; use reth_optimism_primitives::OpPrimitives; use reth_provider::providers::BlockchainProvider; use revm::{ @@ -35,7 +36,7 @@ use std::sync::OnceLock; #[test] fn test_basic_setup() { // parse CLI -> config - let config = NodeConfig::new(OP_MAINNET.clone()); + let config = node_config(OP_MAINNET.clone()); let db = create_test_rw_db(); let args = RollupArgs::default(); let op_node = OpNode::new(args); @@ -206,7 +207,7 @@ fn test_setup_custom_precompiles() { } } - NodeBuilder::new(NodeConfig::new(OP_SEPOLIA.clone())) + NodeBuilder::new(node_config(OP_SEPOLIA.clone())) .with_database(create_test_rw_db()) .with_types::() .with_components( diff --git a/rust/op-reth/crates/node/tests/it/custom_genesis.rs b/rust/op-reth/crates/node/tests/it/custom_genesis.rs index f9d95ee782c..9f1131e3d24 100644 --- a/rust/op-reth/crates/node/tests/it/custom_genesis.rs +++ b/rust/op-reth/crates/node/tests/it/custom_genesis.rs @@ -8,10 +8,13 @@ use reth_db::test_utils::create_test_rw_db_with_path; use reth_e2e_test_utils::{ node::NodeTestContext, transaction::TransactionTestContext, wallet::Wallet, }; -use reth_node_builder::{EngineNodeLauncher, Node, NodeBuilder, NodeConfig}; +use reth_node_builder::{EngineNodeLauncher, Node, NodeBuilder}; use reth_node_core::args::DatadirArgs; use reth_optimism_chainspec::OpChainSpecBuilder; -use reth_optimism_node::{OpNode, utils::optimism_payload_attributes}; +use reth_optimism_node::{ + OpNode, + utils::{node_config, optimism_payload_attributes, unmasked_tree_config}, +}; use reth_provider::{HeaderProvider, StageCheckpointReader, providers::BlockchainProvider}; use reth_stages_types::StageId; use std::sync::Arc; @@ -37,7 +40,7 @@ async fn test_op_node_custom_genesis_number() { let wallet = Arc::new(Mutex::new(Wallet::default().with_chain_id(chain_spec.chain().into()))); // Configure and launch the node - let config = NodeConfig::new(chain_spec.clone()).with_datadir_args(DatadirArgs { + let config = node_config(chain_spec.clone()).with_datadir_args(DatadirArgs { datadir: reth_db::test_utils::tempdir_path().into(), ..Default::default() }); @@ -58,7 +61,7 @@ async fn test_op_node_custom_genesis_number() { let launcher = EngineNodeLauncher::new( runtime.clone(), builder.config.datadir(), - Default::default(), + unmasked_tree_config(), ); builder.launch_with(launcher) }) diff --git a/rust/op-reth/crates/node/tests/it/custom_pool/mod.rs b/rust/op-reth/crates/node/tests/it/custom_pool/mod.rs index c8e1aea2d6b..b4ece55e405 100644 --- a/rust/op-reth/crates/node/tests/it/custom_pool/mod.rs +++ b/rust/op-reth/crates/node/tests/it/custom_pool/mod.rs @@ -25,6 +25,7 @@ use reth_optimism_node::{ OpConsensusBuilder, OpExecutorBuilder, OpNetworkBuilder, OpNodeTypes, OpPayloadBuilder, OpPoolBuilder, }, + utils::node_config, }; use reth_optimism_txpool::OpPooledTransaction; use reth_tasks::Runtime; @@ -40,11 +41,10 @@ fn node_test_setup() -> (NodeConfig, Arc> ); let chain_id: u64 = chain_spec.chain().into(); - let mut config = - NodeConfig::new(chain_spec).with_unused_ports().with_datadir_args(DatadirArgs { - datadir: reth_db::test_utils::tempdir_path().into(), - ..Default::default() - }); + let mut config = node_config(chain_spec).with_datadir_args(DatadirArgs { + datadir: reth_db::test_utils::tempdir_path().into(), + ..Default::default() + }); config.network.discovery.discv5_port = Some(0); config.network.discovery.discv5_port_ipv6 = Some(0); let db = create_test_rw_db_with_path( diff --git a/rust/op-reth/crates/node/tests/it/custom_pool/ordering.rs b/rust/op-reth/crates/node/tests/it/custom_pool/ordering.rs index 48085bc8ca7..8d202377ea5 100644 --- a/rust/op-reth/crates/node/tests/it/custom_pool/ordering.rs +++ b/rust/op-reth/crates/node/tests/it/custom_pool/ordering.rs @@ -9,7 +9,7 @@ use reth_node_builder::{EngineNodeLauncher, NodeBuilder, rpc::BasicEngineValidat use reth_optimism_node::{ OpEngineApiBuilder, OpNode, node::{OpEngineValidatorBuilder, OpPoolBuilder}, - utils::optimism_payload_attributes, + utils::{optimism_payload_attributes, unmasked_tree_config}, }; use reth_optimism_txpool::OpPooledTransaction; use reth_provider::providers::BlockchainProvider; @@ -75,7 +75,7 @@ async fn run_ordering_test( ) .launch_with_fn(|builder| { let launcher = - EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), Default::default()); + EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), unmasked_tree_config()); builder.launch_with(launcher) }) .await diff --git a/rust/op-reth/crates/node/tests/it/custom_pool/validator.rs b/rust/op-reth/crates/node/tests/it/custom_pool/validator.rs index 7cfa9ed8323..49bfa5cb0f1 100644 --- a/rust/op-reth/crates/node/tests/it/custom_pool/validator.rs +++ b/rust/op-reth/crates/node/tests/it/custom_pool/validator.rs @@ -21,7 +21,7 @@ use reth_optimism_node::{ OpNodeTypes, OpPayloadBuilder, OpPoolBuilder, OpValidatorWrapper, }, txpool::OpTransactionValidator, - utils::optimism_payload_attributes, + utils::{optimism_payload_attributes, unmasked_tree_config}, }; use reth_optimism_txpool::OpPooledTransaction; use reth_primitives_traits::SealedBlock; @@ -151,7 +151,7 @@ async fn custom_validator_wrapper_rejects_filtered_txs() { ) .launch_with_fn(|builder| { let launcher = - EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), Default::default()); + EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), unmasked_tree_config()); builder.launch_with(launcher) }) .await @@ -216,7 +216,7 @@ async fn default_validator_admits_all_txs() { ) .launch_with_fn(|builder| { let launcher = - EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), Default::default()); + EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), unmasked_tree_config()); builder.launch_with(launcher) }) .await diff --git a/rust/op-reth/crates/node/tests/it/derived_attributes.rs b/rust/op-reth/crates/node/tests/it/derived_attributes.rs index 8903bf546e1..7822a222a0c 100644 --- a/rust/op-reth/crates/node/tests/it/derived_attributes.rs +++ b/rust/op-reth/crates/node/tests/it/derived_attributes.rs @@ -17,11 +17,14 @@ use reth_chainspec::EthChainSpec; use reth_db_common::init::init_genesis; use reth_e2e_test_utils::{Adapter, TmpDB}; use reth_node_api::NodeTypesWithDBAdapter; -use reth_node_builder::{FullNode, FullNodeTypesAdapter, Node, NodeBuilder, NodeConfig}; +use reth_node_builder::{FullNode, FullNodeTypesAdapter, Node, NodeBuilder}; use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder}; use reth_optimism_evm::OpEvmConfig; use reth_optimism_forks::OpHardforks; -use reth_optimism_node::{OpNode, utils::optimism_payload_attributes}; +use reth_optimism_node::{ + OpNode, + utils::{node_config, optimism_payload_attributes}, +}; use reth_optimism_payload_builder::{OpPayloadAttrs, OpPayloadBuilder, OpPayloadBuilderAttributes}; use reth_optimism_primitives::OpTransactionSigned; use reth_optimism_txpool::OpPooledTransaction; @@ -116,7 +119,7 @@ type TestNode = FullNode< >; async fn launch(chain_spec: Arc) -> eyre::Result { - let config = NodeConfig::test().map_chain(chain_spec).with_unused_ports(); + let config = node_config(chain_spec); let op_node = OpNode::default(); Ok(NodeBuilder::new(config) .testing_node(Runtime::test()) diff --git a/rust/op-reth/crates/node/tests/it/estimate_gas_7825.rs b/rust/op-reth/crates/node/tests/it/estimate_gas_7825.rs index ee04eba341e..3a25ae1d2c7 100644 --- a/rust/op-reth/crates/node/tests/it/estimate_gas_7825.rs +++ b/rust/op-reth/crates/node/tests/it/estimate_gas_7825.rs @@ -14,12 +14,9 @@ use alloy_rpc_types_eth::{ }; use op_alloy_consensus::OpTxEnvelope; use reth_node_builder::{NodeBuilder, NodeHandle}; -use reth_node_core::{ - args::{NetworkArgs, RpcServerArgs}, - node_config::NodeConfig, -}; +use reth_node_core::args::{NetworkArgs, RpcServerArgs}; use reth_optimism_chainspec::OpChainSpecBuilder; -use reth_optimism_node::OpNode; +use reth_optimism_node::{OpNode, utils::node_config}; use reth_rpc_api::clients::EthApiClient; use reth_tasks::Runtime; use revm::primitives::eip7825::TX_GAS_LIMIT_CAP; @@ -45,8 +42,7 @@ async fn test_estimate_gas_under_karst() -> eyre::Result<()> { let mut network_args = NetworkArgs::default().with_unused_ports(); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = NodeConfig::test() - .map_chain(OpChainSpecBuilder::optimism_sepolia().karst_activated().build()) + let node_config = node_config(OpChainSpecBuilder::optimism_sepolia().karst_activated().build()) .with_network(network_args) .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()); diff --git a/rust/op-reth/crates/node/tests/it/invalid_post_exec.rs b/rust/op-reth/crates/node/tests/it/invalid_post_exec.rs index 4e2c41149da..8e60279dcc7 100644 --- a/rust/op-reth/crates/node/tests/it/invalid_post_exec.rs +++ b/rust/op-reth/crates/node/tests/it/invalid_post_exec.rs @@ -16,7 +16,7 @@ use reth_e2e_test_utils::{NodeHelperType, setup_engine_with_connection, wallet:: use reth_optimism_node::{ OpBuiltPayload, OpNode, payload::OpExecData, - utils::{advance_chain, setup}, + utils::{advance_chain, setup, unmasked_tree_config}, }; use reth_optimism_primitives::OpTransactionSigned; use reth_rpc_api::clients::DebugApiClient; @@ -29,7 +29,7 @@ async fn setup_lagoon(num_nodes: usize) -> eyre::Result<(Vec OpPayloadAttrs { /// Returns a test node config serving every RPC module over HTTP and `WebSocket` on unused ports. pub(crate) fn rpc_node_config(chain_spec: &Arc) -> NodeConfig { - NodeConfig::test().map_chain(chain_spec.clone()).with_rpc( + node_config(chain_spec.clone()).with_rpc( RpcServerArgs::default() .with_unused_ports() .with_http() diff --git a/rust/op-reth/crates/node/tests/it/main.rs b/rust/op-reth/crates/node/tests/it/main.rs index dfe4ba647ca..b43aeab4c76 100644 --- a/rust/op-reth/crates/node/tests/it/main.rs +++ b/rust/op-reth/crates/node/tests/it/main.rs @@ -28,4 +28,6 @@ mod p2p_version; mod derived_attributes; +mod state_masking; + const fn main() {} diff --git a/rust/op-reth/crates/node/tests/it/pool_limits.rs b/rust/op-reth/crates/node/tests/it/pool_limits.rs index 2be508d6711..91be1b38e1e 100644 --- a/rust/op-reth/crates/node/tests/it/pool_limits.rs +++ b/rust/op-reth/crates/node/tests/it/pool_limits.rs @@ -5,10 +5,13 @@ use alloy_genesis::Genesis; use alloy_network::eip2718::Encodable2718; use alloy_primitives::{Address, Signature, TxKind}; use reth_db::test_utils::create_test_rw_db_with_path; -use reth_node_builder::{EngineNodeLauncher, Node, NodeBuilder, NodeConfig}; +use reth_node_builder::{EngineNodeLauncher, Node, NodeBuilder}; use reth_node_core::args::DatadirArgs; use reth_optimism_chainspec::OpChainSpecBuilder; -use reth_optimism_node::OpNode; +use reth_optimism_node::{ + OpNode, + utils::{node_config, unmasked_tree_config}, +}; use reth_optimism_primitives::OpTransactionSigned; use reth_optimism_txpool::OpPooledTransaction; use reth_provider::providers::BlockchainProvider; @@ -32,11 +35,10 @@ async fn pool_knows_the_node_limits() { let chain_spec = Arc::new( OpChainSpecBuilder::optimism_sepolia().genesis(genesis).ecotone_activated().build(), ); - let mut config = - NodeConfig::new(chain_spec).with_unused_ports().with_datadir_args(DatadirArgs { - datadir: reth_db::test_utils::tempdir_path().into(), - ..Default::default() - }); + let mut config = node_config(chain_spec).with_datadir_args(DatadirArgs { + datadir: reth_db::test_utils::tempdir_path().into(), + ..Default::default() + }); config.network.discovery.discv5_port = Some(0); config.network.discovery.discv5_port_ipv6 = Some(0); let db = create_test_rw_db_with_path( @@ -57,7 +59,7 @@ async fn pool_knows_the_node_limits() { let launcher = EngineNodeLauncher::new( runtime.clone(), builder.config.datadir(), - Default::default(), + unmasked_tree_config(), ); builder.launch_with(launcher) }) diff --git a/rust/op-reth/crates/node/tests/it/priority.rs b/rust/op-reth/crates/node/tests/it/priority.rs index fd3123b1fea..cd4fc65541f 100644 --- a/rust/op-reth/crates/node/tests/it/priority.rs +++ b/rust/op-reth/crates/node/tests/it/priority.rs @@ -11,7 +11,7 @@ use reth_e2e_test_utils::{ }; use reth_node_api::FullNodeTypes; use reth_node_builder::{ - EngineNodeLauncher, Node, NodeBuilder, NodeConfig, + EngineNodeLauncher, Node, NodeBuilder, components::{BasicPayloadServiceBuilder, ComponentsBuilder}, }; use reth_node_core::args::DatadirArgs; @@ -24,7 +24,7 @@ use reth_optimism_node::{ OpNodeTypes, OpPayloadBuilder, OpPoolBuilder, }, txpool::OpPooledTransaction, - utils::optimism_payload_attributes, + utils::{node_config, optimism_payload_attributes, unmasked_tree_config}, }; use reth_optimism_payload_builder::builder::OpPayloadTransactions; use reth_payload_util::{ @@ -121,11 +121,10 @@ async fn test_custom_block_priority_config() { let wallet = Arc::new(Mutex::new(Wallet::default().with_chain_id(chain_spec.chain().into()))); // Configure and launch the node. - let mut config = - NodeConfig::new(chain_spec).with_unused_ports().with_datadir_args(DatadirArgs { - datadir: reth_db::test_utils::tempdir_path().into(), - ..Default::default() - }); + let mut config = node_config(chain_spec).with_datadir_args(DatadirArgs { + datadir: reth_db::test_utils::tempdir_path().into(), + ..Default::default() + }); config.network.discovery.discv5_port = Some(0); config.network.discovery.discv5_port_ipv6 = Some(0); let db = create_test_rw_db_with_path( @@ -145,7 +144,7 @@ async fn test_custom_block_priority_config() { let launcher = EngineNodeLauncher::new( runtime.clone(), builder.config.datadir(), - Default::default(), + unmasked_tree_config(), ); builder.launch_with(launcher) }) diff --git a/rust/op-reth/crates/node/tests/it/rpc.rs b/rust/op-reth/crates/node/tests/it/rpc.rs index 92aec75775e..8d61b69bafb 100644 --- a/rust/op-reth/crates/node/tests/it/rpc.rs +++ b/rust/op-reth/crates/node/tests/it/rpc.rs @@ -16,12 +16,9 @@ use reth_chainspec::{EthChainSpec, ForkCondition}; use reth_e2e_test_utils::wallet::Wallet; use reth_network::types::NatResolver; use reth_node_builder::{NodeBuilder, NodeHandle}; -use reth_node_core::{ - args::{NetworkArgs, RpcServerArgs, RpcStateCacheArgs}, - node_config::NodeConfig, -}; +use reth_node_core::args::{NetworkArgs, RpcServerArgs, RpcStateCacheArgs}; use reth_optimism_chainspec::{OP_SEPOLIA, OpChainSpecBuilder, OpHardfork}; -use reth_optimism_node::{OpNode, args::RollupArgs}; +use reth_optimism_node::{OpNode, args::RollupArgs, utils::node_config}; use reth_rpc_api::{EthConfigApiClient, servers::AdminApiServer}; use reth_rpc_eth_api::helpers::EthTransactions; use reth_rpc_server_types::RpcModuleSelection; @@ -43,8 +40,7 @@ async fn test_admin_external_ip() -> eyre::Result<()> { .with_nat_resolver(NatResolver::ExternalIp(external_ip)); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = NodeConfig::test() - .map_chain(OP_SEPOLIA.clone()) + let node_config = node_config(OP_SEPOLIA.clone()) .with_network(network_args) .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()); @@ -98,8 +94,7 @@ async fn test_retain_forwarded_txs() -> eyre::Result<()> { let mut network_args = NetworkArgs::default().with_unused_ports(); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = NodeConfig::test() - .map_chain(chain_spec.clone()) + let node_config = node_config(chain_spec.clone()) .with_network(network_args) .with_rpc(RpcServerArgs::default().with_unused_ports()); let op_node = OpNode::new(RollupArgs { @@ -133,8 +128,7 @@ async fn test_eth_config_endpoint_exists() -> eyre::Result<()> { let mut network_args = NetworkArgs::default().with_unused_ports(); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = NodeConfig::test() - .map_chain(network.clone()) + let node_config = node_config(network.clone()) .with_network(network_args) .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()); @@ -164,8 +158,7 @@ async fn test_eth_capabilities_starts_at_bedrock() -> eyre::Result<()> { let mut network_args = NetworkArgs::default().with_unused_ports(); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = NodeConfig::test() - .map_chain(chain_spec) + let node_config = node_config(chain_spec) .with_network(network_args) .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()); @@ -212,13 +205,12 @@ async fn test_block_access_list_methods_unserved() -> eyre::Result<()> { let mut network_args = NetworkArgs::default().with_unused_ports(); network_args.discovery.discv5_port = Some(0); network_args.discovery.discv5_port_ipv6 = Some(0); - let node_config = - NodeConfig::test().map_chain(OP_SEPOLIA.clone()).with_network(network_args).with_rpc( - RpcServerArgs::default() - .with_unused_ports() - .with_http() - .with_http_api(RpcModuleSelection::All), - ); + let node_config = node_config(OP_SEPOLIA.clone()).with_network(network_args).with_rpc( + RpcServerArgs::default() + .with_unused_ports() + .with_http() + .with_http_api(RpcModuleSelection::All), + ); let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(node_config).testing_node(exec).node(OpNode::default()).launch().await?; @@ -250,10 +242,7 @@ async fn test_computed_bal_cache_options_rejected() -> eyre::Result<()> { network_args.discovery.discv5_port_ipv6 = Some(0); let mut rpc = RpcServerArgs::default().with_unused_ports().with_http(); rpc.rpc_state_cache = cache; - let node_config = NodeConfig::test() - .map_chain(OP_SEPOLIA.clone()) - .with_network(network_args) - .with_rpc(rpc); + let node_config = node_config(OP_SEPOLIA.clone()).with_network(network_args).with_rpc(rpc); let launched = NodeBuilder::new(node_config) .testing_node(exec.clone()) diff --git a/rust/op-reth/crates/node/tests/it/state_masking.rs b/rust/op-reth/crates/node/tests/it/state_masking.rs new file mode 100644 index 00000000000..78ddf8e9bbe --- /dev/null +++ b/rust/op-reth/crates/node/tests/it/state_masking.rs @@ -0,0 +1,52 @@ +//! The state masking guard on the launch path. + +use reth_db::test_utils::create_test_rw_db_with_path; +use reth_node_api::TreeConfig; +use reth_node_builder::{EngineNodeLauncher, Node, NodeBuilder}; +use reth_node_core::args::DatadirArgs; +use reth_optimism_chainspec::OP_SEPOLIA; +use reth_optimism_node::{OpNode, state_masking::STATE_MASKING_UNSUPPORTED, utils::node_config}; +use reth_provider::providers::BlockchainProvider; +use reth_tasks::Runtime; + +/// A launcher handed a masked tree config never brings the node up, whatever the node config +/// it was built from says. +#[tokio::test] +async fn launch_rejects_masked_tree_config() { + reth_tracing::init_test_tracing(); + + let mut config = node_config(OP_SEPOLIA.clone()).with_datadir_args(DatadirArgs { + datadir: reth_db::test_utils::tempdir_path().into(), + ..Default::default() + }); + config.network.discovery.discv5_port = Some(0); + config.network.discovery.discv5_port_ipv6 = Some(0); + let db = create_test_rw_db_with_path( + config + .datadir + .datadir + .unwrap_or_chain_default(config.chain.chain(), config.datadir.clone()) + .db(), + ); + let op_node = OpNode::default(); + let runtime = Runtime::test(); + let masked = TreeConfig::default().with_num_state_masking_blocks(30); + + let launched = NodeBuilder::new(config) + .with_database(db) + .with_types_and_provider::>() + .with_components(op_node.components()) + .with_add_ons(op_node.add_ons()) + .launch_with_fn(|builder| { + let launcher = + EngineNodeLauncher::new(runtime.clone(), builder.config.datadir(), masked); + builder.launch_with(launcher) + }) + .await; + let Err(err) = launched else { panic!("a masked tree config must not launch") }; + + assert!( + err.chain().any(|cause| cause.to_string().contains(STATE_MASKING_UNSUPPORTED)), + "{err:#}" + ); +} diff --git a/rust/op-reth/crates/payload/src/validator.rs b/rust/op-reth/crates/payload/src/validator.rs index f03fc8abbe7..6210fb24da0 100644 --- a/rust/op-reth/crates/payload/src/validator.rs +++ b/rust/op-reth/crates/payload/src/validator.rs @@ -1,4 +1,4 @@ -//! Validates execution payload wrt Optimism consensus rules +//! Validates execution payload wrt Optimism consensus rules. use alloc::sync::Arc; use alloy_consensus::Block; @@ -97,3 +97,104 @@ where Ok(sealed_block) } + +#[cfg(test)] +mod tests { + use super::*; + use alloy_consensus::{BlockBody, Header, constants::EMPTY_ROOT_HASH}; + use alloy_eips::{eip4895::Withdrawals, eip7685::EMPTY_REQUESTS_HASH}; + use alloy_primitives::B256; + use alloy_rpc_types_engine::PayloadError; + use op_alloy_rpc_types_engine::OpExecutionPayload; + use reth_chainspec::{EthereumHardfork, ForkCondition}; + use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder}; + use reth_optimism_primitives::OpBlock; + use rstest::rstest; + + fn isthmus_chain_spec() -> OpChainSpec { + OpChainSpecBuilder::optimism_mainnet().isthmus_activated().build() + } + + /// Isthmus plus an L1 Amsterdam activation, which no OP fork implies. + fn amsterdam_chain_spec() -> OpChainSpec { + OpChainSpecBuilder::optimism_mainnet() + .isthmus_activated() + .with_fork(EthereumHardfork::Amsterdam, ForkCondition::Timestamp(0)) + .build() + } + + /// The header of an empty post-Isthmus block, with every field an `engine_newPayloadV4` round + /// trip fills in already set so the payload's claimed hash matches the block it decodes to. + fn isthmus_header() -> Header { + Header { + base_fee_per_gas: Some(0), + blob_gas_used: Some(0), + excess_blob_gas: Some(0), + parent_beacon_block_root: Some(B256::ZERO), + requests_hash: Some(EMPTY_REQUESTS_HASH), + withdrawals_root: Some(EMPTY_ROOT_HASH), + ..Default::default() + } + } + + fn isthmus_block(header: Header) -> OpBlock { + let body = BlockBody { withdrawals: Some(Withdrawals::default()), ..Default::default() }; + OpBlock { header, body } + } + + /// The payload `engine_newPayload` would carry for `block`, claiming the block's own hash. + fn payload_for(block: &OpBlock) -> OpExecutionData { + let (payload, sidecar) = OpExecutionPayload::from_block_slow(block); + OpExecutionData::new(payload, sidecar) + } + + fn validate( + chain_spec: OpChainSpec, + block: &OpBlock, + ) -> Result, OpPayloadError> { + ensure_well_formed_payload(chain_spec, payload_for(block)) + } + + #[test] + fn plain_isthmus_payload_is_well_formed() { + let block = isthmus_block(isthmus_header()); + + let sealed_block = validate(isthmus_chain_spec(), &block).unwrap(); + + assert_eq!(sealed_block.hash(), block.header.hash_slow()); + } + + /// No OP payload version carries the Amsterdam header fields, so a block that has one cannot + /// be represented: the payload drops the field, its claimed hash no longer matches the block + /// it decodes to, and the hash check rejects it before the Amsterdam check runs. + #[rstest] + #[case::block_access_list_hash(Some(B256::ZERO), None)] + #[case::slot_number(None, Some(1))] + fn amsterdam_header_fields_fail_the_block_hash_check( + #[case] block_access_list_hash: Option, + #[case] slot_number: Option, + ) { + let header = Header { block_access_list_hash, slot_number, ..isthmus_header() }; + + let err = validate(isthmus_chain_spec(), &isthmus_block(header)).unwrap_err(); + + assert!(matches!(err, OpPayloadError::Eth(PayloadError::BlockHash { .. })), "{err}"); + } + + /// An OP payload can never satisfy the post-Amsterdam check, so scheduling Amsterdam on an OP + /// chain spec rejects every payload from the activation timestamp on. + #[test] + fn amsterdam_activation_rejects_every_op_payload() { + let block = isthmus_block(isthmus_header()); + + let err = validate(amsterdam_chain_spec(), &block).unwrap_err(); + + assert!( + matches!( + err, + OpPayloadError::Eth(PayloadError::PostAmsterdamBlockWithoutBlockAccessList) + ), + "{err}" + ); + } +}