diff --git a/bin/mega-evme/AGENTS.md b/bin/mega-evme/AGENTS.md index 96d1408c1..8a9ce7c10 100644 --- a/bin/mega-evme/AGENTS.md +++ b/bin/mega-evme/AGENTS.md @@ -22,9 +22,14 @@ Two engines: the in-tree sources run Satin; a spec from `Equivalence` to `Rex6` - Command handlers follow staged flow: parse inputs → build state/env → execute → print summary/receipt/trace. - Replay uses block executor flow, including pre-execution system calls and preceding transactions. - Logging is structured via tracing macros, with explicit progress milestones. +- RPC diagnostics use `display_rpc_url`, which hides userinfo, path, query and fragment; cache identity uses `redact_rpc_url` and keeps the path and query. + Keep the two separate, and remove reqwest's URL before an HTTP error reaches a diagnostic. + `RedactedTransport` wraps the retry layer: the dependency classifies the original errors and consumes backoff hints before final diagnostics replace every JSON-RPC error message and `data` with a fixed local diagnostic (`server error message omitted`) while preserving the numeric error code. - Output paths keep both human-readable summaries and optional machine artifacts (trace/state dump). -- The engine is decided where a command needs it (`cmd.rs` for `run`/`tx`, `replay::Cmd::engine` for a transaction replay, `block/cmd.rs` per block), always through `Engine` (`of_spec`, `of_block`, `of_chain`), and a Satin run's schedule through `common::satin_schedule`; both read the chain's activation table, so they agree. A transaction replay's engine is decided at the timestamp of the block the replay runs it in (its own, or the latest for a pending one), the one whose schedule it runs under: keep a new site to the same pair. +- The engine is decided where a command needs it (`cmd.rs` for `run`/`tx`, `replay::Cmd::engine` for a transaction replay, `block/cmd.rs` per block), always through `Engine` (`of_spec`, `of_block`, `of_chain`), and a Satin run's schedule through `common::satin_schedule`; both read the chain's activation table, so they agree. + A transaction replay's engine is decided at the timestamp of its mined block, the one whose schedule it runs under: keep a new site to the same pair. + Satin transaction replay refuses pending and genesis transactions, checks the fetched parent's number and hash, forks pre-state at that verified parent hash via EIP-1898 (`requireCanonical: true`), and resolves `BLOCKHASH` ancestors on demand by walking `parentHash` via `eth_getBlockByHash` (verifying each ancestor's hash and number). - Output on Satin is additive to the legacy output: every legacy field keeps its name and meaning, and what only Satin counts goes in the `satin` object (`tests/integration.rs` checks it on every fixture pair). ## ANTI-PATTERNS diff --git a/bin/mega-evme/src/common/logging.rs b/bin/mega-evme/src/common/logging.rs index 14ae1ed53..81222d07f 100644 --- a/bin/mega-evme/src/common/logging.rs +++ b/bin/mega-evme/src/common/logging.rs @@ -12,6 +12,8 @@ use clap::Parser; use tracing::Level; use tracing_subscriber::{fmt, EnvFilter}; +use super::sanitize_terminal_text; + /// Logging configuration arguments. #[derive(Debug, Clone, Default, Parser)] pub struct LogArgs { @@ -73,7 +75,14 @@ impl LogArgs { .init(); } Err(e) => { - eprintln!("Failed to create log file '{}': {}", log_file.display(), e); + eprintln!( + "{}", + sanitize_terminal_text(&format!( + "Failed to create log file '{}': {}", + log_file.display(), + e + )) + ); fmt() .with_env_filter(filter) .with_target(show_target) diff --git a/bin/mega-evme/src/common/provider/mod.rs b/bin/mega-evme/src/common/provider/mod.rs index ae6f54f45..c60fd635a 100644 --- a/bin/mega-evme/src/common/provider/mod.rs +++ b/bin/mega-evme/src/common/provider/mod.rs @@ -35,7 +35,7 @@ use tracing::{debug, info, warn}; pub use self::cache_store::{ExternalEnvSnapshot, RpcCacheStore}; use self::{ cache_store::CacheFileEnvelope, - transport::{CachingTransport, ReplayTransport, TransportCache}, + transport::{CachingTransport, RedactedTransport, ReplayTransport, TransportCache}, }; use super::{EvmeError, Result}; @@ -166,7 +166,7 @@ impl RpcArgs { } let provider = build_bare_op_provider(self.build_retry_client(url)); info!( - rpc_url = %redact_rpc_url(rpc_url_str), + rpc_url = %display_rpc_url(rpc_url_str), max_retries = self.max_retries, backoff_ms = self.backoff_ms, "Built RPC provider (cache disabled)", @@ -226,7 +226,7 @@ impl RpcArgs { .connect_client(client); info!( - rpc_url = %redact_rpc_url(rpc_url_str), + rpc_url = %display_rpc_url(rpc_url_str), cache_size = self.cache_size, max_retries = self.max_retries, backoff_ms = self.backoff_ms, @@ -350,7 +350,7 @@ impl RpcArgs { let chain_id = provider.get_chain_id().await.map_err(|e| { EvmeError::RpcError(format!( "Failed to fetch chain ID from '{}': {e}", - redact_rpc_url(rpc_url_str) + display_rpc_url(rpc_url_str) )) })?; @@ -372,7 +372,7 @@ impl RpcArgs { } info!( - rpc_url = %redact_rpc_url(rpc_url_str), + rpc_url = %display_rpc_url(rpc_url_str), path = %path.display(), chain_id, "Built RPC provider (capture to cache file)", @@ -394,8 +394,9 @@ impl RpcArgs { } /// Build an `RpcClient` over an arbitrary transport, wired with the configured - /// retry layer (or bare when `max_retries == 0`). `url` is used only to detect - /// whether the endpoint is local. + /// retry layer (or bare when `max_retries == 0`). `url` is used to detect whether + /// the endpoint is local; [`RedactedTransport`] wraps the outermost layer so the + /// retry policy sees original server errors before final diagnostics replace them. fn build_client( &self, transport: T, @@ -403,6 +404,7 @@ impl RpcArgs { ) -> RpcClient { let is_local = url.host_str().is_some_and(|h| h == "localhost" || h == "127.0.0.1" || h == "::1"); + let max_retries = self.max_retries; if self.max_retries > 0 { let policy = RateLimitRetryPolicy::default().or(|err: &TransportError| { matches!(err, RpcError::Transport(TransportErrorKind::Custom(_))) @@ -413,16 +415,22 @@ impl RpcArgs { self.compute_units_per_sec, policy, ); - ClientBuilder::default().layer(retry).transport(transport, is_local) + ClientBuilder::default() + .layer(tower::layer::layer_fn(move |inner| { + RedactedTransport::new(inner, max_retries) + })) + .layer(retry) + .transport(transport, is_local) } else { - ClientBuilder::default().transport(transport, is_local) + ClientBuilder::default() + .transport(RedactedTransport::new(transport, max_retries), is_local) } } /// Resolve the chain ID by issuing `eth_chainId` against a throwaway /// cache-less provider using the configured retry policy. async fn resolve_chain_id(&self, url: alloy_transport_http::reqwest::Url) -> Result { - let url_str = redact_rpc_url(url.as_str()); + let url_str = display_rpc_url(url.as_str()); let bare = build_bare_op_provider(self.build_retry_client(url)); bare.get_chain_id().await.map_err(|e| { EvmeError::RpcError(format!("Failed to fetch chain ID from '{}': {e}", url_str)) @@ -461,7 +469,7 @@ fn build_bare_op_provider(client: RpcClient) -> OpProvider { ) } -/// The URL with any userinfo removed, for a log line, an error, or a cache file name. +/// The normalized endpoint identity used for cache file names, with any userinfo removed. /// /// Userinfo is the part of the authority before its last `@`, as the URL standard reads it, so a /// password holding an `@` goes whole. It is removed from the text itself, so it holds for a URL @@ -484,16 +492,27 @@ fn normalized(raw: &str) -> String { .map_or_else(|_| raw.to_string(), |u| u.to_string()) } +/// The public origin of an RPC URL, without userinfo, path, query or fragment. +/// +/// This is for logs and errors only: cache keys keep the whole endpoint identity through +/// `redact_rpc_url`. An unparseable URL is hidden whole rather than guessed at. +pub(crate) fn display_rpc_url(raw: &str) -> String { + raw.parse::() + .ok() + .filter(|url| url.host_str().is_some()) + .map_or_else(|| "".to_string(), |url| url.origin().ascii_serialization()) +} + /// Parses `--rpc`. The transport speaks HTTP, so any other scheme is refused here, before the URL /// reaches a library whose error text would repeat it whole. fn parse_rpc_url(raw: &str) -> Result { let url: alloy_transport_http::reqwest::Url = raw.parse().map_err(|e| { - EvmeError::RpcError(format!("Invalid RPC URL '{}': {e}", redact_rpc_url(raw))) + EvmeError::RpcError(format!("Invalid RPC URL '{}': {e}", display_rpc_url(raw))) })?; if !matches!(url.scheme(), "http" | "https") { return Err(EvmeError::InvalidInput(format!( "Invalid RPC URL '{}': it has to start with http:// or https://", - redact_rpc_url(raw) + display_rpc_url(raw) ))); } Ok(url) @@ -555,6 +574,19 @@ mod tests { use super::*; + #[test] + fn test_display_rpc_url_hides_every_credential_location() { + for url in [ + "https://alice:secret@rpc.example:8443/v2/path-secret?key=query-secret#fragment-secret", + "https://rpc.example:8443/another-secret", + ] { + assert_eq!(display_rpc_url(url), "https://rpc.example:8443"); + } + for url in ["alice:secret@rpc.example/path-secret", "http://bad host/path-secret"] { + assert_eq!(display_rpc_url(url), ""); + } + } + /// Explicit `--rpc.cache-dir` is used verbatim. The file names the chain and a hash of the /// endpoint, and the hash does not change when only the userinfo does. #[test] @@ -568,6 +600,12 @@ mod tests { assert!(!name.contains("secret") && !name.contains("alice") && !name.contains("example")); assert_eq!(name, rpc_cache_file_name(4326, "http://example.com/rpc")); assert_ne!(name, rpc_cache_file_name(4326, "http://other.example/rpc")); + assert_ne!(name, rpc_cache_file_name(4326, "http://example.com/another-path")); + assert_ne!(name, rpc_cache_file_name(4326, "http://example.com/rpc?key=one")); + assert_ne!( + rpc_cache_file_name(4326, "http://example.com/rpc?key=one"), + rpc_cache_file_name(4326, "http://example.com/rpc?key=two"), + ); } #[test] diff --git a/bin/mega-evme/src/common/provider/transport.rs b/bin/mega-evme/src/common/provider/transport.rs index 077395552..7a41032b3 100644 --- a/bin/mega-evme/src/common/provider/transport.rs +++ b/bin/mega-evme/src/common/provider/transport.rs @@ -20,6 +20,104 @@ use serde::{Deserialize, Serialize}; use crate::common::{EvmeError, Result}; +/// Fixed local diagnostic replacing every server-supplied JSON-RPC error message so server error +/// payloads can never disclose endpoint credentials or other server-echoed text. +const LOCAL_RPC_ERROR_MESSAGE: &str = "server error message omitted"; + +/// Hides transport diagnostics only after the dependency has applied its retry policy. +/// The inner future runs without tracing because dependency spans can contain the endpoint URL. +#[derive(Clone)] +pub(super) struct RedactedTransport { + inner: T, + max_retries: u32, +} + +impl RedactedTransport { + pub(super) fn new(inner: T, max_retries: u32) -> Self { + Self { inner, max_retries } + } +} + +impl tower::Service for RedactedTransport { + type Response = ResponsePacket; + type Error = TransportError; + type Future = TransportFut<'static>; + + fn poll_ready( + &mut self, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + self.inner.poll_ready(cx).map_err(|error| redact_transport_error(error, self.max_retries)) + } + + fn call(&mut self, req: RequestPacket) -> Self::Future { + use tracing::{instrument::WithSubscriber, subscriber::NoSubscriber}; + + let future = + tracing::subscriber::with_default(NoSubscriber::default(), || self.inner.call(req)); + let max_retries = self.max_retries; + Box::pin(async move { + let mut response = future + .with_subscriber(NoSubscriber::default()) + .await + .map_err(|error| redact_transport_error(error, max_retries))?; + match &mut response { + ResponsePacket::Single(response) => redact_error_response(response), + ResponsePacket::Batch(responses) => { + responses.iter_mut().for_each(redact_error_response) + } + } + Ok(response) + }) + } +} + +fn redact_error_payload(error: &mut alloy_json_rpc::ErrorPayload) { + error.message = LOCAL_RPC_ERROR_MESSAGE.into(); + // Arbitrary error data need not be interpreted or re-serialized for a diagnostic. + // Backoff hints have already been consumed by the inner retry layer. + error.data = None; +} + +fn redact_error_response(response: &mut alloy_json_rpc::Response) { + if let alloy_json_rpc::ResponsePayload::Failure(error) = &mut response.payload { + redact_error_payload(error); + } +} + +fn redact_transport_error(error: TransportError, max_retries: u32) -> TransportError { + use alloy_provider::transport::RpcError; + match error { + RpcError::ErrorResp(mut error) => { + redact_error_payload(&mut error); + RpcError::ErrorResp(error) + } + RpcError::Transport(TransportErrorKind::Custom(error)) => { + match error.downcast::() { + Ok(error) => TransportErrorKind::custom((*error).without_url()), + Err(error) if error.to_string().starts_with("Max retries exceeded ") => { + TransportErrorKind::custom_str(&format!( + "Max retries exceeded ({max_retries} retries; details redacted)" + )) + } + Err(_) => TransportErrorKind::custom_str("RPC transport error (details redacted)"), + } + } + RpcError::Transport(TransportErrorKind::HttpError(mut error)) => { + error.body = "".to_string(); + RpcError::Transport(TransportErrorKind::HttpError(error)) + } + RpcError::DeserError { err, .. } => RpcError::DeserError { + err: serde_json::Error::io(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("invalid RPC response at line {}, column {}", err.line(), err.column()), + )), + text: "".to_string(), + }, + error => error, + } +} + /// A single entry in the transport-level cache. #[derive(Debug, Serialize, Deserialize)] struct TransportCacheEntry { diff --git a/bin/mega-evme/src/common/state.rs b/bin/mega-evme/src/common/state.rs index cf2f88035..525ab0991 100644 --- a/bin/mega-evme/src/common/state.rs +++ b/bin/mega-evme/src/common/state.rs @@ -2,6 +2,7 @@ use std::{collections::BTreeMap, path::PathBuf, str::FromStr}; +use alloy_eips::BlockId; use alloy_network::Network; use alloy_primitives::{Address, BlockNumber, Bytes, B256, U256}; use alloy_provider::Provider; @@ -820,8 +821,19 @@ where BlockNumber::from(latest_block) }; - // Create AlloyDB with the provider and block number - let alloy_db = AlloyDB::new(provider, block_num.into()); + Self::new_forked_at(provider, block_num.into(), prestate, block_hashes) + } + + /// Create a new forked state bound to an explicit EIP-1898 [`BlockId`] (number or hash), + /// with optional prestate overrides and block hash overrides. + pub fn new_forked_at( + provider: P, + block_id: BlockId, + prestate: EvmState, + block_hashes: HashMap, + ) -> Result { + // Create AlloyDB with the provider and block selector + let alloy_db = AlloyDB::new(provider, block_id); // Wrap the AlloyDB for synchronous access with the runtime let wrapped_db = diff --git a/bin/mega-evme/src/replay/cmd.rs b/bin/mega-evme/src/replay/cmd.rs index b2256685e..70c565ea3 100644 --- a/bin/mega-evme/src/replay/cmd.rs +++ b/bin/mega-evme/src/replay/cmd.rs @@ -1,6 +1,7 @@ -use std::time::Instant; +use std::{collections::HashMap, future::IntoFuture, sync::Mutex, time::Instant}; use alloy_consensus::{BlockHeader, Transaction as _}; +use alloy_eips::BlockId; use alloy_primitives::{B256, U256}; use alloy_provider::Provider; use alloy_rpc_types_eth::Block; @@ -17,7 +18,8 @@ use mega_evm::{ inspector::JournalExt, interpreter::{CallInputs, CallOutcome, CreateInputs, CreateOutcome, Interpreter}, primitives::{eip4844, Address, Log}, - DatabaseRef, Inspector, + state::{AccountInfo, Bytecode}, + Database, DatabaseRef, Inspector, }, BlockLimits, MegaBlockExecutionCtx, MegaBlockExecutor, MegaEvmFactory, MegaHardforks, MegaSpecId, @@ -135,8 +137,9 @@ impl Cmd { /// Without an override this reads the chain id, and, only on a chain that switches engines /// at a scheduled timestamp, the timestamp of the block the replay runs the transaction in, /// from the source the replay itself reads, through a provider that persists nothing: the - /// transaction's block, or for a pending transaction the latest block, whose environment and - /// schedule the replay runs it in. Under `--genesis` a chain other than the file's is refused + /// transaction's block, or for a pending transaction the latest block. The selected engine + /// decides whether it can replay a pending transaction. + /// Under `--genesis` a chain other than the file's is refused /// once the chain id is read, before an engine is picked for it. pub async fn engine(&self) -> Result { let tx_hash = self.tx_hash(); @@ -280,7 +283,7 @@ impl Cmd { info!(path = %path.display(), "Provider mode: offline replay from cache file"); self.rpc_args.build_replay_provider().await? } else if let Some(rpc) = &self.rpc_args.rpc_url { - info!(rpc = %crate::redact_rpc_url(rpc), "Provider mode: online RPC"); + info!(rpc = %crate::display_rpc_url(rpc), "Provider mode: online RPC"); self.rpc_args.build_provider().await? } else { return Err(ReplayError::Other( @@ -308,19 +311,11 @@ impl Cmd { .ok_or(ReplayError::TransactionNotFound(tx_hash))?; debug!(block_number = ?target_tx.block_number, "Transaction found"); - let (state_base_block, block_number, is_pending) = if let Some(n) = target_tx.block_number { - (n - 1, n, false) - } else { - let latest = provider - .get_block_number() - .await - .map_err(|e| ReplayError::RpcError(format!("RPC transport error: {e}")))?; - (latest, latest, true) - }; + let block_number = mined_block_number(tx_hash, target_tx.block_number)?; + let state_base_block = block_number - 1; debug!( state_base_block = state_base_block, block = block_number, - is_pending, "Block numbers determined", ); @@ -335,6 +330,18 @@ impl Cmd { .map_err(|e| ReplayError::RpcError(format!("RPC transport error: {e}")))? .ok_or(ReplayError::BlockNotFound(block_number))?; + if parent_block.number() != state_base_block || + parent_block.hash() != block.header.parent_hash() + { + return Err(ReplayError::Other(format!( + "parent block does not match block {block_number}: expected \ + {state_base_block} ({}), fetched {} ({})", + block.header.parent_hash(), + parent_block.number(), + parent_block.hash() + ))); + } + let preceding_tx_hashes = preceding_transactions( tx_hash, target_tx.block_number, @@ -427,14 +434,22 @@ impl Cmd { self.limits_override.as_ref().map(|limits| satin_limits(Some(limits))).transpose()?; debug!(chain_id = ctx.chain_id, spec = %MegaSpecId::SATIN, "Chain configuration"); - info!(fork_block = ctx.parent_block.header.number(), "Forking state from parent block",); - let mut database = EvmeState::new_forked( + // Bind pre-state reads to the verified parent hash with `requireCanonical: true` + // (EIP-1898) rather than its height, and resolve `BLOCKHASH` queries by walking + // `parentHash` links from that verified parent instead of querying `eth_getBlockByNumber`. + let parent_block_id = BlockId::hash_canonical(ctx.parent_block.hash()); + info!( + fork_block = ctx.parent_block.header.number(), + fork_block_hash = %ctx.parent_block.hash(), + "Forking state from verified parent block", + ); + let state_db = EvmeState::new_forked_at( provider.clone(), - Some(ctx.parent_block.header.number()), + parent_block_id, Default::default(), Default::default(), - ) - .await?; + )?; + let mut database = VerifiedReplayDb::new(state_db, provider.clone(), &ctx.parent_block); let block_env = retrieve_block_env(&ctx.block)?; trace!(?block_env, "Block environment built"); @@ -652,6 +667,156 @@ impl Cmd { } } +/// Forked replay database whose `BLOCKHASH` reads are bound to the verified parent block's +/// `parentHash` chain rather than fetched by block number from `AlloyDB`. +#[derive(Debug)] +struct VerifiedReplayDb

+where + P: Provider, +{ + state: EvmeState, + ancestors: Mutex>, +} + +impl

VerifiedReplayDb

+where + P: Provider, +{ + fn new( + state: EvmeState, + provider: P, + parent_block: &Block, + ) -> Self { + Self { state, ancestors: Mutex::new(VerifiedAncestorChain::new(provider, parent_block)) } + } +} + +impl

Database for VerifiedReplayDb

+where + P: Provider + std::fmt::Debug, +{ + type Error = crate::common::EvmeError; + + fn basic(&mut self, address: Address) -> std::result::Result, Self::Error> { + self.state.basic(address) + } + + fn code_by_hash(&mut self, code_hash: B256) -> std::result::Result { + self.state.code_by_hash(code_hash) + } + + fn storage(&mut self, address: Address, index: U256) -> std::result::Result { + self.state.storage(address, index) + } + + fn block_hash(&mut self, number: u64) -> std::result::Result { + self.ancestors.lock().expect("ancestor chain lock poisoned").resolve(number) + } +} + +impl

DatabaseRef for VerifiedReplayDb

+where + P: Provider + std::fmt::Debug, +{ + type Error = crate::common::EvmeError; + + fn basic_ref(&self, address: Address) -> std::result::Result, Self::Error> { + self.state.basic_ref(address) + } + + fn code_by_hash_ref(&self, code_hash: B256) -> std::result::Result { + self.state.code_by_hash_ref(code_hash) + } + + fn storage_ref(&self, address: Address, index: U256) -> std::result::Result { + self.state.storage_ref(address, index) + } + + fn block_hash_ref(&self, number: u64) -> std::result::Result { + self.ancestors.lock().expect("ancestor chain lock poisoned").resolve(number) + } +} + +/// Lazily walks `parentHash` links from the verified parent block via `eth_getBlockByHash`, +/// checking each fetched header's hash and number so `BLOCKHASH` can never mix forks. +#[derive(Debug)] +struct VerifiedAncestorChain

{ + provider: P, + runtime: tokio::runtime::Handle, + parent_number: u64, + frontier_number: u64, + frontier_parent_hash: B256, + verified: HashMap, +} + +impl

VerifiedAncestorChain

+where + P: Provider, +{ + fn new(provider: P, parent_block: &Block) -> Self { + let parent_number = parent_block.number(); + let parent_hash = parent_block.hash(); + let mut verified = HashMap::new(); + verified.insert(parent_number, parent_hash); + Self { + provider, + runtime: tokio::runtime::Handle::current(), + parent_number, + frontier_number: parent_number, + frontier_parent_hash: parent_block.header.parent_hash(), + verified, + } + } + + fn resolve(&mut self, number: u64) -> crate::common::Result { + if let Some(&hash) = self.verified.get(&number) { + return Ok(hash); + } + if number > self.parent_number { + return Err(crate::common::EvmeError::RpcError(format!( + "Ancestor block height {number} is not before verified parent {}", + self.parent_number, + ))); + } + if self.parent_number - number >= 256 { + return Err(crate::common::EvmeError::RpcError(format!( + "Ancestor block height {number} is outside the 256-block BLOCKHASH window of \ + parent {}", + self.parent_number, + ))); + } + while self.frontier_number > number { + let expected_number = self.frontier_number - 1; + let expected_hash = self.frontier_parent_hash; + let fetched = tokio::task::block_in_place(|| { + self.runtime.block_on(self.provider.get_block_by_hash(expected_hash).into_future()) + }) + .map_err(|e| { + crate::common::EvmeError::RpcError(format!( + "Failed to fetch ancestor block {expected_number} ({expected_hash}): {e}" + )) + })? + .ok_or_else(|| { + crate::common::EvmeError::RpcError(format!( + "Ancestor block {expected_number} ({expected_hash}) not found" + )) + })?; + if fetched.number() != expected_number || fetched.hash() != expected_hash { + return Err(crate::common::EvmeError::RpcError(format!( + "Ancestor block mismatch at height {expected_number}: expected \ + {expected_number} ({expected_hash}), fetched {} ({})", + fetched.number(), + fetched.hash(), + ))); + } + self.verified.insert(expected_number, expected_hash); + self.frontier_number = expected_number; + self.frontier_parent_hash = fetched.header.parent_hash(); + } + Ok(*self.verified.get(&number).expect("verified chain reached target height")) + } +} + /// What the preceding-transaction loop does with an execution error. #[derive(Debug)] enum PredecessorError { @@ -674,6 +839,19 @@ fn predecessor_error(error: &BlockExecutionError) -> PredecessorError { } } +/// A historical replay needs a mined block and its predecessor. +fn mined_block_number(hash: B256, number: Option) -> Result { + match number { + Some(0) => Err(ReplayError::Other(format!( + "transaction {hash} is in genesis; replay requires a parent block" + ))), + Some(number) => Ok(number), + None => Err(ReplayError::Other(format!( + "transaction {hash} is pending; replay requires a mined transaction" + ))), + } +} + /// Hashes of the transactions before `target_hash` in the fetched block. /// /// A pending transaction has no block yet, so it has no predecessors here. A mined transaction @@ -873,6 +1051,14 @@ mod tests { } } + #[test] + fn test_replay_requires_a_mined_block_with_a_predecessor() { + let hash = B256::repeat_byte(1); + assert!(mined_block_number(hash, None).unwrap_err().to_string().contains("pending")); + assert!(mined_block_number(hash, Some(0)).unwrap_err().to_string().contains("genesis")); + assert_eq!(mined_block_number(hash, Some(1)).unwrap(), 1); + } + #[test] fn test_preceding_transactions_stop_at_the_target() { let target = B256::repeat_byte(1); diff --git a/bin/mega-evme/tests/integration.rs b/bin/mega-evme/tests/integration.rs index 6a07f0416..166a115f6 100644 --- a/bin/mega-evme/tests/integration.rs +++ b/bin/mega-evme/tests/integration.rs @@ -11,6 +11,7 @@ use std::{path::Path, process::Command}; use rstest::rstest; use serde::Deserialize; +use wiremock::{matchers, Mock, MockServer, ResponseTemplate}; /// A test fixture: CLI args + expected JSON output. #[derive(Deserialize)] @@ -336,6 +337,8 @@ fn test_the_rpc_url_userinfo_reaches_no_output() { "http://alice:s3cret@127.0.0.1:9/", "http://alice:s3c@ret@127.0.0.1:9/", "alice:s3cret@127.0.0.1:9", + "http://alice:s3cret@127.0.0.1:9/v2/path-secret?key=query-secret#fragment-secret", + "http://bad host/path-secret?key=query-secret", ] { let output = Command::new(env!("CARGO_BIN_EXE_mega-evme")) .args(["replay", &format!("0x{}", "11".repeat(32)), "--override.spec", "Satin"]) @@ -350,6 +353,169 @@ fn test_the_rpc_url_userinfo_reaches_no_output() { assert_eq!(output.status.code(), Some(1), "{url}: {text}"); assert!(!text.contains("s3c") && !text.contains("ret@"), "{url}: {text}"); assert!(!text.contains("alice"), "{url}: {text}"); + for secret in ["path-secret", "query-secret", "fragment-secret"] { + assert!(!text.contains(secret), "{url}: {text}"); + } + } +} + +#[test] +fn test_a_log_file_creation_failure_filters_the_path() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("\u{1b}[2J\u{202e}missing-parent").join("tool.log"); + let output = Command::new(env!("CARGO_BIN_EXE_mega-evme")) + .env_remove("RUST_LOG") + .arg("--log.no-color") + .args(["run", "0x00", "--spec", "Satin", "-v"]) + .arg("--log.file") + .arg(&path) + .output() + .unwrap(); + assert!(output.status.success()); + let stderr = String::from_utf8(output.stderr).unwrap(); + assert!( + stderr.contains("Failed to create log file") && stderr.contains("tool.log"), + "{stderr}" + ); + assert!(!stderr.contains('\u{202e}') && !stderr.contains('\u{1b}'), "{stderr:?}"); +} + +/// The CLI must also hide raw response diagnostics and dependency spans, on both log writers. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_rpc_response_errors_hide_credentials_in_stderr_and_log_files() { + let server = MockServer::start().await; + let url = format!( + "{}/v2/(path-secret)?key=query-secret#fragment-secret", + server.uri().replacen("http://", "http://alice:userinfo-secret@", 1) + ); + Mock::given(matchers::method("POST")) + .respond_with( + ResponseTemplate::new(503) + .set_body_string(format!("{{\"url\":\"{}\"}}", url.replace('/', "\\/"))), + ) + .mount(&server) + .await; + let dir = tempfile::tempdir().unwrap(); + for to_file in [false, true] { + let path = dir.path().join("rpc.log"); + let before = server.received_requests().await.unwrap().len(); + let mut cmd = Command::new(env!("CARGO_BIN_EXE_mega-evme")); + cmd.env("RUST_LOG", "mega_evme=debug,alloy_transport_http=trace,alloy_transport=trace") + .args([ + "--log.no-color", + "replay", + &format!("0x{}", "11".repeat(32)), + "--override.spec", + "Satin", + ]) + .args([ + "--rpc", + &url, + "--rpc.max-retries", + "1", + "--rpc.backoff-ms", + "1", + "--rpc.no-cache-file", + "-vvvv", + ]); + if to_file { + cmd.arg("--log.file").arg(&path); + } + let output = tokio::task::spawn_blocking(move || cmd.output()).await.unwrap().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert_eq!(server.received_requests().await.unwrap().len() - before, 2); + let mut text = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + if to_file { + text.push_str(&std::fs::read_to_string(&path).unwrap()); + } + assert!( + text.contains("Failed to fetch chain ID") && text.contains("Max retries exceeded"), + "{text}" + ); + for secret in ["alice", "userinfo-secret", "path-secret", "query-secret", "fragment-secret"] + { + assert!(!text.contains(secret), "{text}"); + } + } +} + +/// CLI stderr and `--log.file` must report the JSON-RPC error code with a local diagnostic and +/// never echo server error text (key value alone, path segment alone, or benign `/rpc` messages). +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_rpc_json_error_stderr_and_log_never_echo_server_message() { + let cases = [ + ( + "/v2/path-secret?key=query-secret", + -32000i64, + "invalid API key query-secret", + &["query-secret", "path-secret", "invalid API key"][..], + ), + ( + "/v2/path-secret?key=query-secret", + -32001i64, + "unknown project path-secret", + &["path-secret", "query-secret", "unknown project"][..], + ), + ("/rpc", -32601i64, "header not found", &["header not found"][..]), + ]; + + let dir = tempfile::tempdir().unwrap(); + for (idx, (suffix, code, server_msg, forbidden)) in cases.into_iter().enumerate() { + let server = MockServer::start().await; + let url = format!("{}{suffix}", server.uri()); + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "jsonrpc": "2.0", + "id": 0, + "error": { "code": code, "message": server_msg, "data": { "raw": server_msg } }, + }))) + .mount(&server) + .await; + + for to_file in [false, true] { + let log_path = dir.path().join(format!("rpc-{idx}-{to_file}.log")); + let mut cmd = Command::new(env!("CARGO_BIN_EXE_mega-evme")); + cmd.env("RUST_LOG", "mega_evme=debug,alloy_transport_http=trace,alloy_transport=trace") + .args([ + "--log.no-color", + "replay", + &format!("0x{}", "11".repeat(32)), + "--override.spec", + "Satin", + "--rpc", + &url, + "--rpc.max-retries", + "0", + "--rpc.no-cache-file", + "-vvvv", + ]); + if to_file { + cmd.arg("--log.file").arg(&log_path); + } + let output = tokio::task::spawn_blocking(move || cmd.output()).await.unwrap().unwrap(); + assert_eq!(output.status.code(), Some(1)); + let mut text = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + if to_file { + text.push_str(&std::fs::read_to_string(&log_path).unwrap()); + } + assert!( + text.contains(&code.to_string()) && + text.contains("server returned an error response") && + text.contains("server error message omitted"), + "expected error code and local diagnostic in: {text}" + ); + for item in forbidden { + assert!(!text.contains(item), "server text {item:?} leaked in: {text}"); + } + } } } diff --git a/bin/mega-evme/tests/provider.rs b/bin/mega-evme/tests/provider.rs index bfc24c798..a4eb00bc4 100644 --- a/bin/mega-evme/tests/provider.rs +++ b/bin/mega-evme/tests/provider.rs @@ -15,7 +15,9 @@ use alloy_primitives::B256; use alloy_provider::Provider; use clap::Parser; use mega_evme::common::{BuildProviderOutput, EvmeError, RpcArgs}; +use rstest::rstest; use tempfile::tempdir; +use wiremock::{matchers, Mock, MockServer, ResponseTemplate}; mod common; use common::{rpc_cache_path, test_rpc_args, test_rpc_args_cached, MockRpcServer}; @@ -145,35 +147,221 @@ async fn test_build_provider_with_cache_names_file_from_fetched_chain_id() { #[tokio::test(flavor = "multi_thread")] async fn test_build_provider_invalid_url() { - let args = RpcArgs::parse_from(["mega-evme", "--rpc", "not a url", "--rpc.cache-size", "0"]); + let args = RpcArgs::parse_from([ + "mega-evme", + "--rpc", + "http://bad host/path-secret?key=query-secret", + "--rpc.cache-size", + "0", + ]); let err = args.build_provider().await.expect_err("build_provider should fail"); match err { EvmeError::RpcError(msg) => { - assert!(msg.contains("not a url"), "error must echo the original input, got: {msg}"); + assert!(msg.contains("Invalid RPC URL ''"), "{msg}"); + assert!(!msg.contains("path-secret") && !msg.contains("query-secret"), "{msg}"); } other => panic!("expected EvmeError::RpcError, got {other:?}"), } } -/// A chain-id failure reports the endpoint without the URL's userinfo. +/// HTTP, response-decoding and JSON-RPC errors hide endpoint credentials echoed by the server. +#[rstest] +#[case::http(500, "failed {rpc}")] +#[case::malformed(200, r#"{"url":"{rpc}"}"#)] +#[case::rpc(200, r#"{"jsonrpc":"2.0","id":0,"error":{"code":-32603,"message":"failed {rpc}","data":{"url":"{rpc}"}}}"#)] #[tokio::test(flavor = "multi_thread")] -async fn test_chain_id_error_omits_rpc_userinfo() { - let server = MockRpcServer::start().await; - server.respond_status_always(500).await; - let url = server.uri().replacen("http://", "http://alice:secret@", 1); - let args = RpcArgs::parse_from([ - "mega-evme", - "--rpc", - &url, - "--rpc.cache-size", - "0", - "--rpc.max-retries", - "0", - ]); - let err = args.build_provider().await.expect_err("chain id fetch fails"); - let msg = err.to_string(); - assert!(msg.contains("Failed to fetch chain ID"), "{msg}"); - assert!(!msg.contains("secret") && !msg.contains("alice"), "{msg}"); +async fn test_chain_id_error_omits_rpc_credentials(#[case] status: u16, #[case] body: &str) { + let server = MockServer::start().await; + let url = format!( + "{}/v2/(path-secret)?key=query-secret#fragment-secret", + server.uri().replacen("http://", "http://alice:userinfo-secret@", 1) + ); + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(status).set_body_string(body.replace("{rpc}", &url))) + .mount(&server) + .await; + let error = test_rpc_args(&url, Some(0)).build_provider().await.expect_err("request fails"); + assert_eq!(server.received_requests().await.unwrap().len(), 1); + let message = error.to_string(); + assert!(message.contains("Failed to fetch chain ID"), "{message}"); + for secret in ["alice", "userinfo-secret", "path-secret", "query-secret", "fragment-secret"] { + assert!(!message.contains(secret), "{message}"); + } +} + +/// JSON-RPC errors that echo configured endpoint components without a scheme, percent-encoded, +/// or with JSON-escaped separators must never leak those secrets while keeping the error code. +#[rstest] +#[case::schemeless_path("unauthorized request to /v2/path-secret")] +#[case::host_and_path("upstream {host}/v2/path-secret?key=query-secret refused")] +#[case::query_key("invalid query parameter key=query-secret")] +#[case::userinfo("authentication failed for alice:userinfo-secret")] +#[case::fragment("unexpected fragment fragment-secret")] +#[case::percent_encoded_url( + "bad upstream http%3A%2F%2F{host}%2Fv2%2Fpath-secret%3Fkey%3Dquery-secret" +)] +#[case::percent_encoded_path("unauthorized %2Fv2%2Fpath-secret")] +#[case::escaped_separator_configured( + r#"{"url":"http:\/\/alice:userinfo-secret@{host}\/v2\/path-secret?key=query-secret"}"# +)] +#[case::escaped_separator_other(r#"{"upstream":"https:\/\/other.example\/v2\/other-secret"}"#)] +#[tokio::test(flavor = "multi_thread")] +async fn test_chain_id_error_redacts_schemeless_and_encoded_echo(#[case] server_msg: &str) { + let server = MockServer::start().await; + let host = server.uri().strip_prefix("http://").unwrap().to_string(); + let url = format!( + "{}/v2/path-secret?key=query-secret#fragment-secret", + server.uri().replacen("http://", "http://alice:userinfo-secret@", 1) + ); + let rendered_msg = server_msg.replace("{host}", &host); + let body = serde_json::json!({ + "jsonrpc": "2.0", + "id": 0, + "error": { "code": -32000, "message": rendered_msg }, + }); + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(body)) + .mount(&server) + .await; + let error = test_rpc_args(&url, Some(0)).build_provider().await.expect_err("request fails"); + assert_eq!(server.received_requests().await.unwrap().len(), 1); + let message = error.to_string(); + assert!(message.contains("Failed to fetch chain ID"), "{message}"); + assert!(message.contains("-32000"), "JSON-RPC error code must be preserved: {message}"); + for secret in [ + "alice", + "userinfo-secret", + "path-secret", + "query-secret", + "fragment-secret", + "other-secret", + ] { + assert!(!message.contains(secret), "secret {secret:?} leaked in: {message}"); + } +} + +/// Percent-decoded endpoint components and ordinary server errors alike are replaced with a +/// local diagnostic that preserves only the JSON-RPC error code. +#[tokio::test(flavor = "multi_thread")] +async fn test_chain_id_error_redacts_percent_decoded_echo_and_omits_server_message() { + let server = MockServer::start().await; + let host = server.uri().strip_prefix("http://").unwrap().to_string(); + let url = format!("{}/v2/my%20key-secret?x=tail-secret", server.uri()); + + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "jsonrpc": "2.0", + "id": 0, + "error": { + "code": -32001, + "message": format!("bad upstream http://{host}/v2/my key-secret?x=tail-secret"), + }, + }))) + .up_to_n_times(1) + .with_priority(1) + .mount(&server) + .await; + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "jsonrpc": "2.0", + "id": 0, + "error": { "code": -32601, "message": "method not found" }, + }))) + .with_priority(2) + .mount(&server) + .await; + + let leaked = test_rpc_args(&url, Some(0)).build_provider().await.expect_err("echo fails"); + let leaked_msg = leaked.to_string(); + assert!( + leaked_msg.contains("-32001") && leaked_msg.contains("server error message omitted"), + "{leaked_msg}" + ); + for secret in ["key-secret", "tail-secret", "bad upstream"] { + assert!(!leaked_msg.contains(secret), "server text {secret:?} leaked in: {leaked_msg}"); + } + + let safe = test_rpc_args(&url, Some(0)).build_provider().await.expect_err("safe error fails"); + let safe_msg = safe.to_string(); + assert!( + safe_msg.contains("-32601") && safe_msg.contains("server error message omitted"), + "{safe_msg}" + ); + assert!(!safe_msg.contains("method not found"), "{safe_msg}"); +} + +/// JSON-RPC server error messages and data are never forwarded: every JSON-RPC error response is +/// replaced with a local diagnostic that preserves the numeric error code and omits all server +/// text (whether it echoes a single key value, a single path segment, or a benign message under a +/// short public path such as `/rpc`). +#[rstest] +#[case::key_value_alone( + "/v2/path-secret?key=query-secret", + -32000, + "invalid API key query-secret", + Some(serde_json::json!({"detail": "secret-data"})), + &["query-secret", "path-secret", "invalid API key", "secret-data"][..] +)] +#[case::path_segment_alone( + "/v2/path-secret?key=query-secret", + -32001, + "unknown project path-secret", + Some(serde_json::json!("secret-data")), + &["path-secret", "query-secret", "unknown project", "secret-data"][..] +)] +#[case::benign_under_short_public_path( + "/rpc", + -32005, + "invalid jsonrpc request: rate limit exceeded", + None, + &["invalid jsonrpc", "rate limit exceeded"][..] +)] +#[case::benign_not_found_under_rpc( + "/rpc", + -32601, + "header not found", + Some(serde_json::json!({"hint": "server-hint"})), + &["header not found", "server-hint"][..] +)] +#[tokio::test(flavor = "multi_thread")] +async fn test_rpc_error_uses_local_diagnostic_and_never_echoes_server_message( + #[case] suffix: &str, + #[case] code: i64, + #[case] server_msg: &str, + #[case] server_data: Option, + #[case] forbidden: &[&str], +) { + let server = MockServer::start().await; + let url = format!("{}{suffix}", server.uri()); + let mut err_obj = serde_json::json!({ "code": code, "message": server_msg }); + if let Some(data) = server_data { + err_obj["data"] = data; + } + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "jsonrpc": "2.0", + "id": 0, + "error": err_obj, + }))) + .mount(&server) + .await; + + let error = test_rpc_args(&url, Some(0)).build_provider().await.expect_err("request fails"); + assert_eq!(server.received_requests().await.unwrap().len(), 1); + let message = error.to_string(); + assert!(message.contains("Failed to fetch chain ID"), "{message}"); + assert!( + message.contains(&code.to_string()), + "JSON-RPC error code {code} must be preserved: {message}" + ); + assert!( + message.contains("server returned an error response") && + message.contains("server error message omitted"), + "expected local diagnostic in: {message}" + ); + for text in forbidden { + assert!(!message.contains(text), "server text {text:?} leaked in: {message}"); + } } // ─── Chain-id resolution ───────────────────────────────────────────────────── @@ -462,19 +650,34 @@ async fn test_build_provider_auto_creates_cache_dir() { // ─── Retry layer behavior tests ────────────────────────────────────────────── -/// Server fails 503 twice then succeeds; `max-retries=3` must reach success. -/// `eth_chainId` mock at highest priority ensures `build_provider` succeeds. +/// Status errors and nonstandard rate-limit bodies keep the dependency's retry behavior. +#[rstest] +#[case::http_503(503, "temporarily unavailable")] +#[case::bare_429(200, r#"{"code":429,"message":"rate limit"}"#)] +#[case::missing_id(200, r#"{"jsonrpc":"2.0","error":{"code":-32005,"message":"rate limit"}}"#)] #[tokio::test(flavor = "multi_thread")] -async fn test_retry_layer_retries_on_503_then_succeeds() { - let server = MockRpcServer::start().await; - server.respond_eth_chain_id(4326, 1).await; - server.respond_status_n_times(503, 2, 2).await; - server.respond_jsonrpc_result("0x10e6", 3).await; - - let BuildProviderOutput { provider, .. } = - test_rpc_args(&server.uri(), Some(3)).build_provider().await.expect("build_provider"); - let block = provider.get_block_number().await.expect("must succeed after 2 retries"); - assert_eq!(block, 0x10e6); +async fn test_retry_layer_retries_transient_response_then_succeeds( + #[case] status: u16, + #[case] body: &str, +) { + let server = MockServer::start().await; + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(status).set_body_string(body)) + .up_to_n_times(2) + .with_priority(1) + .mount(&server) + .await; + Mock::given(matchers::method("POST")) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "jsonrpc":"2.0", "id":0, "result":"0x10e6" + }))) + .with_priority(2) + .mount(&server) + .await; + + let result = test_rpc_args(&server.uri(), Some(3)).build_provider().await; + assert_eq!(server.received_requests().await.unwrap().len(), 3, "{result:?}"); + assert_eq!(result.expect("must succeed after two transient errors").chain_id, 4326); } /// `max-retries=0` must surface the first 503 immediately, with no retries. diff --git a/bin/mega-evme/tests/replay_paths.rs b/bin/mega-evme/tests/replay_paths.rs index 4762cf033..780dd9780 100644 --- a/bin/mega-evme/tests/replay_paths.rs +++ b/bin/mega-evme/tests/replay_paths.rs @@ -67,7 +67,17 @@ impl Recording { fn result(&self, method: &str, params: &[Value]) -> Result { let block_arg = |i: usize| params.get(i).and_then(Value::as_str).unwrap_or_default(); - let at_parent = |i: usize| block_arg(i) == format!("0x{:x}", BLOCK - 1); + let at_parent_number = |i: usize| block_arg(i) == format!("0x{:x}", BLOCK - 1); + let at_parent_hash = |i: usize| { + params.get(i) == + Some(&json!({ + "blockHash": self.block.block["parentHash"], + "requireCanonical": true, + })) + }; + // The legacy 1.7.1 engine queries state by parent number; Satin binds state to the + // verified canonical parent hash (EIP-1898). + let at_parent_state = |i: usize| at_parent_number(i) || at_parent_hash(i); let address = || -> Result { serde_json::from_value(params[0].clone()).map_err(|e| e.to_string()) }; @@ -102,8 +112,8 @@ impl Recording { } Ok(block) } - "eth_getBlockByNumber" if at_parent(0) => Ok(self.parent()), - "eth_getBalance" | "eth_getTransactionCount" | "eth_getCode" if at_parent(1) => { + "eth_getBlockByNumber" if at_parent_number(0) => Ok(self.parent()), + "eth_getBalance" | "eth_getTransactionCount" | "eth_getCode" if at_parent_state(1) => { let account = self.account(address()?).ok_or("account not recorded")?; Ok(match method { "eth_getBalance" => json!(account.balance), @@ -118,7 +128,7 @@ impl Recording { .unwrap_or_else(|| "0x".to_string())), }) } - "eth_getStorageAt" if at_parent(2) => { + "eth_getStorageAt" if at_parent_state(2) => { let account = self.account(address()?).ok_or("account not recorded")?; let slot: U256 = serde_json::from_value(params[1].clone()).map_err(|e| e.to_string())?; @@ -214,6 +224,102 @@ fn checked(rows: &[Value], held: Option) -> Vec { picks } +#[tokio::test(flavor = "multi_thread")] +async fn test_replay_rejects_an_unlinked_parent_before_reading_state() { + for wrong_hash in [true, false] { + let recorded = read_block(fixtures(), BLOCK); + let target = recorded.block["transactions"][1]["hash"].as_str().unwrap().to_owned(); + let recording = + Recording { block: recorded, parent_timestamp: None, codes: HashMap::default() }; + let mut parent = recording.parent(); + if wrong_hash { + parent["hash"] = json!(B256::repeat_byte(0x42)); + } else { + parent["number"] = json!(format!("0x{:x}", BLOCK - 2)); + } + let server = Server::start(recording.block).await; + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByNumber", "params": [format!("0x{:x}", BLOCK - 1), false] + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", "id": body["id"], "result": parent + })) + }) + .with_priority(1) + .expect(1) + .mount(&server.0) + .await; + let run = run_evme(&[ + "replay", + &target, + "--rpc", + &server.0.uri(), + "--rpc.no-cache-file", + "--rpc.max-retries", + "0", + "--override.spec", + "Satin", + ]); + assert_ne!(run.code, 0, "the mismatched parent must be rejected"); + assert!(run.stderr.contains("parent block does not match"), "{}{}", run.stdout, run.stderr); + let requests = server.0.received_requests().await.unwrap(); + assert!( + requests.iter().all(|request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + matches!( + body["method"].as_str(), + Some("eth_chainId" | "eth_getTransactionByHash" | "eth_getBlockByNumber") + ) + }), + "a mismatched parent is rejected before reading state or receipts" + ); + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn test_replay_rejects_a_pending_transaction_before_fetching_blocks() { + let recorded = read_block(fixtures(), BLOCK); + let mut tx = recorded.block["transactions"][1].clone(); + let target = tx["hash"].as_str().unwrap().to_owned(); + tx["blockNumber"] = Value::Null; + tx["blockHash"] = Value::Null; + tx["transactionIndex"] = Value::Null; + let server = Server::start(recorded).await; + Mock::given(matchers::body_partial_json(json!({"method": "eth_getTransactionByHash"}))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", "id": body["id"], "result": tx + })) + }) + .with_priority(1) + .expect(1) + .mount(&server.0) + .await; + let run = run_evme(&[ + "replay", + &target, + "--rpc", + &server.0.uri(), + "--rpc.no-cache-file", + "--rpc.max-retries", + "0", + "--override.spec", + "Satin", + ]); + assert_ne!(run.code, 0); + assert!(run.stderr.contains("pending"), "{}{}", run.stdout, run.stderr); + assert!( + server.0.received_requests().await.unwrap().iter().all(|request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + matches!(body["method"].as_str(), Some("eth_chainId" | "eth_getTransactionByHash")) + }), + "a pending transaction must not fetch a block or read state" + ); +} + #[tokio::test(flavor = "multi_thread")] async fn test_a_transaction_replayed_alone_is_the_one_the_block_replay_runs() { let recorded = read_block(fixtures(), BLOCK); @@ -528,7 +634,12 @@ async fn test_a_replay_stops_when_a_predecessor_account_cannot_be_read() { assert_ne!(run.code, 0, "a failed predecessor read is not skipped: {}", run.stdout); let output = format!("{}{}", run.stdout, run.stderr); assert!(output.contains("Block execution error"), "{output}"); - assert!(output.contains("account not recorded"), "{output}"); + assert!(output.contains(&MISSING.to_string()), "{output}"); + assert!( + output.contains("-32000") && output.contains("server error message omitted"), + "{output}" + ); + assert!(!output.contains("account not recorded"), "{output}"); } /// A block of calls from one payer to contracts the test adds to the recorded block's parent @@ -672,3 +783,454 @@ async fn test_a_replay_dump_carries_the_code_of_a_predecessors_account() { "{summary}" ); } + +/// Replay binds pre-state reads to the verified parent block hash (EIP-1898 with +/// `requireCanonical: true`) rather than its height, so a reorg or inconsistent load-balanced +/// endpoint serving different state at the parent number cannot silently feed the wrong pre-state. +#[tokio::test(flavor = "multi_thread")] +async fn test_replay_reads_prestate_at_parent_hash() { + // PUSH0 SLOAD PUSH0 MSTORE PUSH1 0x20 PUSH0 RETURN: returns storage slot 0. + let reader = address!("0x8888888888888888888888888888888888888888"); + let code = vec![0x5f, 0x54, 0x5f, 0x52, 0x60, 0x20, 0x5f, 0xf3]; + let hash_value = U256::from(0xbeefu64); + let height_value = U256::from(0xdeadu64); + + let mut recorded = read_block(fixtures(), BLOCK); + recorded + .prestate + .accounts + .insert(PAYER, PreAccount { balance: U256::from(10u128.pow(18)), ..PreAccount::default() }); + let code_hash = alloy_primitives::keccak256(&code); + let codes = HashMap::from_iter([(code_hash, code)]); + recorded.prestate.accounts.insert( + reader, + PreAccount { + code_hash: Some(code_hash), + storage: BTreeMap::from([(B256::ZERO, hash_value)]), + ..Default::default() + }, + ); + let target = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"; + recorded.block["transactions"] = json!([{ + "type": "0x2", + "chainId": "0x10e6", + "nonce": "0x0", + "gas": "0x4c4b40", + "maxFeePerGas": "0xf4240", + "maxPriorityFeePerGas": "0x0", + "to": reader, + "value": "0x0", + "accessList": [], + "input": "0x", + "r": "0x64c0524917585101b7ffb2286d4de676ca987554b33c6f9a37e7ad7508bfbb84", + "s": "0xe5199cacc6e07c6a9f8c74d0b06f964ce36dd362199b1c19d866f1a6f52d510", + "yParity": "0x1", + "v": "0x1", + "hash": target, + "blockHash": recorded.block["hash"], + "blockNumber": recorded.block["number"], + "transactionIndex": "0x0", + "from": PAYER, + "gasPrice": "0xf4240", + }]); + + let parent_hash = recorded.block["parentHash"].clone(); + let expected_selector = json!({ + "blockHash": parent_hash, + "requireCanonical": true, + }); + let parent_height = format!("0x{:x}", BLOCK - 1); + let server = Server::with_codes(recorded, None, codes).await; + + // Serve different storage at the parent height (simulating a reorg / split-view RPC). + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getStorageAt", + "params": [reader, U256::ZERO, parent_height], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": B256::from(height_value), + })) + }) + .with_priority(1) + .mount(&server.0) + .await; + + let run = run_evme(&[ + "replay", + target, + "--rpc", + &server.0.uri(), + "--rpc.no-cache-file", + "--rpc.max-retries", + "0", + "--override.spec", + "Satin", + "--json", + ]); + assert_eq!(run.code, 0, "{}{}", run.stderr, run.stdout); + let summary: Value = serde_json::from_str(&run.stdout).unwrap(); + assert_eq!(summary["success"], true, "{summary}"); + assert_eq!( + summary["output"], + json!(B256::from(hash_value)), + "replay must observe the state bound to the verified parent hash, not the parent height" + ); + + let requests = server.0.received_requests().await.unwrap(); + let mut state_reads = 0usize; + for request in &requests { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + let block_param = match body["method"].as_str() { + Some("eth_getBalance" | "eth_getTransactionCount" | "eth_getCode") => { + Some(&body["params"][1]) + } + Some("eth_getStorageAt") => Some(&body["params"][2]), + _ => None, + }; + if let Some(selector) = block_param { + state_reads += 1; + assert_eq!( + selector, &expected_selector, + "state read {} must pass EIP-1898 canonical parent hash, got {selector}", + body["method"] + ); + } + } + assert!(state_reads > 0, "expected at least one state RPC read"); +} + +/// Bytecode that returns `BLOCKHASH(NUMBER - 1) || BLOCKHASH(NUMBER - 2)` (64 bytes). +fn blockhash_ancestors_code() -> Vec { + vec![ + 0x60, 0x01, // PUSH1 1 + 0x43, // NUMBER + 0x03, // SUB -> N - 1 + 0x40, // BLOCKHASH + 0x5f, // PUSH0 + 0x52, // MSTORE -> mem[0x00..0x20] = BLOCKHASH(N - 1) + 0x60, 0x02, // PUSH1 2 + 0x43, // NUMBER + 0x03, // SUB -> N - 2 + 0x40, // BLOCKHASH + 0x60, 0x20, // PUSH1 0x20 + 0x52, // MSTORE -> mem[0x20..0x40] = BLOCKHASH(N - 2) + 0x60, 0x40, // PUSH1 0x40 + 0x5f, // PUSH0 + 0xf3, // RETURN + ] +} + +/// Satin transaction replay resolves `BLOCKHASH` ancestors by walking `parentHash` links from the +/// verified parent block (fetching earlier headers by hash and checking their hash and number), +/// never by calling `eth_getBlockByNumber` for ancestor heights. +#[tokio::test(flavor = "multi_thread")] +async fn test_replay_blockhash_walks_verified_ancestors_by_hash() { + let reader = address!("0x9999999999999999999999999999999999999999"); + let code = blockhash_ancestors_code(); + + let mut recorded = read_block(fixtures(), BLOCK); + recorded + .prestate + .accounts + .insert(PAYER, PreAccount { balance: U256::from(10u128.pow(18)), ..PreAccount::default() }); + let code_hash = alloy_primitives::keccak256(&code); + let codes = HashMap::from_iter([(code_hash, code)]); + recorded + .prestate + .accounts + .insert(reader, PreAccount { code_hash: Some(code_hash), ..Default::default() }); + let target = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01"; + recorded.block["transactions"] = json!([{ + "type": "0x2", + "chainId": "0x10e6", + "nonce": "0x0", + "gas": "0x4c4b40", + "maxFeePerGas": "0xf4240", + "maxPriorityFeePerGas": "0x0", + "to": reader, + "value": "0x0", + "accessList": [], + "input": "0x", + "r": "0x64c0524917585101b7ffb2286d4de676ca987554b33c6f9a37e7ad7508bfbb84", + "s": "0xe5199cacc6e07c6a9f8c74d0b06f964ce36dd362199b1c19d866f1a6f52d510", + "yParity": "0x1", + "v": "0x1", + "hash": target, + "blockHash": recorded.block["hash"], + "blockNumber": recorded.block["number"], + "transactionIndex": "0x0", + "from": PAYER, + "gasPrice": "0xf4240", + }]); + + let parent_hash: B256 = serde_json::from_value(recorded.block["parentHash"].clone()).unwrap(); + let grandparent_hash = B256::repeat_byte(0x22); + let great_grandparent_hash = B256::repeat_byte(0x33); + let fork_parent_hash = B256::repeat_byte(0xcc); + let fork_grandparent_hash = B256::repeat_byte(0xdd); + + let mut verified_parent = recorded.block.clone(); + verified_parent["number"] = json!(format!("0x{:x}", BLOCK - 1)); + verified_parent["hash"] = json!(parent_hash); + verified_parent["parentHash"] = json!(grandparent_hash); + verified_parent["transactions"] = json!([]); + + let mut fork_parent = verified_parent.clone(); + fork_parent["hash"] = json!(fork_parent_hash); + + let mut verified_grandparent = recorded.block.clone(); + verified_grandparent["number"] = json!(format!("0x{:x}", BLOCK - 2)); + verified_grandparent["hash"] = json!(grandparent_hash); + verified_grandparent["parentHash"] = json!(great_grandparent_hash); + verified_grandparent["transactions"] = json!([]); + + let mut fork_grandparent = verified_grandparent.clone(); + fork_grandparent["hash"] = json!(fork_grandparent_hash); + + let server = Server::with_codes(recorded, None, codes).await; + + // First `eth_getBlockByNumber(N-1)` serves the verified parent for header validation; any + // subsequent by-number lookup of N-1 serves a conflicting fork header. + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByNumber", + "params": [format!("0x{:x}", BLOCK - 1), false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": verified_parent, + })) + }) + .up_to_n_times(1) + .with_priority(1) + .mount(&server.0) + .await; + + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByNumber", + "params": [format!("0x{:x}", BLOCK - 1), false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": fork_parent, + })) + }) + .with_priority(2) + .mount(&server.0) + .await; + + // `eth_getBlockByNumber(N-2)` serves a different fork header than the hash-walked ancestor. + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByNumber", + "params": [format!("0x{:x}", BLOCK - 2), false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": fork_grandparent, + })) + }) + .with_priority(1) + .mount(&server.0) + .await; + + // `eth_getBlockByHash(grandparent_hash)` serves the verified ancestor reachable via parentHash. + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByHash", + "params": [grandparent_hash, false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": verified_grandparent, + })) + }) + .with_priority(1) + .mount(&server.0) + .await; + + let run = run_evme(&[ + "replay", + target, + "--rpc", + &server.0.uri(), + "--rpc.no-cache-file", + "--rpc.max-retries", + "0", + "--override.spec", + "Satin", + "--json", + ]); + assert_eq!(run.code, 0, "{}{}", run.stderr, run.stdout); + let summary: Value = serde_json::from_str(&run.stdout).unwrap(); + assert_eq!(summary["success"], true, "{summary}"); + + let mut expected_output = Vec::with_capacity(64); + expected_output.extend_from_slice(parent_hash.as_slice()); + expected_output.extend_from_slice(grandparent_hash.as_slice()); + assert_eq!( + summary["output"], + json!(alloy_primitives::hex::encode_prefixed(&expected_output)), + "BLOCKHASH(N-1) and BLOCKHASH(N-2) must observe hash-walked ancestors, not by-number headers" + ); + + let requests = server.0.received_requests().await.unwrap(); + let grandparent_num_hex = format!("0x{:x}", BLOCK - 2); + let parent_num_hex = format!("0x{:x}", BLOCK - 1); + let mut parent_by_number_calls = 0usize; + let mut grandparent_by_hash_calls = 0usize; + for request in &requests { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + match body["method"].as_str() { + Some("eth_getBlockByNumber") if body["params"][0] == grandparent_num_hex => { + panic!("replay must never query ancestor N-2 by block number: {body}"); + } + Some("eth_getBlockByNumber") if body["params"][0] == parent_num_hex => { + parent_by_number_calls += 1; + } + Some("eth_getBlockByHash") if body["params"][0] == json!(grandparent_hash) => { + grandparent_by_hash_calls += 1; + } + _ => {} + } + } + assert_eq!( + parent_by_number_calls, 1, + "N-1 should be fetched by number only once during header verification" + ); + assert_eq!( + grandparent_by_hash_calls, 1, + "N-2 must be fetched once by hash along the parentHash chain" + ); +} + +/// If walking `parentHash` encounters a missing ancestor or a header whose hash or number does +/// not match the expected link, Satin replay must fail explicitly. +#[tokio::test(flavor = "multi_thread")] +async fn test_replay_blockhash_fails_on_broken_ancestor_link() { + let reader = address!("0x9999999999999999999999999999999999999999"); + let code = blockhash_ancestors_code(); + let grandparent_hash = B256::repeat_byte(0x22); + + for (label, bad_grandparent) in [ + ("wrong_hash", Some((BLOCK - 2, B256::repeat_byte(0xee)))), + ("wrong_number", Some((BLOCK - 3, grandparent_hash))), + ("missing", None), + ] { + let mut recorded = read_block(fixtures(), BLOCK); + recorded.prestate.accounts.insert( + PAYER, + PreAccount { balance: U256::from(10u128.pow(18)), ..PreAccount::default() }, + ); + let code_hash = alloy_primitives::keccak256(&code); + let codes = HashMap::from_iter([(code_hash, code.clone())]); + recorded + .prestate + .accounts + .insert(reader, PreAccount { code_hash: Some(code_hash), ..Default::default() }); + let target = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff02"; + recorded.block["transactions"] = json!([{ + "type": "0x2", + "chainId": "0x10e6", + "nonce": "0x0", + "gas": "0x4c4b40", + "maxFeePerGas": "0xf4240", + "maxPriorityFeePerGas": "0x0", + "to": reader, + "value": "0x0", + "accessList": [], + "input": "0x", + "r": "0x64c0524917585101b7ffb2286d4de676ca987554b33c6f9a37e7ad7508bfbb84", + "s": "0xe5199cacc6e07c6a9f8c74d0b06f964ce36dd362199b1c19d866f1a6f52d510", + "yParity": "0x1", + "v": "0x1", + "hash": target, + "blockHash": recorded.block["hash"], + "blockNumber": recorded.block["number"], + "transactionIndex": "0x0", + "from": PAYER, + "gasPrice": "0xf4240", + }]); + + let mut verified_parent = recorded.block.clone(); + verified_parent["number"] = json!(format!("0x{:x}", BLOCK - 1)); + verified_parent["hash"] = recorded.block["parentHash"].clone(); + verified_parent["parentHash"] = json!(grandparent_hash); + verified_parent["transactions"] = json!([]); + + let bad_result = bad_grandparent.map(|(num, hash)| { + let mut b = recorded.block.clone(); + b["number"] = json!(format!("0x{num:x}")); + b["hash"] = json!(hash); + b["parentHash"] = json!(B256::repeat_byte(0x33)); + b["transactions"] = json!([]); + b + }); + + let server = Server::with_codes(recorded, None, codes).await; + + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByNumber", + "params": [format!("0x{:x}", BLOCK - 1), false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": verified_parent, + })) + }) + .with_priority(1) + .mount(&server.0) + .await; + + Mock::given(matchers::body_partial_json(json!({ + "method": "eth_getBlockByHash", + "params": [grandparent_hash, false], + }))) + .respond_with(move |request: &wiremock::Request| { + let body: Value = serde_json::from_slice(&request.body).unwrap(); + let result = bad_result.clone().unwrap_or(Value::Null); + ResponseTemplate::new(200).set_body_json(json!({ + "jsonrpc": "2.0", + "id": body["id"], + "result": result, + })) + }) + .with_priority(1) + .mount(&server.0) + .await; + + let run = run_evme(&[ + "replay", + target, + "--rpc", + &server.0.uri(), + "--rpc.no-cache-file", + "--rpc.max-retries", + "0", + "--override.spec", + "Satin", + ]); + assert_ne!(run.code, 0, "{label}: broken ancestor link must fail replay: {}", run.stdout); + assert!( + run.stderr.contains("Ancestor block"), + "{label}: expected ancestor diagnostic in stderr: {}", + run.stderr + ); + } +} diff --git a/crates/mega-state-test/AGENTS.md b/crates/mega-state-test/AGENTS.md index c353eb2ab..52514bc87 100644 --- a/crates/mega-state-test/AGENTS.md +++ b/crates/mega-state-test/AGENTS.md @@ -7,7 +7,8 @@ The `state-test` CLI (`crates/state-test`) is a thin front end over this crate. `README.md` says what the two modes are and how a test is judged. ## STRUCTURE -- `src/runner.rs`: discovery, parallel execution, judging a test, the report, the summary and the gate. +- `src/runner.rs`: parallel execution, judging a test, the report, the summary and the gate. +- `src/input.rs`: bounded fixture discovery and reads, and account/storage-slot cardinality checks before materializing state or hashing it. - `src/mode.rs`: the two configurations a test runs under, built from `mega-evm`'s `test_utils::{neutral_cfg, neutralize_evm}`. - `src/fork.rs`: the fixture forks a run executes (Osaka, Amsterdam). - `src/exceptions.rs`: validation errors mapped to the execution-spec exception names. @@ -23,10 +24,12 @@ The `state-test` CLI (`crates/state-test`) is a thin front end over this crate. The fixture types are the revm fork's own (`revm::statetest_types`, the `test-types` feature), re-exported as `state_test::types`. ## KEY PATTERNS +- Discovery and input/state bounds fail explicitly when exceeded; never truncate the corpus or a state and report it as complete. - A fixture's expectation is Ethereum's; a test here that needs one fills it from revm's mainnet EVM, never from `MegaEvm`. - Every failure is a `FailureKind`; equivalence mode names the deviation that explains it, and one it does not name is unattributed. - A deviation lists the exact entries it explains — file, test, data, gas and value indices — with the hashes Satin produces; a failure is its only with those hashes, and `--expect-deviations` requires every listed entry to fail exactly as listed. - Skips mirror the reference runner's, so the executed and skipped counts equal the ones `.github/workflows/exec-spec.yml` pins for it. +- Historical slow-test filenames do not skip fixtures for the supported forks; the blockchain runner's skips remain separate. - A blockchain test is skipped only for a class decided from its content (`src/blockchain/skips.rs`); a block that differs passes only when a deviation the registry already has lists that test and block with exactly the outcome Satin produces, and the chain is imported on from Satin's state, so a listed block exempts no later one. - An account is taken out of a blockchain test's state root only by `SATIN_ACCOUNTS`, only when the pre-state does not hold it and it holds exactly what the seed or a Satin block put there. diff --git a/crates/mega-state-test/README.md b/crates/mega-state-test/README.md index b97af158f..2dd386cf1 100644 --- a/crates/mega-state-test/README.md +++ b/crates/mega-state-test/README.md @@ -44,6 +44,8 @@ Base fees go to Optimism's base-fee vault on Satin, where Ethereum burns them. The runner takes the vault back out of the post-state only when the fee routing made it: the pre-state has no vault, and the vault holds exactly the base fee of the gas the transaction used. The runner skips what the reference runner skips (`src/skips.rs`), for the same reasons: the EIP-7610 collision fixtures with storage, which revm cannot see, and a transaction that cannot be built when the fixture expects it to be invalid — here only when the fixture names the reason it cannot be, an invalid signature or a blob or EIP-7702 transaction without a recipient. +That recipient check also applies after construction when an explicit transaction type bypasses the fixture types' inferred-type guard. +Historical slow-test filenames do not exempt Osaka or Amsterdam fixtures from execution. So the two runners execute the same population, and the executed and skipped counts equal the reference runner's. ## Running it @@ -58,6 +60,20 @@ cargo run --release -p state-test -- --mode satin --fork Osaka

/state_test `--expect-executed`, `--expect-skipped` and `--expect-deviations` turn a full run into the pinned gate CI runs (`.github/workflows/exec-spec-satin.yml`). `--json-outcome` prints one JSON line per test, a failure's produced hashes included, and `--trace` runs each test under an EIP-3155 tracer. +## Fixture input limits + +Discovery accepts at most 100,000 fixture files across all input paths and entries at most 64 levels below each input directory. +Discovery does not follow directory symlinks: a symlinked directory (given as a root or encountered beneath one) fails discovery with an error naming the path rather than being silently dropped or traversed through aliases, and broken symlinks fail discovery as well. +Only regular files (including symlinks to regular `*.json` files) are admitted: a non-regular input or `*.json` entry (such as a FIFO, socket, device, or directory) fails discovery with an error naming the path before any file is opened. +The bounded reader opens files non-blocking on Unix (`O_NONBLOCK`) and validates the opened descriptor's file type before reading, so a path swapped to a FIFO after discovery cannot hang the runner. +A traversal error or an exceeded limit fails the run; neither a deep subtree nor excess files are silently omitted. +The state-test runner accepts fixture files up to 64 MiB; the blockchain-test runner accepts up to 256 MiB because a file bundles many block sequences, and the pinned execution-spec fixtures include files around 160 MiB. +Both reject oversized files before reading their contents and cap the read itself in case a file grows. +Before materializing a fixture's state or computing its root, the runners require at most 100,000 accounts and 1,000,000 storage slots across the whole state. +The blockchain runner checks both the fixture's pre-state and its expected post-state, and the witness runner checks its pre-state too. +An exceeded state limit fails the test; a truncated state is never used to compute a root. +These are input bounds for the developer tools, separate from the protocol limits on execution. + ## The outcome digest A verdict is the post-state root and the logs hash, so a change that moves gas between ledgers and keeps the receipt's total, or changes the bytes a limit counted, leaves every verdict as it was. diff --git a/crates/mega-state-test/src/blockchain/chain.rs b/crates/mega-state-test/src/blockchain/chain.rs index 2ab2410d1..4f2897ef8 100644 --- a/crates/mega-state-test/src/blockchain/chain.rs +++ b/crates/mega-state-test/src/blockchain/chain.rs @@ -206,6 +206,7 @@ pub(crate) fn plain_account(account: &Account) -> Result { /// The root of a state the fixture writes out, such as its post-state. pub(crate) fn fixture_state_root(accounts: &BTreeMap) -> Result { + crate::input::check_state_size(accounts.values().map(|account| account.storage.len()))?; let plain = accounts .iter() .map(|(address, account)| Ok((*address, plain_account(account)?))) @@ -269,6 +270,7 @@ impl Chain { chain_id: u64, blob_base_fee_update_fraction: u64, ) -> Result { + crate::input::check_state_size(pre.values().map(|account| account.storage.len()))?; let mut state = CacheState::new(); for (address, account) in pre { let plain = plain_account(account).map_err(|error| format!("{address}: {error}"))?; @@ -289,6 +291,9 @@ impl Chain { let storage = storage.into_iter().filter(|(_, value)| !value.is_zero()).collect(); state.insert_account_with_storage(address, info, storage); } + crate::input::check_state_size( + state.trie_account().into_iter().map(|(_, account)| account.storage.len()), + )?; Ok(Self { state, hashes: BTreeMap::from([genesis]), @@ -407,6 +412,10 @@ impl Chain { } }?; + crate::input::check_state_size( + state.cache.trie_account().into_iter().map(|(_, account)| account.storage.len()), + ) + .map_err(|detail| Refusal { names: &[], detail })?; let routed_base_fee = self.routed_base_fee + U256::from(header.base_fee_per_gas.unwrap_or_default()) * U256::from(gas_used); self.state = state.cache; diff --git a/crates/mega-state-test/src/blockchain/mod.rs b/crates/mega-state-test/src/blockchain/mod.rs index 2a52cfa1e..47ed06ab5 100644 --- a/crates/mega-state-test/src/blockchain/mod.rs +++ b/crates/mega-state-test/src/blockchain/mod.rs @@ -498,7 +498,7 @@ pub fn run(files: &[PathBuf], config: Config) -> Report { /// not be read. pub fn run_file(path: &Path, config: Config) -> Result, Failure> { let path_str = path.to_string_lossy().into_owned(); - let json = std::fs::read_to_string(path) + let json = crate::input::read_blockchain_fixture(path) .map_err(|error| Failure::new(FailureKind::Fixture, None, format!("read: {error}")))?; let suite = serde_json::from_str::>(&json) .map_err(|error| Failure::new(FailureKind::Fixture, None, format!("parse: {error}")))?; diff --git a/crates/mega-state-test/src/exceptions.rs b/crates/mega-state-test/src/exceptions.rs index 3c8b04618..127ae1af6 100644 --- a/crates/mega-state-test/src/exceptions.rs +++ b/crates/mega-state-test/src/exceptions.rs @@ -93,12 +93,19 @@ pub fn check( /// for a blob or EIP-7702 transaction without a recipient. Empty for anything else. pub fn unbuildable_names(error: &TestError, unit: &TestUnit) -> &'static [&'static str] { let invalid_type = || { - if unit.transaction.max_fee_per_blob_gas.is_some() { - &["TYPE_3_TX_CONTRACT_CREATION"][..] - } else if unit.transaction.authorization_list.is_some() { - &["TYPE_4_TX_CONTRACT_CREATION"][..] - } else { - &[][..] + if unit.transaction.to.is_some() { + return &[][..]; + } + match unit.transaction.tx_type { + Some(3) => &["TYPE_3_TX_CONTRACT_CREATION"][..], + Some(4) => &["TYPE_4_TX_CONTRACT_CREATION"][..], + None if unit.transaction.max_fee_per_blob_gas.is_some() => { + &["TYPE_3_TX_CONTRACT_CREATION"][..] + } + None if unit.transaction.authorization_list.is_some() => { + &["TYPE_4_TX_CONTRACT_CREATION"][..] + } + _ => &[][..], } }; match error { @@ -272,6 +279,12 @@ mod tests { let blob = unit(serde_json::json!({ "maxFeePerBlobGas": "0x01" })); let auth = unit(serde_json::json!({ "authorizationList": [] })); + let explicit_blob = unit(serde_json::json!({ "type": 3 })); + let explicit_auth = unit(serde_json::json!({ "type": 4, "maxFeePerBlobGas": "0x01" })); + let other_type = unit(serde_json::json!({ "type": 2, "maxFeePerBlobGas": "0x01" })); + let call = unit(serde_json::json!({ + "type": 3, "to": "0x0000000000000000000000000000000000000001", + })); for error in [ TestError::InvalidTransactionType, TestError::UnexpectedException { @@ -279,36 +292,42 @@ mod tests { got_exception: Some("Invalid transaction type".into()), }, ] { - assert_eq!( - check_unbuildable( - "TransactionException.TYPE_3_TX_CONTRACT_CREATION", - &error, - &blob - ), - Ok(()) - ); - assert!(check_unbuildable( - "TransactionException.TYPE_4_TX_CONTRACT_CREATION", - &error, - &blob - ) - .is_err()); - assert_eq!( - check_unbuildable( + for transaction in [&blob, &explicit_blob] { + assert_eq!( + check_unbuildable( + "TransactionException.TYPE_3_TX_CONTRACT_CREATION", + &error, + transaction + ), + Ok(()) + ); + assert!(check_unbuildable( "TransactionException.TYPE_4_TX_CONTRACT_CREATION", &error, - &auth - ), - Ok(()) - ); - assert_eq!( - check_unbuildable( - "TransactionException.TYPE_3_TX_CONTRACT_CREATION", - &error, - &plain - ), - Err(Mismatch::Unnamed) - ); + transaction + ) + .is_err()); + } + for transaction in [&auth, &explicit_auth] { + assert_eq!( + check_unbuildable( + "TransactionException.TYPE_4_TX_CONTRACT_CREATION", + &error, + transaction + ), + Ok(()) + ); + } + for transaction in [&plain, &other_type, &call] { + assert_eq!( + check_unbuildable( + "TransactionException.TYPE_3_TX_CONTRACT_CREATION", + &error, + transaction + ), + Err(Mismatch::Unnamed) + ); + } } let overflow = TestError::UnexpectedException { expected_exception: None, diff --git a/crates/mega-state-test/src/input.rs b/crates/mega-state-test/src/input.rs new file mode 100644 index 000000000..766f06ea5 --- /dev/null +++ b/crates/mega-state-test/src/input.rs @@ -0,0 +1,505 @@ +//! Bounds on fixture discovery, input bytes and the state materialized by a test. + +use std::{ + fs::File, + io::{self, Read}, + path::{Path, PathBuf}, +}; + +use walkdir::WalkDir; + +/// The largest state-test fixture file the runner reads: 64 MiB. +const MAX_FIXTURE_BYTES: u64 = 64 * 1024 * 1024; +/// The largest blockchain-test fixture file: 256 MiB, accommodating bundled block sequences. +const MAX_BLOCKCHAIN_FIXTURE_BYTES: u64 = 256 * 1024 * 1024; +/// The largest collection of JSON files one discovery accepts, across all input paths. +const MAX_FIXTURE_FILES: usize = 100_000; +/// The deepest entry discovery accepts, relative to each input directory. +const MAX_FIXTURE_DEPTH: usize = 64; +/// The largest state a fixture may materialize, in accounts. +const MAX_STATE_ACCOUNTS: usize = 100_000; +/// The largest state a fixture may materialize, in storage slots across all accounts. +const MAX_STATE_SLOTS: usize = 1_000_000; + +/// Reads a UTF-8 fixture, refusing non-regular and oversized files before allocating contents. +pub(crate) fn read_fixture(path: &Path) -> io::Result { + read_file_with_limit(path, MAX_FIXTURE_BYTES) +} + +/// Reads a blockchain fixture under its larger bound, using the same guarded reader. +pub(crate) fn read_blockchain_fixture(path: &Path) -> io::Result { + read_file_with_limit(path, MAX_BLOCKCHAIN_FIXTURE_BYTES) +} + +#[cfg(unix)] +fn unix_o_nonblock() -> Option { + #[cfg(any( + target_os = "macos", + target_os = "ios", + target_os = "freebsd", + target_os = "openbsd", + target_os = "netbsd", + target_os = "dragonfly", + ))] + { + Some(0x0004) + } + #[cfg(all( + any(target_os = "linux", target_os = "android"), + any(target_arch = "mips", target_arch = "mips64"), + ))] + { + Some(0x0080) + } + #[cfg(all( + any(target_os = "linux", target_os = "android"), + any(target_arch = "sparc", target_arch = "sparc64"), + ))] + { + Some(0x4000) + } + #[cfg(all( + any(target_os = "linux", target_os = "android"), + not(any( + target_arch = "mips", + target_arch = "mips64", + target_arch = "sparc", + target_arch = "sparc64", + )), + ))] + { + Some(0x0800) + } + #[cfg(not(any( + target_os = "macos", + target_os = "ios", + target_os = "freebsd", + target_os = "openbsd", + target_os = "netbsd", + target_os = "dragonfly", + target_os = "linux", + target_os = "android", + )))] + { + None + } +} + +/// Opens `path` read-only without blocking on Unix FIFOs (`O_NONBLOCK`), so descriptor validation +/// via `fstat` (`File::metadata`) rejects non-regular files even if the path is swapped between +/// discovery and open. +fn open_nonblocking(path: &Path) -> io::Result { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + if let Some(flag) = unix_o_nonblock() { + options.custom_flags(flag); + } else { + let metadata = path.metadata().map_err(|error| { + io::Error::new(error.kind(), format!("{}: {error}", path.display())) + })?; + if !metadata.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file: {}", path.display()), + )); + } + } + } + #[cfg(not(unix))] + { + let metadata = path.metadata().map_err(|error| { + io::Error::new(error.kind(), format!("{}: {error}", path.display())) + })?; + if !metadata.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file: {}", path.display()), + )); + } + } + options + .open(path) + .map_err(|error| io::Error::new(error.kind(), format!("{}: {error}", path.display()))) +} + +fn read_file_with_limit(path: &Path, limit: u64) -> io::Result { + let file = open_nonblocking(path)?; + read_with_limit(file, limit) + .map_err(|error| io::Error::new(error.kind(), format!("{}: {error}", path.display()))) +} + +fn read_with_limit(file: File, limit: u64) -> io::Result { + let metadata = file.metadata()?; + if !metadata.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "fixture input is not a regular file", + )); + } + if metadata.len() > limit { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("fixture exceeds {limit} bytes"), + )); + } + read_bounded(file, limit) +} + +fn read_bounded(reader: impl Read, limit: u64) -> io::Result { + // The same opened file may grow after metadata was read. Never read it past this sentinel. + let mut json = String::new(); + reader.take(limit + 1).read_to_string(&mut json)?; + if json.len() as u64 > limit { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("fixture exceeds {limit} bytes"), + )); + } + Ok(json) +} + +/// Every JSON file under `path`, or `path` itself when it is a file, in path order. +/// Walking errors and exceeded limits fail discovery rather than dropping any input. +pub fn find_json_files(path: &Path) -> io::Result> { + let mut files = Vec::new(); + discover(path, &mut files, MAX_FIXTURE_DEPTH, MAX_FIXTURE_FILES)?; + files.sort(); + Ok(files) +} + +/// Discovers all `paths` under one file-count budget; an empty input directory is an error. +pub fn find_json_files_in_paths(paths: &[PathBuf]) -> io::Result> { + let mut files = Vec::new(); + for path in paths { + let before = files.len(); + discover(path, &mut files, MAX_FIXTURE_DEPTH, MAX_FIXTURE_FILES)?; + if files.len() == before { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("no JSON fixtures under {}", path.display()), + )); + } + } + files.sort(); + Ok(files) +} + +fn discover(path: &Path, files: &mut Vec, depth: usize, limit: usize) -> io::Result<()> { + // Strip any trailing separator so Unix `lstat` inspects a symlinked root directory itself + // rather than following it through the trailing slash. + let probe: PathBuf = path.components().collect(); + let symlink_meta = std::fs::symlink_metadata(&probe) + .map_err(|error| io::Error::new(error.kind(), format!("{}: {error}", path.display())))?; + if symlink_meta.file_type().is_symlink() { + let target_meta = path.metadata().map_err(|error| { + io::Error::new(error.kind(), format!("{}: {error}", path.display())) + })?; + if target_meta.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("symlinked fixture directory is not allowed: {}", path.display()), + )); + } + if target_meta.is_file() { + return add_file(path.to_path_buf(), files, limit); + } + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file or directory: {}", path.display()), + )); + } + if symlink_meta.is_file() { + return add_file(path.to_path_buf(), files, limit); + } + if !symlink_meta.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file or directory: {}", path.display()), + )); + } + // Do not follow directory symlinks: a symlinked directory fails discovery explicitly so + // aliased subtrees can neither execute fixtures twice nor cause exponential walks. Symlinks + // to regular `*.json` files are permitted; broken links and links to non-regular `*.json` + // targets fail explicitly. + for entry in WalkDir::new(path) { + let entry = entry?; + // WalkDir::max_depth would silently omit descendants, so inspect the first excess entry. + if entry.depth() > depth { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture directory depth exceeds {depth}: {}", entry.path().display()), + )); + } + if entry.file_type().is_symlink() { + let target_meta = entry.path().metadata().map_err(|error| { + io::Error::new(error.kind(), format!("{}: {error}", entry.path().display())) + })?; + if target_meta.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "symlinked fixture directory is not allowed: {}", + entry.path().display() + ), + )); + } + if entry.path().extension().is_some_and(|ext| ext == "json") { + if !target_meta.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file: {}", entry.path().display()), + )); + } + add_file(entry.into_path(), files, limit)?; + } + continue; + } + if entry.path().extension().is_some_and(|ext| ext == "json") { + if !entry.file_type().is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture input is not a regular file: {}", entry.path().display()), + )); + } + add_file(entry.into_path(), files, limit)?; + } + } + Ok(()) +} + +fn add_file(path: PathBuf, files: &mut Vec, limit: usize) -> io::Result<()> { + if files.len() >= limit { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("fixture file count exceeds {limit}: {}", path.display()), + )); + } + files.push(path); + Ok(()) +} + +/// Checks account and slot cardinality before copying fixture state or building its tries. +pub(crate) fn check_state_size(slot_counts: impl IntoIterator) -> Result<(), String> { + let mut total_slots = 0usize; + for (index, slots) in slot_counts.into_iter().enumerate() { + if index >= MAX_STATE_ACCOUNTS { + return Err(format!("state account count exceeds {MAX_STATE_ACCOUNTS}")); + } + total_slots = total_slots + .checked_add(slots) + .filter(|total| *total <= MAX_STATE_SLOTS) + .ok_or_else(|| format!("state storage exceeds {MAX_STATE_SLOTS} total slots"))?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use std::io::{Seek, Write}; + + use super::*; + + #[test] + fn test_discovery_fails_instead_of_truncating_at_depth_limit() { + let root = tempfile::tempdir().unwrap(); + let nested = root.path().join("a"); + std::fs::create_dir(&nested).unwrap(); + std::fs::write(nested.join("one.json"), "{}").unwrap(); + assert!(discover(root.path(), &mut Vec::new(), 2, 1).is_ok()); + std::fs::create_dir_all(nested.join("b").join("c")).unwrap(); + let error = discover(root.path(), &mut Vec::new(), 2, 1).unwrap_err(); + assert!(error.to_string().contains("depth exceeds 2")); + } + + #[test] + fn test_discovery_file_budget_is_shared_between_roots() { + let root = tempfile::tempdir().unwrap(); + let mut files = Vec::new(); + for name in ["one.json", "two.json"] { + let path = root.path().join(name); + std::fs::write(&path, "{}").unwrap(); + discover(&path, &mut files, 1, 2).unwrap(); + } + let error = discover(&root.path().join("one.json"), &mut files, 1, 2).unwrap_err(); + assert!(error.to_string().contains("file count exceeds 2")); + } + + #[test] + fn test_discovery_propagates_missing_root() { + let root = tempfile::tempdir().unwrap(); + let missing = root.path().join("absent"); + let error = find_json_files(&missing).unwrap_err(); + assert!(error.to_string().contains(&missing.display().to_string())); + assert!(find_json_files_in_paths(&[root.path().to_path_buf()]).is_err()); + } + + #[test] + fn test_read_limit_accepts_exact_size_and_rejects_one_more_byte() { + let mut file = tempfile::tempfile().unwrap(); + file.write_all(b"{}\n").unwrap(); + file.rewind().unwrap(); + assert_eq!(read_with_limit(file.try_clone().unwrap(), 3).unwrap(), "{}\n"); + file.rewind().unwrap(); + assert!(read_with_limit(file, 2).unwrap_err().to_string().contains("exceeds 2 bytes")); + } + + #[test] + fn test_reader_refuses_growth_past_the_metadata_bound() { + let contents = std::io::Cursor::new(b"{}\nmore bytes"); + let error = read_bounded(contents, 3).unwrap_err(); + assert!(error.to_string().contains("exceeds 3 bytes")); + } + + #[test] + fn test_state_limits_accept_boundaries_and_reject_each_dimension() { + assert!(check_state_size(std::iter::repeat_n(0, MAX_STATE_ACCOUNTS)).is_ok()); + let error = check_state_size(std::iter::repeat_n(0, MAX_STATE_ACCOUNTS + 1)).unwrap_err(); + assert!(error.contains("account count")); + assert!(check_state_size([MAX_STATE_SLOTS / 2, MAX_STATE_SLOTS / 2]).is_ok()); + assert!(check_state_size([MAX_STATE_SLOTS, 1]).unwrap_err().contains("total slots")); + assert!(check_state_size([1, usize::MAX]).is_err()); + } + + #[cfg(unix)] + #[test] + fn test_discovery_rejects_fifo_and_non_regular_json_without_hanging() { + use std::{process::Command, sync::mpsc, time::Duration}; + + let root = tempfile::tempdir().unwrap(); + std::fs::write(root.path().join("valid.json"), "{}").unwrap(); + let fifo = root.path().join("p.json"); + let status = Command::new("mkfifo").arg(&fifo).status().expect("mkfifo runs"); + assert!(status.success(), "mkfifo failed"); + + // Discovery of a directory containing a FIFO named `*.json` fails explicitly without + // hanging. + let err = find_json_files(root.path()).expect_err("FIFO named *.json must fail discovery"); + assert!(err.to_string().contains(&fifo.display().to_string()), "{err}"); + let err = find_json_files_in_paths(&[root.path().to_path_buf()]) + .expect_err("multi-path discovery must also reject FIFO named *.json"); + assert!(err.to_string().contains(&fifo.display().to_string()), "{err}"); + + // Passing the FIFO directly also fails discovery explicitly. + let err = find_json_files(&fifo).expect_err("direct FIFO input must fail discovery"); + assert!(err.to_string().contains(&fifo.display().to_string()), "{err}"); + + // Even a direct `read_fixture` call refuses the FIFO without blocking, because the Unix + // reader opens with `O_NONBLOCK` and validates the opened descriptor's file type. + let fifo_clone = fifo.clone(); + let (tx, rx) = mpsc::channel(); + std::thread::spawn(move || { + let _ = tx.send(read_fixture(&fifo_clone)); + }); + let read_res = rx + .recv_timeout(Duration::from_secs(2)) + .expect("read_fixture must reject FIFO without blocking"); + let read_err = read_res.expect_err("FIFO must not be read as a fixture"); + assert_eq!(read_err.kind(), io::ErrorKind::InvalidInput, "{read_err}"); + assert!(read_err.to_string().contains(&fifo.display().to_string()), "{read_err}"); + + // Exercise `open_nonblocking` directly on the FIFO (no pre-open metadata check) to prove + // the `O_NONBLOCK` open itself returns immediately and `read_with_limit` rejects the fd. + let (tx2, rx2) = mpsc::channel(); + std::thread::spawn(move || { + let res = open_nonblocking(&fifo).and_then(|file| read_with_limit(file, 64)); + let _ = tx2.send(res); + }); + let direct_err = rx2 + .recv_timeout(Duration::from_secs(2)) + .expect("O_NONBLOCK open on FIFO must not block") + .expect_err("descriptor validation must reject opened FIFO"); + assert_eq!(direct_err.kind(), io::ErrorKind::InvalidInput, "{direct_err}"); + + // A directory whose name ends in `.json` is also a non-regular entry and fails discovery. + let dir_root = tempfile::tempdir().unwrap(); + let json_dir = dir_root.path().join("nested.json"); + std::fs::create_dir(&json_dir).unwrap(); + std::fs::write(json_dir.join("real.json"), "{}").unwrap(); + let err = find_json_files(dir_root.path()) + .expect_err("directory named *.json must fail discovery"); + assert!(err.to_string().contains(&json_dir.display().to_string()), "{err}"); + } + + #[cfg(unix)] + #[test] + fn test_discovery_rejects_symlinked_directories_and_allows_regular_file_symlinks() { + use std::os::unix::fs::symlink; + + let root = tempfile::tempdir().unwrap(); + let real = root.path().join("real"); + std::fs::create_dir(&real).unwrap(); + let real_file = real.join("a.json"); + std::fs::write(&real_file, "{}").unwrap(); + + // 1. Symlinked subdirectory fails discovery explicitly with InvalidInput naming the path. + let top = root.path().join("top"); + std::fs::create_dir(&top).unwrap(); + std::fs::write(top.join("b.json"), "{}").unwrap(); + let sub_link = top.join("link"); + symlink(&real, &sub_link).unwrap(); + let err = find_json_files(&top).expect_err("symlinked subdirectory must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + assert!(err.to_string().contains(&sub_link.display().to_string()), "{err}"); + + // 2. Symlinked root directory (with or without trailing slash) fails discovery explicitly. + let root_link = root.path().join("root_link"); + symlink(&real, &root_link).unwrap(); + let err = + find_json_files(&root_link).expect_err("symlinked root directory must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + assert!(err.to_string().contains(&root_link.display().to_string()), "{err}"); + + let root_link_slash = PathBuf::from(format!("{}/", root_link.display())); + let err = find_json_files(&root_link_slash) + .expect_err("symlinked root directory with trailing slash must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + + // 3. Two links to one directory cannot cause duplicate execution (discovery fails). + let dup_root = root.path().join("dup"); + std::fs::create_dir(&dup_root).unwrap(); + symlink(&real, dup_root.join("first")).unwrap(); + symlink(&real, dup_root.join("second")).unwrap(); + let err = find_json_files(&dup_root) + .expect_err("multiple directory symlinks must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + + // 4. Symlink loop fails discovery explicitly. + let loop_root = tempfile::tempdir().unwrap(); + let sub = loop_root.path().join("sub"); + std::fs::create_dir(&sub).unwrap(); + let back = sub.join("back"); + symlink(loop_root.path(), &back).unwrap(); + let err = find_json_files(loop_root.path()).expect_err("symlink loop must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + assert!(err.to_string().contains(&back.display().to_string()), "{err}"); + + // 5. Symlink to a regular `*.json` file is permitted (both inside a directory and as root). + let file_link_root = root.path().join("file_links"); + std::fs::create_dir(&file_link_root).unwrap(); + let file_link = file_link_root.join("linked.json"); + symlink(&real_file, &file_link).unwrap(); + let files = find_json_files(&file_link_root).expect("regular file symlink is allowed"); + assert_eq!(files, vec![file_link.clone()]); + assert_eq!( + find_json_files(&file_link).expect("direct file symlink root"), + vec![file_link.clone()] + ); + assert_eq!(read_fixture(&file_link).expect("read regular file symlink"), "{}"); + + // 6. Symlink to a non-regular file (FIFO) named `*.json` fails discovery explicitly. + let fifo_target = root.path().join("pipe"); + let status = + std::process::Command::new("mkfifo").arg(&fifo_target).status().expect("mkfifo runs"); + assert!(status.success(), "mkfifo failed"); + let fifo_link_root = tempfile::tempdir().unwrap(); + let fifo_link = fifo_link_root.path().join("linked.json"); + symlink(&fifo_target, &fifo_link).unwrap(); + let err = find_json_files(fifo_link_root.path()) + .expect_err("symlink to FIFO named *.json must fail discovery"); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + assert!(err.to_string().contains(&fifo_link.display().to_string()), "{err}"); + } +} diff --git a/crates/mega-state-test/src/lib.rs b/crates/mega-state-test/src/lib.rs index d25456f3d..581de0f57 100644 --- a/crates/mega-state-test/src/lib.rs +++ b/crates/mega-state-test/src/lib.rs @@ -28,6 +28,7 @@ pub mod deviations; pub mod digest; pub mod exceptions; mod fork; +mod input; mod mode; pub mod roots; pub mod runner; diff --git a/crates/mega-state-test/src/runner.rs b/crates/mega-state-test/src/runner.rs index f53ff005e..5f22bca66 100644 --- a/crates/mega-state-test/src/runner.rs +++ b/crates/mega-state-test/src/runner.rs @@ -60,7 +60,6 @@ use serde::{ de::{Error as _, MapAccess, Visitor}, Deserialize, Deserializer, Serialize, }; -use walkdir::WalkDir; use crate::{ deviations::{self, Deviation}, @@ -69,7 +68,7 @@ use crate::{ mode::MAX_BLOBS_PER_TX, roots::{logs_hash, state_root}, skips::{skip_file, SkipReason}, - types::{Test, TestSuite, TestUnit}, + types::{Test, TestError, TestSuite, TestUnit}, Fork, Mode, }; @@ -451,20 +450,7 @@ impl fmt::Display for TestId { } } -/// Every JSON file under `path`, or `path` itself when it is a file, in path order. -pub fn find_json_files(path: &Path) -> Vec { - if path.is_file() { - return vec![path.to_path_buf()]; - } - let mut files: Vec<_> = WalkDir::new(path) - .into_iter() - .filter_map(Result::ok) - .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "json")) - .map(walkdir::DirEntry::into_path) - .collect(); - files.sort(); - files -} +pub use crate::input::{find_json_files, find_json_files_in_paths}; /// Executes every entry `files` define for `config.fork`, in `config.mode`, and reports each. pub fn run(files: &[PathBuf], config: Config) -> Report { @@ -503,7 +489,7 @@ pub fn run(files: &[PathBuf], config: Config) -> Report { /// the file could not be read. pub fn run_file(path: &Path, config: Config) -> Result, Failure> { let path_str = path.to_string_lossy().into_owned(); - let json = std::fs::read_to_string(path) + let json = crate::input::read_fixture(path) .map_err(|error| Failure::new(FailureKind::Fixture, format!("read: {error}")))?; let suite = serde_json::from_str::(&json) .map_err(|error| Failure::new(FailureKind::Fixture, format!("parse: {error}")))? @@ -821,6 +807,14 @@ pub(crate) fn prepare( test: &Test, ) -> Result, Failure> { let chain_id = chain_id(unit)?; + if let Some(excess) = unit.env.current_excess_blob_gas { + u64::try_from(excess).map_err(|_| { + Failure::new( + FailureKind::Fixture, + format!("currentExcessBlobGas {excess} does not fit a u64"), + ) + })?; + } let mut eth_cfg = CfgEnv::new(); eth_cfg.chain_id = chain_id; @@ -828,7 +822,16 @@ pub(crate) fn prepare( eth_cfg.set_max_blobs_per_tx(MAX_BLOBS_PER_TX); let block = unit.block_env(&mut eth_cfg); - let tx = match (test.tx_env(unit), &test.expect_exception) { + // An explicit type bypasses the fixture types' inferred-type recipient guard. Reject the + // built transaction through the same exception-matching path as an inferred invalid type. + let tx = test.tx_env(unit).and_then(|tx| { + if matches!(tx.tx_type, 3 | 4) && tx.kind.is_create() { + Err(TestError::InvalidTransactionType) + } else { + Ok(tx) + } + }); + let tx = match (tx, &test.expect_exception) { (Ok(tx), _) => tx, (Err(error), Some(expected)) => { return match exceptions::check_unbuildable(expected, &error, unit) { @@ -843,6 +846,8 @@ pub(crate) fn prepare( return Err(Failure::new(FailureKind::Fixture, format!("transaction: {error}"))) } }; + crate::input::check_state_size(unit.pre.values().map(|account| account.storage.len())) + .map_err(|error| Failure::new(FailureKind::Fixture, format!("pre-state: {error}")))?; let tx = OpTx(OpTransaction { base: tx, enveloped_tx: Some(Bytes::new()), ..Default::default() }); Ok(Ok(Ready { chain_id, block, tx })) @@ -913,6 +918,10 @@ fn check( } let logs: &[_] = result.as_ref().map(ExecutionResult::logs).unwrap_or_default(); let logs_hash = logs_hash(logs); + crate::input::check_state_size( + state.cache.trie_account().into_iter().map(|(_, account)| account.storage.len()), + ) + .map_err(|error| Failure::new(FailureKind::Fixture, format!("post-state: {error}")))?; let root = state_root(state.cache.trie_account()); let executed = result.as_ref().map(|r| format!("; {}", describe(r))).unwrap_or_default(); if logs_hash != test.logs { diff --git a/crates/mega-state-test/src/skips.rs b/crates/mega-state-test/src/skips.rs index 6b8ae5571..f357e4551 100644 --- a/crates/mega-state-test/src/skips.rs +++ b/crates/mega-state-test/src/skips.rs @@ -67,10 +67,6 @@ pub fn skip_file(path: &str) -> Option { "InitCollisionParis.json" | "test_init_collision_create_opcode.json" => Some(SkipReason::CreateCollisionWithStorage), "ValueOverflow.json" | "ValueOverflowParis.json" => Some(SkipReason::MalformedValue), - "Call50000_sha256.json" | - "static_Call50000_sha256.json" | - "loopMul.json" | - "CALLBlake2f_MaxRounds.json" => Some(SkipReason::Slow), _ => None, } } @@ -98,7 +94,14 @@ mod tests { Some(SkipReason::CreateCollisionWithStorage) ); assert_eq!(skip_file("a/ValueOverflow.json"), Some(SkipReason::MalformedValue)); - assert_eq!(skip_file("a/loopMul.json"), Some(SkipReason::Slow)); + for name in [ + "Call50000_sha256.json", + "static_Call50000_sha256.json", + "loopMul.json", + "CALLBlake2f_MaxRounds.json", + ] { + assert_eq!(skip_file(name), None); + } assert_eq!(skip_file("a/xloopMul.json"), None); assert_eq!(skip_file("a/loopMul.json.bak"), None); assert_eq!(skip_file("osaka/eip7951_p256verify_precompiles/test_valid.json"), None); diff --git a/crates/mega-state-test/tests/blockchain.rs b/crates/mega-state-test/tests/blockchain.rs index c46a66133..2621483bb 100644 --- a/crates/mega-state-test/tests/blockchain.rs +++ b/crates/mega-state-test/tests/blockchain.rs @@ -443,6 +443,39 @@ fn test_a_chain_filled_from_ethereum_passes() { .all(|p| !p.contains("executed"))); } +#[test] +fn test_oversized_fixture_fails_before_parsing() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("oversized.json"); + std::fs::File::create(&path).unwrap().set_len(256 * 1024 * 1024 + 1).unwrap(); + let failure = state_test::blockchain::run_file(&path, config(&[])).unwrap_err(); + assert_eq!(failure.kind, FailureKind::Fixture); + assert!(failure.detail.contains("exceeds 268435456 bytes"), "{failure:?}"); +} + +/// Input-state bounds reject the whole fixture before materialization or root construction. +#[test] +fn test_wide_pre_and_post_states_fail_instead_of_hashing_partial_state() { + let clean = chain(&[BlockSpec::valid(vec![])]); + assert_eq!(outcome(&run_one(clean.clone())), &Outcome::Passed); + for (field, label) in [("pre", "pre-state"), ("postState", "post-state")] { + let mut test = clean.clone(); + let accounts = (0..=100_000) + .map(|index| (format!("0x{index:040x}"), account(U256::ZERO, "0x", 0))) + .collect(); + test[field] = Value::Object(accounts); + let report = run_one(test); + let Outcome::Failed(failure) = outcome(&report) else { + panic!("oversized {label} was accepted: {report:?}") + }; + assert_eq!(failure.kind, FailureKind::Fixture); + assert!( + failure.detail.contains(&format!("{label}: state account count exceeds")), + "{failure:?}" + ); + } +} + /// A block the fixture expects to be invalid for its nonce is refused for that reason, and the /// chain stays at the previous block: the fixture's last block and post-state are the first /// block's. diff --git a/crates/mega-state-test/tests/runner.rs b/crates/mega-state-test/tests/runner.rs index 874234f57..2c2c6e3e8 100644 --- a/crates/mega-state-test/tests/runner.rs +++ b/crates/mega-state-test/tests/runner.rs @@ -271,7 +271,7 @@ fn test_what_is_counted_and_what_is_skipped() { json!("TransactionException.INTRINSIC_GAS_TOO_LOW"); let others = write( dir.path(), - "o.json", + "loopMul.json", json!({ "a_both": both, "b_expected_invalid": expected_invalid, @@ -300,6 +300,37 @@ fn test_what_is_counted_and_what_is_skipped() { ); } +/// An explicit transaction type must not bypass the fixture types' inferred-type rejection of +/// blob and EIP-7702 creates. Only the corresponding expected exception permits the skip. +#[test] +fn test_explicit_blob_and_authorization_creates_are_rejected_for_the_named_reason() { + let dir = tempfile::tempdir().unwrap(); + for (ty, exception) in [(3, "TYPE_3_TX_CONTRACT_CREATION"), (4, "TYPE_4_TX_CONTRACT_CREATION")] + { + let mut expected = unit(&[Fork::Osaka], json!({ "type": ty, "to": "" })); + expected["post"]["Osaka"][0]["expectException"] = + json!(format!("TransactionException.{exception}")); + let mut wrong = expected.clone(); + wrong["post"]["Osaka"][0]["expectException"] = + json!("TransactionException.INVALID_SIGNATURE_VRS"); + let missing = unit(&[Fork::Osaka], json!({ "type": ty, "to": "" })); + let path = write( + dir.path(), + &format!("type{ty}.json"), + json!({ "a_expected": expected, "b_wrong": wrong, "c_missing": missing }), + ); + let report = run(&[path], config(Mode::Equivalence, Fork::Osaka)); + let outcomes = outcomes(&report); + assert_eq!( + outcomes[0], + &Outcome::Skipped { reason: SkipReason::UnbuildableInvalidTransaction }, + "type {ty}" + ); + assert_eq!(failure_kind(outcomes[1]), Some(FailureKind::WrongException)); + assert_eq!(failure_kind(outcomes[2]), Some(FailureKind::Fixture)); + } +} + /// A test name that appears twice in a file fails the file, rather than the second test silently /// replacing the first. #[test] @@ -318,17 +349,22 @@ fn test_a_duplicate_test_name_fails_the_file() { ); } -/// A file that cannot be parsed is a failure of the run, not a set of skipped tests. +/// A malformed or oversized file fails the gate instead of becoming a set of skipped tests. #[test] fn test_an_unreadable_file_fails_the_gate() { let dir = tempfile::tempdir().unwrap(); - let path = write(dir.path(), "broken.json", json!({ "t": { "env": {} } })); - let report = run(&[path], config(Mode::Equivalence, Fork::Osaka)); - assert!(report.results.is_empty()); - assert_eq!(report.file_failures.len(), 1); - let summary = report.summary(); - assert_eq!((summary.file_failures, summary.defined), (1, 0)); - assert_eq!(report.gate(None, None, false), ["1 fixture files could not be read"]); + let broken = write(dir.path(), "broken.json", json!({ "t": { "env": {} } })); + let oversized = dir.path().join("oversized.json"); + std::fs::File::create(&oversized).unwrap().set_len(64 * 1024 * 1024 + 1).unwrap(); + for (path, detail) in [(broken, "parse:"), (oversized, "exceeds 67108864 bytes")] { + let report = run(&[path], config(Mode::Equivalence, Fork::Osaka)); + assert!(report.results.is_empty()); + assert_eq!(report.file_failures.len(), 1); + assert!(report.file_failures[0].1.detail.contains(detail), "{:?}", report.file_failures); + let summary = report.summary(); + assert_eq!((summary.file_failures, summary.defined), (1, 0)); + assert_eq!(report.gate(None, None, false), ["1 fixture files could not be read"]); + } } /// The base-fee vault Satin credits is taken back only when the fee routing made it: a vault the @@ -344,25 +380,58 @@ fn test_the_fee_vault_is_taken_back_only_when_the_routing_made_it() { assert_eq!(failure_kind(outcomes(&report)[0]), Some(FailureKind::StateRootMismatch)); } -/// A chain id that does not fit a `u64`, and a fixture value the transaction builder cannot take, -/// fail the test they belong to and nothing else. +/// Chain-id and blob-gas values wider than `u64`, and a value the transaction builder cannot +/// take, fail only the test they belong to. #[test] fn test_bad_fixture_values_fail_their_own_test() { let dir = tempfile::tempdir().unwrap(); let good = fill_from_ethereum(unit(&[Fork::Osaka], json!({}))); let mut chain = good.clone(); chain["env"]["currentChainID"] = json!("0x010000000000000000"); + let mut blob = good.clone(); + blob["env"]["currentExcessBlobGas"] = json!("0x010000000000000000"); + blob["post"]["Osaka"][0]["expectException"] = + json!("TransactionException.INVALID_SIGNATURE_VRS"); let fee = unit( &[Fork::Osaka], // One more than `u128::MAX`, which the fixture types' transaction builder refuses with a // panic rather than an error. json!({ "maxFeePerGas": "0x0a", "maxPriorityFeePerGas": "0x0100000000000000000000000000000000" }), ); - let path = - write(dir.path(), "a.json", json!({ "a_chain": chain, "b_fee": fee, "c_good": good })); + let path = write( + dir.path(), + "a.json", + json!({ + "a_chain": chain, "b_blob": blob, "c_fee": fee, "d_good": good, + }), + ); let report = run(&[path], config(Mode::Equivalence, Fork::Osaka)); let kinds: Vec<_> = outcomes(&report).into_iter().map(failure_kind).collect(); - assert_eq!(kinds, [Some(FailureKind::Fixture), Some(FailureKind::Panic), None]); + assert_eq!( + kinds, + [Some(FailureKind::Fixture), Some(FailureKind::Fixture), Some(FailureKind::Panic), None] + ); + let Outcome::Failed(blob) = &report.results[1].outcome else { panic!("must reject blob gas") }; + assert!(blob.detail.contains("currentExcessBlobGas")); +} + +/// A compact state can fit the input-byte bound and still exceed the account bound. It must +/// fail before the runner copies it into a database or computes a state root from it. +#[test] +fn test_wide_prestate_is_rejected_before_materializing_it() { + let dir = tempfile::tempdir().unwrap(); + let mut wide = unit(&[Fork::Osaka], json!({})); + let account = json!({ "balance": "0x0", "code": "0x", "nonce": "0x0", "storage": {} }); + wide["pre"] = Value::Object( + (0..=100_000).map(|index| (format!("0x{index:040x}"), account.clone())).collect(), + ); + let path = write(dir.path(), "wide.json", json!({ "wide": wide })); + assert!(std::fs::metadata(&path).unwrap().len() < 64 * 1024 * 1024); + let report = run(&[path], config(Mode::Equivalence, Fork::Osaka)); + let [result] = &report.results[..] else { panic!("must exercise one fixture entry") }; + let Outcome::Failed(failure) = &result.outcome else { panic!("must reject oversized state") }; + assert_eq!(failure.kind, FailureKind::Fixture); + assert!(failure.detail.contains("pre-state: state account count exceeds"), "{failure:?}"); } /// A registry of one Osaka deviation listing every failure of `report`, in the file at `path` diff --git a/crates/mega-state-test/tests/witness.rs b/crates/mega-state-test/tests/witness.rs index f3ca0b993..45028143d 100644 --- a/crates/mega-state-test/tests/witness.rs +++ b/crates/mega-state-test/tests/witness.rs @@ -132,7 +132,7 @@ fn test_a_sample_of_the_fixtures_replays_from_its_witness() { let mode: Mode = std::env::var("MEGA_STATE_TEST_MODE").map_or(Mode::Satin, |m| m.parse().expect("a mode")); - let files = find_json_files(&root); + let files = find_json_files(&root).expect("fixture discovery succeeds"); assert!(!files.is_empty(), "no fixtures under {}", root.display()); let stride = files.len().checked_div(sample).unwrap_or(1).max(1); let sampled: Vec<_> = diff --git a/crates/state-test/src/btest.rs b/crates/state-test/src/btest.rs index 16ffa19e9..b18930de7 100644 --- a/crates/state-test/src/btest.rs +++ b/crates/state-test/src/btest.rs @@ -13,7 +13,7 @@ use clap::Parser; use state_test::{ blockchain::{run, Config, Outcome, Report, SkipReason, Summary, TestResult, NETWORK}, deviations, - runner::find_json_files, + runner::find_json_files_in_paths, }; /// How many unattributed failures, and how many unreproduced tests, the summary lists. @@ -77,19 +77,13 @@ pub(crate) fn main(args: impl IntoIterator) -> ExitCo return ExitCode::FAILURE; } } - let mut files = Vec::new(); - for path in &cmd.paths { - if !path.exists() { - eprintln!("error: {} does not exist", path.display()); + let files = match find_json_files_in_paths(&cmd.paths) { + Ok(files) => files, + Err(error) => { + eprintln!("error: {error}"); return ExitCode::FAILURE; } - let found = find_json_files(path); - if found.is_empty() { - eprintln!("error: no JSON fixtures under {}", path.display()); - return ExitCode::FAILURE; - } - files.extend(found); - } + }; let threads = cmd.threads.unwrap_or_else(|| std::thread::available_parallelism().map_or(1, |n| n.get())); diff --git a/crates/state-test/src/main.rs b/crates/state-test/src/main.rs index de8bdec7e..84241ad7d 100644 --- a/crates/state-test/src/main.rs +++ b/crates/state-test/src/main.rs @@ -17,7 +17,9 @@ use std::{path::PathBuf, process::ExitCode}; use clap::Parser; use state_test::{ deviations, - runner::{find_json_files, run, Config, DigestForm, Outcome, Report, Summary, TestResult}, + runner::{ + find_json_files_in_paths, run, Config, DigestForm, Outcome, Report, Summary, TestResult, + }, EvmTxRuntimeLimits, Fork, Mode, }; @@ -110,19 +112,13 @@ fn main() -> ExitCode { return btest::main(std::env::args_os().skip(1)); } let cmd = Cmd::parse(); - let mut files = Vec::new(); - for path in &cmd.paths { - if !path.exists() { - eprintln!("error: {} does not exist", path.display()); - return ExitCode::FAILURE; - } - let found = find_json_files(path); - if found.is_empty() { - eprintln!("error: no JSON fixtures under {}", path.display()); + let files = match find_json_files_in_paths(&cmd.paths) { + Ok(files) => files, + Err(error) => { + eprintln!("error: {error}"); return ExitCode::FAILURE; } - files.extend(found); - } + }; let threads = if cmd.trace { 1 diff --git a/crates/state-test/tests/cli.rs b/crates/state-test/tests/cli.rs index a979e0271..6f4f93eb7 100644 --- a/crates/state-test/tests/cli.rs +++ b/crates/state-test/tests/cli.rs @@ -527,3 +527,35 @@ fn test_btest_bad_arguments_exit_non_zero() { std::fs::write(dir.path().join("broken.json"), "{").unwrap(); assert_eq!(btest(&[], dir.path()).status.code(), Some(1), "an unreadable fixture"); } + +fn check_discovery_depth(args: &[&str]) { + let root = tempfile::tempdir().unwrap(); + std::fs::write(root.path().join("visible.json"), "{}").unwrap(); + let mut nested = root.path().to_path_buf(); + for _ in 0..63 { + nested.push("d"); + } + std::fs::create_dir_all(&nested).unwrap(); + std::fs::write(nested.join("boundary.json"), "{}").unwrap(); + let run = || state_test(args, root.path()); + let boundary = run(); + assert!(boundary.status.success(), "{}", String::from_utf8_lossy(&boundary.stderr)); + assert!(String::from_utf8_lossy(&boundary.stdout).contains("2 files")); + + nested.push("one-too-deep"); + std::fs::create_dir(&nested).unwrap(); + std::fs::write(nested.join("hidden.json"), "{}").unwrap(); + let output = run(); + assert!(!output.status.success(), "deep fixture tree was accepted"); + assert!(String::from_utf8_lossy(&output.stderr).contains("depth exceeds 64")); +} + +#[test] +fn test_state_cli_refuses_excessive_fixture_depth() { + check_discovery_depth(&["--fork", "Osaka"]); +} + +#[test] +fn test_blockchain_cli_refuses_excessive_fixture_depth() { + check_discovery_depth(&["btest"]); +} diff --git a/docs/mega-evme/commands/replay.md b/docs/mega-evme/commands/replay.md index cf7682795..9225211d7 100644 --- a/docs/mega-evme/commands/replay.md +++ b/docs/mega-evme/commands/replay.md @@ -8,6 +8,13 @@ Re-execute a historical transaction locally using an RPC endpoint or a previousl In online mode, `mega-evme` fetches the transaction, block environment, and pre-state from the RPC and re-executes locally. In offline mode (`--rpc.replay-file`), all data is served from a local fixture captured by an earlier run — no network access is required. +A Satin transaction replay requires a mined transaction in a block after genesis. +Pending transactions have no canonical block context and are refused. +Before reading the parent state, replay checks that the fetched parent has the preceding block number and the hash named by the transaction's block, and then issues pre-state reads (`eth_getBalance`, `eth_getCode`, `eth_getStorageAt`, `eth_getTransactionCount`) against that verified parent hash using EIP-1898 (`{"blockHash": ..., "requireCanonical": true}`) rather than by block number. +`BLOCKHASH` queries resolve the parent directly from that verified header and walk earlier ancestors on demand by `parentHash` (`eth_getBlockByHash`, checking each header's hash and number across the 256-block window) instead of fetching ancestors by block number. +A header mismatch, broken ancestor link, or non-canonical parent response from the RPC server aborts the replay (cached hash-keyed hits and offline replay serve their recorded hash-bound entries); retry with a consistent RPC endpoint or recording. +Because Satin pre-state reads are keyed by the EIP-1898 parent block hash and `BLOCKHASH` ancestors are fetched via `eth_getBlockByHash` instead of block number, per-endpoint cache entries (`--rpc.cache-dir`) and `--rpc.capture-file` / `--rpc.replay-file` keys differ from height-keyed captures; Satin replay files captured before this change must be re-captured. + With `--block`, `replay` re-executes whole blocks instead and compares every receipt with the chain's; see [Replaying Whole Blocks](#replaying-whole-blocks). ## Usage diff --git a/docs/mega-evme/configuration/state-management.md b/docs/mega-evme/configuration/state-management.md index eb47a04b4..dc9950b42 100644 --- a/docs/mega-evme/configuration/state-management.md +++ b/docs/mega-evme/configuration/state-management.md @@ -227,10 +227,15 @@ When using fork mode, `mega-evme` caches RPC responses to avoid redundant networ These options take effect only when an RPC endpoint is actually contacted: on `run` and `tx` that requires `--fork` (without `--fork` they are accepted but silently ignored); on `replay` they apply to its `--rpc` fetches (an offline `--rpc.replay-file` run contacts no endpoint and does not use them). +On Satin, RPC URL diagnostics show only the scheme, host and port: userinfo, path, query and fragment are hidden. +Every JSON-RPC error response replaces the server's error message and `data` with a fixed local diagnostic (`server error message omitted`) while keeping the JSON-RPC error code, so server error text is never forwarded. +This applies to verbose logs and log files as well as stderr. + ### Per-Endpoint Cache Files Each endpoint of a chain gets its own cache file named `rpc-cache-{chain_id}-{endpoint}.json`. `{endpoint}` is a short hash of the URL with any userinfo removed, so the name does not contain the URL. +The hash still includes the path and query, even though diagnostic output hides them, so endpoints that differ there keep separate cache files. A cache populated from one endpoint cannot be loaded for another, including another endpoint of the same chain. The default cache directory is the platform cache directory: diff --git a/docs/mega-evme/overview.md b/docs/mega-evme/overview.md index 05b52fedc..18a65ed19 100644 --- a/docs/mega-evme/overview.md +++ b/docs/mega-evme/overview.md @@ -110,10 +110,10 @@ Text a person reads on a terminal is filtered: escape sequences, control charact This covers error lines and a transaction's revert reason. `--json` output is not filtered: a JSON string carries the same characters, escaped. -The URL given to `--rpc` is written to no log line, error or cache file name with its userinfo (`user:password@`). -The userinfo is the part of the authority before its last `@`, and it is removed from a URL written without its scheme too. -A credential anywhere else in the URL, such as an API key in the path or the query, is not recognised and is printed with the URL. +On Satin, `--rpc` diagnostics (logs, errors, and `--log.file`) show only the scheme, host and port: userinfo, path, query and fragment are hidden, and every JSON-RPC error response replaces the server's message and `data` with a fixed local diagnostic while preserving the numeric error code. +Per-endpoint cache file names hash the URL with userinfo removed so endpoints with different paths or queries stay separate without placing the URL in the file name. The URL has to start with `http://` or `https://`. +On the legacy 1.7.1 engine, cache files are named `rpc-cache-{chain_id}.json` (without an endpoint component) and `--rpc` diagnostics are unchanged from 1.7.1. ## Read more