From 39be695a99a7475d6dfd1fa831e700bc6e00f065 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 30 Sep 2026 15:39:45 +0200 Subject: [PATCH 01/11] perf(rpc): stop long blocking jobs once their request is dropped (#27462) Picked as a prerequisite of the eth_callBundle bound (#28253), whose transaction loop stops on reth_tasks::cancel::is_cancelled(). Adapted: the fork's backport of #28266 replays block transactions through the block executor. The cancellation checks upstream placed in the replaced EVM loops go into the executor loops instead (replay_transactions_until_with_executor, trace_block_until_with_inspector and debug intermediate roots), as upstream did when #28266 landed. Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit 13a59de4bad087057f591fe14aa1884bd7e79cab) --- .../rpc-eth-api/src/helpers/blocking_task.rs | 15 +++- crates/rpc/rpc-eth-api/src/helpers/call.rs | 9 ++- .../rpc/rpc-eth-api/src/helpers/estimate.rs | 5 ++ crates/rpc/rpc-eth-api/src/helpers/trace.rs | 4 + crates/rpc/rpc/src/debug.rs | 11 ++- crates/rpc/rpc/src/eth/core.rs | 28 ++++++- crates/rpc/rpc/src/trace.rs | 5 +- crates/tasks/src/cancel.rs | 79 ++++++++++++++++++- 8 files changed, 146 insertions(+), 10 deletions(-) diff --git a/crates/rpc/rpc-eth-api/src/helpers/blocking_task.rs b/crates/rpc/rpc-eth-api/src/helpers/blocking_task.rs index f6918948cf0..15bc881d0cc 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/blocking_task.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/blocking_task.rs @@ -5,7 +5,7 @@ use futures::Future; use reth_rpc_eth_types::EthApiError; use reth_tasks::{ pool::{BlockingTaskGuard, BlockingTaskPool}, - Runtime, + CancelOnDrop, Runtime, }; use std::sync::Arc; use tokio::sync::{oneshot, AcquireError, OwnedSemaphorePermit, Semaphore}; @@ -156,6 +156,9 @@ pub trait SpawnBlocking: EthApiTypes + Clone + Send + Sync + 'static { /// /// Note: This is expected for futures that are dominated by blocking IO operations, for tracing /// or CPU bound operations in general use [`spawn_tracing`](Self::spawn_tracing). + /// + /// The task keeps running when the returned future is dropped, long running work in `f` + /// should check [`is_cancelled`](reth_tasks::cancel::is_cancelled) to stop early. fn spawn_blocking_io(&self, f: F) -> impl Future> + Send where F: FnOnce(Self) -> Result + Send + 'static, @@ -163,15 +166,21 @@ pub trait SpawnBlocking: EthApiTypes + Clone + Send + Sync + 'static { { let (tx, rx) = oneshot::channel(); let this = self.clone(); + let cancel = CancelOnDrop::default(); + let request = cancel.clone(); self.io_task_spawner().spawn_blocking_task(async move { if tx.is_closed() { return } - let res = f(this); + let res = cancel.scope(|| f(this)); let _ = tx.send(res); }); - async move { rx.await.map_err(|_| EthApiError::InternalEthError)? } + async move { + // cancels the blocking task once the request is dropped + let _request = request; + rx.await.map_err(|_| EthApiError::InternalEthError)? + } } /// Executes the future on a new blocking task. diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 0e20e1d39b2..33ee262f224 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -46,7 +46,7 @@ use reth_rpc_eth_types::{ use reth_storage_api::{ BlockIdReader, EvmStateProviderAdapter, ProviderTx, StateProvider, StateProviderBox, }; -use reth_tasks::CancelOnDrop; +use reth_tasks::{cancel::is_cancelled, CancelOnDrop}; use revm::{ context::Block, context_interface::{result::ResultAndState, Cfg, Transaction}, @@ -406,6 +406,10 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA // transact all transactions in the bundle for (tx_index, tx) in transactions.into_iter().enumerate() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } + // Apply overrides, state overrides are only applied for the first tx in the // request let overrides = @@ -808,6 +812,9 @@ pub trait Call: Txs: IntoIterator>>, { for tx in transactions.into_iter().take(target_tx_index) { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } executor.execute_transaction(tx).map_err(Self::Error::from_eth_err)?; } Ok(()) diff --git a/crates/rpc/rpc-eth-api/src/helpers/estimate.rs b/crates/rpc/rpc-eth-api/src/helpers/estimate.rs index 7f4fe4f5b71..21113e85094 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/estimate.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/estimate.rs @@ -27,6 +27,7 @@ use reth_rpc_eth_types::{ }; use reth_rpc_server_types::constants::gas_oracle::{CALL_STIPEND_GAS, ESTIMATE_GAS_ERROR_RATIO}; use reth_storage_api::{EvmStateProvider, StateProvider}; +use reth_tasks::cancel::is_cancelled; use revm::{ context::Block, context_interface::{result::ExecutionResult, Cfg, Transaction}, @@ -266,6 +267,10 @@ pub trait EstimateCall: Call { break }; + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } + let mut mid_tx_env = tx_env.clone(); mid_tx_env.set_gas_limit(mid_gas_limit); diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index 010c153f195..3ad228cb8b4 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs @@ -18,6 +18,7 @@ use reth_rpc_eth_types::{ EthApiError, }; use reth_storage_api::ProviderBlock; +use reth_tasks::cancel::is_cancelled; use revm::{context::Block, context_interface::result::ResultAndState, state::bal::Bal as RevmBal}; use revm_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; use std::sync::Arc; @@ -366,6 +367,9 @@ pub trait Trace: LoadState> + Call { let mut transactions = block.transactions_recovered().take(max_transactions).enumerate().peekable(); while let Some((idx, tx)) = transactions.next() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } let output = executor .execute_transaction_without_commit(tx) .map_err(Self::Error::from_eth_err)?; diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 35a9ee16b2a..82a54bf6321 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -42,7 +42,7 @@ use reth_storage_api::{ ReceiptProviderIdExt, StateProviderBox, StateProviderFactory, StateRootProvider, StorageRootProvider, TransactionVariant, }; -use reth_tasks::{pool::BlockingTaskGuard, Runtime}; +use reth_tasks::{cancel::is_cancelled, pool::BlockingTaskGuard, Runtime}; use reth_transaction_pool::TransactionPool; use reth_trie_common::{ root::storage_root_unsorted, updates::TrieUpdates, ExecutionWitnessMode, HashedPostState, @@ -139,6 +139,9 @@ where let mut transactions = block.transactions_recovered().enumerate().peekable(); while let Some((index, tx)) = transactions.next() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } let tx_env = eth_api.evm_config().tx_env(tx); let output = executor @@ -552,6 +555,9 @@ where let mut transactions = transactions.into_iter().peekable(); while let Some(tx) = transactions.next() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } // apply state overrides only once, before the first transaction let state_overrides = state_overrides.take(); let overrides = EvmOverrides::new(state_overrides, block_overrides.clone()); @@ -824,6 +830,9 @@ where let mut roots = Vec::with_capacity(block.body().transactions().len()); for tx in block.transactions_recovered() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } executor.execute_transaction(tx).map_err(Eth::Error::from_eth_err)?; let state = executor.evm_mut().db_mut(); diff --git a/crates/rpc/rpc/src/eth/core.rs b/crates/rpc/rpc/src/eth/core.rs index 4fb2731cf14..6082442a3f8 100644 --- a/crates/rpc/rpc/src/eth/core.rs +++ b/crates/rpc/rpc/src/eth/core.rs @@ -570,7 +570,7 @@ mod tests { PruneCheckpointReader, StageCheckpointReader, }; use reth_rpc_eth_api::{ - helpers::{EthBlocks, EthCall}, + helpers::{EthBlocks, EthCall, SpawnBlocking}, node::RpcNodeCoreAdapter, EthApiServer, }; @@ -578,8 +578,10 @@ mod tests { use reth_storage_api::{ BalProvider, BlockReader, BlockReaderIdExt, NodePrimitivesProvider, StateProviderFactory, }; + use reth_tasks::cancel::is_cancelled; use reth_testing_utils::generators; use reth_transaction_pool::test_utils::{testing_pool, TestPool}; + use std::time::{Duration, Instant}; type FakeEthApi

= EthApi< RpcNodeCoreAdapter, @@ -626,6 +628,30 @@ mod tests { .build() } + #[tokio::test(flavor = "multi_thread")] + async fn blocking_task_observes_dropped_request() { + let api = build_test_eth_api(MockEthProvider::default()); + let (started_tx, started_rx) = tokio::sync::oneshot::channel(); + let (cancelled_tx, cancelled_rx) = tokio::sync::oneshot::channel(); + + let request = api.spawn_blocking_io(move |_| { + let _ = started_tx.send(()); + let deadline = Instant::now() + Duration::from_secs(5); + while !is_cancelled() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(1)); + } + let _ = cancelled_tx.send(is_cancelled()); + Ok(()) + }); + // drop the request while its blocking task is running + tokio::select! { + _ = request => panic!("blocking task completed before the request was dropped"), + _ = started_rx => {} + } + + assert!(cancelled_rx.await.unwrap()); + } + #[tokio::test] async fn test_transaction_by_sender_and_nonce_without_sender_transaction_returns_none() { use reth_rpc_eth_api::helpers::EthTransactions; diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index 19fbfc6570c..b95b3c95c38 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -28,7 +28,7 @@ use reth_rpc_eth_api::{ }; use reth_rpc_eth_types::{error::EthApiError, EthConfig}; use reth_storage_api::{BlockNumReader, BlockReader}; -use reth_tasks::pool::BlockingTaskGuard; +use reth_tasks::{cancel::is_cancelled, pool::BlockingTaskGuard}; use reth_transaction_pool::{PoolPooledTx, PoolTransaction, TransactionPool}; use revm::DatabaseCommit; use revm_inspectors::{ @@ -164,6 +164,9 @@ where let mut calls = calls.into_iter().peekable(); while let Some((call, trace_types)) = calls.next() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } let (evm_env, tx_env) = eth_api.prepare_call_env( evm_env.clone(), call, diff --git a/crates/tasks/src/cancel.rs b/crates/tasks/src/cancel.rs index 0d09172958b..807fbaa4cef 100644 --- a/crates/tasks/src/cancel.rs +++ b/crates/tasks/src/cancel.rs @@ -1,8 +1,11 @@ //! Cooperative cancellation of spawned work. -use std::sync::{ - atomic::{AtomicBool, AtomicU8, Ordering}, - Arc, +use std::{ + cell::RefCell, + sync::{ + atomic::{AtomicBool, AtomicU8, Ordering}, + Arc, + }, }; /// The work is still in progress. @@ -48,6 +51,24 @@ impl CancelOnDrop { pub fn is_finalization_requested(&self) -> bool { self.0.load(Ordering::Relaxed) == FINALIZATION_REQUESTED } + + /// Runs `f` with this as the cancellation state of the current thread, see [`is_cancelled`]. + /// + /// This lets code deep inside `f`, such as a loop over transactions, observe cancellation + /// without threading the [`CancelOnDrop`] through every call. The previous state of the + /// thread is restored once `f` returns or unwinds. Leaving the scope does not cancel. + pub fn scope(&self, f: impl FnOnce() -> R) -> R { + struct Restore(Option>); + + impl Drop for Restore { + fn drop(&mut self) { + CURRENT.set(self.0.take()); + } + } + + let _restore = Restore(CURRENT.replace(Some(self.0.clone()))); + f() + } } impl Drop for CancelOnDrop { @@ -79,6 +100,25 @@ impl ManualCancel { } } +/// Returns `true` if the work running on the current thread was cancelled. +/// +/// This reads the state of the innermost [`CancelOnDrop::scope`] on this thread, for example a +/// blocking RPC call whose request was dropped because the client disconnected. Long running work +/// should check this between units of work, such as transactions, and stop early since nobody +/// waits for the result. +/// +/// Always returns `false` outside of a [`CancelOnDrop::scope`]. +pub fn is_cancelled() -> bool { + CURRENT.with_borrow(|state| { + state.as_ref().is_some_and(|state| state.load(Ordering::Relaxed) == CANCELLED) + }) +} + +thread_local! { + /// Cancellation state of the innermost [`CancelOnDrop::scope`] on this thread. + static CURRENT: RefCell>> = const { RefCell::new(None) }; +} + #[cfg(test)] mod tests { use super::*; @@ -200,4 +240,37 @@ mod tests { assert!(clone.is_cancelled()); assert!(!clone.is_finalization_requested()); } + + #[test] + fn test_scope_is_cancelled() { + assert!(!is_cancelled()); + + let outer = CancelOnDrop::default(); + let inner = CancelOnDrop::default(); + outer.scope(|| { + assert!(!is_cancelled()); + inner.scope(|| { + drop(inner.clone()); + assert!(is_cancelled()); + }); + // the outer state is restored + assert!(!is_cancelled()); + outer.request_finalization(); + assert!(!is_cancelled()); + }); + + // leaving the scope does not cancel + assert!(!is_cancelled()); + assert!(!outer.is_cancelled()); + } + + #[test] + fn test_scope_restores_on_panic() { + let cancel = CancelOnDrop::default(); + drop(cancel.clone()); + + let res = std::panic::catch_unwind(|| cancel.scope(|| panic!("scope panicked"))); + assert!(res.is_err()); + assert!(!is_cancelled()); + } } From d1007f9e96a33b954ba6322df0c421b22e4e7523 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 4 Oct 2026 17:22:57 +0000 Subject: [PATCH 02/11] fix(nippy-jar): handle uncommitted rows in prune_rows (#27719) Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit 0d1324f14712cefca117bdcb561ed9393192160a) --- crates/storage/nippy-jar/src/lib.rs | 109 +++++++++++++++++++++++++ crates/storage/nippy-jar/src/writer.rs | 12 +++ 2 files changed, 121 insertions(+) diff --git a/crates/storage/nippy-jar/src/lib.rs b/crates/storage/nippy-jar/src/lib.rs index c1d6cefb316..3f892deaace 100644 --- a/crates/storage/nippy-jar/src/lib.rs +++ b/crates/storage/nippy-jar/src/lib.rs @@ -841,6 +841,93 @@ mod tests { } } + #[test] + fn test_prune_partially_uncommitted_rows() { + let (col1, col2) = test_data(None); + let num_columns = 2; + let file_path = tempfile::NamedTempFile::new().unwrap(); + + // Two committed rows. + append_two_rows(num_columns, file_path.path(), &col1, &col2); + + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + let mut writer = NippyJarWriter::new(nippy).unwrap(); + + // Two uncommitted rows, of which only one is pruned again. + append_row(&mut writer, &col1, &col2, 2); + append_row(&mut writer, &col1, &col2, 3); + writer.prune_rows(1).unwrap(); + assert_eq!(writer.rows(), 3); + + // The config on disk must never claim rows whose offsets are not on disk. + let expected_offsets_len = 1 + 3 * num_columns as u64 * 8 + 8; + assert_eq!( + File::open(writer.offsets_path()).unwrap().metadata().unwrap().len(), + expected_offsets_len + ); + let expected_data_len = col1[..3].iter().chain(&col2[..3]).map(Vec::len).sum::(); + assert_eq!( + File::open(writer.data_path()).unwrap().metadata().unwrap().len() as usize, + expected_data_len + ); + assert_jar_rows(file_path.path(), &col1, &col2, 3); + + // Committing must not change anything, and the writer remains usable. + writer.commit().unwrap(); + assert_eq!( + File::open(writer.offsets_path()).unwrap().metadata().unwrap().len(), + expected_offsets_len + ); + assert_jar_rows(file_path.path(), &col1, &col2, 3); + + append_row(&mut writer, &col1, &col2, 3); + writer.commit().unwrap(); + assert_eq!( + File::open(writer.offsets_path()).unwrap().metadata().unwrap().len(), + 1 + 4 * num_columns as u64 * 8 + 8 + ); + assert_jar_rows(file_path.path(), &col1, &col2, 4); + } + + #[test] + fn test_prune_uncommitted_and_committed_rows() { + let (col1, col2) = test_data(None); + let num_columns = 2; + let file_path = tempfile::NamedTempFile::new().unwrap(); + + // Two committed rows. + append_two_rows(num_columns, file_path.path(), &col1, &col2); + + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + let mut writer = NippyJarWriter::new(nippy).unwrap(); + + // One uncommitted row, which is pruned together with one committed row. + append_row(&mut writer, &col1, &col2, 2); + writer.prune_rows(2).unwrap(); + assert_eq!(writer.rows(), 1); + + // Committing must not write the stale in-memory offset of the pruned rows. + writer.commit().unwrap(); + assert_eq!( + File::open(writer.offsets_path()).unwrap().metadata().unwrap().len(), + 1 + num_columns as u64 * 8 + 8 + ); + assert_eq!( + File::open(writer.data_path()).unwrap().metadata().unwrap().len() as usize, + col1[0].len() + col2[0].len() + ); + assert_jar_rows(file_path.path(), &col1, &col2, 1); + + // Rows appended through the same writer afterwards read back correctly. + append_row(&mut writer, &col1, &col2, 1); + writer.commit().unwrap(); + assert_eq!( + File::open(writer.offsets_path()).unwrap().metadata().unwrap().len(), + 1 + 2 * num_columns as u64 * 8 + 8 + ); + assert_jar_rows(file_path.path(), &col1, &col2, 2); + } + fn test_append_consistency_partial_commit( file_path: &Path, col1: &[Vec], @@ -1097,4 +1184,26 @@ mod tests { // runs the consistency check. let _ = NippyJarWriter::new(nippy).unwrap(); } + + fn append_row(writer: &mut NippyJarWriter, col1: &[Vec], col2: &[Vec], row: usize) { + writer.append_column(Some(Ok(&col1[row]))).unwrap(); + writer.append_column(Some(Ok(&col2[row]))).unwrap(); + } + + /// Asserts that the jar on disk is consistent, claims exactly `rows` rows, and that every + /// one of them reads back as the corresponding entry of `col1` and `col2`. + fn assert_jar_rows(file_path: &Path, col1: &[Vec], col2: &[Vec], rows: usize) { + let nippy = NippyJar::load_without_header(file_path).unwrap(); + assert_eq!(nippy.rows, rows); + NippyJarChecker::new(NippyJar::load_without_header(file_path).unwrap()) + .check_consistency() + .unwrap(); + + let mut cursor = NippyJarCursor::new(&nippy).unwrap(); + for row in 0..rows { + let values = cursor.row_by_number(row).unwrap().unwrap(); + assert_eq!((values[0], values[1]), (col1[row].as_slice(), col2[row].as_slice())); + } + assert!(cursor.row_by_number(rows).unwrap().is_none()); + } } diff --git a/crates/storage/nippy-jar/src/writer.rs b/crates/storage/nippy-jar/src/writer.rs index 44fd47d0d42..6bcc7bf3831 100644 --- a/crates/storage/nippy-jar/src/writer.rs +++ b/crates/storage/nippy-jar/src/writer.rs @@ -319,6 +319,18 @@ impl NippyJarWriter { } } + if remaining_to_prune > 0 { + // Every uncommitted row was pruned, so the remaining in-memory offset (the previous + // data file end) is stale: the on-disk prune shrank the data file below it. + self.offsets.clear(); + } else { + // The config written below counts the uncommitted rows that were not pruned, so their + // offsets must be on disk too. If we crash before the config is written, the offsets + // file is longer than the config expects and the checker heals it as an interrupted + // append. + self.commit_offsets_inner()?; + } + self.offsets_file.get_ref().sync_all()?; self.data_file.get_ref().sync_all()?; From d87506d353600b5d1fd1b206f63f97748dd8a844 Mon Sep 17 00:00:00 2001 From: Angel Valkov Date: Sun, 4 Oct 2026 18:11:40 +0000 Subject: [PATCH 03/11] fix(nippy-jar): end last value at its offset (#27678) Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit 22e3708b2408709796925919a61783b094cd1db8) --- crates/storage/nippy-jar/src/cursor.rs | 19 +++--- crates/storage/nippy-jar/src/lib.rs | 60 ++++++++++++++++++- .../provider/src/providers/static_file/mod.rs | 33 +++++++++- 3 files changed, 103 insertions(+), 9 deletions(-) diff --git a/crates/storage/nippy-jar/src/cursor.rs b/crates/storage/nippy-jar/src/cursor.rs index 4fc74a57264..7e62c53a222 100644 --- a/crates/storage/nippy-jar/src/cursor.rs +++ b/crates/storage/nippy-jar/src/cursor.rs @@ -150,13 +150,18 @@ impl<'a, H: NippyJarHeader> NippyJarCursor<'a, H> { let offset_pos = self.row as usize * self.jar.columns + column; let value_offset = self.reader.offset(offset_pos)? as usize; - let column_offset_range = if self.jar.rows * self.jar.columns == offset_pos + 1 { - // It's the last column of the last row - value_offset..self.reader.size() - } else { - let next_value_offset = self.reader.offset(offset_pos + 1)? as usize; - value_offset..next_value_offset - }; + // A value ends where the next one starts. After the last value, the final offset marks the + // end of the committed data, which is not the end of the data file while a writer appends + // rows it has not committed yet. + let next_value_offset = self.reader.offset(offset_pos + 1)? as usize; + + // Offsets read through a stale mmap while a writer truncates the files, e.g. a concurrent + // prune, can be out of order or past the mapped data. Slicing with them would panic. + if next_value_offset < value_offset || next_value_offset > self.reader.size() { + return Err(NippyJarError::InconsistentState) + } + + let column_offset_range = value_offset..next_value_offset; if let Some(compression) = self.jar.compressor() { // The decompressors write into the spare capacity of the buffer, so it has to fit any diff --git a/crates/storage/nippy-jar/src/lib.rs b/crates/storage/nippy-jar/src/lib.rs index 3f892deaace..fc1a381f3ef 100644 --- a/crates/storage/nippy-jar/src/lib.rs +++ b/crates/storage/nippy-jar/src/lib.rs @@ -449,7 +449,10 @@ mod tests { use super::*; use compression::Compression; use rand::{rngs::SmallRng, seq::SliceRandom, RngCore, SeedableRng}; - use std::{fs::OpenOptions, io::Read}; + use std::{ + fs::OpenOptions, + io::{Read, Seek, SeekFrom}, + }; type ColumnResults = Vec>; type ColumnValues = Vec>; @@ -841,6 +844,61 @@ mod tests { } } + #[test] + fn test_read_last_row_while_appending() { + let (col1, col2) = test_data(None); + let num_columns = 2; + let file_path = tempfile::NamedTempFile::new().unwrap(); + + append_two_rows(num_columns, file_path.path(), &col1, &col2); + + // Appends a third row to the data file, without committing its offsets and the row count. + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + let mut writer = NippyJarWriter::new(nippy).unwrap(); + writer.append_column(Some(Ok(&col1[2]))).unwrap(); + writer.append_column(Some(Ok(&col2[2]))).unwrap(); + writer.data_file().flush().unwrap(); + + // A jar loaded meanwhile reads the last committed row as it was written. + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + assert_eq!(nippy.rows, 2); + let mut cursor = NippyJarCursor::new(&nippy).unwrap(); + assert_eq!( + cursor.row_by_number(1).unwrap().unwrap().as_slice(), + [col1[1].as_slice(), col2[1].as_slice()] + ); + assert!(cursor.row_by_number(2).unwrap().is_none()); + } + + #[test] + fn test_read_last_row_with_invalid_final_offset() { + let (col1, col2) = test_data(None); + let num_columns = 2; + + // Overwrites the final offset of a committed jar, then reads its last row. + let assert_inconsistent = |final_offset: fn(u64) -> u64| { + let file_path = tempfile::NamedTempFile::new().unwrap(); + append_two_rows(num_columns, file_path.path(), &col1, &col2); + + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + let data_len = File::open(nippy.data_path()).unwrap().metadata().unwrap().len(); + let mut offsets = + OpenOptions::new().read(true).write(true).open(nippy.offsets_path()).unwrap(); + offsets.seek(SeekFrom::End(-8)).unwrap(); + offsets.write_all(&final_offset(data_len).to_le_bytes()).unwrap(); + offsets.sync_all().unwrap(); + + let nippy = NippyJar::load_without_header(file_path.path()).unwrap(); + let mut cursor = NippyJarCursor::new(&nippy).unwrap(); + assert!(matches!(cursor.row_by_number(1), Err(NippyJarError::InconsistentState))); + }; + + // Before the start of the last value. + assert_inconsistent(|_| 0); + // Past the end of the data file. + assert_inconsistent(|data_len| data_len + 1); + } + #[test] fn test_prune_partially_uncommitted_rows() { let (col1, col2) = test_data(None); diff --git a/crates/storage/provider/src/providers/static_file/mod.rs b/crates/storage/provider/src/providers/static_file/mod.rs index 2fac861f3c4..b2c6570c12b 100644 --- a/crates/storage/provider/src/providers/static_file/mod.rs +++ b/crates/storage/provider/src/providers/static_file/mod.rs @@ -104,7 +104,8 @@ mod tests { find_fixed_range, SegmentRangeInclusive, DEFAULT_BLOCKS_PER_STATIC_FILE, }; use reth_storage_api::{ - ChangeSetReader, ReceiptProvider, StorageChangeSetReader, TransactionsProvider, + BlockHashReader, ChangeSetReader, ReceiptProvider, StorageChangeSetReader, + TransactionsProvider, }; use reth_testing_utils::generators::{self, random_header_range}; use std::{collections::BTreeMap, fmt::Debug, fs, ops::Range, path::Path}; @@ -1417,4 +1418,34 @@ mod tests { let entry = result.unwrap(); assert_eq!(entry.value, U256::from(42)); } + + #[test] + fn test_read_headers_while_appending() { + let (static_dir, _) = create_test_static_files_dir(); + let sf_rw: StaticFileProvider = + StaticFileProviderBuilder::read_write(&static_dir) + .build() + .expect("Failed to build static file provider"); + let hash = |num: u64| B256::with_last_byte(num as u8 + 1); + + let mut header_writer = sf_rw.latest_writer(StaticFileSegment::Headers).unwrap(); + let mut header = Header::default(); + for num in 0..=2 { + header.number = num; + header_writer.append_header(&header, &hash(num)).unwrap(); + } + header_writer.commit().unwrap(); + + // Writes the next header to disk without committing it, as persistence does before the + // database commit. + header.number = 3; + header_writer.append_header(&header, &hash(3)).unwrap(); + header_writer.sync_all().unwrap(); + + // Reloads the jar from disk meanwhile, as every static file deletion by the pruner does. + sf_rw.initialize_index().unwrap(); + + assert_eq!(sf_rw.block_hash(2).unwrap(), Some(hash(2))); + assert_eq!(sf_rw.block_hash(3).unwrap(), None); + } } From c4c6e622eea08efc7841414f08cffaaea81368ec Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 26 Oct 2026 10:13:54 +0100 Subject: [PATCH 04/11] fix(engine): retain recreated account state in cache (#28148) Adapted: the test imports StorageSlot from reth_revm::db::states (revm 43) instead of reth_trie_common::bundle_state, which wraps evm2 types upstream. (cherry picked from commit 3c8b4e84357882802722f6adfdfde8f6167399d5) --- .../execution-cache/src/cached_state.rs | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/crates/engine/execution-cache/src/cached_state.rs b/crates/engine/execution-cache/src/cached_state.rs index 1783bd07f15..9532519e9e7 100644 --- a/crates/engine/execution-cache/src/cached_state.rs +++ b/crates/engine/execution-cache/src/cached_state.rs @@ -1159,8 +1159,9 @@ impl ExecutionCache { // because we can't efficiently invalidate all storage slots for a single address. // This should only happen on pre-Dencun networks. // - // If the original account had no code (was an EOA or a not yet deployed contract), we - // just remove the account from cache - no storage exists for it. + // If the original account had no code (was an EOA or a not yet deployed contract), a + // destroyed account can be removed from the cache. A re-created account still has + // post-block storage and info that must be written below. if account.was_destroyed() { let had_code = account.original_info.as_ref().is_some_and(|info| !info.is_empty_code_hash()); @@ -1178,8 +1179,10 @@ impl ExecutionCache { return Ok(()) } - self.0.account_cache.remove(addr); - continue; + if account.info.is_none() { + self.0.account_cache.remove(addr); + continue; + } } // If we have an account that was modified, but it has a `None` account info, some wild @@ -1311,7 +1314,7 @@ mod tests { use super::*; use alloy_primitives::{map::HashMap, U256}; use reth_provider::test_utils::{ExtendedAccount, MockEthProvider}; - use reth_revm::db::{AccountStatus, BundleAccount}; + use reth_revm::db::{states::StorageSlot, AccountStatus, BundleAccount}; use reth_storage_api::StateProvider; use revm::state::AccountInfo; @@ -1512,6 +1515,51 @@ mod tests { assert!(caches.0.storage_cache.get(&(addr1, storage_key)).is_some()); } + #[test] + fn test_insert_state_recreated_account_updates_cached_storage() { + let caches = ExecutionCache::new(1000); + let addr = Address::random(); + let other_addr = Address::random(); + let key = StorageKey::from(U256::from(1)); + let other_key = StorageKey::from(U256::from(2)); + let provider = MockEthProvider::default(); + provider.extend_accounts(vec![(addr, ExtendedAccount::new(0, U256::from(100)))]); + let state_provider = + CachedStateProvider::new_prewarm(provider.into_evm_state_provider(), caches.clone()); + assert_eq!(state_provider.storage(addr, key).unwrap(), None); + assert_eq!(caches.0.storage_cache.get(&(addr, key)), Some(U256::ZERO)); + drop(state_provider); + caches.insert_storage(other_addr, other_key, Some(U256::from(42))); + + let current_info = + AccountInfo { nonce: 1, code_hash: B256::random(), ..Default::default() }; + let bundle = BundleState { + state: HashMap::from_iter([( + addr, + BundleAccount::new( + Some(AccountInfo { + balance: U256::from(100), + code_hash: alloy_primitives::KECCAK256_EMPTY, + ..Default::default() + }), + Some(current_info.clone()), + HashMap::from_iter([( + U256::from(1), + StorageSlot::new_changed(U256::ZERO, U256::from(7)), + )]), + AccountStatus::DestroyedChanged, + ), + )]), + ..Default::default() + }; + + assert!(caches.insert_state(&bundle).is_ok()); + assert_eq!(caches.0.storage_cache.get(&(addr, key)), Some(U256::from(7))); + let cached_account = caches.0.account_cache.get(&addr).flatten().unwrap(); + assert_eq!(cached_account.bytecode_hash, Some(current_info.code_hash)); + assert_eq!(caches.0.storage_cache.get(&(other_addr, other_key)), Some(U256::from(42))); + } + #[test] fn test_insert_state_destroyed_account_no_original_info_removes_only_account() { let caches = ExecutionCache::new(1000); From 2b59ebdd143775d08758e3da8acd7412dbe4e827 Mon Sep 17 00:00:00 2001 From: onbjerg Date: Tue, 3 Nov 2026 13:11:06 +0000 Subject: [PATCH 05/11] fix(rpc): bound eth_callBundle work (#28253) Adapted to revm 43 / alloy-evm 0.39, which this base still runs instead of evm2: - EIP-8037 detection reads `cfg_env.enable_amsterdam_eip8037` and the cap `Cfg::tx_gas_limit_cap()` instead of `version().feature(EIP8037)` and `version().tx_gas_limit_cap`, the same values alloy-evm's block executor admits transactions with. - Per-transaction regular and state gas come from `ResultGas::block_regular_gas_used()` / `block_state_gas_used()`, which compute what evm2's `execution_gas_spent()` / `state_gas_spent()` do. - The block gas limit is a u64 on the base's block env, and the loop keeps the base's peekable iteration and deferred commit. (cherry picked from commit 329fcfc73efe3c46e55e4b96abdbfe7d9a29e9ed) --- crates/rpc/rpc/src/eth/bundle.rs | 189 ++++++++++++++++++++++++++++++- 1 file changed, 186 insertions(+), 3 deletions(-) diff --git a/crates/rpc/rpc/src/eth/bundle.rs b/crates/rpc/rpc/src/eth/bundle.rs index 43b776afece..5f9d6f90bf1 100644 --- a/crates/rpc/rpc/src/eth/bundle.rs +++ b/crates/rpc/rpc/src/eth/bundle.rs @@ -16,12 +16,14 @@ use reth_rpc_eth_types::{ EthApiError, PendingBlockEnv, PendingBlockEnvOrigin, RpcInvalidTransactionError, }; use reth_storage_api::BlockReaderIdExt; -use reth_tasks::pool::BlockingTaskGuard; +use reth_tasks::{cancel::is_cancelled, pool::BlockingTaskGuard}; use reth_transaction_pool::{ EthBlobTransactionSidecar, EthPoolTransaction, PoolPooledTx, PoolTransaction, TransactionPool, }; use revm::{ - context::Block, context_interface::result::ResultAndState, DatabaseCommit, DatabaseRef, + context::{Block, Cfg}, + context_interface::result::ResultAndState, + DatabaseCommit, DatabaseRef, }; use std::sync::Arc; @@ -149,7 +151,8 @@ where } // Apply gas limit: default to call gas limit unless user requests a smaller limit - evm_env.block_env.inner_mut().gas_limit = gas_limit.unwrap_or(call_gas_limit); + let block_gas_limit = gas_limit.unwrap_or(call_gas_limit); + evm_env.block_env.inner_mut().gas_limit = block_gas_limit; // The bundle is simulated on top of the state block, so default to the next block's base // fee. @@ -168,8 +171,15 @@ where // use the block number of the request evm_env.block_env.inner_mut().number = U256::from(block_number); + let permit = self + .eth_api() + .acquire_owned_blocking_io() + .await + .map_err(|_| EthApiError::InternalEthError)?; + self.eth_api() .spawn_with_state_at_block(at, move |eth_api, db| { + let _permit = permit; let coinbase = evm_env.block_env.beneficiary(); let basefee = evm_env.block_env.basefee(); @@ -178,9 +188,13 @@ where .map_err(Eth::Error::from_eth_err)? .map(|acc| acc.balance) .unwrap_or_default(); + let separate_block_gas = evm_env.cfg_env.enable_amsterdam_eip8037; + let tx_gas_limit_cap = evm_env.cfg_env.tx_gas_limit_cap(); let mut coinbase_balance_before_tx = initial_coinbase; let mut coinbase_balance_after_tx = initial_coinbase; let mut total_gas_used = 0u64; + let mut regular_gas_used = 0u64; + let mut state_gas_used = 0u64; let mut total_gas_fees = U256::ZERO; let mut hasher = Keccak256::new(); @@ -190,6 +204,26 @@ where let mut transactions = transactions.into_iter().peekable(); while let Some(tx) = transactions.next() { + if is_cancelled() { + return Err(EthApiError::InternalEthError.into()) + } + + // Admit each transaction against the gas left in the block, like the block + // executor does: EIP-8037 keeps separate regular and state gas pools. + let regular_limit = tx.gas_limit().min(tx_gas_limit_cap); + let exceeds_block_gas = if separate_block_gas { + regular_limit > block_gas_limit.saturating_sub(regular_gas_used) || + tx.gas_limit() > block_gas_limit.saturating_sub(state_gas_used) + } else { + regular_limit > block_gas_limit.saturating_sub(total_gas_used) + }; + if exceeds_block_gas { + return Err(EthApiError::InvalidParams( + EthBundleError::BundleGasLimitExceeded(block_gas_limit).to_string(), + ) + .into()) + } + let signer = tx.signer(); let tx = { let mut tx = ::Transaction::from_pooled(tx); @@ -217,6 +251,8 @@ where .expect("fee is always valid; execution succeeded"); let gas_used = result.tx_gas_used(); total_gas_used += gas_used; + regular_gas_used += result.gas().block_regular_gas_used(); + state_gas_used += result.gas().block_state_gas_used(); let gas_fees = U256::from(gas_used) * U256::from(gas_price); total_gas_fees += gas_fees; @@ -330,6 +366,9 @@ pub enum EthBundleError { /// Thrown when the blob gas usage of the blob transactions in a bundle exceed the maximum. #[error("blob gas usage exceeds the limit of {0} gas per block.")] Eip4844BlobGasExceeded(u64), + /// Thrown when the gas limits of the bundle's transactions exceed the block gas limit. + #[error("bundle gas exceeds the block gas limit of {0}")] + BundleGasLimitExceeded(u64), } #[cfg(test)] @@ -350,6 +389,7 @@ mod tests { use reth_provider::{ providers::BlockchainProvider, test_utils::create_test_provider_factory_with_chain_spec, }; + use reth_rpc_eth_api::helpers::SpawnBlocking; use reth_storage_overlay::OverlayManager; use reth_testing_utils::generators::{self, generate_key, sign_tx_with_key_pair}; use reth_transaction_pool::test_utils::testing_pool; @@ -453,6 +493,149 @@ mod tests { } } + /// The bundle's transactions share the block gas limit, and the simulation waits for a + /// blocking IO permit. + #[tokio::test(flavor = "multi_thread")] + async fn call_bundle_shares_gas_limit_and_takes_blocking_io_permit() { + let mut rng = generators::rng(); + let key = generate_key(&mut rng); + let txs: Vec<_> = (0..2) + .map(|nonce| { + sign_tx_with_key_pair( + key, + reth_ethereum_primitives::Transaction::Eip1559(TxEip1559 { + chain_id: 1, + nonce, + gas_limit: 21_000, + max_fee_per_gas: INITIAL_BASE_FEE as u128, + to: TxKind::Call(Address::random()), + ..Default::default() + }), + ) + }) + .collect(); + let sender = txs[0].recover_signer().unwrap(); + + let genesis = Genesis::default() + .with_gas_limit(30_000_000) + .with_base_fee(Some(INITIAL_BASE_FEE as u128)) + .extend_accounts([( + sender, + GenesisAccount::default().with_balance(U256::from(10u128.pow(18))), + )]); + let chain_spec = + Arc::new(ChainSpecBuilder::mainnet().cancun_activated().genesis(genesis).build()); + let factory = create_test_provider_factory_with_chain_spec(chain_spec); + init_genesis(&factory).unwrap(); + let provider = BlockchainProvider::new(factory).unwrap(); + + let eth_api = EthApiBuilder::new( + provider.clone(), + testing_pool(), + NoopNetwork::default(), + EthEvmConfig::new(provider.chain_spec()), + ) + .max_blocking_io_requests(1) + .build(); + let api = EthBundle::new(eth_api.clone(), BlockingTaskGuard::new(4)); + + let bundle = EthCallBundle { + txs: txs.iter().map(|tx| tx.encoded_2718().into()).collect(), + block_number: 1, + state_block_number: BlockNumberOrTag::Number(0), + ..Default::default() + }; + + let error = api + .call_bundle(EthCallBundle { gas_limit: Some(41_999), ..bundle.clone() }) + .await + .unwrap_err(); + assert_eq!( + error.to_string(), + EthApiError::InvalidParams(EthBundleError::BundleGasLimitExceeded(41_999).to_string()) + .to_string() + ); + + let bundle = EthCallBundle { gas_limit: Some(42_000), ..bundle }; + let permit = eth_api.acquire_owned_blocking_io().await.unwrap(); + assert!(tokio::time::timeout( + std::time::Duration::from_millis(200), + api.call_bundle(bundle.clone()) + ) + .await + .is_err()); + + drop(permit); + let response = api.call_bundle(bundle).await.unwrap(); + assert_eq!(response.total_gas_used, 42_000); + } + + /// Under EIP-8037 the bundle admits transactions against separate regular and state gas + /// pools, so a bundle that fits a block is accepted even when its receipt gas does not fit + /// one pool. + #[tokio::test(flavor = "multi_thread")] + async fn call_bundle_uses_separate_gas_pools_after_amsterdam() { + const TX_GAS_LIMIT: u64 = 1_000_000; + + let mut rng = generators::rng(); + let key = generate_key(&mut rng); + // Value transfers to new accounts spend both regular and state gas. + let txs: Vec<_> = (0..2) + .map(|nonce| { + sign_tx_with_key_pair( + key, + reth_ethereum_primitives::Transaction::Eip1559(TxEip1559 { + chain_id: 1, + nonce, + gas_limit: TX_GAS_LIMIT, + max_fee_per_gas: INITIAL_BASE_FEE as u128, + to: TxKind::Call(Address::random()), + value: U256::from(1), + ..Default::default() + }), + ) + }) + .collect(); + let sender = txs[0].recover_signer().unwrap(); + + let genesis = Genesis::default() + .with_gas_limit(30_000_000) + .with_base_fee(Some(INITIAL_BASE_FEE as u128)) + .extend_accounts([( + sender, + GenesisAccount::default().with_balance(U256::from(10u128.pow(18))), + )]); + let chain_spec = + Arc::new(ChainSpecBuilder::mainnet().amsterdam_activated().genesis(genesis).build()); + let factory = create_test_provider_factory_with_chain_spec(chain_spec); + init_genesis(&factory).unwrap(); + let provider = BlockchainProvider::new(factory).unwrap(); + + let eth_api = EthApiBuilder::new( + provider.clone(), + testing_pool(), + NoopNetwork::default(), + EthEvmConfig::new(provider.chain_spec()), + ) + .build(); + let api = EthBundle::new(eth_api, BlockingTaskGuard::new(4)); + + let bundle = EthCallBundle { + txs: txs.iter().map(|tx| tx.encoded_2718().into()).collect(), + block_number: 1, + state_block_number: BlockNumberOrTag::Number(0), + ..Default::default() + }; + let first_gas_used = api.call_bundle(bundle.clone()).await.unwrap().results[0].gas_used; + + // The second transaction fits both pools, but not one pool holding the first + // transaction's regular and state gas together. + let gas_limit = TX_GAS_LIMIT + first_gas_used - 1; + let response = + api.call_bundle(EthCallBundle { gas_limit: Some(gas_limit), ..bundle }).await.unwrap(); + assert_eq!(response.results.len(), 2); + } + fn assert_bundle_fees(response: &EthCallBundleResponse, expected_tip: u128) { assert_eq!(response.results.len(), 1); assert_eq!(response.total_gas_used, 21_000); From b6dcf2d7e80cccd086b0216312bdcf803554fe9e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 Nov 2026 09:34:01 +0100 Subject: [PATCH 06/11] fix(rpc): bound eth_callMany, trace_callMany and debug_traceCallMany (#28314) Adapted to revm 43 / alloy-evm 0.39, which this base still runs instead of evm2. The limits, error variants, messages and codes are upstream's. - `CallManyBudget::admit` takes the call's `TransactionEnvMut` tx env directly; the base's call env is not wrapped in `Recovered`. - `CallManyBudget::charge` takes revm's `ExecutionResult`. evm2's "out of gas, except MemoryLimitOOG" check becomes a halt with any `OutOfGasError` except `MemoryLimit`, matched through `H::from(HaltReason)` so it also covers wrapping halt reasons such as op-revm's. - The base's trace_callMany and debug_traceCallMany pass the tx env by value and an `&mut` inspector, so the budget calls wrap those `inspect` calls. - The budget's unit tests build revm `TxEnv`s and `ExecutionResult`s instead of evm2 envelopes and `TxResultExt`s; the core.rs test imports TraceType, BlockingTaskGuard and Runtime, which upstream's test module got through imports this base lacks. Not picked: 13a59de4ba (#27462) is not needed by this bound; it is in this stack only for #28253's cancellation check. (cherry picked from commit faf670e16dedad9786ba0faee00aee123133d176) --- crates/node/core/src/args/rpc_server.rs | 2 + crates/rpc/rpc-eth-api/src/core.rs | 7 +- crates/rpc/rpc-eth-api/src/helpers/call.rs | 20 +- crates/rpc/rpc-eth-types/src/error/mod.rs | 14 +- crates/rpc/rpc-eth-types/src/utils.rs | 206 ++++++++++++++++++- crates/rpc/rpc-server-types/src/constants.rs | 9 + crates/rpc/rpc/src/debug.rs | 16 +- crates/rpc/rpc/src/eth/core.rs | 109 +++++++++- crates/rpc/rpc/src/trace.rs | 11 +- docs/vocs/docs/pages/cli/reth/node.mdx | 4 +- 10 files changed, 372 insertions(+), 26 deletions(-) diff --git a/crates/node/core/src/args/rpc_server.rs b/crates/node/core/src/args/rpc_server.rs index 86d01bf434c..eda9ae6f9ad 100644 --- a/crates/node/core/src/args/rpc_server.rs +++ b/crates/node/core/src/args/rpc_server.rs @@ -637,6 +637,8 @@ pub struct RpcServerArgs { pub rpc_max_logs_per_response: ZeroAsNoneU64, /// Maximum gas limit for `eth_call`, `eth_estimateGas`, and call tracing RPC methods. + /// + /// The calls of an `eth_callMany`, `trace_callMany` or `debug_traceCallMany` request share it. #[arg( long = "rpc.gascap", alias = "rpc-gascap", diff --git a/crates/rpc/rpc-eth-api/src/core.rs b/crates/rpc/rpc-eth-api/src/core.rs index 78a6c90fcb7..06f86edb4fa 100644 --- a/crates/rpc/rpc-eth-api/src/core.rs +++ b/crates/rpc/rpc-eth-api/src/core.rs @@ -265,8 +265,11 @@ pub trait EthApi< #[method(name = "fillTransaction")] async fn fill_transaction(&self, request: TxReq) -> RpcResult>; - /// Simulate arbitrary number of transactions at an arbitrary blockchain index, with the - /// optionality of state overrides + /// Simulate transactions at an arbitrary blockchain index, with the optionality of state + /// overrides. + /// + /// The request is bounded by [`CallManyBudget`](reth_rpc_eth_types::utils::CallManyBudget): + /// its calls share the RPC gas cap. #[method(name = "callMany")] async fn call_many( &self, diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 33ee262f224..270da6c37c2 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -41,6 +41,7 @@ use reth_rpc_eth_types::{ cache::db::attach_bal_before_tx, error::{AsEthApiError, FromEthApiError}, simulate::{self, EthSimulateError}, + utils::CallManyBudget, EthApiError, StateCacheDb, }; use reth_storage_api::{ @@ -319,8 +320,10 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA } } - /// Simulate arbitrary number of transactions at an arbitrary blockchain index, with the - /// optionality of state overrides + /// Simulate transactions at an arbitrary blockchain index, with the optionality of state + /// overrides. + /// + /// The request is bounded by [`CallManyBudget`]: its calls share the RPC gas cap. fn call_many( &self, bundles: Vec::Network>>>, @@ -335,6 +338,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA if bundles.is_empty() { return Err(EthApiError::InvalidParams(String::from("bundles are empty.")).into()); } + let mut budget = CallManyBudget::for_bundles(&bundles, self.call_gas_limit())?; let permit = self .acquire_owned_blocking_io() @@ -415,7 +419,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA let overrides = EvmOverrides::new(state_override.take(), block_overrides.clone()); - let (current_evm_env, prepared_tx) = this + let (current_evm_env, mut prepared_tx) = this .prepare_call_env(evm_env.clone(), tx, &mut db, overrides) .map_err(|err| { Self::Error::from_eth_err(EthApiError::call_many_error( @@ -424,15 +428,23 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA err.into(), )) })?; + budget.admit(&mut prepared_tx)?; let res = this.transact(&mut db, current_evm_env, prepared_tx).map_err( |err| { Self::Error::from_eth_err(EthApiError::call_many_error( bundle_index, tx_index, - err.into(), + budget.map_call_err(err).into(), )) }, )?; + budget.charge(&res.result).map_err(|err| { + Self::Error::from_eth_err(EthApiError::call_many_error( + bundle_index, + tx_index, + err.into(), + )) + })?; match Self::Error::ensure_success(res.result) { Ok(output) => { diff --git a/crates/rpc/rpc-eth-types/src/error/mod.rs b/crates/rpc/rpc-eth-types/src/error/mod.rs index 2049db7cb68..c507b4f5f78 100644 --- a/crates/rpc/rpc-eth-types/src/error/mod.rs +++ b/crates/rpc/rpc-eth-types/src/error/mod.rs @@ -224,6 +224,10 @@ pub enum EthApiError { /// Error thrown when trying to access block access list for blocks before Amsterdam #[error("Block access list not available for pre-Amsterdam blocks")] BlockAccessListNotAvailablePreAmsterdam, + /// Thrown when the calls of an `eth_callMany`, `trace_callMany` or `debug_traceCallMany` + /// request need more gas than the RPC gas cap they share. + #[error("the request's calls exceed the RPC gas cap of {0}")] + CallManyGasCapExceeded(u64), /// Any other error #[error("{0}")] Other(Box), @@ -351,10 +355,12 @@ impl From for jsonrpsee_types::error::ErrorObject<'static> { EthApiError::Unsupported(msg) => internal_rpc_err(msg), EthApiError::InternalJsTracerError(msg) => internal_rpc_err(msg), EthApiError::InvalidParams(msg) => invalid_params_rpc_err(msg), - err @ EthApiError::ExecutionTimedOut(_) => rpc_error_with_code( - jsonrpsee_types::error::CALL_EXECUTION_FAILED_CODE, - err.to_string(), - ), + err @ (EthApiError::ExecutionTimedOut(_) | EthApiError::CallManyGasCapExceeded(_)) => { + rpc_error_with_code( + jsonrpsee_types::error::CALL_EXECUTION_FAILED_CODE, + err.to_string(), + ) + } err @ (EthApiError::InternalBlockingTaskError | EthApiError::InternalEthError) => { internal_rpc_err(err.to_string()) } diff --git a/crates/rpc/rpc-eth-types/src/utils.rs b/crates/rpc/rpc-eth-types/src/utils.rs index fee5d4f24f8..6ac5ddbade6 100644 --- a/crates/rpc/rpc-eth-types/src/utils.rs +++ b/crates/rpc/rpc-eth-types/src/utils.rs @@ -1,9 +1,16 @@ //! Commonly used code snippets -use super::{EthApiError, EthResult}; +use super::{ + error::{AsEthApiError, FromEthApiError}, + EthApiError, EthResult, +}; use alloy_consensus::TxReceipt; +use alloy_rpc_types_eth::Bundle; +use reth_evm::TransactionEnvMut; use reth_primitives_traits::{Recovered, SignedTransaction}; -use std::future::Future; +use reth_rpc_server_types::constants::{DEFAULT_CALL_MANY_TIMEOUT, DEFAULT_MAX_CALL_MANY_CALLS}; +use revm::context_interface::result::{ExecutionResult, HaltReason, HaltReasonTr, OutOfGasError}; +use std::{future::Future, time::Instant}; /// Calculates the gas used and next log index for a transaction at the given index pub fn calculate_gas_used_and_next_log_index( @@ -97,9 +104,204 @@ pub fn checked_blob_gas_used_ratio(blob_gas_used: u64, max_blob_gas_per_block: u } } +/// Bounds the work of one `eth_callMany`, `trace_callMany` or `debug_traceCallMany` request. +/// +/// The request may carry at most [`DEFAULT_MAX_CALL_MANY_CALLS`] calls, all of its calls share +/// one gas budget of the RPC gas cap, and no call starts more than [`DEFAULT_CALL_MANY_TIMEOUT`] +/// after the first one. +#[derive(Debug)] +pub struct CallManyBudget { + /// The RPC gas cap the calls share, reported once they exceed it. + gas_cap: u64, + /// Gas left for the remaining calls, `None` if the RPC gas cap is disabled. + remaining_gas: Option, + /// No call starts after this instant. Set when the first call is admitted, so waiting for a + /// permit or replaying the block's transactions does not count. + deadline: Option, + /// Whether [`Self::admit`] lowered the gas limit of the last admitted call. + capped: bool, +} + +impl CallManyBudget { + /// Creates the budget for a request of `calls` calls that share `gas_cap` gas. + /// + /// Returns an error if `calls` exceeds [`DEFAULT_MAX_CALL_MANY_CALLS`]. A `gas_cap` of `0` + /// disables the gas budget, as it disables the RPC gas cap. + pub fn new(calls: usize, gas_cap: u64) -> Result { + if calls > DEFAULT_MAX_CALL_MANY_CALLS { + return Err(EthApiError::InvalidParams(format!( + "call count {calls} exceeds limit {DEFAULT_MAX_CALL_MANY_CALLS}" + ))) + } + Ok(Self { + gas_cap, + remaining_gas: (gas_cap != 0).then_some(gas_cap), + deadline: None, + capped: false, + }) + } + + /// Creates the budget for a request of `bundles`, see [`Self::new`]. + /// + /// An empty bundle counts as one call, so requests of empty bundles are bounded too. + pub fn for_bundles(bundles: &[Bundle], gas_cap: u64) -> Result { + Self::new(bundles.iter().map(|bundle| bundle.transactions.len().max(1)).sum(), gas_cap) + } + + /// Admits the request's next call, lowering the gas limit of `tx_env` to the gas left for the + /// request's remaining calls. + /// + /// The first admitted call starts the request's timeout. Returns + /// [`EthApiError::ExecutionTimedOut`] once it has expired. + pub fn admit(&mut self, tx_env: &mut T) -> Result<(), EthApiError> { + let now = Instant::now(); + if now >= *self.deadline.get_or_insert(now + DEFAULT_CALL_MANY_TIMEOUT) { + return Err(EthApiError::ExecutionTimedOut(DEFAULT_CALL_MANY_TIMEOUT)) + } + self.capped = false; + if let Some(remaining) = self.remaining_gas && + tx_env.gas_limit() > remaining + { + tx_env.set_gas_limit(remaining); + self.capped = true; + } + Ok(()) + } + + /// Returns [`EthApiError::CallManyGasCapExceeded`] in place of `err` if the last admitted call + /// failed its intrinsic gas check because [`Self::admit`] lowered its gas limit. + pub fn map_call_err(&self, err: E) -> E { + if self.capped && err.is_gas_too_low() { + return E::from_eth_err(EthApiError::CallManyGasCapExceeded(self.gas_cap)) + } + err + } + + /// Charges the gas the last admitted call used against the budget. + /// + /// Returns [`EthApiError::CallManyGasCapExceeded`] if the call ran out of gas after + /// [`Self::admit`] lowered its gas limit, and an internal error if it used more gas than is + /// left, which an admitted call cannot do. + pub fn charge( + &mut self, + result: &ExecutionResult, + ) -> Result<(), EthApiError> { + if self.capped && is_out_of_gas_below_memory_limit(result) { + return Err(EthApiError::CallManyGasCapExceeded(self.gas_cap)) + } + if let Some(remaining) = &mut self.remaining_gas { + *remaining = + remaining.checked_sub(result.tx_gas_used()).ok_or(EthApiError::InternalEthError)?; + } + Ok(()) + } +} + +/// Returns whether `result` halted out of gas for any reason other than exceeding the memory +/// limit. +fn is_out_of_gas_below_memory_limit(result: &ExecutionResult) -> bool { + let ExecutionResult::Halt { reason, .. } = result else { return false }; + [ + OutOfGasError::Basic, + OutOfGasError::Memory, + OutOfGasError::Precompile, + OutOfGasError::InvalidOperand, + OutOfGasError::ReentrancySentry, + ] + .into_iter() + .any(|oog| *reason == H::from(HaltReason::OutOfGas(oog))) +} + #[cfg(test)] mod tests { use super::*; + use revm::context::{ + result::{Output, ResultGas, SuccessReason}, + Transaction, TxEnv, + }; + + #[test] + fn call_many_budget_limits_calls() { + assert!(CallManyBudget::new(DEFAULT_MAX_CALL_MANY_CALLS, 0).is_ok()); + + let empty = Bundle::<()> { transactions: Vec::new(), block_override: None }; + let full = + Bundle { transactions: vec![(); DEFAULT_MAX_CALL_MANY_CALLS], block_override: None }; + assert!(CallManyBudget::for_bundles(&vec![empty.clone(); DEFAULT_MAX_CALL_MANY_CALLS], 0) + .is_ok()); + assert_eq!( + CallManyBudget::for_bundles(&vec![empty.clone(); DEFAULT_MAX_CALL_MANY_CALLS + 1], 0) + .unwrap_err() + .to_string(), + "call count 1025 exceeds limit 1024" + ); + assert_eq!( + CallManyBudget::for_bundles(&[full, empty], 0).unwrap_err().to_string(), + "call count 1025 exceeds limit 1024" + ); + } + + fn tx_env(gas_limit: u64) -> TxEnv { + TxEnv { gas_limit, ..Default::default() } + } + + /// A successful result, or a halt with `halt` as its reason. + fn tx_result(gas_used: u64, halt: Option) -> ExecutionResult { + let gas = ResultGas::default().with_total_gas_spent(gas_used); + match halt { + Some(reason) => ExecutionResult::Halt { reason, gas, logs: Vec::new() }, + None => ExecutionResult::Success { + reason: SuccessReason::Stop, + gas, + logs: Vec::new(), + output: Output::Call(Default::default()), + }, + } + } + + #[test] + fn call_many_budget_shares_gas_cap() { + let admitted = |budget: &mut CallManyBudget, gas_limit| { + let mut tx_env = tx_env(gas_limit); + budget.admit(&mut tx_env).unwrap(); + tx_env.gas_limit() + }; + + let mut budget = CallManyBudget::new(3, 50_000).unwrap(); + assert_eq!(admitted(&mut budget, 30_000), 30_000); + // A call that was not capped keeps its own out-of-gas result. + budget + .charge(&tx_result(30_000, Some(HaltReason::OutOfGas(OutOfGasError::Basic)))) + .unwrap(); + assert_eq!(admitted(&mut budget, 30_000_000), 20_000); + assert!(matches!( + budget.charge(&tx_result(20_000, Some(HaltReason::OutOfGas(OutOfGasError::Basic)))), + Err(EthApiError::CallManyGasCapExceeded(50_000)) + )); + assert_eq!(admitted(&mut budget, 10_000), 10_000); + assert!(matches!( + budget.charge(&tx_result(20_001, None)), + Err(EthApiError::InternalEthError) + )); + + let mut unlimited = CallManyBudget::new(3, 0).unwrap(); + unlimited.charge(&tx_result(u64::MAX, None)).unwrap(); + assert_eq!(admitted(&mut unlimited, u64::MAX), u64::MAX); + } + + #[test] + fn call_many_budget_times_out() { + let mut budget = CallManyBudget::new(2, 0).unwrap(); + assert_eq!(budget.deadline, None); + budget.admit(&mut tx_env(21_000)).unwrap(); + assert!(budget.deadline.is_some()); + + budget.deadline = Some(Instant::now()); + assert!(matches!( + budget.admit(&mut tx_env(21_000)), + Err(EthApiError::ExecutionTimedOut(timeout)) if timeout == DEFAULT_CALL_MANY_TIMEOUT + )); + } #[tokio::test] async fn test_binary_search() { diff --git a/crates/rpc/rpc-server-types/src/constants.rs b/crates/rpc/rpc-server-types/src/constants.rs index 750b37f8cf2..b9780a30c08 100644 --- a/crates/rpc/rpc-server-types/src/constants.rs +++ b/crates/rpc/rpc-server-types/src/constants.rs @@ -55,6 +55,15 @@ pub const DEFAULT_ENGINE_API_IPC_ENDPOINT: &str = "/tmp/reth_engine_api.ipc"; /// The default limit for blocks count in `eth_simulateV1`. pub const DEFAULT_MAX_SIMULATE_BLOCKS: u64 = 256; +/// The default maximum number of calls in a single `eth_callMany`, `trace_callMany` or +/// `debug_traceCallMany` request. +pub const DEFAULT_MAX_CALL_MANY_CALLS: usize = 1024; + +/// The default timeout of an `eth_callMany`, `trace_callMany` or `debug_traceCallMany` request, +/// matching geth's `eth_call` timeout. It starts with the request's first call, and no call +/// starts after it expires. +pub const DEFAULT_CALL_MANY_TIMEOUT: Duration = Duration::from_secs(5); + /// The default maximum number of total storage slots for `eth_getStorageValues`. pub const DEFAULT_MAX_STORAGE_VALUES_SLOTS: usize = 1024; diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 82a54bf6321..8d15dc267f2 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -35,7 +35,7 @@ use reth_rpc_eth_api::{ helpers::{EthTransactions, TraceExt}, AsEthApiError, FromEthApiError, RpcConvert, RpcNodeCore, }; -use reth_rpc_eth_types::{EthApiError, StateCacheDb}; +use reth_rpc_eth_types::{utils::CallManyBudget, EthApiError, StateCacheDb}; use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult}; use reth_storage_api::{ BlockIdReader, BlockReaderIdExt, HashedPostStateProvider, HeaderProvider, ProviderBlock, @@ -497,6 +497,7 @@ where if bundles.is_empty() { return Err(EthApiError::InvalidParams(String::from("bundles are empty.")).into()) } + let mut budget = CallManyBudget::for_bundles(&bundles, self.eth_api().call_gas_limit())?; let StateContext { transaction_index, block_number } = state_context.unwrap_or_default(); let transaction_index = transaction_index.unwrap_or_default(); @@ -562,15 +563,14 @@ where let state_overrides = state_overrides.take(); let overrides = EvmOverrides::new(state_overrides, block_overrides.clone()); - let (evm_env, tx_env) = + let (evm_env, mut tx_env) = eth_api.prepare_call_env(evm_env.clone(), tx, &mut db, overrides)?; + budget.admit(&mut tx_env)?; - let res = eth_api.inspect( - &mut db, - evm_env.clone(), - tx_env.clone(), - &mut inspector, - )?; + let res = eth_api + .inspect(&mut db, evm_env.clone(), tx_env.clone(), &mut inspector) + .map_err(|err| budget.map_call_err(err))?; + budget.charge(&res.result)?; let trace = inspector .get_result(None, &tx_env, &evm_env.block_env, &res, &mut db) .map_err(Eth::Error::from_eth_err)?; diff --git a/crates/rpc/rpc/src/eth/core.rs b/crates/rpc/rpc/src/eth/core.rs index 6082442a3f8..38b694dbb3c 100644 --- a/crates/rpc/rpc/src/eth/core.rs +++ b/crates/rpc/rpc/src/eth/core.rs @@ -558,6 +558,7 @@ mod tests { state::{AccountOverride, EvmOverrides, StateOverride}, Bundle, TransactionRequest, }; + use alloy_rpc_types_trace::parity::TraceType; use jsonrpsee_types::error::INVALID_PARAMS_CODE; use rand::Rng; use reth_chain_state::CanonStateSubscriptions; @@ -574,11 +575,12 @@ mod tests { node::RpcNodeCoreAdapter, EthApiServer, }; - use reth_rpc_eth_types::RpcInvalidTransactionError; + use reth_rpc_eth_types::{EthConfig, RpcInvalidTransactionError}; + use reth_rpc_server_types::constants::DEFAULT_MAX_CALL_MANY_CALLS; use reth_storage_api::{ BalProvider, BlockReader, BlockReaderIdExt, NodePrimitivesProvider, StateProviderFactory, }; - use reth_tasks::cancel::is_cancelled; + use reth_tasks::{cancel::is_cancelled, pool::BlockingTaskGuard, Runtime}; use reth_testing_utils::generators; use reth_transaction_pool::test_utils::{testing_pool, TestPool}; use std::time::{Duration, Instant}; @@ -1040,6 +1042,109 @@ mod tests { ); } + #[tokio::test] + async fn call_many_methods_limit_calls_and_share_gas_cap() { + let provider = MockEthProvider::default(); + let header = Header { number: 1, gas_limit: 30_000_000, ..Default::default() }; + provider.add_block(header.hash_slow(), Block { header, body: BlockBody::default() }); + // Loops until it runs out of gas. + let looping = Address::with_last_byte(0x43); + provider.add_account( + looping, + ExtendedAccount::new(0, U256::ZERO).with_bytecode(alloy_primitives::bytes!("5b600056")), + ); + // A transfer to an empty account uses 21_000 gas, so a third one only gets 8_000 and a + // loop after one transfer runs out of the 29_000 left. + let api = build_test_eth_api_with_gas_cap(provider, 50_000); + let trace_api = + crate::TraceApi::new(api.clone(), BlockingTaskGuard::new(1), EthConfig::default()); + let debug_api = crate::DebugApi::new( + api.clone(), + BlockingTaskGuard::new(1), + &Runtime::test(), + futures::stream::empty(), + ); + let transfer = TransactionRequest::default().to(Address::with_last_byte(0x42)); + let transfers = |calls| vec![transfer.clone(); calls]; + let transfer_and_loop = vec![transfer.clone(), TransactionRequest::default().to(looping)]; + let bundles = |calls: Vec| { + vec![Bundle { transactions: calls, block_override: None }] + }; + let trace_calls = |calls: Vec| { + calls + .into_iter() + .map(|call| (call, std::iter::once(TraceType::Trace).collect())) + .collect::>() + }; + + assert_eq!( + EthCall::call_many(&api, bundles(transfers(2)), None, None).await.unwrap()[0].len(), + 2 + ); + assert_eq!( + trace_api.trace_call_many(trace_calls(transfers(2)), None).await.unwrap().len(), + 2 + ); + assert_eq!( + debug_api.debug_trace_call_many(bundles(transfers(2)), None, None).await.unwrap()[0] + .len(), + 2 + ); + + let gas_cap_err = "the request's calls exceed the RPC gas cap of 50000"; + for (calls, tx_index) in [(transfers(3), 2), (transfer_and_loop, 1)] { + assert_eq!( + EthCall::call_many(&api, bundles(calls.clone()), None, None) + .await + .unwrap_err() + .to_string(), + format!("call_many error in bundle 0 and transaction {tx_index}: {gas_cap_err}") + ); + assert_eq!( + trace_api + .trace_call_many(trace_calls(calls.clone()), None) + .await + .unwrap_err() + .to_string(), + gas_cap_err + ); + assert_eq!( + debug_api + .debug_trace_call_many(bundles(calls), None, None) + .await + .unwrap_err() + .to_string(), + gas_cap_err + ); + } + + let too_many = transfers(DEFAULT_MAX_CALL_MANY_CALLS + 1); + let limit_err = "call count 1025 exceeds limit 1024"; + assert_eq!( + EthCall::call_many(&api, bundles(too_many.clone()), None, None) + .await + .unwrap_err() + .to_string(), + limit_err + ); + assert_eq!( + trace_api + .trace_call_many(trace_calls(too_many.clone()), None) + .await + .unwrap_err() + .to_string(), + limit_err + ); + assert_eq!( + debug_api + .debug_trace_call_many(bundles(too_many), None, None) + .await + .unwrap_err() + .to_string(), + limit_err + ); + } + #[tokio::test] /// Requesting no block should result in a default response async fn test_fee_history_no_block_requested() { diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index b95b3c95c38..4e7405f0ef8 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -26,7 +26,7 @@ use reth_rpc_eth_api::{ helpers::{Call, LoadPendingBlock, LoadTransaction, Trace, TraceExt}, FromEthApiError, RpcNodeCore, }; -use reth_rpc_eth_types::{error::EthApiError, EthConfig}; +use reth_rpc_eth_types::{error::EthApiError, utils::CallManyBudget, EthConfig}; use reth_storage_api::{BlockNumReader, BlockReader}; use reth_tasks::{cancel::is_cancelled, pool::BlockingTaskGuard}; use reth_transaction_pool::{PoolPooledTx, PoolTransaction, TransactionPool}; @@ -154,6 +154,7 @@ where calls: Vec<(RpcTxReq, HashSet)>, block_id: Option, ) -> Result, Eth::Error> { + let mut budget = CallManyBudget::new(calls.len(), self.eth_api().call_gas_limit())?; let at = block_id.unwrap_or_default(); let (evm_env, at) = self.eth_api().evm_env_at(at).await?; @@ -167,15 +168,19 @@ where if is_cancelled() { return Err(EthApiError::InternalEthError.into()) } - let (evm_env, tx_env) = eth_api.prepare_call_env( + let (evm_env, mut tx_env) = eth_api.prepare_call_env( evm_env.clone(), call, &mut db, Default::default(), )?; + budget.admit(&mut tx_env)?; let config = TracingInspectorConfig::from_parity_config(&trace_types); let mut inspector = TracingInspector::new(config); - let res = eth_api.inspect(&mut db, evm_env, tx_env, &mut inspector)?; + let res = eth_api + .inspect(&mut db, evm_env, tx_env, &mut inspector) + .map_err(|err| budget.map_call_err(err))?; + budget.charge(&res.result)?; let trace_res = inspector .into_parity_builder() diff --git a/docs/vocs/docs/pages/cli/reth/node.mdx b/docs/vocs/docs/pages/cli/reth/node.mdx index 29c42d9925f..dbe3487dce3 100644 --- a/docs/vocs/docs/pages/cli/reth/node.mdx +++ b/docs/vocs/docs/pages/cli/reth/node.mdx @@ -486,7 +486,9 @@ RPC: [default: 20000] --rpc.gascap - Maximum gas limit for `eth_call`, `eth_estimateGas`, and call tracing RPC methods + Maximum gas limit for `eth_call`, `eth_estimateGas`, and call tracing RPC methods. + + The calls of an `eth_callMany`, `trace_callMany` or `debug_traceCallMany` request share it. [default: 50000000] From 067560768dd68bdfcd05dfcf523c84a8eae80bab Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 Nov 2026 10:15:03 +0100 Subject: [PATCH 07/11] fix(rpc): cap eth_getProof and eth_getMultiProof key counts (#27716) Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit b5dc94ca372e337d057911cdb4772ea58104bcbe) --- crates/rpc/rpc-eth-api/src/helpers/state.rs | 28 ++++++++++- crates/rpc/rpc-server-types/src/constants.rs | 7 +++ crates/rpc/rpc/src/eth/helpers/state.rs | 52 ++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc-eth-api/src/helpers/state.rs b/crates/rpc/rpc-eth-api/src/helpers/state.rs index ecf933eacac..ff6f9bffa0b 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/state.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/state.rs @@ -19,7 +19,7 @@ use reth_rpc_eth_types::{ error::{FromEvmError, IntoEthApiError}, EthApiError, PendingBlockEnv, RpcInvalidTransactionError, SignError, }; -use reth_rpc_server_types::constants::DEFAULT_MAX_STORAGE_VALUES_SLOTS; +use reth_rpc_server_types::constants::{DEFAULT_MAX_PROOF_SLOTS, DEFAULT_MAX_STORAGE_VALUES_SLOTS}; use reth_storage_api::{ BlockIdReader, BlockReaderIdExt, StateProvider, StateProviderBox, StateProviderFactory, }; @@ -152,6 +152,9 @@ pub trait EthState: LoadState + SpawnBlocking { } /// Returns values stored of given account, with Merkle-proof, at given blocknumber. + /// + /// Enforces a cap of [`DEFAULT_MAX_PROOF_SLOTS`] on the number of requested keys, before a + /// proof permit is acquired. fn get_proof( &self, address: Address, @@ -164,6 +167,13 @@ pub trait EthState: LoadState + SpawnBlocking { where Self: EthApiSpec, { + if keys.len() > DEFAULT_MAX_PROOF_SLOTS { + return Err(Self::Error::from_eth_err(EthApiError::InvalidParams(format!( + "storage key count {} exceeds limit {DEFAULT_MAX_PROOF_SLOTS}", + keys.len(), + )))); + } + Ok(async move { let permit = self .acquire_owned_tracing() @@ -187,6 +197,9 @@ pub trait EthState: LoadState + SpawnBlocking { } /// Returns account and storage proofs for multiple targets at the given block number. + /// + /// Enforces a cap of [`DEFAULT_MAX_PROOF_SLOTS`] on both the number of targets and the total + /// slot count across all targets, before a proof permit is acquired. fn get_multi_proof( &self, targets: Vec<(Address, Vec)>, @@ -198,6 +211,19 @@ pub trait EthState: LoadState + SpawnBlocking { where Self: EthApiSpec, { + if targets.len() > DEFAULT_MAX_PROOF_SLOTS { + return Err(Self::Error::from_eth_err(EthApiError::InvalidParams(format!( + "target count {} exceeds limit {DEFAULT_MAX_PROOF_SLOTS}", + targets.len(), + )))); + } + let total_slots: usize = targets.iter().map(|(_, slots)| slots.len()).sum(); + if total_slots > DEFAULT_MAX_PROOF_SLOTS { + return Err(Self::Error::from_eth_err(EthApiError::InvalidParams(format!( + "total slot count {total_slots} exceeds limit {DEFAULT_MAX_PROOF_SLOTS}", + )))); + } + Ok(async move { let permit = self .acquire_owned_tracing() diff --git a/crates/rpc/rpc-server-types/src/constants.rs b/crates/rpc/rpc-server-types/src/constants.rs index b9780a30c08..4579b0b0e11 100644 --- a/crates/rpc/rpc-server-types/src/constants.rs +++ b/crates/rpc/rpc-server-types/src/constants.rs @@ -67,6 +67,13 @@ pub const DEFAULT_CALL_MANY_TIMEOUT: Duration = Duration::from_secs(5); /// The default maximum number of total storage slots for `eth_getStorageValues`. pub const DEFAULT_MAX_STORAGE_VALUES_SLOTS: usize = 1024; +/// The default maximum number of storage slots (and, for multi-proofs, targets) accepted by a +/// single `eth_getProof` or multi-proof request. +/// +/// Bounds the work a request holds a proof permit for, since proof node selection scales with +/// the number of requested slots. +pub const DEFAULT_MAX_PROOF_SLOTS: usize = 1024; + /// The default eth historical proof window. pub const DEFAULT_ETH_PROOF_WINDOW: u64 = 0; diff --git a/crates/rpc/rpc/src/eth/helpers/state.rs b/crates/rpc/rpc/src/eth/helpers/state.rs index ee30428095b..691096944cb 100644 --- a/crates/rpc/rpc/src/eth/helpers/state.rs +++ b/crates/rpc/rpc/src/eth/helpers/state.rs @@ -35,6 +35,7 @@ mod tests { Address, StorageKey, StorageValue, B256, U256, }; use alloy_rpc_types_eth::TransactionRequest; + use alloy_serde::JsonStorageKey; use reth_chainspec::ChainSpec; use reth_ethereum_primitives::Block; use reth_evm_ethereum::EthEvmConfig; @@ -49,6 +50,7 @@ mod tests { EthApiTypes, }; use reth_rpc_eth_types::{EthApiSettings, EthStateCache, PendingBlock}; + use reth_rpc_server_types::constants::DEFAULT_MAX_PROOF_SLOTS; use reth_storage_api::{StateProviderBox, StateProviderFactory}; use reth_tasks::{ pool::{BlockingTaskGuard, BlockingTaskPool}, @@ -116,6 +118,56 @@ mod tests { assert!(account.is_none()); } + #[test] + fn test_get_proof_key_cap() { + let eth_api = noop_eth_api(); + let address = Address::random(); + let keys = |len| vec![JsonStorageKey::from(B256::ZERO); len]; + + assert!(eth_api.get_proof(address, keys(DEFAULT_MAX_PROOF_SLOTS), None).is_ok()); + + let Err(err) = eth_api.get_proof(address, keys(DEFAULT_MAX_PROOF_SLOTS + 1), None) else { + panic!("expected over-limit request to be rejected"); + }; + assert_eq!(err.to_string(), "storage key count 1025 exceeds limit 1024"); + } + + #[test] + fn test_get_multi_proof_cap() { + let eth_api = noop_eth_api(); + let address = Address::random(); + + // Exactly at the limit, on both axes. + let targets = vec![(address, vec![B256::ZERO; DEFAULT_MAX_PROOF_SLOTS])]; + assert!(eth_api.get_multi_proof(targets, None).is_ok()); + let targets = vec![(address, Vec::new()); DEFAULT_MAX_PROOF_SLOTS]; + assert!(eth_api.get_multi_proof(targets, None).is_ok()); + + // Too many slots in a single target. + let targets = vec![(address, vec![B256::ZERO; DEFAULT_MAX_PROOF_SLOTS + 1])]; + let Err(err) = eth_api.get_multi_proof(targets, None) else { + panic!("expected over-limit request to be rejected"); + }; + assert_eq!(err.to_string(), "total slot count 1025 exceeds limit 1024"); + + // Too many slots in total, spread across targets. + let targets = vec![ + (address, vec![B256::ZERO; DEFAULT_MAX_PROOF_SLOTS]), + (Address::random(), vec![B256::ZERO]), + ]; + let Err(err) = eth_api.get_multi_proof(targets, None) else { + panic!("expected over-limit request to be rejected"); + }; + assert_eq!(err.to_string(), "total slot count 1025 exceeds limit 1024"); + + // Too many targets without any slots. + let targets = vec![(address, Vec::new()); DEFAULT_MAX_PROOF_SLOTS + 1]; + let Err(err) = eth_api.get_multi_proof(targets, None) else { + panic!("expected over-limit request to be rejected"); + }; + assert_eq!(err.to_string(), "target count 1025 exceeds limit 1024"); + } + #[test] fn pending_state_and_access_list_do_not_deadlock() { let runtime = tokio::runtime::Builder::new_multi_thread() From 423a3405aeba722b1a2f433496b676101b6f54e6 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:32:17 +0000 Subject: [PATCH 08/11] fix(provider): defer static file truncation on reorg (#28370) Co-authored-by: Claude Opus 5.5 Co-authored-by: Matthias Seitz (cherry picked from commit 9e10691e0e1d14d1d66e0587683bb294fa38f878) --- .../types/src/changeset_offsets.rs | 22 ++++++++- crates/storage/nippy-jar/src/writer.rs | 49 +++++++++++++++++-- .../provider/src/providers/static_file/mod.rs | 13 +++++ .../src/providers/static_file/writer.rs | 31 ++++++++++-- 4 files changed, 106 insertions(+), 9 deletions(-) diff --git a/crates/static-file/types/src/changeset_offsets.rs b/crates/static-file/types/src/changeset_offsets.rs index fe22b8135b4..33d89e2fd70 100644 --- a/crates/static-file/types/src/changeset_offsets.rs +++ b/crates/static-file/types/src/changeset_offsets.rs @@ -17,6 +17,8 @@ pub struct ChangesetOffsetWriter { file: File, /// Number of records written. records_written: u64, + /// Records to keep once the deferred truncation is applied, or `None` if none is pending. + pending_truncation: Option, } impl ChangesetOffsetWriter { @@ -106,11 +108,12 @@ impl ChangesetOffsetWriter { let records_written = committed_len; let file = OpenOptions::new().create(true).append(true).open(path)?; - Ok(Self { file, records_written }) + Ok(Self { file, records_written, pending_truncation: None }) } /// Appends a single changeset offset record. pub fn append(&mut self, offset: &ChangesetOffset) -> io::Result<()> { + self.apply_pending_truncation()?; let mut buf = [0u8; Self::RECORD_SIZE]; buf[..8].copy_from_slice(&offset.offset().to_le_bytes()); buf[8..].copy_from_slice(&offset.num_changes().to_le_bytes()); @@ -140,10 +143,27 @@ impl ChangesetOffsetWriter { pub fn truncate(&mut self, len: u64) -> io::Result<()> { self.file.set_len(len * Self::RECORD_SIZE as u64)?; self.file.sync_all()?; + self.pending_truncation = None; self.records_written = len; Ok(()) } + /// Drops records like [`Self::truncate`], but leaves them on disk until + /// [`Self::apply_pending_truncation`]. + pub const fn truncate_deferred(&mut self, len: u64) { + self.records_written = len; + self.pending_truncation = Some(len); + } + + /// Truncates the file to the records kept by [`Self::truncate_deferred`]. + /// + /// Returns `false` if no truncation was pending. + pub fn apply_pending_truncation(&mut self) -> io::Result { + let Some(len) = self.pending_truncation else { return Ok(false) }; + self.truncate(len)?; + Ok(true) + } + /// Returns the number of records in the file. pub const fn len(&self) -> u64 { self.records_written diff --git a/crates/storage/nippy-jar/src/writer.rs b/crates/storage/nippy-jar/src/writer.rs index 6bcc7bf3831..2d280e1c4f2 100644 --- a/crates/storage/nippy-jar/src/writer.rs +++ b/crates/storage/nippy-jar/src/writer.rs @@ -43,6 +43,13 @@ pub struct NippyJarWriter { column: usize, /// Whether the writer has changed data that needs to be committed. dirty: bool, + pending_truncation: Option, +} + +#[derive(Debug, Clone, Copy)] +struct PendingTruncation { + offsets_len: u64, + data_len: u64, } impl NippyJarWriter { @@ -79,6 +86,7 @@ impl NippyJarWriter { offsets: Vec::with_capacity(1_000_000), column: 0, dirty: false, + pending_truncation: None, }; if !is_created { @@ -197,6 +205,7 @@ impl NippyJarWriter { &mut self, column: Option>>, ) -> Result<(), NippyJarError> { + self.apply_pending_truncation()?; self.dirty = true; match column { @@ -248,6 +257,15 @@ impl NippyJarWriter { /// Prunes rows from data and offsets file and updates its configuration on disk pub fn prune_rows(&mut self, num_rows: usize) -> Result<(), NippyJarError> { + self.prune_rows_deferred(num_rows)?; + self.apply_pending_truncation()?; + Ok(()) + } + + /// Prunes rows like [`Self::prune_rows`], but leaves the committed rows' bytes on disk until + /// [`Self::apply_pending_truncation`]. + pub fn prune_rows_deferred(&mut self, num_rows: usize) -> Result<(), NippyJarError> { + self.apply_pending_truncation()?; self.dirty = true; self.offsets_file.flush()?; @@ -297,8 +315,10 @@ impl NippyJarWriter { // file size = 0). This maintains the same structure as when // a file is initially created. // See `NippyJarWriter::create_or_open_files` for the initial file format. - self.offsets_file.get_mut().set_len(1 + OFFSET_SIZE_BYTES as u64)?; - self.data_file.get_mut().set_len(0)?; + self.pending_truncation = Some(PendingTruncation { + offsets_len: 1 + OFFSET_SIZE_BYTES as u64, + data_len: 0, + }); } else { // Calculate the new length for the on-disk offset list let new_len = 1 + new_num_offsets * OFFSET_SIZE_BYTES as u64; @@ -311,8 +331,8 @@ impl NippyJarWriter { let last_offset = u64::from_le_bytes(last_offset); // Update the lengths of both the offsets and data files - self.offsets_file.get_mut().set_len(new_len)?; - self.data_file.get_mut().set_len(last_offset)?; + self.pending_truncation = + Some(PendingTruncation { offsets_len: new_len, data_len: last_offset }); } } else { return Err(NippyJarError::InvalidPruning(0, remaining_to_prune as u64)) @@ -346,6 +366,27 @@ impl NippyJarWriter { Ok(()) } + /// Truncates the files to the rows kept by [`Self::prune_rows_deferred`]. + /// + /// Returns `false` if no truncation was pending. + pub fn apply_pending_truncation(&mut self) -> Result { + let Some(PendingTruncation { offsets_len, data_len }) = self.pending_truncation else { + return Ok(false) + }; + + self.offsets_file.get_mut().set_len(offsets_len)?; + self.data_file.get_mut().set_len(data_len)?; + + self.offsets_file.get_ref().sync_all()?; + self.data_file.get_ref().sync_all()?; + + self.offsets_file.seek(SeekFrom::End(0))?; + self.data_file.seek(SeekFrom::End(0))?; + self.pending_truncation = None; + + Ok(true) + } + /// Updates [`NippyJar`] with the new row count and maximum uncompressed row size, while /// resetting internal fields. fn finalize_row(&mut self) { diff --git a/crates/storage/provider/src/providers/static_file/mod.rs b/crates/storage/provider/src/providers/static_file/mod.rs index b2c6570c12b..3b416048367 100644 --- a/crates/storage/provider/src/providers/static_file/mod.rs +++ b/crates/storage/provider/src/providers/static_file/mod.rs @@ -117,6 +117,17 @@ mod tests { Ok(()) } + fn assert_cached_size_matches_disk( + sf_rw: &StaticFileProvider, + segment: StaticFileSegment, + block: u64, + ) -> eyre::Result<()> { + let jar_provider = sf_rw.get_segment_provider_for_block(segment, block, None)?; + let disk_size = fs::metadata(jar_provider.data_path())?.len() + + fs::metadata(jar_provider.offsets_path())?.len(); + assert_eyre(jar_provider.size() as u64, disk_size, "cached size mismatch") + } + #[test] fn test_static_files() { // Ranges @@ -239,6 +250,7 @@ mod tests { expected_tip, "header mismatch", )?; + assert_cached_size_matches_disk(sf_rw, StaticFileSegment::Headers, id)?; } // Validate the number of files remaining in the directory @@ -492,6 +504,7 @@ mod tests { "block mismatch", )?; assert_eyre(sf_rw.get_highest_static_file_tx(segment), expected_tx_tip, "tx mismatch")?; + assert_cached_size_matches_disk(sf_rw, segment, last_block)?; // Verify that transactions and receipts are returned correctly. Uses // cumulative_gas_used & nonce as ids. diff --git a/crates/storage/provider/src/providers/static_file/writer.rs b/crates/storage/provider/src/providers/static_file/writer.rs index 0d33110d4f9..ea8ac3342b2 100644 --- a/crates/storage/provider/src/providers/static_file/writer.rs +++ b/crates/storage/provider/src/providers/static_file/writer.rs @@ -668,9 +668,28 @@ impl StaticFileProviderRW { self.update_index()?; } + // Pruned rows are cut from the files only now. Both the cached jar `update_index` just + // replaced and the one it installed may still map the uncut length: the replaced one waits + // for its readers, and the installed one never reads past its row count. Indexing again + // caches a jar that maps the cut length. + if self.apply_pending_truncation()? { + self.update_index()?; + } + Ok(()) } + /// Applies the truncation deferred by pruning to the data and changeset offset files. + /// + /// Returns `false` if no truncation was pending. + fn apply_pending_truncation(&mut self) -> ProviderResult { + let mut truncated = self.writer.apply_pending_truncation().map_err(ProviderError::other)?; + if let Some(writer) = &mut self.changeset_offsets { + truncated |= writer.apply_pending_truncation().map_err(ProviderError::other)?; + } + Ok(truncated) + } + /// Commits configuration changes to disk and updates the reader index with the new changes. /// /// CAUTION: does not call `sync_all` on the files. @@ -947,7 +966,9 @@ impl StaticFileProviderRW { self.writer.user_header_mut().prune(blocks_to_remove); // Prune the actual rows - self.writer.prune_rows(rows_to_delete as usize).map_err(ProviderError::other)?; + self.writer + .prune_rows_deferred(rows_to_delete as usize) + .map_err(ProviderError::other)?; } // Update the block range @@ -958,7 +979,7 @@ impl StaticFileProviderRW { // Truncate the sidecar file to match the new block count if let Some(writer) = &mut self.changeset_offsets { - writer.truncate(blocks_to_keep).map_err(ProviderError::other)?; + writer.truncate_deferred(blocks_to_keep); } // Clear current changeset offset tracking since we've pruned @@ -1004,7 +1025,7 @@ impl StaticFileProviderRW { } else { // Update `SegmentHeader` self.writer.user_header_mut().prune(len); - self.writer.prune_rows(len as usize).map_err(ProviderError::other)?; + self.writer.prune_rows_deferred(len as usize).map_err(ProviderError::other)?; break } @@ -1014,7 +1035,9 @@ impl StaticFileProviderRW { self.writer.user_header_mut().prune(remaining_rows); // Truncate data - self.writer.prune_rows(remaining_rows as usize).map_err(ProviderError::other)?; + self.writer + .prune_rows_deferred(remaining_rows as usize) + .map_err(ProviderError::other)?; remaining_rows = 0; } } From fc96895d2869b997fc141039be6c3601b4830439 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 10 Nov 2026 16:21:30 +0000 Subject: [PATCH 09/11] perf(net): skip decoding unrequested responses (#28403) Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit b5e7d63d94d23718545f3efbac718dcc5a6b2139) --- crates/net/eth-wire-types/src/snap.rs | 28 +- crates/net/eth-wire/src/errors/eth.rs | 28 +- crates/net/eth-wire/src/eth_snap.rs | 346 +++++++++++++++++++++- crates/net/eth-wire/src/ethstream.rs | 357 ++++++++++++++++++++++- crates/net/network/src/eth_requests.rs | 13 +- crates/net/network/src/session/active.rs | 35 +++ 6 files changed, 785 insertions(+), 22 deletions(-) diff --git a/crates/net/eth-wire-types/src/snap.rs b/crates/net/eth-wire-types/src/snap.rs index 3a2c40f0920..b6a15d64d16 100644 --- a/crates/net/eth-wire-types/src/snap.rs +++ b/crates/net/eth-wire-types/src/snap.rs @@ -331,7 +331,8 @@ pub enum SnapProtocolMessage { BlockAccessLists(BlockAccessListsMessage), } -/// Error decoding an inbound `snap` protocol message from its framed bytes. +/// Error decoding an inbound `snap` protocol message from its framed bytes, or matching a response +/// to its request. #[derive(thiserror::Error, Debug)] pub enum SnapProtocolError { /// The payload was empty and carried no message id. @@ -344,6 +345,31 @@ pub enum SnapProtocolError { /// Decoding the RLP message body failed. #[error("RLP error: {0}")] Rlp(#[from] alloy_rlp::Error), + /// The response answers no request sent on this connection. + /// + /// The response is rejected before its body is decoded. + #[error("received unsolicited {message_id:?} response with request id {request_id}")] + UnsolicitedResponse { + /// The message id of the response. + message_id: SnapMessageId, + /// The request id the response carried. + request_id: u64, + }, + /// A list in the response is longer than its request allows: more entries than requested, or + /// more range proof nodes than two boundary paths can hold. + /// + /// The response is rejected before its body is decoded. + #[error( + "{message_id:?} response with request id {request_id} holds a list longer than the {max_items} items its request allows" + )] + ResponseExceedsRequest { + /// The message id of the response. + message_id: SnapMessageId, + /// The request id the response carried. + request_id: u64, + /// The longest the list may be. + max_items: usize, + }, } impl SnapProtocolMessage { diff --git a/crates/net/eth-wire/src/errors/eth.rs b/crates/net/eth-wire/src/errors/eth.rs index f8826a73838..615c191d84f 100644 --- a/crates/net/eth-wire/src/errors/eth.rs +++ b/crates/net/eth-wire/src/errors/eth.rs @@ -5,7 +5,7 @@ use crate::{ }; use alloy_chains::Chain; use alloy_primitives::B256; -use reth_eth_wire_types::{snap::SnapProtocolError, EthVersion}; +use reth_eth_wire_types::{snap::SnapProtocolError, EthMessageID, EthVersion}; use reth_ethereum_forks::ValidationError; use reth_primitives_traits::{GotExpected, GotExpectedBoxed}; use std::io; @@ -52,6 +52,30 @@ pub enum EthStreamError { /// The identifier of the unknown Ethereum message. message_id: u8, }, + /// Received a response that answers no request sent on this stream. + /// + /// The response is rejected before its items are decoded. + #[error("received unsolicited {message_id:?} response with request id {request_id}")] + UnsolicitedResponse { + /// The message id of the response. + message_id: EthMessageID, + /// The request id the response carried. + request_id: u64, + }, + /// Received a response with more items than its request asked for. + /// + /// The response is rejected before its items are decoded. + #[error( + "{message_id:?} response with request id {request_id} exceeds the {max_items} requested items" + )] + ResponseExceedsRequest { + /// The message id of the response. + message_id: EthMessageID, + /// The request id the response carried. + request_id: u64, + /// The number of items the request asked for. + max_items: usize, + }, } // === impl EthStreamError === @@ -78,6 +102,8 @@ impl EthStreamError { Self::MessageTooBig(_) | Self::TransactionHashesInvalidLenOfFields { .. } | Self::UnsupportedMessage { .. } | + Self::UnsolicitedResponse { .. } | + Self::ResponseExceedsRequest { .. } | Self::P2PStreamError( P2PStreamError::Rlp(_) | P2PStreamError::Snap(_) | diff --git a/crates/net/eth-wire/src/eth_snap.rs b/crates/net/eth-wire/src/eth_snap.rs index 963a53c4096..f178928eb3b 100644 --- a/crates/net/eth-wire/src/eth_snap.rs +++ b/crates/net/eth-wire/src/eth_snap.rs @@ -8,19 +8,25 @@ use crate::{ capability::SharedCapabilities, errors::{EthStreamError, P2PStreamError}, + ethstream::list_exceeds_items, handshake::EthRlpxHandshake, message::EthBroadcastMessage, Capability, EthMessage, EthNetworkPrimitives, EthStreamInner, EthVersion, NetworkPrimitives, P2PStream, UnifiedStatus, HANDSHAKE_TIMEOUT, }; -use alloy_primitives::bytes::{Bytes, BytesMut}; +use alloy_primitives::{ + bytes::{Bytes, BytesMut}, + B256, +}; +use alloy_rlp::{Decodable, Header}; use futures::{Sink, SinkExt, Stream, StreamExt}; use reth_eth_wire_types::{ - snap::{SnapProtocolMessage, SnapVersion}, + snap::{SnapMessageId, SnapProtocolError, SnapProtocolMessage, SnapVersion}, RawCapabilityMessage, }; use reth_ethereum_forks::ForkFilter; use std::{ + collections::BTreeMap, io, pin::Pin, sync::Arc, @@ -43,6 +49,12 @@ pub struct EthSnapStream { /// or above it are snap, rebased to snap-relative and validated by /// [`SnapProtocolMessage::decode_versioned`]. snap_offset: u8, + /// Responses the remote owes for `snap/2` requests sent on this stream, keyed by request id. + /// + /// Like the `eth` codec's pending requests, a snap response is only decoded if it answers one + /// of these, and the entry is removed when it does, so the map is bounded by the snap requests + /// the owner keeps in flight. + pending_snap_responses: BTreeMap, } impl EthSnapStream @@ -70,7 +82,7 @@ where handshake.handshake(&mut conn, status, fork_filter, HANDSHAKE_TIMEOUT).await?; let eth = EthStreamInner::with_max_message_size(eth_version, eth_max_message_size); - Ok((Self { conn, eth, snap_offset }, their_status)) + Ok((Self { conn, eth, snap_offset, pending_snap_responses: BTreeMap::new() }, their_status)) } } @@ -104,6 +116,51 @@ impl EthSnapStream { pub fn into_inner(self) -> P2PStream { self.conn } + + /// Consumes the pending request a framed `snap/2` response answers, without decoding the + /// response's lists. + /// + /// Like the `eth` codec, this rejects a response before decoding allocates per list entry if + /// nobody asked for it, if it holds more entries than requested, or if its range proof holds + /// more than [`MAX_RANGE_PROOF_NODES`] nodes. Messages that are not responses pass through. + fn take_pending_snap_response(&mut self, bytes: &[u8]) -> Result<(), SnapProtocolError> { + let Some((&id, mut buf)) = bytes.split_first() else { return Ok(()) }; + let Some(message_id) = [ + SnapMessageId::AccountRange, + SnapMessageId::StorageRanges, + SnapMessageId::ByteCodes, + SnapMessageId::BlockAccessLists, + ] + .into_iter() + .find(|response| *response as u8 == id) else { + return Ok(()) + }; + + // Every response is a list that starts with the request id. + let mut payload = Header::decode_bytes(&mut buf, true)?; + let request_id = u64::decode(&mut payload)?; + + let Some(pending) = self + .pending_snap_responses + .remove(&request_id) + .filter(|pending| pending.message_id == message_id) + else { + return Err(SnapProtocolError::UnsolicitedResponse { message_id, request_id }) + }; + + // The request sizes the list right after the request id; range responses follow it with + // their proof. + let max_items = if list_exceeds_items(&mut payload, pending.max_items)? { + pending.max_items + } else if matches!(message_id, SnapMessageId::AccountRange | SnapMessageId::StorageRanges) && + list_exceeds_items(&mut payload, MAX_RANGE_PROOF_NODES)? + { + MAX_RANGE_PROOF_NODES + } else { + return Ok(()) + }; + Err(SnapProtocolError::ResponseExceedsRequest { message_id, request_id, max_items }) + } } impl EthSnapStream @@ -120,6 +177,9 @@ where } /// Sends a raw capability message over the connection. + /// + /// Raw messages are not recorded as requests, so the response to an `eth` or `snap/2` request + /// sent this way is rejected as unsolicited. Send requests as [`EthSnapMessage`]s instead. pub fn start_send_raw(&mut self, msg: RawCapabilityMessage) -> Result<(), EthStreamError> { self.conn.start_send_unpin(msg.encoded()).map_err(Into::into) } @@ -160,6 +220,9 @@ where return Poll::Ready(Some(this.eth.decode_message(bytes).map(EthSnapMessage::Eth))) }; bytes[0] = snap_id; + if let Err(err) = this.take_pending_snap_response(&bytes) { + return Poll::Ready(Some(Err(err.into()))) + } Poll::Ready(Some( SnapProtocolMessage::decode_versioned(SnapVersion::V2, &bytes) .map(EthSnapMessage::Snap) @@ -184,6 +247,9 @@ where let bytes = match item { EthSnapMessage::Eth(msg) => this.eth.encode_message(msg)?, EthSnapMessage::Snap(msg) => { + if let Some((request_id, response)) = PendingSnapResponse::for_request(&msg) { + this.pending_snap_responses.insert(request_id, response); + } // Reclaim the freshly-encoded buffer as mutable without copying the payload, then // rebase the snap-relative id into the combined message space. let mut buf = @@ -204,6 +270,45 @@ where } } +/// The response the remote owes for a `snap/2` request sent on an [`EthSnapStream`]. +#[derive(Clone, Copy, Debug)] +struct PendingSnapResponse { + /// Message id the response must carry. + message_id: SnapMessageId, + /// Number of entries the request sized the response's leading list to. + max_items: usize, +} + +impl PendingSnapResponse { + /// Returns the request id and the response owed for `msg`, or `None` if it is a response. + const fn for_request(msg: &SnapProtocolMessage) -> Option<(u64, Self)> { + let (message_id, max_items) = match msg { + // Accounts are bounded by the requested response bytes, not by a count. + SnapProtocolMessage::GetAccountRange(_) => (SnapMessageId::AccountRange, usize::MAX), + // One slot list per requested account. + SnapProtocolMessage::GetStorageRanges(req) => { + (SnapMessageId::StorageRanges, req.account_hashes.len()) + } + SnapProtocolMessage::GetByteCodes(req) => (SnapMessageId::ByteCodes, req.hashes.len()), + SnapProtocolMessage::GetBlockAccessLists(req) => { + (SnapMessageId::BlockAccessLists, req.block_hashes.len()) + } + SnapProtocolMessage::AccountRange(_) | + SnapProtocolMessage::StorageRanges(_) | + SnapProtocolMessage::ByteCodes(_) | + SnapProtocolMessage::BlockAccessLists(_) => return None, + }; + Some((msg.request_id(), Self { message_id, max_items })) + } +} + +/// Maximum number of nodes in a `snap/2` range proof. +/// +/// The proof covers two boundary paths, each holding at most one hashed node per nibble of a +/// 32-byte key plus its leaf, since inline nodes are never separate proof entries. The range proof +/// verifier in `reth-trie-common` rejects longer proofs as well. +const MAX_RANGE_PROOF_NODES: usize = 2 * (2 * B256::len_bytes() + 1); + /// Resolves the `snap/2` message-id offset from the negotiated capabilities, accepting only a /// connection that shares exactly `eth` at relative offset 0 immediately followed by `snap/2`. /// @@ -247,11 +352,83 @@ mod tests { UnauthedP2PStream, }; use reth_eth_wire_types::{ - snap::{BlockAccessListsMessage, GetBlockAccessListsMessage}, - EthVersion, + snap::{ + AccountRangeMessage, BlockAccessListsMessage, ByteCodesMessage, GetAccountRangeMessage, + GetBlockAccessListsMessage, GetByteCodesMessage, GetStorageRangesMessage, + StorageRangesMessage, + }, + BlockAccessLists, EthVersion, }; - use tokio::net::TcpListener; - use tokio_util::codec::Decoder; + use tokio::net::{TcpListener, TcpStream}; + use tokio_util::codec::{Decoder, Framed}; + + type TestEthSnapStream = + EthSnapStream, EthNetworkPrimitives>; + + /// Connects two peers that negotiate `eth` + `snap/2` and returns their streams. + async fn eth_snap_pair() -> (TestEthSnapStream, TestEthSnapStream) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let local_addr = listener.local_addr().unwrap(); + let (status, fork_filter) = eth_handshake(); + let server_fork_filter = fork_filter.clone(); + + let server = tokio::spawn(async move { + let (incoming, _) = listener.accept().await.unwrap(); + let stream = crate::PassthroughCodec::default().framed(incoming); + let (conn, _) = + UnauthedP2PStream::new(stream).handshake(eth_snap_hello()).await.unwrap(); + EthSnapStream::handshake( + conn, + status, + server_fork_filter, + Arc::new(EthHandshake::default()), + MAX_MESSAGE_SIZE, + ) + .await + .unwrap() + .0 + }); + + let conn = connect_passthrough(local_addr, eth_snap_hello()).await; + let (client, _) = EthSnapStream::handshake( + conn, + status, + fork_filter, + Arc::new(EthHandshake::default()), + MAX_MESSAGE_SIZE, + ) + .await + .unwrap(); + (client, server.await.unwrap()) + } + + /// Sends `msg` from `from` and returns the error `to` reports for it. + async fn send_and_expect_error( + from: &mut TestEthSnapStream, + to: &mut TestEthSnapStream, + msg: SnapProtocolMessage, + ) -> SnapProtocolError { + from.send(EthSnapMessage::Snap(msg)).await.unwrap(); + match to.next().await.unwrap() { + Err(EthStreamError::InvalidSnapMessage(err)) => err, + other => panic!("expected a snap error, got {other:?}"), + } + } + + fn get_byte_codes(request_id: u64, hashes: usize) -> SnapProtocolMessage { + SnapProtocolMessage::GetByteCodes(GetByteCodesMessage { + request_id, + hashes: vec![B256::ZERO; hashes], + response_bytes: u64::MAX, + }) + } + + fn byte_codes(request_id: u64, codes: usize) -> SnapProtocolMessage { + SnapProtocolMessage::ByteCodes(ByteCodesMessage { + request_id, + codes: vec![Bytes::new().into(); codes], + }) + } /// Builds shared capabilities from matching local protocols and peer capabilities. fn shared_caps(local: Vec, peer: Vec) -> SharedCapabilities { @@ -379,7 +556,7 @@ mod tests { .send(EthSnapMessage::Snap(SnapProtocolMessage::GetBlockAccessLists( GetBlockAccessListsMessage { request_id: 7, - block_hashes: Vec::new(), + block_hashes: vec![B256::ZERO], response_bytes: u64::MAX, }, ))) @@ -456,4 +633,157 @@ mod tests { ); } } + + #[tokio::test(flavor = "multi_thread")] + async fn unsolicited_snap_response_is_rejected() { + let (mut client, mut server) = eth_snap_pair().await; + + let err = send_and_expect_error(&mut client, &mut server, byte_codes(7, 1)).await; + assert!(matches!( + err, + SnapProtocolError::UnsolicitedResponse { + message_id: SnapMessageId::ByteCodes, + request_id: 7 + } + )); + } + + #[tokio::test(flavor = "multi_thread")] + async fn snap_response_to_sent_request_is_decoded_once() { + let (mut client, mut server) = eth_snap_pair().await; + server.send(EthSnapMessage::Snap(get_byte_codes(7, 2))).await.unwrap(); + assert!(matches!( + client.next().await.unwrap().unwrap(), + EthSnapMessage::Snap(SnapProtocolMessage::GetByteCodes(_)) + )); + + client.send(EthSnapMessage::Snap(byte_codes(7, 2))).await.unwrap(); + assert!(matches!( + server.next().await.unwrap().unwrap(), + EthSnapMessage::Snap(SnapProtocolMessage::ByteCodes(_)) + )); + + let err = send_and_expect_error(&mut client, &mut server, byte_codes(7, 2)).await; + assert!(matches!( + err, + SnapProtocolError::UnsolicitedResponse { + message_id: SnapMessageId::ByteCodes, + request_id: 7 + } + )); + } + + #[tokio::test(flavor = "multi_thread")] + async fn snap_response_of_other_type_is_rejected() { + let (mut client, mut server) = eth_snap_pair().await; + server.send(EthSnapMessage::Snap(get_byte_codes(7, 1))).await.unwrap(); + client.next().await.unwrap().unwrap(); + + let response = SnapProtocolMessage::BlockAccessLists(BlockAccessListsMessage { + request_id: 7, + block_access_lists: BlockAccessLists(Vec::new()), + }); + let err = send_and_expect_error(&mut client, &mut server, response).await; + assert!(matches!( + err, + SnapProtocolError::UnsolicitedResponse { + message_id: SnapMessageId::BlockAccessLists, + request_id: 7 + } + )); + } + + #[tokio::test(flavor = "multi_thread")] + async fn snap_response_with_more_entries_than_requested_is_rejected() { + let (mut client, mut server) = eth_snap_pair().await; + server.send(EthSnapMessage::Snap(get_byte_codes(7, 2))).await.unwrap(); + client.next().await.unwrap().unwrap(); + + let err = send_and_expect_error(&mut client, &mut server, byte_codes(7, 3)).await; + assert!(matches!( + err, + SnapProtocolError::ResponseExceedsRequest { + message_id: SnapMessageId::ByteCodes, + request_id: 7, + max_items: 2 + } + )); + } + + #[tokio::test(flavor = "multi_thread")] + async fn snap_range_proof_is_bounded() { + let (mut client, mut server) = eth_snap_pair().await; + let request = |request_id| { + SnapProtocolMessage::GetAccountRange(GetAccountRangeMessage { + request_id, + root_hash: B256::ZERO, + starting_hash: B256::ZERO, + limit_hash: B256::repeat_byte(0xff), + response_bytes: u64::MAX, + }) + }; + let response = |request_id, proof_nodes| { + SnapProtocolMessage::AccountRange(AccountRangeMessage { + request_id, + accounts: Vec::new(), + proof: vec![Bytes::new().into(); proof_nodes], + }) + }; + + server.send(EthSnapMessage::Snap(request(1))).await.unwrap(); + client.next().await.unwrap().unwrap(); + client.send(EthSnapMessage::Snap(response(1, MAX_RANGE_PROOF_NODES))).await.unwrap(); + assert!(matches!( + server.next().await.unwrap().unwrap(), + EthSnapMessage::Snap(SnapProtocolMessage::AccountRange(_)) + )); + + server.send(EthSnapMessage::Snap(request(2))).await.unwrap(); + client.next().await.unwrap().unwrap(); + let err = + send_and_expect_error(&mut client, &mut server, response(2, MAX_RANGE_PROOF_NODES + 1)) + .await; + assert!(matches!( + err, + SnapProtocolError::ResponseExceedsRequest { + message_id: SnapMessageId::AccountRange, + request_id: 2, + max_items: MAX_RANGE_PROOF_NODES + } + )); + } + + #[tokio::test(flavor = "multi_thread")] + async fn snap_storage_ranges_hold_one_slot_list_per_account() { + let (mut client, mut server) = eth_snap_pair().await; + server + .send(EthSnapMessage::Snap(SnapProtocolMessage::GetStorageRanges( + GetStorageRangesMessage { + request_id: 7, + root_hash: B256::ZERO, + account_hashes: vec![B256::ZERO], + starting_hash: B256::ZERO.into(), + limit_hash: B256::repeat_byte(0xff).into(), + response_bytes: u64::MAX, + }, + ))) + .await + .unwrap(); + client.next().await.unwrap().unwrap(); + + let response = SnapProtocolMessage::StorageRanges(StorageRangesMessage { + request_id: 7, + slots: vec![Vec::new(); 2], + proof: Vec::new(), + }); + let err = send_and_expect_error(&mut client, &mut server, response).await; + assert!(matches!( + err, + SnapProtocolError::ResponseExceedsRequest { + message_id: SnapMessageId::StorageRanges, + request_id: 7, + max_items: 1 + } + )); + } } diff --git a/crates/net/eth-wire/src/ethstream.rs b/crates/net/eth-wire/src/ethstream.rs index a42f6d3a471..779aab7fd75 100644 --- a/crates/net/eth-wire/src/ethstream.rs +++ b/crates/net/eth-wire/src/ethstream.rs @@ -7,17 +7,19 @@ use crate::{ errors::{EthHandshakeError, EthStreamError}, handshake::EthereumEthHandshake, - message::{EthBroadcastMessage, MAX_MESSAGE_SIZE, TX_MEMORY_BUDGET_MULTIPLIER}, + message::{EthBroadcastMessage, MessageError, MAX_MESSAGE_SIZE, TX_MEMORY_BUDGET_MULTIPLIER}, p2pstream::HANDSHAKE_TIMEOUT, CanDisconnect, DisconnectReason, EthMessage, EthNetworkPrimitives, EthVersion, ProtocolMessage, UnifiedStatus, }; use alloy_primitives::bytes::{Bytes, BytesMut}; +use alloy_rlp::{Decodable, Header}; use futures::{ready, Sink, SinkExt}; use pin_project::pin_project; use reth_eth_wire_types::{EthMessageID, NetworkPrimitives, RawCapabilityMessage}; use reth_ethereum_forks::ForkFilter; use std::{ + collections::BTreeMap, future::Future, pin::Pin, task::{Context, Poll}, @@ -110,6 +112,13 @@ pub struct EthStreamInner { /// When true, `NewBlock` (0x07) and `NewBlockHashes` (0x01) messages are rejected before RLP /// decoding to avoid any memory impact for non-PoW networks. reject_block_announcements: bool, + /// Responses the remote owes for requests encoded by [`Self::encode_message`], keyed by + /// request id. + /// + /// [`Self::decode_message`] only decodes a response that answers one of these requests, and + /// removes the entry when it does. The map is therefore bounded by the requests the owner + /// keeps in flight. + pending_responses: BTreeMap, _pd: std::marker::PhantomData, } @@ -128,6 +137,7 @@ where version, max_message_size, reject_block_announcements: false, + pending_responses: BTreeMap::new(), _pd: std::marker::PhantomData, } } @@ -145,7 +155,11 @@ where } /// Decodes incoming bytes into an [`EthMessage`]. - pub fn decode_message(&self, bytes: BytesMut) -> Result, EthStreamError> { + /// + /// A response is only decoded if it answers a request sent through [`Self::encode_message`] + /// and carries at most as many items as that request asked for, see + /// [`EthStreamError::UnsolicitedResponse`] and [`EthStreamError::ResponseExceedsRequest`]. + pub fn decode_message(&mut self, bytes: BytesMut) -> Result, EthStreamError> { if bytes.len() > self.max_message_size { return Err(EthStreamError::MessageTooBig(bytes.len())); } @@ -157,6 +171,8 @@ where return Err(EthStreamError::UnsupportedMessage { message_id: id }); } + self.take_pending_response(&bytes)?; + let msg = match ProtocolMessage::decode_message_with_tx_memory_budget( self.version, &mut bytes.as_ref(), @@ -188,13 +204,66 @@ where /// Encodes an [`EthMessage`] to bytes. /// /// Validates that Status messages are not sent after handshake, enforcing protocol rules. - pub fn encode_message(&self, item: EthMessage) -> Result { + /// Requests are recorded so that [`Self::decode_message`] accepts their responses. + pub fn encode_message(&mut self, item: EthMessage) -> Result { if matches!(item, EthMessage::Status(_)) { return Err(EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake)); } + if let Some((request_id, response)) = PendingResponse::for_request(&item) { + self.pending_responses.insert(request_id, response); + } + Ok(Bytes::from(alloy_rlp::encode(ProtocolMessage::from(item)))) } + + /// Consumes the pending request an encoded response answers, without decoding the response's + /// items. + /// + /// Decoding allocates per item, and an item can be a single RLP byte, so a few hundred KiB of + /// compressed frame can decode into hundreds of MiB. Responses nobody asked for, and responses + /// with more items than requested, are rejected before any of that happens. Messages that are + /// not responses pass through. + fn take_pending_response(&mut self, bytes: &[u8]) -> Result<(), EthStreamError> { + let mut buf = bytes; + let message_id = EthMessageID::decode(&mut buf).map_err(MessageError::from)?; + if !matches!( + message_id, + EthMessageID::BlockHeaders | + EthMessageID::BlockBodies | + EthMessageID::PooledTransactions | + EthMessageID::NodeData | + EthMessageID::Receipts | + EthMessageID::BlockAccessLists | + EthMessageID::Cells + ) { + return Ok(()) + } + + // Every response is a list that starts with the request id. + let mut payload = Header::decode_bytes(&mut buf, true).map_err(MessageError::from)?; + let request_id = u64::decode(&mut payload).map_err(MessageError::from)?; + + let Some(pending) = self + .pending_responses + .remove(&request_id) + .filter(|pending| pending.message_id == message_id) + else { + return Err(EthStreamError::UnsolicitedResponse { message_id, request_id }) + }; + + if response_exceeds_items(self.version, message_id, payload, pending.max_items) + .map_err(MessageError::from)? + { + return Err(EthStreamError::ResponseExceedsRequest { + message_id, + request_id, + max_items: pending.max_items, + }) + } + + Ok(()) + } } /// An `EthStream` wraps over any `Stream` that yields bytes and makes it @@ -273,6 +342,9 @@ where } /// Sends a raw capability message directly over the stream + /// + /// Raw messages are not recorded as requests, so the response to an `eth` request sent this + /// way is rejected as unsolicited. Send requests as [`EthMessage`]s instead. pub fn start_send_raw(&mut self, msg: RawCapabilityMessage) -> Result<(), EthStreamError> { self.inner.start_send_unpin(msg.encoded())?; Ok(()) @@ -323,9 +395,9 @@ where return Err(EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake)) } - self.project() - .inner - .start_send(Bytes::from(alloy_rlp::encode(ProtocolMessage::from(item))))?; + let this = self.project(); + let bytes = this.eth.encode_message(item)?; + this.inner.start_send(bytes)?; Ok(()) } @@ -353,20 +425,114 @@ where } } +/// The response the remote owes for a request sent on an [`EthStreamInner`]. +#[derive(Clone, Copy, Debug)] +struct PendingResponse { + /// Message id the response must carry. + message_id: EthMessageID, + /// Number of items the request asked for, which the response must not exceed. + max_items: usize, +} + +impl PendingResponse { + /// Returns the request id and the response owed for `msg`, or `None` if it is not a request. + fn for_request(msg: &EthMessage) -> Option<(u64, Self)> { + let (request_id, message_id, max_items) = match msg { + EthMessage::GetBlockHeaders(req) => ( + req.request_id, + EthMessageID::BlockHeaders, + usize::try_from(req.message.limit).unwrap_or(usize::MAX), + ), + EthMessage::GetBlockBodies(req) => { + (req.request_id, EthMessageID::BlockBodies, req.message.0.len()) + } + EthMessage::GetPooledTransactions(req) => { + (req.request_id, EthMessageID::PooledTransactions, req.message.0.len()) + } + EthMessage::GetNodeData(req) => { + (req.request_id, EthMessageID::NodeData, req.message.0.len()) + } + EthMessage::GetReceipts(req) => { + (req.request_id, EthMessageID::Receipts, req.message.0.len()) + } + EthMessage::GetReceipts70(req) => { + (req.request_id, EthMessageID::Receipts, req.message.block_hashes.len()) + } + EthMessage::GetBlockAccessLists(req) => { + (req.request_id, EthMessageID::BlockAccessLists, req.message.0.len()) + } + EthMessage::GetCells(req) => { + (req.request_id, EthMessageID::Cells, req.message.hashes.len()) + } + _ => return None, + }; + Some((request_id, Self { message_id, max_items })) + } +} + +/// Returns whether the response payload following the request id holds more than `max_items` +/// items. +fn response_exceeds_items( + version: EthVersion, + message_id: EthMessageID, + mut payload: &[u8], + max_items: usize, +) -> alloy_rlp::Result { + match message_id { + // eth/70 receipts carry the `lastBlockIncomplete` flag before the receipt lists. + EthMessageID::Receipts if version >= EthVersion::Eth70 => { + bool::decode(&mut payload)?; + } + // Cells wraps the per-transaction hashes and cell lists in a struct. + EthMessageID::Cells => { + let mut cells = Header::decode_bytes(&mut payload, true)?; + return Ok(list_exceeds_items(&mut cells, max_items)? || + list_exceeds_items(&mut cells, max_items)?) + } + _ => {} + } + list_exceeds_items(&mut payload, max_items) +} + +/// Decodes the header of the RLP list at the front of `buf` and returns whether the list holds +/// more than `max_items` items, without decoding them. +/// +/// Advances `buf` past the list. +pub(crate) fn list_exceeds_items(buf: &mut &[u8], max_items: usize) -> alloy_rlp::Result { + let mut items = Header::decode_bytes(buf, true)?; + let mut count = 0; + while !items.is_empty() { + if count == max_items { + return Ok(true) + } + let header = Header::decode(&mut items)?; + // `Header::decode` checked that the payload is in bounds. + items = &items[header.payload_length..]; + count += 1; + } + Ok(false) +} + #[cfg(test)] mod tests { - use super::UnauthedEthStream; + use super::{EthStreamInner, UnauthedEthStream}; use crate::{ broadcast::BlockHashNumber, errors::{EthHandshakeError, EthStreamError}, ethstream::RawCapabilityMessage, hello::DEFAULT_TCP_PORT, + message::RequestPair, p2pstream::UnauthedP2PStream, - EthMessage, EthStream, EthVersion, HelloMessageWithProtocols, PassthroughCodec, - ProtocolVersion, Status, StatusMessage, + BlockAccessLists, BlockBodies, BlockHeaders, Cells, EthMessage, EthMessageID, EthStream, + EthVersion, GetBlockAccessLists, GetBlockBodies, GetCells, GetReceipts70, + HelloMessageWithProtocols, PassthroughCodec, ProtocolMessage, ProtocolVersion, Receipts70, + Status, StatusMessage, }; use alloy_chains::NamedChain; - use alloy_primitives::{bytes::Bytes, B256, U256}; + use alloy_primitives::{ + bytes::{Bytes, BytesMut}, + B128, B256, U256, + }; use alloy_rlp::Decodable; use futures::{SinkExt, StreamExt}; use reth_ecies::stream::ECIESStream; @@ -829,4 +995,175 @@ mod tests { handle.await.unwrap(); } + + /// Encodes `msg` with its message id, as [`EthStreamInner::decode_message`] receives it. + fn encode(msg: EthMessage) -> BytesMut { + alloy_rlp::encode(ProtocolMessage::from(msg)).as_slice().into() + } + + fn get_block_access_lists(request_id: u64, hashes: usize) -> EthMessage { + EthMessage::GetBlockAccessLists(RequestPair { + request_id, + message: GetBlockAccessLists(vec![B256::ZERO; hashes]), + }) + } + + fn block_access_lists(request_id: u64, entries: usize) -> EthMessage { + EthMessage::BlockAccessLists(RequestPair { + request_id, + message: BlockAccessLists(vec![None; entries]), + }) + } + + #[test] + fn unsolicited_response_is_rejected() { + let mut eth = EthStreamInner::::new(EthVersion::Eth71); + + let err = eth.decode_message(encode(block_access_lists(7, 1))).unwrap_err(); + assert!(matches!( + err, + EthStreamError::UnsolicitedResponse { + message_id: EthMessageID::BlockAccessLists, + request_id: 7 + } + )); + assert!(err.is_protocol_breach()); + } + + #[test] + fn response_to_sent_request_is_decoded_once() { + let mut eth = EthStreamInner::::new(EthVersion::Eth71); + eth.encode_message(get_block_access_lists(7, 2)).unwrap(); + + let response = block_access_lists(7, 2); + assert_eq!(eth.decode_message(encode(response.clone())).unwrap(), response); + assert!(matches!( + eth.decode_message(encode(response)).unwrap_err(), + EthStreamError::UnsolicitedResponse { + message_id: EthMessageID::BlockAccessLists, + request_id: 7 + } + )); + } + + #[test] + fn response_with_more_items_than_requested_is_rejected() { + let mut eth = EthStreamInner::::new(EthVersion::Eth71); + eth.encode_message(get_block_access_lists(7, 2)).unwrap(); + + let err = eth.decode_message(encode(block_access_lists(7, 3))).unwrap_err(); + assert!(matches!( + err, + EthStreamError::ResponseExceedsRequest { + message_id: EthMessageID::BlockAccessLists, + request_id: 7, + max_items: 2 + } + )); + assert!(err.is_protocol_breach()); + } + + #[test] + fn response_of_other_type_is_rejected() { + let mut eth = EthStreamInner::::new(EthVersion::Eth71); + eth.encode_message(EthMessage::GetBlockBodies(RequestPair { + request_id: 7, + message: GetBlockBodies(vec![B256::ZERO]), + })) + .unwrap(); + + let response = EthMessage::BlockHeaders(RequestPair { + request_id: 7, + message: BlockHeaders(Vec::new()), + }); + assert!(matches!( + eth.decode_message(encode(response)).unwrap_err(), + EthStreamError::UnsolicitedResponse { + message_id: EthMessageID::BlockHeaders, + request_id: 7 + } + )); + + // The request was consumed, so its proper response is now unsolicited as well. + let response = EthMessage::BlockBodies(RequestPair { + request_id: 7, + message: BlockBodies(Vec::new()), + }); + assert!(matches!( + eth.decode_message(encode(response)).unwrap_err(), + EthStreamError::UnsolicitedResponse { message_id: EthMessageID::BlockBodies, .. } + )); + } + + #[test] + fn receipts70_response_items_are_receipt_lists() { + let mut eth = EthStreamInner::::new(EthVersion::Eth70); + let request = |request_id| { + EthMessage::GetReceipts70(RequestPair { + request_id, + message: GetReceipts70 { + first_block_receipt_index: 0, + block_hashes: vec![B256::ZERO], + }, + }) + }; + let response = |request_id, lists| { + EthMessage::Receipts70(RequestPair { + request_id, + message: Receipts70 { + last_block_incomplete: true, + receipts: vec![Vec::new(); lists], + }, + }) + }; + + eth.encode_message(request(1)).unwrap(); + assert_eq!(eth.decode_message(encode(response(1, 1))).unwrap(), response(1, 1)); + + eth.encode_message(request(2)).unwrap(); + assert!(matches!( + eth.decode_message(encode(response(2, 2))).unwrap_err(), + EthStreamError::ResponseExceedsRequest { + message_id: EthMessageID::Receipts, + request_id: 2, + max_items: 1 + } + )); + } + + #[test] + fn cells_response_bounds_hashes_and_cells() { + let mut eth = EthStreamInner::::new(EthVersion::Eth72); + let request = |request_id| { + EthMessage::GetCells(RequestPair { + request_id, + message: GetCells { hashes: vec![B256::ZERO], cell_mask: B128::ZERO }, + }) + }; + let response = |request_id, hashes, cells| { + EthMessage::Cells(RequestPair { + request_id, + message: Cells { + hashes: vec![B256::ZERO; hashes], + cells: vec![Vec::new(); cells], + cell_mask: B128::ZERO, + }, + }) + }; + + eth.encode_message(request(1)).unwrap(); + assert_eq!(eth.decode_message(encode(response(1, 1, 1))).unwrap(), response(1, 1, 1)); + + for (request_id, hashes, cells) in [(2, 2, 1), (3, 1, 2)] { + eth.encode_message(request(request_id)).unwrap(); + assert!(matches!( + eth.decode_message(encode(response(request_id, hashes, cells))).unwrap_err(), + EthStreamError::ResponseExceedsRequest { + message_id: EthMessageID::Cells, + max_items: 1, + .. + } + )); + } + } } diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index 7900629bd38..8a11390080c 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -1747,9 +1747,18 @@ mod tests { } let encoded = encode_receipts70_response(resp); assert!(encoded.len() <= MAX_MESSAGE_SIZE); - EthStreamInner::::new(EthVersion::Eth70) - .decode_message(encoded.as_ref().into()) + // The requesting side only decodes responses to requests it sent. + let mut requester = EthStreamInner::::new(EthVersion::Eth70); + requester + .encode_message(EthMessage::GetReceipts70(RequestPair { + request_id: u64::MAX, + message: GetReceipts70 { + first_block_receipt_index: first_receipt_index, + block_hashes: vec![hash], + }, + })) .unwrap(); + requester.decode_message(encoded.as_ref().into()).unwrap(); } /// Encodes a receipts response with the largest possible request ID. diff --git a/crates/net/network/src/session/active.rs b/crates/net/network/src/session/active.rs index 8f7b38e0684..7136fc7f957 100644 --- a/crates/net/network/src/session/active.rs +++ b/crates/net/network/src/session/active.rs @@ -1623,6 +1623,41 @@ mod tests { rx.await.unwrap(); } + #[tokio::test(flavor = "multi_thread")] + async fn test_unsolicited_response_disconnects_with_protocol_breach() { + let mut builder = SessionBuilder::default(); + + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let local_addr = listener.local_addr().unwrap(); + + let fut = builder.with_client_stream(local_addr, async move |mut client_stream| { + assert_eq!(client_stream.version(), EthVersion::Eth71); + client_stream + .send(EthMessage::BlockAccessLists(RequestPair { + request_id: 7, + message: BlockAccessLists(vec![None; 1024]), + })) + .await + .unwrap(); + + let msg = client_stream.next().await.unwrap().unwrap_err(); + assert_eq!(msg.as_disconnected(), Some(DisconnectReason::ProtocolBreach)); + }); + + let (tx, rx) = oneshot::channel(); + + tokio::task::spawn(async move { + let (incoming, _) = listener.accept().await.unwrap(); + let session = builder.connect_incoming(incoming).await; + session.await; + + tx.send(()).unwrap(); + }); + + fut.await; + rx.await.unwrap(); + } + #[tokio::test(flavor = "multi_thread")] async fn handle_dropped_stream() { let mut builder = SessionBuilder::default(); From e493f0f5cbd4a18ee16aa819db3b295ac867c6a7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 10 Nov 2026 23:03:25 +0100 Subject: [PATCH 10/11] fix(net): keep disconnecting peers on repeated removal (#28423) Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com> (cherry picked from commit c050a76fd1de19b742299845b711515d16485a68) --- crates/net/network/src/peers.rs | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/net/network/src/peers.rs b/crates/net/network/src/peers.rs index 39c0d090a10..57f6232dc04 100644 --- a/crates/net/network/src/peers.rs +++ b/crates/net/network/src/peers.rs @@ -916,10 +916,19 @@ impl PeersManager { /// Removes the tracked node from the set. pub(crate) fn remove_peer(&mut self, peer_id: PeerId) { - let Entry::Occupied(entry) = self.peers.entry(peer_id) else { return }; + let Entry::Occupied(mut entry) = self.peers.entry(peer_id) else { return }; if entry.get().is_trusted() { return } + if matches!( + entry.get().state, + PeerConnectionState::DisconnectingIn | PeerConnectionState::DisconnectingOut + ) { + // a previous removal already queued the disconnect and `PeerRemoved`, the entry must + // stay so the session close handler can release its slot + entry.get_mut().remove_after_disconnect = true; + return + } let mut peer = entry.remove(); trace!(target: "net::peers", ?peer_id, "remove discovered node"); @@ -2953,6 +2962,29 @@ mod tests { assert!(!peers.peers.contains_key(&peer_id)) } + #[tokio::test] + async fn test_repeated_remove_while_disconnecting_releases_slot() { + let peer_id = PeerId::random(); + let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8009); + let mut peers = PeersManager::default(); + + peers.on_incoming_pending_session(addr.ip()).unwrap(); + peers.on_incoming_session_established(peer_id, addr); + assert_eq!(peers.connection_info.num_inbound, 1); + + peers.remove_peer(peer_id); + peers.remove_peer(peer_id); + + let peer = peers.peers.get(&peer_id).unwrap(); + assert_eq!(peer.state, PeerConnectionState::DisconnectingIn); + assert!(peer.remove_after_disconnect); + assert_eq!(peers.connection_info.num_inbound, 1); + + peers.on_active_session_gracefully_closed(peer_id); + assert_eq!(peers.connection_info.num_inbound, 0); + assert!(!peers.peers.contains_key(&peer_id)); + } + #[tokio::test] async fn test_keep_incoming_after_disconnect_if_discovered() { let peer_id = PeerId::random(); From e1bf9724204b28fb1ea85023ee95aa1bc02213f7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 11 Nov 2026 01:08:32 +0100 Subject: [PATCH 11/11] fix(rpc): select simulated block fork from its timestamp (#28431) (cherry picked from commit 999be5fdd0f85fd95d0406e129844ee7c434ad7c) --- crates/rpc/rpc-eth-api/src/helpers/pending_block.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs index 7b85eecb9ce..f44b0f52e9c 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs @@ -544,7 +544,11 @@ impl BuildPendingEnv for NextBlockEnvAttributes { block_overrides: Option<&BlockOverrides>, ) -> Self { let mut attributes = Self { - timestamp: parent.timestamp().saturating_add(12), + // The timestamp selects the hardfork rules, so it must be overridden before the EVM + // environment is derived from these attributes. + timestamp: block_overrides + .and_then(|overrides| overrides.time) + .unwrap_or_else(|| parent.timestamp().saturating_add(12)), suggested_fee_recipient: parent.beneficiary(), prev_randao: B256::random(), gas_limit: parent.gas_limit(),