Skip to content
Closed
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
2 changes: 2 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions rust/op-reth/crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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}");
}

<Self as clap::CommandFactory>::command().mut_subcommands(hide_denied_args)
}
Expand Down Expand Up @@ -219,9 +230,8 @@ where
if let Some(node) = matches.subcommand_matches("node") &&
node.get_one::<u64>(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()));
}
<Self as clap::FromArgMatches>::from_arg_matches_mut(&mut matches)
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -413,6 +423,7 @@ mod test {

#[test]
fn parse_dev() {
init_op_engine_defaults().unwrap();
let cmd = NodeCommand::<OpChainSpecParser, NoArgs>::parse_from(["op-reth", "--dev"]);
let chain = OP_DEV.clone();
assert_eq!(cmd.chain.chain, chain.chain);
Expand All @@ -431,6 +442,7 @@ mod test {

#[test]
fn parse_node() {
init_op_engine_defaults().unwrap();
let cmd = Cli::<OpChainSpecParser, RollupArgs>::parse_from([
"op-reth",
"node",
Expand Down
4 changes: 3 additions & 1 deletion rust/op-reth/crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions rust/op-reth/crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions rust/op-reth/crates/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<NodeBuilder<DatabaseEnv, OpChainSpec>>,
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 `<reth-data-dir>/historical-proofs` when not supplied — see
Expand Down Expand Up @@ -1082,10 +1078,12 @@ where
EVB: EngineValidatorBuilder<N>,
RpcMiddleware: Send,
{
type ValidatorBuilder = EVB;
type ValidatorBuilder = NoStateMaskingValidatorBuilder<EVB>;

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,
))
}
}

Expand Down
97 changes: 97 additions & 0 deletions rust/op-reth/crates/node/src/state_masking.rs
Original file line number Diff line number Diff line change
@@ -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<EVB> {
inner: EVB,
}

impl<EVB> NoStateMaskingValidatorBuilder<EVB> {
/// Wraps `inner`.
pub const fn new(inner: EVB) -> Self {
Self { inner }
}
}

impl<N, EVB> EngineValidatorBuilder<N> for NoStateMaskingValidatorBuilder<EVB>
where
N: FullNodeComponents,
EVB: EngineValidatorBuilder<N>,
{
type EngineValidator = EVB::EngineValidator;

async fn build_tree_validator(
self,
ctx: &AddOnsContext<'_, N>,
tree_config: TreeConfig,
overlay_manager: OverlayManager<PrimitivesTy<N::Types>>,
) -> eyre::Result<Self::EngineValidator> {
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);
}
}
34 changes: 30 additions & 4 deletions rust/op-reth/crates/node/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
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;

/// Optimism Node Helper type
pub(crate) type OpNode =
NodeHelperType<OtherOpNode, BlockchainProvider<NodeTypesWithDBAdapter<OtherOpNode, TmpDB>>>;

/// 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<Arc<OpChainSpec>>) -> NodeConfig<OpChainSpec> {
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<OpNode>, Wallet)> {
let genesis: Genesis =
Expand All @@ -26,7 +52,7 @@ pub async fn setup(num_nodes: usize) -> eyre::Result<(Vec<OpNode>, Wallet)> {
OpChainSpecBuilder::optimism_sepolia().genesis(genesis).ecotone_activated().build(),
),
false,
Default::default(),
unmasked_tree_config(),
optimism_payload_attributes,
)
.await
Expand Down
10 changes: 7 additions & 3 deletions rust/op-reth/crates/node/tests/e2e-testsuite/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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::<OpEngineTypes>::new(
Expand Down Expand Up @@ -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::<OpEngineTypes>::new(
Expand Down
Loading
Loading