From c7c31d67a5ffc2244cf20c475ea7e478ce464fe1 Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Thu, 12 Nov 2026 18:24:23 +0100 Subject: [PATCH] op-reth: test the eth_getLogs errors for a range past the head or over pruned receipts reth 2.6 answers -32602 for a range reaching past the head (reth#26891) and 4444 for a range or block whose receipts are pruned (reth#27002). One node-integration test pins both codes with their exact messages for a fromBlock past the head, a toBlock past the head, a pruned range and a pruned block hash, and a served range, over http and ws. The pruned fixture drives reth's own receipts pruner through PruningArgs, with v1 storage so receipts are trimmed block by block (the v2 static-file path is not covered) and a zero-length RPC cache so the cache does not mask the pruning. The engine driver of the persistence test moves into a shared module that can put extra sequencer transactions into a block. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- .../crates/node/tests/it/engine_driver.rs | 151 ++++++++++++ .../crates/node/tests/it/get_logs_errors.rs | 228 ++++++++++++++++++ rust/op-reth/crates/node/tests/it/main.rs | 4 + .../tests/it/payload_during_persistence.rs | 116 +-------- 4 files changed, 389 insertions(+), 110 deletions(-) create mode 100644 rust/op-reth/crates/node/tests/it/engine_driver.rs create mode 100644 rust/op-reth/crates/node/tests/it/get_logs_errors.rs diff --git a/rust/op-reth/crates/node/tests/it/engine_driver.rs b/rust/op-reth/crates/node/tests/it/engine_driver.rs new file mode 100644 index 00000000000..886edeb1f15 --- /dev/null +++ b/rust/op-reth/crates/node/tests/it/engine_driver.rs @@ -0,0 +1,151 @@ +//! An engine API block builder for a launched node, and the timeouts that bound waits on it. + +use alloy_primitives::{B256, Bytes}; +use alloy_rpc_types_engine::{ForkchoiceState, PayloadId, PayloadStatus}; +use alloy_signer_local::PrivateKeySigner; +use jsonrpsee::{core::client::ClientT, rpc_params}; +use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelopeV3; +use reth_e2e_test_utils::{transaction::TransactionTestContext, wallet::Wallet}; +use reth_node_api::ConsensusEngineHandle; +use reth_optimism_node::{OpEngineTypes, utils::optimism_payload_attributes}; +use std::time::{Duration, Instant}; + +/// Bounds every engine call and every wait on the node, so a stall fails the test instead of +/// hanging it. +const WAIT_TIMEOUT: Duration = Duration::from_secs(20); + +/// A payload job the engine has started but not resolved. +pub(crate) struct StartedPayloadJob { + number: u64, + pub(crate) payload_id: PayloadId, + start_duration: Duration, +} + +/// Builds blocks the way a consensus client does, through the engine API. +pub(crate) struct EngineDriver { + pub(crate) client: C, + pub(crate) engine: ConsensusEngineHandle, + pub(crate) head: B256, + pub(crate) wallet: Wallet, +} + +impl EngineDriver { + /// A funded account the driver never signs with: the L1 info transactions it signs use up + /// the wallet's nonces. + pub(crate) fn spare_signer(&self) -> PrivateKeySigner { + Wallet::new(2).with_chain_id(self.wallet.chain_id).wallet_gen().remove(1) + } + + pub(crate) async fn build_block(&mut self, number: u64) -> eyre::Result { + self.build_block_with(number, Vec::new()).await + } + + /// Builds a block that carries the given sequencer transactions after the block's L1 info + /// transaction. + pub(crate) async fn build_block_with( + &mut self, + number: u64, + sequencer_txs: Vec, + ) -> eyre::Result { + let job = self.start_payload_job_with(number, sequencer_txs).await?; + self.finish_payload_job(job).await + } + + /// Starts a payload job on the head, with the block's L1 info transaction as its only + /// sequencer transaction. + pub(crate) async fn start_payload_job(&self, number: u64) -> eyre::Result { + self.start_payload_job_with(number, Vec::new()).await + } + + /// Starts a payload job on the head whose sequencer transactions are the block's L1 info + /// transaction followed by the given ones. + async fn start_payload_job_with( + &self, + number: u64, + sequencer_txs: Vec, + ) -> eyre::Result { + let mut attrs = optimism_payload_attributes(number); + let mut transactions = vec![ + TransactionTestContext::optimism_l1_block_info_tx( + self.wallet.chain_id, + self.wallet.inner.clone(), + number - 1, + ) + .await, + ]; + transactions.extend(sequencer_txs); + attrs.0.transactions = Some(transactions); + let started_at = Instant::now(); + let fcu = within_timeout( + "forkchoice update with attributes stalled", + self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), Some(attrs)), + ) + .await?; + let start_duration = started_at.elapsed(); + assert!(fcu.payload_status.is_valid(), "forkchoice update for block {number}: {fcu:?}"); + let payload_id = fcu.payload_id.expect("a payload job must start"); + Ok(StartedPayloadJob { number, payload_id, start_duration }) + } + + /// Fetches the job's payload and makes it canonical. Returns how long the engine took to + /// answer the forkchoice update that started the job and the getPayload that resolved it, + /// leaving out whatever the caller waited on in between. + pub(crate) async fn finish_payload_job( + &mut self, + job: StartedPayloadJob, + ) -> eyre::Result { + let resolve_started_at = Instant::now(); + let payload: OpExecutionPayloadEnvelopeV3 = within_timeout( + "getPayload stalled", + self.client.request("engine_getPayloadV3", rpc_params![job.payload_id]), + ) + .await?; + let job_duration = job.start_duration + resolve_started_at.elapsed(); + + let block = &payload.execution_payload.payload_inner.payload_inner; + assert_eq!(block.block_number, job.number); + assert!( + !block.transactions.is_empty(), + "block {} lacks its L1 info transaction", + job.number + ); + self.head = block.block_hash; + let status: PayloadStatus = within_timeout( + "newPayload stalled", + self.client.request( + "engine_newPayloadV3", + rpc_params![ + payload.execution_payload, + Vec::::new(), + payload.parent_beacon_block_root + ], + ), + ) + .await?; + assert!(status.is_valid(), "newPayload for block {}: {status:?}", job.number); + let fcu = within_timeout( + "canonical forkchoice update stalled", + self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), None), + ) + .await?; + assert!( + fcu.payload_status.is_valid(), + "forkchoice update to block {}: {fcu:?}", + job.number + ); + Ok(job_duration) + } +} + +pub(crate) async fn within_timeout(message: &str, future: impl Future) -> T { + tokio::time::timeout(WAIT_TIMEOUT, future).await.unwrap_or_else(|_| panic!("{message}")) +} + +pub(crate) async fn wait_until(message: &str, mut ready: impl FnMut() -> bool) { + within_timeout(message, async { + while !ready() { + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await; +} diff --git a/rust/op-reth/crates/node/tests/it/get_logs_errors.rs b/rust/op-reth/crates/node/tests/it/get_logs_errors.rs new file mode 100644 index 00000000000..98bfe3043a0 --- /dev/null +++ b/rust/op-reth/crates/node/tests/it/get_logs_errors.rs @@ -0,0 +1,228 @@ +//! `eth_getLogs` must answer an error, not a silently empty or partial array, for a range the +//! node cannot serve in full: a range reaching past the head and a range or block whose receipts +//! the node pruned. An indexer that polls `lastSeen + 1`, or reads a range whose receipts are gone, +//! sees the error instead of mistaking the missing logs for an empty range. + +use crate::engine_driver::{EngineDriver, wait_until}; +use alloy_consensus::{SignableTransaction, TxEip1559}; +use alloy_genesis::Genesis; +use alloy_network::{TxSignerSync, eip2718::Encodable2718}; +use alloy_primitives::{Address, B256, Bytes, TxKind}; +use alloy_rpc_types_eth::{Filter, Log}; +use alloy_signer_local::PrivateKeySigner; +use jsonrpsee::{ + core::client::{ClientT, Error as ClientError}, + rpc_params, + types::error::INVALID_PARAMS_CODE, +}; +use reth_chainspec::EthChainSpec; +use reth_e2e_test_utils::wallet::Wallet; +use reth_node_builder::{NodeBuilder, NodeConfig, NodeHandle, rpc::RethRpcServerHandles}; +use reth_node_core::args::{PruningArgs, RpcServerArgs}; +use reth_optimism_chainspec::OpChainSpecBuilder; +use reth_optimism_node::OpNode; +use reth_provider::{BlockHashReader, ChainSpecProvider, ReceiptProvider}; +use reth_tasks::Runtime; +use std::sync::Arc; + +/// The receipts of every block below this one are pruned; the blocks from it on keep theirs. +const RECEIPTS_PRUNED_BEFORE: u64 = 4; +/// The height the chain is built to. The head carries the one log a served range answers with. +const CHAIN_HEIGHT: u64 = 6; +/// Init code that emits an empty `LOG0` and deploys no runtime code: `PUSH1 0 PUSH1 0 LOG0 STOP`. +const LOG_EMITTING_INIT_CODE: [u8; 6] = [0x60, 0x00, 0x60, 0x00, 0xa0, 0x00]; +/// EIP-4444's code for history the node no longer holds. +const PRUNED_HISTORY_UNAVAILABLE_CODE: i32 = 4444; + +/// A node whose chain reaches `CHAIN_HEIGHT` with the receipts below `RECEIPTS_PRUNED_BEFORE` +/// pruned and one log, from a contract created at the head, in the receipts it kept. +struct PrunedReceiptsNode { + handles: RethRpcServerHandles, + last_pruned_block_hash: B256, + log_address: Address, + /// Keeps the node behind the RPC handles running. + _node: N, +} + +async fn launch_pruned_receipts_node() -> eyre::Result> { + super::crash_backtrace::install(); + reth_tracing::init_test_tracing(); + + let genesis: Genesis = serde_json::from_str(include_str!("../assets/genesis.json"))?; + let chain_spec = Arc::new( + OpChainSpecBuilder::optimism_sepolia().genesis(genesis).ecotone_activated().build(), + ); + let chain_id = chain_spec.chain_id(); + let mut config = NodeConfig::test() + .map_chain(chain_spec) + .with_unused_ports() + .with_rpc(RpcServerArgs::default().with_unused_ports().with_http().with_ws()) + // The pruner runs after every persisted block, and a minimum distance of one lets it + // reach a receipts prune target this close to the head of a short chain. + .with_pruning(PruningArgs { + block_interval: Some(1), + minimum_distance: Some(1), + receipts_before: Some(RECEIPTS_PRUNED_BEFORE), + ..Default::default() + }); + config.network.discovery.discv5_port = Some(0); + config.network.discovery.discv5_port_ipv6 = Some(0); + // Every block is persisted as soon as it is canonical, so the pruner sees the chain's height. + config.engine.persistence_threshold = 0; + config.engine.memory_block_buffer_target = Some(0); + config.engine.num_state_masking_blocks = 0; + // Under the v2 layout, op-reth's default, the receipts of a short chain share one static file, + // which the pruner only drops whole, so the v2 pruned-receipts path is not covered here; the + // v1 layout holds them in the database, pruned block by block. + config.storage.v2 = false; + // The RPC cache would keep serving receipts the pruner removed; on a real chain the pruned + // range lies far outside its window. + config.rpc.rpc_state_cache.set_zero_lengths(); + + let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(config) + .testing_node(Runtime::test()) + .node(OpNode::default()) + .launch() + .await?; + let handles = node.add_ons_handle.rpc_server_handles().clone(); + let mut driver = EngineDriver { + client: node.auth_server_handle().http_client(), + engine: node.add_ons_handle.beacon_engine_handle.clone(), + head: node.provider.chain_spec().genesis_hash(), + wallet: Wallet::default().with_chain_id(chain_id), + }; + + for number in 1..CHAIN_HEIGHT { + driver.build_block(number).await?; + } + let creator = driver.spare_signer(); + let log_address = creator.address().create(0); + driver + .build_block_with(CHAIN_HEIGHT, vec![log_emitting_creation_tx(chain_id, &creator)]) + .await?; + + let last_pruned_block = RECEIPTS_PRUNED_BEFORE - 1; + wait_until("the receipts below the prune target were never pruned", || { + matches!(node.provider.receipts_by_block(last_pruned_block.into()), Ok(None)) + }) + .await; + assert!( + node.provider + .receipts_by_block(RECEIPTS_PRUNED_BEFORE.into())? + .is_some_and(|receipts| !receipts.is_empty()), + "the first block past the prune target must keep its receipts" + ); + let last_pruned_block_hash = + node.provider.block_hash(last_pruned_block)?.expect("a pruned block keeps its header"); + + Ok(PrunedReceiptsNode { handles, last_pruned_block_hash, log_address, _node: node }) +} + +/// A contract creation whose init code emits one log and deploys nothing. +fn log_emitting_creation_tx(chain_id: u64, creator: &PrivateKeySigner) -> Bytes { + let mut creation_tx = TxEip1559 { + chain_id, + gas_limit: 100_000, + input: Bytes::from_static(&LOG_EMITTING_INIT_CODE), + max_fee_per_gas: 20_000_000_000, + max_priority_fee_per_gas: 1_000_000_000, + to: TxKind::Create, + ..Default::default() + }; + let signature = creator.sign_transaction_sync(&mut creation_tx).expect("the creation signs"); + op_alloy_consensus::OpPooledTransaction::Eip1559(creation_tx.into_signed(signature)) + .encoded_2718() + .into() +} + +async fn get_logs(client: &impl ClientT, filter: Filter) -> Result, ClientError> { + client.request("eth_getLogs", rpc_params![filter]).await +} + +/// Ranges the node cannot serve in full, each with the error code and exact message it answers. +fn unservable_ranges(last_pruned_block_hash: B256) -> [(&'static str, Filter, i32, String); 4] { + [ + ( + "fromBlock past the head", + Filter::new().from_block(CHAIN_HEIGHT + 1), + INVALID_PARAMS_CODE, + "invalid block range params".to_owned(), + ), + ( + "toBlock past the head", + Filter::new().from_block(1u64).to_block(CHAIN_HEIGHT + 1), + INVALID_PARAMS_CODE, + format!( + "block range extends beyond current head block: requested {}, head {CHAIN_HEIGHT}", + CHAIN_HEIGHT + 1 + ), + ), + ( + "range over pruned receipts", + Filter::new().from_block(1u64).to_block(CHAIN_HEIGHT), + PRUNED_HISTORY_UNAVAILABLE_CODE, + "pruned history unavailable".to_owned(), + ), + ( + "block hash with pruned receipts", + Filter::new().at_block_hash(last_pruned_block_hash), + PRUNED_HISTORY_UNAVAILABLE_CODE, + "pruned history unavailable".to_owned(), + ), + ] +} + +#[track_caller] +fn assert_rpc_error(result: Result, ClientError>, case: &str, code: i32, message: &str) { + match result { + Err(ClientError::Call(err)) => { + assert_eq!(err.code(), code, "unexpected error code for {case}: {err:?}"); + assert_eq!(err.message(), message, "unexpected error message for {case}: {err:?}"); + } + other => panic!("expected error {code} `{message}` for {case}, got {other:?}"), + } +} + +async fn assert_unservable_ranges_rejected( + client: &impl ClientT, + transport: &str, + last_pruned_block_hash: B256, +) { + for (range, filter, code, message) in unservable_ranges(last_pruned_block_hash) { + let result = get_logs(client, filter).await; + + assert_rpc_error(result, &format!("{range} over {transport}"), code, &message); + } +} + +async fn assert_kept_range_served( + client: &impl ClientT, + transport: &str, + log_address: Address, +) -> eyre::Result<()> { + let logs = + get_logs(client, Filter::new().from_block(RECEIPTS_PRUNED_BEFORE).to_block(CHAIN_HEIGHT)) + .await?; + + assert_eq!( + logs.len(), + 1, + "the kept range over {transport} holds exactly the created contract's log: {logs:?}" + ); + assert_eq!(logs[0].address(), log_address, "log address over {transport}"); + assert_eq!(logs[0].block_number, Some(CHAIN_HEIGHT), "log block over {transport}"); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn get_logs_rejects_only_ranges_it_cannot_serve_in_full() -> eyre::Result<()> { + let node = launch_pruned_receipts_node().await?; + let http_client = node.handles.rpc.http_client().expect("http is served"); + let ws_client = node.handles.rpc.ws_client().await.expect("ws is served"); + + assert_unservable_ranges_rejected(&http_client, "http", node.last_pruned_block_hash).await; + assert_unservable_ranges_rejected(&ws_client, "ws", node.last_pruned_block_hash).await; + assert_kept_range_served(&http_client, "http", node.log_address).await?; + assert_kept_range_served(&ws_client, "ws", node.log_address).await?; + Ok(()) +} diff --git a/rust/op-reth/crates/node/tests/it/main.rs b/rust/op-reth/crates/node/tests/it/main.rs index f2aff40c977..d061402ed58 100644 --- a/rust/op-reth/crates/node/tests/it/main.rs +++ b/rust/op-reth/crates/node/tests/it/main.rs @@ -26,4 +26,8 @@ mod derived_attributes; mod payload_during_persistence; +mod engine_driver; + +mod get_logs_errors; + const fn main() {} diff --git a/rust/op-reth/crates/node/tests/it/payload_during_persistence.rs b/rust/op-reth/crates/node/tests/it/payload_during_persistence.rs index 15df2440e12..ec22091e47f 100644 --- a/rust/op-reth/crates/node/tests/it/payload_during_persistence.rs +++ b/rust/op-reth/crates/node/tests/it/payload_during_persistence.rs @@ -4,11 +4,11 @@ //! the write transaction it starts with, before it writes anything, so the test controls when it //! lands. +use crate::engine_driver::{EngineDriver, StartedPayloadJob, wait_until, within_timeout}; use alloy_genesis::Genesis; use alloy_primitives::B256; -use alloy_rpc_types_engine::{ForkchoiceState, PayloadId, PayloadStatus}; +use alloy_rpc_types_engine::ForkchoiceState; use jsonrpsee::{core::client::ClientT, rpc_params}; -use op_alloy_rpc_types_engine::OpExecutionPayloadEnvelopeV3; use reth_chainspec::EthChainSpec; use reth_db::test_utils::{create_test_rw_db_with_datadir, tempdir_path}; use reth_db_api::{Database, DatabaseError, database_metrics::DatabaseMetrics}; @@ -17,7 +17,7 @@ use reth_node_api::ConsensusEngineHandle; use reth_node_builder::{Node, NodeBuilder, NodeConfig}; use reth_node_core::args::{DatadirArgs, RpcServerArgs}; use reth_optimism_chainspec::OpChainSpecBuilder; -use reth_optimism_node::{OpEngineTypes, OpNode, utils::optimism_payload_attributes}; +use reth_optimism_node::{OpEngineTypes, OpNode}; use reth_payload_builder::PayloadBuilderHandle; use reth_provider::{BlockNumReader, ChainSpecProvider, DatabaseProviderFactory}; use reth_tasks::Runtime; @@ -36,9 +36,6 @@ const BLOCK_TIME: Duration = Duration::from_secs(2); /// How often a payload job rebuilds, so a transaction sent to the pool mid-job lands in the /// job's payload well inside the block time. const BUILDER_INTERVAL: Duration = Duration::from_millis(50); -/// Bounds every other engine call and wait, so a stalled engine fails the test instead of -/// hanging it. -const ENGINE_CALL_TIMEOUT: Duration = Duration::from_secs(20); /// How long the payload builder lets a job run before ending it on its own, far past every wait a /// job started by the test outlives, so a job only ends when the test resolves it. const PAYLOAD_JOB_DEADLINE: Duration = Duration::from_secs(600); @@ -132,108 +129,8 @@ impl Database for WriteGatedDatabase { impl DatabaseMetrics for WriteGatedDatabase {} -/// A payload job the engine has started but not resolved. -struct StartedPayloadJob { - number: u64, - payload_id: PayloadId, - start_duration: Duration, -} - -/// Builds blocks the way a consensus client does, through the engine API. -struct EngineDriver { - client: C, - engine: ConsensusEngineHandle, - head: B256, - wallet: Wallet, -} - -impl EngineDriver { - async fn build_block(&mut self, number: u64) -> eyre::Result { - let job = self.start_payload_job(number).await?; - self.finish_payload_job(job).await - } - - /// Starts a payload job on the head, with the block's L1 info deposit as its only - /// sequencer transaction. - async fn start_payload_job(&self, number: u64) -> eyre::Result { - let mut attrs = optimism_payload_attributes(number); - attrs.0.transactions = Some(vec![ - TransactionTestContext::optimism_l1_block_info_tx( - self.wallet.chain_id, - self.wallet.inner.clone(), - number - 1, - ) - .await, - ]); - let started_at = Instant::now(); - let fcu = within_timeout( - "forkchoice update with attributes stalled", - self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), Some(attrs)), - ) - .await?; - let start_duration = started_at.elapsed(); - assert!(fcu.payload_status.is_valid(), "forkchoice update for block {number}: {fcu:?}"); - let payload_id = fcu.payload_id.expect("a payload job must start"); - Ok(StartedPayloadJob { number, payload_id, start_duration }) - } - - /// Fetches the job's payload and makes it canonical. Returns how long the engine took to - /// answer the forkchoice update that started the job and the getPayload that resolved it, - /// leaving out whatever the caller waited on in between. - async fn finish_payload_job(&mut self, job: StartedPayloadJob) -> eyre::Result { - let resolve_started_at = Instant::now(); - let payload: OpExecutionPayloadEnvelopeV3 = within_timeout( - "getPayload stalled", - self.client.request("engine_getPayloadV3", rpc_params![job.payload_id]), - ) - .await?; - let job_duration = job.start_duration + resolve_started_at.elapsed(); - - let block = &payload.execution_payload.payload_inner.payload_inner; - assert_eq!(block.block_number, job.number); - assert!(!block.transactions.is_empty(), "block {} lacks its L1 info deposit", job.number); - self.head = block.block_hash; - let status: PayloadStatus = within_timeout( - "newPayload stalled", - self.client.request( - "engine_newPayloadV3", - rpc_params![ - payload.execution_payload, - Vec::::new(), - payload.parent_beacon_block_root - ], - ), - ) - .await?; - assert!(status.is_valid(), "newPayload for block {}: {status:?}", job.number); - let fcu = within_timeout( - "canonical forkchoice update stalled", - self.engine.fork_choice_updated(ForkchoiceState::same_hash(self.head), None), - ) - .await?; - assert!( - fcu.payload_status.is_valid(), - "forkchoice update to block {}: {fcu:?}", - job.number - ); - Ok(job_duration) - } -} - -async fn within_timeout(message: &str, future: impl Future) -> T { - tokio::time::timeout(ENGINE_CALL_TIMEOUT, future).await.unwrap_or_else(|_| panic!("{message}")) -} - -async fn wait_until(message: &str, mut ready: impl FnMut() -> bool) { - within_timeout(message, async { - while !ready() { - tokio::time::sleep(Duration::from_millis(10)).await; - } - }) - .await; -} - -/// Waits for the job's best payload to hold the block's deposit and the pool transaction. +/// Waits for the job's best payload to hold the block's L1 info transaction and the pool +/// transaction. async fn wait_for_pool_transaction_built( builder: &PayloadBuilderHandle, job: &StartedPayloadJob, @@ -384,8 +281,7 @@ async fn run_scenario(persistence_threshold: u64) -> eyre::Result<()> { slowest_forkchoice_update <= BLOCK_TIME, "a forkchoice update took {slowest_forkchoice_update:?} while a {batch_tip}-block persistence batch landed, more than the block time {BLOCK_TIME:?}" ); - // From a second funded account: the deposits the wallet signs have used up its nonces. - let pool_sender = Wallet::new(2).with_chain_id(driver.wallet.chain_id).wallet_gen().remove(1); + let pool_sender = driver.spare_signer(); let pool_transaction = TransactionTestContext::transfer_tx_bytes(driver.wallet.chain_id, pool_sender).await; let rebuild_started_at = Instant::now();