diff --git a/rust/justfile b/rust/justfile index a33ec90bc17..6bb3a467770 100644 --- a/rust/justfile +++ b/rust/justfile @@ -409,7 +409,8 @@ rust-review base='': ############################ no_std ################################# -# Check no_std compatibility for proof, protocol, alloy, and op-alloy crates +# Check no_std compatibility for proof, protocol, alloy, and op-alloy crates, plus the host-target +# no_std build of op-reth's evm crate, whose reth dependencies do not build for riscv. check-no-std: #!/usr/bin/env bash set -euo pipefail @@ -454,6 +455,16 @@ check-no-std: echo "Successfully checked no_std build for: $package" done + host_no_std_packages=( + reth-optimism-evm + ) + + for package in "${host_no_std_packages[@]}"; do + echo "Checking host-target no_std build for: $package" + cargo check -p "$package" --no-default-features + echo "Successfully checked host-target no_std build for: $package" + done + ########################### Benchmarks ############################## # Run benchmarks (compile only) diff --git a/rust/op-reth/crates/evm/src/lib.rs b/rust/op-reth/crates/evm/src/lib.rs index 1869e753da6..203d689b97c 100644 --- a/rust/op-reth/crates/evm/src/lib.rs +++ b/rust/op-reth/crates/evm/src/lib.rs @@ -18,10 +18,11 @@ use alloy_op_evm::{ block::{OpTxEnv, receipt_builder::OpReceiptBuilder}, evm_env_for_op_block, evm_env_for_op_next_block, }; +use alloy_primitives::B256; use core::fmt::Debug; use op_alloy_consensus::{ - EIP1559ParamError, OpTransaction as OpConsensusTransaction, - parse_post_exec_payload_from_transactions, + EIP1559ParamError, OpTransaction as OpConsensusTransaction, PostExecPayloadValidationError, + parse_post_exec_payload_from_transactions, validate_post_exec_entry_count, }; use op_revm::OpSpecId; use reth_chainspec::EthChainSpec; @@ -45,7 +46,6 @@ use { #[cfg(feature = "std")] use { alloy_op_evm::evm_env_for_op_payload, - op_alloy_consensus::{PostExecPayloadValidationError, validate_post_exec_entry_count}, reth_evm::{ConfigureEngineEvm, ExecutableTxIterator}, }; @@ -80,6 +80,7 @@ mod post_exec_ext; pub use post_exec_ext::*; mod sdm_metrics; +pub use sdm_metrics::report_post_exec_validation_failure; /// Optimism-related EVM configuration. #[derive(Debug)] @@ -189,6 +190,18 @@ pub(crate) fn preflight_post_exec_payload( }) } +/// Runs the encoded-transaction preflight for payload-builder attributes +/// (`forkchoiceUpdated` with transactions); a rejection is logged against the parent block hash +/// and counted under the failed rule and as a failed validation. +pub fn validate_post_exec_attributes( + transactions: &[Bytes], + parent_hash: B256, +) -> Result<(), PostExecPayloadValidationError> { + validate_post_exec_entry_count(transactions).inspect_err(|error| { + sdm_metrics::report_post_exec_attributes_rejection(parent_hash, *error); + }) +} + impl OpEvmConfig where ChainSpec: OpHardforks, diff --git a/rust/op-reth/crates/evm/src/sdm_metrics.rs b/rust/op-reth/crates/evm/src/sdm_metrics.rs index cf28491b4c5..c2bf3a867e8 100644 --- a/rust/op-reth/crates/evm/src/sdm_metrics.rs +++ b/rust/op-reth/crates/evm/src/sdm_metrics.rs @@ -4,7 +4,10 @@ //! dashboard can show SDM validation activity rather than inferring it from generic block //! validation. //! -//! Recorded from the execution-context builders and the Engine API payload preflight. Beyond +//! Recorded from the execution-context builders, the Engine API payload preflight, and the +//! payload-builder attributes preflight. Attributes count once per conversion into builder +//! attributes, so a rejection repeats only when a builder reconverts the same attributes, and a +//! replay of attributes through the debug RPC namespace counts like an engine delivery. Beyond //! live import, several paths re-execute stored canonical blocks that already passed this check: //! RPC replays, the proofs ExEx's periodic re-execution, and pipeline or backfill sync. //! Re-execution cannot inflate the failure counters, but every pass recounts `ok` — on a @@ -88,9 +91,9 @@ pub(crate) fn report_post_exec_validation_ok() { result_counter(RESULT_OK).increment(1); } -/// Logs a block rejected for its post-exec transaction and, with `std`, counts it under the -/// failed rule and as a failed validation. -pub(crate) fn report_post_exec_validation_failure( +/// Logs a block rejected for its post-exec transaction, whether imported or being built, and +/// with `std` counts it under the failed rule and as a failed validation. +pub fn report_post_exec_validation_failure( block_number: u64, error: PostExecPayloadValidationError, ) { @@ -101,11 +104,34 @@ pub(crate) fn report_post_exec_validation_failure( %error, "block rejected: post-exec transaction failed structural validation" ); + count_post_exec_failure(reason); +} + +/// Counts one failed validation under its rule; a no-op without `std`. +fn count_post_exec_failure(reason: &'static str) { #[cfg(feature = "std")] { failure_counter(reason).increment(1); result_counter(RESULT_FAIL).increment(1); } + #[cfg(not(feature = "std"))] + let _ = reason; +} + +/// Logs payload-builder attributes rejected for their post-exec transaction and counts the +/// rejection like [`report_post_exec_validation_failure`], attributed to the parent block hash. +pub(crate) fn report_post_exec_attributes_rejection( + parent_hash: alloy_primitives::B256, + error: PostExecPayloadValidationError, +) { + let reason = error.as_reason(); + tracing::warn!( + %parent_hash, + reason, + %error, + "payload attributes rejected: post-exec transaction failed structural validation" + ); + count_post_exec_failure(reason); } #[cfg(all(test, feature = "std"))] @@ -244,13 +270,29 @@ mod tests { assert_results(&exposition, /* ok */ 0.0, /* fail */ 1.0); } - /// Drives the Engine API preflight helper with raw encoded transactions against a recorder - /// private to the test, returning the result alongside the rendered exposition. - fn preflight(transactions: Vec) -> (Result<(), ()>, String) { + /// The two encoded-transaction preflights, which share one set of counters. + #[derive(Clone, Copy, Debug)] + enum Preflight { + EngineApi, + Attributes, + } + + /// Drives one preflight with raw encoded transactions against a recorder private to the + /// test, returning the result alongside the rendered exposition. + fn preflight( + entry: Preflight, + transactions: Vec, + ) -> (Result<(), ()>, String) { let (recorder, handle) = recorder(); let result = with_local_recorder(&recorder, || { super::register_sdm_metrics_at_zero(); - crate::preflight_post_exec_payload(&transactions, BLOCK) + match entry { + Preflight::EngineApi => crate::preflight_post_exec_payload(&transactions, BLOCK), + Preflight::Attributes => crate::validate_post_exec_attributes( + &transactions, + alloy_primitives::B256::ZERO, + ), + } }); (result.map_err(drop), handle.render()) } @@ -287,13 +329,14 @@ mod tests { #[case::too_many_entries(vec![filler(), encoded_post_exec(2)], "too_many_gas_refund_entries")] #[case::malformed(vec![bare_post_exec()], "malformed_post_exec_payload")] #[case::unsupported_version(vec![filler(), versioned_post_exec(2)], "unsupported_post_exec_payload_version")] - fn rejected_payload_counts_on_the_engine_api_path( + fn rejected_transactions_count_at_every_preflight( + #[values(Preflight::EngineApi, Preflight::Attributes)] entry: Preflight, #[case] transactions: Vec, #[case] reason: &str, ) { - let (result, exposition) = preflight(transactions); + let (result, exposition) = preflight(entry, transactions); - assert!(result.is_err(), "preflight rejects"); + assert!(result.is_err(), "{entry:?} preflight rejects"); assert_failures(&exposition, Some(reason)); assert_results(&exposition, /* ok */ 0.0, /* fail */ 1.0); } @@ -323,17 +366,18 @@ mod tests { assert_results(&exposition, /* ok */ 0.0, /* fail */ 1.0); } - /// The preflight does not count `ok`: a pass is not yet a validated post-exec transaction — + /// A preflight does not count `ok`: a pass is not yet a validated post-exec transaction — /// the parse on the decoded transactions counts that. #[rstest] #[case::with_post_exec(vec![filler(), encoded_post_exec(1)])] #[case::without_post_exec(vec![filler()])] - fn accepted_payload_counts_nothing_at_preflight( + fn accepted_transactions_count_nothing_at_any_preflight( + #[values(Preflight::EngineApi, Preflight::Attributes)] entry: Preflight, #[case] transactions: Vec, ) { - let (result, exposition) = preflight(transactions); + let (result, exposition) = preflight(entry, transactions); - assert!(result.is_ok(), "preflight passes"); + assert!(result.is_ok(), "{entry:?} preflight passes"); assert_failures(&exposition, None); assert_results(&exposition, /* ok */ 0.0, /* fail */ 0.0); } diff --git a/rust/op-reth/crates/flashblocks/Cargo.toml b/rust/op-reth/crates/flashblocks/Cargo.toml index 85a0f2128c5..4b711406aed 100644 --- a/rust/op-reth/crates/flashblocks/Cargo.toml +++ b/rust/op-reth/crates/flashblocks/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth reth-optimism-primitives = { workspace = true, features = ["serde"] } -reth-optimism-evm.workspace = true +reth-optimism-evm = { workspace = true, features = ["std"] } reth-chain-state = { workspace = true, features = ["serde"] } reth-primitives-traits = { workspace = true, features = ["serde"] } reth-engine-primitives = { workspace = true, features = ["std"] } diff --git a/rust/op-reth/crates/payload/Cargo.toml b/rust/op-reth/crates/payload/Cargo.toml index a7ebfc9b484..1223dd3ccdf 100644 --- a/rust/op-reth/crates/payload/Cargo.toml +++ b/rust/op-reth/crates/payload/Cargo.toml @@ -28,7 +28,7 @@ reth-payload-validator.workspace = true reth-metrics.workspace = true # op-reth -reth-optimism-evm.workspace = true +reth-optimism-evm = { workspace = true, features = ["std"] } reth-optimism-forks.workspace = true reth-optimism-primitives = { workspace = true, features = ["reth-codec"] } reth-optimism-txpool.workspace = true diff --git a/rust/op-reth/crates/payload/src/builder.rs b/rust/op-reth/crates/payload/src/builder.rs index 07065f684fc..be9f2b5e0cf 100644 --- a/rust/op-reth/crates/payload/src/builder.rs +++ b/rust/op-reth/crates/payload/src/builder.rs @@ -381,6 +381,9 @@ where /// Converts `BuildArguments` from [`OpPayloadAttrs`](crate::payload::OpPayloadAttrs) to /// [`OpPayloadBuilderAttributes`], decoding the RPC transaction bytes into typed transactions. +/// +/// Counts a rejected post-exec transaction once per call, so a job that converts the same +/// attributes more than once counts each conversion. fn convert_build_args( args: BuildArguments>, ) -> Result< @@ -908,7 +911,7 @@ where /// /// Returns `Ok(None)` when no such tx is present; errors if the embedded payload is /// structurally invalid (present before SDM activation, duplicated, not last, or anchored - /// to the wrong block number). + /// to the wrong block number), logging and counting the rejection under its failed rule. fn parse_embedded_post_exec( &self, ) -> Result, PayloadBuilderError> { @@ -922,6 +925,9 @@ where next_block_number, sdm_active, ) + .inspect_err(|error| { + reth_optimism_evm::report_post_exec_validation_failure(next_block_number, *error); + }) .map_err(PayloadBuilderError::other) } diff --git a/rust/op-reth/crates/payload/src/builder/tests.rs b/rust/op-reth/crates/payload/src/builder/tests.rs index fa3457d925b..362a5d51317 100644 --- a/rust/op-reth/crates/payload/src/builder/tests.rs +++ b/rust/op-reth/crates/payload/src/builder/tests.rs @@ -993,6 +993,70 @@ fn execute_best_transactions_excludes_interop_txs_when_failsafe_active() { assert_eq!(build(&failsafe), vec![normal_hash, interop_hash]); } +/// Builds a ctx rebuilding a derived block whose embedded `0x7D` tx anchors to +/// `post_exec_anchor`. The parent is block 0, so block 1 is the only valid anchor, and Lagoon is +/// either active from genesis or not at all. +fn derived_ctx( + lagoon_active: bool, + post_exec_anchor: u64, +) -> OpPayloadBuilderCtx< + OpEvmConfig, + OpChainSpec, + OpPayloadBuilderAttributes, +> { + let spec = OpChainSpecBuilder::optimism_mainnet(); + let spec = if lagoon_active { spec.lagoon_activated() } else { spec }; + let chain_spec = Arc::new(spec.build()); + let parent = SealedHeader::seal_slow(Header { number: 0, timestamp: 0, ..Default::default() }); + let attributes = OpPayloadBuilderAttributes { + no_tx_pool: true, + timestamp: 1, + transactions: vec![ + deposit_with_encoded(), + post_exec_with_encoded(post_exec_anchor, entries(&[(0, 1)])), + ], + ..Default::default() + }; + + OpPayloadBuilderCtx { + evm_config: OpEvmConfig::optimism(chain_spec.clone()), + builder_config: OpBuilderConfig::default(), + chain_spec, + config: PayloadConfig { + parent_header: Arc::new(parent), + parent_block_info: None, + payload_id: attributes.id, + attributes, + }, + cancel: Default::default(), + best_payload: None, + } +} + +/// The rules the encoded-transaction preflight cannot check — activation and the anchor — are +/// applied when the builder parses the decoded attributes, and a rejection there reaches the +/// same counters. +#[rstest] +#[case::wrong_anchor(/* lagoon_active */ true, /* post_exec_anchor */ 2, "block_number_mismatch")] +#[case::before_activation(/* lagoon_active */ false, /* post_exec_anchor */ 1, "unexpected_post_exec_tx")] +fn rejected_embedded_post_exec_counts_under_the_failed_rule( + #[case] lagoon_active: bool, + #[case] post_exec_anchor: u64, + #[case] reason: &str, +) { + use crate::sdm_metrics::test_support::{FAILURES, RESULTS, recorder, series}; + + let (recorder, handle) = recorder(); + let result = with_local_recorder(&recorder, || { + derived_ctx(lagoon_active, post_exec_anchor).post_exec_mode() + }); + let exposition = handle.render(); + + assert!(result.is_err(), "the embedded post-exec tx is rejected"); + assert_eq!(series(&exposition, FAILURES, "reason", reason), Some(1.0), "{exposition}"); + assert_eq!(series(&exposition, RESULTS, "result", "fail"), Some(1.0), "{exposition}"); +} + #[derive(Debug, thiserror::Error)] #[error("unrelated executor failure")] struct UnrelatedExecutionError; diff --git a/rust/op-reth/crates/payload/src/payload.rs b/rust/op-reth/crates/payload/src/payload.rs index 5eefa020e5b..2c57ad964ec 100644 --- a/rust/op-reth/crates/payload/src/payload.rs +++ b/rust/op-reth/crates/payload/src/payload.rs @@ -17,7 +17,6 @@ use alloy_rpc_types_engine::{ }; use op_alloy_consensus::{ EIP1559ParamError, decode_2718_canonical, encode_holocene_extra_data, encode_jovian_extra_data, - validate_post_exec_entry_count, }; use op_alloy_rpc_types_engine::{ OpExecutionPayloadEnvelope, OpExecutionPayloadEnvelopeV3, OpExecutionPayloadEnvelopeV4, @@ -316,14 +315,15 @@ impl /// Creates a new payload builder from RPC attributes with a pre-computed payload ID. /// /// This is used when the payload ID has already been computed (e.g., by the payload service) - /// and we just need to decode the transaction bytes. + /// and we just need to decode the transaction bytes. A post-exec transaction that fails + /// structural validation is logged and counted before the decode. pub fn from_rpc_attrs( parent: B256, id: PayloadId, attributes: OpPayloadAttributes, ) -> Result { let encoded_transactions = attributes.transactions.unwrap_or_default(); - validate_post_exec_entry_count(&encoded_transactions) + reth_optimism_evm::validate_post_exec_attributes(&encoded_transactions, parent) .map_err(|error| alloy_rlp::Error::Custom(error.as_reason()))?; let transactions = encoded_transactions .into_iter() @@ -682,9 +682,13 @@ where #[cfg(test)] mod tests { use super::*; - use crate::OpPayloadAttributes; + use crate::{ + OpPayloadAttributes, + sdm_metrics::test_support::{FAILURES, RESULTS, recorder, series}, + }; use alloy_primitives::{FixedBytes, address, b256, bytes}; use alloy_rpc_types_engine::PayloadAttributes; + use op_alloy_consensus::PostExecPayloadValidationError; use reth_optimism_primitives::OpTransactionSigned; use reth_payload_primitives::EngineApiMessageVersion; use std::str::FromStr; @@ -841,22 +845,7 @@ mod tests { let mut tagged = vec![0x00]; tagged.extend_from_slice(&legacy); - let attrs = OpPayloadAttributes { - payload_attributes: PayloadAttributes { - timestamp: 1, - prev_randao: B256::ZERO, - suggested_fee_recipient: Address::ZERO, - withdrawals: Some(vec![]), - parent_beacon_block_root: Some(B256::ZERO), - slot_number: None, - target_gas_limit: None, - }, - transactions: Some(vec![tagged.into()]), - no_tx_pool: Some(true), - gas_limit: Some(30_000_000), - eip_1559_params: None, - min_base_fee: None, - }; + let attrs = attrs_with_transactions(vec![tagged.into()]); OpPayloadBuilderAttributes::::try_new(B256::ZERO, attrs.clone(), 3) .unwrap_err(); OpPayloadBuilderAttributes::::from_rpc_attrs( @@ -866,4 +855,79 @@ mod tests { ) .unwrap_err(); } + + /// Derived-block attributes (`no_tx_pool`) carrying exactly `transactions`. + fn attrs_with_transactions(transactions: Vec) -> OpPayloadAttributes { + OpPayloadAttributes { + payload_attributes: PayloadAttributes { + parent_beacon_block_root: Some(B256::ZERO), + prev_randao: B256::ZERO, + slot_number: None, + suggested_fee_recipient: Address::ZERO, + target_gas_limit: None, + timestamp: 1, + withdrawals: Some(vec![]), + }, + eip_1559_params: None, + gas_limit: Some(30_000_000), + min_base_fee: None, + no_tx_pool: Some(true), + transactions: Some(transactions), + } + } + + /// Attributes whose only transaction is a post-exec tx carrying `entries` refund entries. + fn attrs_with_post_exec(entries: usize) -> OpPayloadAttributes { + use alloy_eips::eip2718::Encodable2718; + use op_alloy_consensus::{SDMGasEntry, build_post_exec_tx}; + let tx = build_post_exec_tx( + 1, + (0..entries).map(|index| SDMGasEntry { index: index as u64, gas_refund: 1 }).collect(), + ) + .encoded_2718(); + attrs_with_transactions(vec![tx.into()]) + } + + /// Converts `attrs` against a recorder private to the test, returning the result alongside + /// the rendered exposition. + fn convert( + attrs: OpPayloadAttributes, + ) -> (Result, alloy_rlp::Error>, String) { + use reth_metrics::metrics::with_local_recorder; + let (recorder, handle) = recorder(); + let result = with_local_recorder(&recorder, || { + OpPayloadBuilderAttributes::from_rpc_attrs(B256::ZERO, PayloadId::new([0; 8]), attrs) + }); + (result, handle.render()) + } + + /// A post-exec rejection on the attributes path must reach the same counters as the Engine + /// API preflight: the failed rule and a failed validation. + #[test] + fn rejected_attributes_count_the_failure() { + let (result, exposition) = convert(attrs_with_post_exec(2)); + + assert!(result.is_err(), "two refund entries with no preceding transactions must reject"); + assert_eq!( + series(&exposition, FAILURES, "reason", "too_many_gas_refund_entries"), + Some(1.0), + "{exposition}" + ); + assert_eq!(series(&exposition, RESULTS, "result", "fail"), Some(1.0), "{exposition}"); + } + + /// Accepted attributes count nothing at this stage: like the Engine API preflight, a pass is + /// not yet a validated post-exec transaction — the import path counts that. + #[test] + fn accepted_attributes_count_nothing() { + let (result, exposition) = convert(attrs_with_post_exec(0)); + + assert!(result.is_ok(), "a post-exec transaction with no entries is structurally valid"); + for reason in PostExecPayloadValidationError::ALL_REASONS { + let failures = series(&exposition, FAILURES, "reason", reason).unwrap_or(0.0); + assert_eq!(failures, 0.0, "{reason}: {exposition}"); + } + let fail = series(&exposition, RESULTS, "result", "fail").unwrap_or(0.0); + assert_eq!(fail, 0.0, "the attributes path must not count a pass: {exposition}"); + } } diff --git a/rust/op-reth/crates/payload/src/sdm_metrics.rs b/rust/op-reth/crates/payload/src/sdm_metrics.rs index 3a38ae43a2e..c5cb9a2b29b 100644 --- a/rust/op-reth/crates/payload/src/sdm_metrics.rs +++ b/rust/op-reth/crates/payload/src/sdm_metrics.rs @@ -81,3 +81,27 @@ pub(crate) fn record_operator_opt_in(enabled: bool) { const fn gauge_value(flag: bool) -> f64 { if flag { 1.0 } else { 0.0 } } + +#[cfg(test)] +pub(crate) mod test_support { + use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle, PrometheusRecorder}; + use metrics_util::layers::{Layer, Prefix, PrefixLayer}; + + pub(crate) const FAILURES: &str = "reth_op_sdm_post_exec_validation_failure_total"; + pub(crate) const RESULTS: &str = "reth_op_sdm_post_exec_validation_total"; + + /// A recorder private to one test, prefixed like the node's recorder stack so the rendered + /// series names are the ones operators query. + pub(crate) fn recorder() -> (Prefix, PrometheusHandle) { + let recorder = PrometheusBuilder::new().build_recorder(); + let handle = recorder.handle(); + (PrefixLayer::new("reth").layer(recorder), handle) + } + + /// Reads one exactly-labeled series out of the exposition. `None` distinguishes a series + /// that was never registered from one reporting 0. + pub(crate) fn series(exposition: &str, metric: &str, label: &str, value: &str) -> Option { + let series = format!("{metric}{{{label}=\"{value}\"}} "); + exposition.lines().find_map(|line| line.strip_prefix(series.as_str())?.trim().parse().ok()) + } +} diff --git a/rust/op-reth/crates/txpool/Cargo.toml b/rust/op-reth/crates/txpool/Cargo.toml index 9b87f069af2..9f03c0f3a65 100644 --- a/rust/op-reth/crates/txpool/Cargo.toml +++ b/rust/op-reth/crates/txpool/Cargo.toml @@ -38,7 +38,7 @@ op-revm.workspace = true op-alloy-consensus.workspace = true op-alloy-flz.workspace = true op-alloy-rpc-types.workspace = true -reth-optimism-evm.workspace = true +reth-optimism-evm = { workspace = true, features = ["std"] } reth-optimism-forks.workspace = true reth-optimism-primitives.workspace = true