Skip to main content

reth_rpc_eth_types/
simulate.rs

1//! Utilities for serving `eth_simulateV1`
2
3use crate::{
4    error::{api::FromEthApiError, FromEvmError, ToRpcError},
5    EthApiError,
6};
7use alloy_chains::Chain;
8use alloy_consensus::{transaction::TxHashRef, BlockHeader, Transaction as _};
9use alloy_eips::eip2718::WithEncoded;
10use alloy_evm::{block::TxResult, precompiles::PrecompilesMap};
11use alloy_network::{NetworkTransactionBuilder, TransactionBuilder};
12use alloy_rpc_types_eth::{
13    simulate::{SimBlock, SimCallResult, SimulateError, SimulatedBlock},
14    state::StateOverride,
15    BlockId, BlockOverrides, BlockTransactionsKind,
16};
17use jsonrpsee_types::{error::INTERNAL_ERROR_CODE, ErrorObject};
18use reth_evm::{
19    execute::{BlockBuilder, BlockBuilderOutcome, BlockExecutor},
20    Evm, HaltReasonFor,
21};
22use reth_primitives_traits::{
23    BlockBody as _, BlockTy, NodePrimitives, Recovered, RecoveredBlock, SealedHeader,
24};
25use reth_rpc_convert::{RpcBlock, RpcConvert, RpcTxReq};
26use reth_rpc_server_types::result::{block_id_to_str, rpc_err};
27use reth_storage_api::{noop::NoopProvider, StateProvider};
28use revm::{
29    context::Block,
30    context_interface::result::ExecutionResult,
31    primitives::{Address, Bytes, TxKind, U256},
32    Database,
33};
34
35/// Fallback seconds added between simulated block timestamps when neither the user nor the chain
36/// hint provides a value.
37const SIMULATE_FALLBACK_TIMESTAMP_INCREMENT: u64 = 12;
38
39/// Error code for execution reverted in `eth_simulateV1`.
40///
41/// Consistent with `eth_call` revert error code.
42///
43/// <https://github.com/ethereum/execution-apis/pull/748>
44pub const SIMULATE_REVERT_CODE: i32 = 3;
45
46/// Error code for VM execution errors (e.g., out of gas) in `eth_simulateV1`.
47///
48/// <https://github.com/ethereum/execution-apis>
49pub const SIMULATE_VM_ERROR_CODE: i32 = -32015;
50
51/// Errors which may occur during `eth_simulateV1` execution.
52#[derive(Debug, thiserror::Error)]
53pub enum EthSimulateError {
54    /// Total gas limit of transactions for the block exceeds the block gas limit.
55    #[error("Block gas limit exceeded by the block's transactions")]
56    BlockGasLimitExceeded,
57    /// Number of simulated blocks exceeds the configured client limit.
58    #[error("too many blocks")]
59    TooManyBlocks,
60    /// Max gas limit for entire operation exceeded.
61    #[error("Client adjustable limit reached")]
62    GasLimitReached,
63    /// Base block for the simulation was not found.
64    #[error("block not found: {}", block_id_to_str(*block))]
65    BlockNotFound {
66        /// The block id that was requested.
67        block: BlockId,
68    },
69    /// Block number in sequence did not increase.
70    #[error("block numbers must be in order: {got} <= {parent}")]
71    BlockNumberInvalid {
72        /// The block number that was provided.
73        got: u64,
74        /// The parent block number.
75        parent: u64,
76    },
77    /// Block timestamp in sequence did not increase.
78    #[error("block timestamps must be in order: {got} <= {parent}")]
79    BlockTimestampInvalid {
80        /// The block timestamp that was provided.
81        got: u64,
82        /// The parent block timestamp.
83        parent: u64,
84    },
85    /// Transaction nonce is too low.
86    #[error("nonce too low: next nonce {state}, tx nonce {tx}")]
87    NonceTooLow {
88        /// Transaction nonce.
89        tx: u64,
90        /// Current state nonce.
91        state: u64,
92    },
93    /// Transaction nonce is too high.
94    #[error("nonce too high")]
95    NonceTooHigh,
96    /// Transaction nonce cannot be incremented.
97    #[error("nonce has max value")]
98    NonceMaxValue,
99    /// Transaction's baseFeePerGas is too low.
100    #[error("max fee per gas less than block base fee")]
101    BaseFeePerGasTooLow,
102    /// Not enough gas provided to pay for intrinsic gas.
103    #[error("intrinsic gas too low")]
104    IntrinsicGasTooLow,
105    /// Insufficient funds to pay for gas fees and value.
106    #[error("insufficient funds for gas * price + value: have {balance} want {cost}")]
107    InsufficientFunds {
108        /// Transaction cost.
109        cost: U256,
110        /// Sender balance.
111        balance: U256,
112    },
113    /// Sender is not an EOA.
114    #[error("sender is not an EOA")]
115    SenderNotEOA,
116    /// Max init code size exceeded.
117    #[error("max initcode size exceeded")]
118    MaxInitCodeSizeExceeded,
119    /// Attempted to move a non-precompile address.
120    #[error("account {0} is not a precompile")]
121    NotAPrecompile(Address),
122    /// Attempted to move a precompile to its own address.
123    #[error("cannot move precompile {0} to itself")]
124    MovePrecompileToSelf(Address),
125}
126
127impl EthSimulateError {
128    /// Returns the JSON-RPC error code for a `eth_simulateV1` error.
129    pub const fn error_code(&self) -> i32 {
130        match self {
131            Self::NonceTooLow { .. } => -38010,
132            Self::NonceTooHigh => -38011,
133            Self::NonceMaxValue => INTERNAL_ERROR_CODE,
134            Self::BaseFeePerGasTooLow => -38012,
135            Self::IntrinsicGasTooLow => -38013,
136            Self::InsufficientFunds { .. } => -38014,
137            Self::BlockGasLimitExceeded => -38015,
138            Self::BlockNumberInvalid { .. } => -38020,
139            Self::BlockTimestampInvalid { .. } => -38021,
140            Self::SenderNotEOA => -38024,
141            Self::MaxInitCodeSizeExceeded => -38025,
142            Self::TooManyBlocks | Self::GasLimitReached => -38026,
143            Self::MovePrecompileToSelf(_) => -38022,
144            Self::BlockNotFound { .. } | Self::NotAPrecompile(_) => -32000,
145        }
146    }
147}
148
149impl ToRpcError for EthSimulateError {
150    fn to_rpc_error(&self) -> ErrorObject<'static> {
151        rpc_err(self.error_code(), self.to_string(), None)
152    }
153}
154
155/// Sanitizes and gap-fills the chain of [`SimBlock`]s for `eth_simulateV1`.
156///
157/// Walks the provided block-state calls in order and:
158/// - validates that each block number and timestamp strictly increases relative to the parent and
159///   prior simulated block;
160/// - inserts empty filler blocks for every gap in block numbers, so a request like `[block at
161///   number N + k]` over a parent at `N` expands to `k - 1` empty blocks followed by the requested
162///   one (per the execution-apis spec: "If the block number is increased more than `1` compared to
163///   the previous block, new empty blocks are generated in between.");
164/// - assigns default block numbers (`prev_number + 1`) and timestamps (`prev_timestamp + chain
165///   block time`) when missing, so every returned entry has explicit `number` and `time` overrides.
166///   The block time defaults to [`Chain::average_blocktime_hint`] for the chain id, falling back to
167///   `SIMULATE_FALLBACK_TIMESTAMP_INCREMENT` when no hint is registered. Sub-second chain hints are
168///   rounded up because block timestamps are second-granular;
169/// - enforces the global `max_simulate_blocks` cap on the total number of blocks (including
170///   generated fillers).
171pub fn sanitize_chain<TxReq, H>(
172    blocks: Vec<SimBlock<TxReq>>,
173    parent: &SealedHeader<H>,
174    chain_id: u64,
175    max_simulate_blocks: u64,
176) -> Result<Vec<SimBlock<TxReq>>, EthApiError>
177where
178    H: BlockHeader,
179{
180    let timestamp_increment = Chain::from(chain_id)
181        .average_blocktime_hint()
182        .map(|d| d.as_secs().saturating_add(u64::from(d.subsec_nanos() > 0)))
183        .filter(|&s| s > 0)
184        .unwrap_or(SIMULATE_FALLBACK_TIMESTAMP_INCREMENT);
185
186    let mut out = Vec::with_capacity(blocks.len());
187    let base_number = parent.number();
188    let mut prev_number = base_number;
189    let mut prev_timestamp = parent.timestamp();
190
191    for mut block in blocks {
192        let overrides = block.block_overrides.get_or_insert_with(BlockOverrides::default);
193
194        // Default block number to prev + 1 if not specified.
195        let target_number = if let Some(n) = overrides.number {
196            u64::try_from(n).unwrap_or(u64::MAX)
197        } else {
198            let n = prev_number.saturating_add(1);
199            overrides.number = Some(U256::from(n));
200            n
201        };
202
203        if target_number <= prev_number {
204            return Err(EthApiError::other(EthSimulateError::BlockNumberInvalid {
205                got: target_number,
206                parent: prev_number,
207            }));
208        }
209
210        if target_number.saturating_sub(base_number) > max_simulate_blocks {
211            return Err(EthApiError::other(EthSimulateError::TooManyBlocks));
212        }
213
214        // Insert empty filler blocks for any gap between prev_number and target_number.
215        let gap = target_number - prev_number;
216        if gap > 1 {
217            for i in 1..gap {
218                let filler_number = prev_number + i;
219                let filler_time = prev_timestamp + timestamp_increment;
220                out.push(SimBlock {
221                    block_overrides: Some(BlockOverrides {
222                        number: Some(U256::from(filler_number)),
223                        time: Some(filler_time),
224                        ..Default::default()
225                    }),
226                    state_overrides: None,
227                    calls: Vec::new(),
228                });
229                prev_timestamp = filler_time;
230            }
231        }
232
233        prev_number = target_number;
234        // Default timestamp to prev + increment if not specified, otherwise validate ordering.
235        let block_time = if let Some(t) = overrides.time {
236            if t <= prev_timestamp {
237                return Err(EthApiError::other(EthSimulateError::BlockTimestampInvalid {
238                    got: t,
239                    parent: prev_timestamp,
240                }));
241            }
242            t
243        } else {
244            let t = prev_timestamp + timestamp_increment;
245            overrides.time = Some(t);
246            t
247        };
248        prev_timestamp = block_time;
249
250        out.push(block);
251    }
252
253    Ok(out)
254}
255
256/// Applies precompile move overrides from state overrides to the EVM's precompiles map.
257///
258/// This function processes `movePrecompileToAddress` entries from the state overrides and
259/// moves precompiles from their original addresses to new addresses. The original address
260/// is cleared (precompile removed) and the precompile is installed at the destination address.
261pub fn apply_precompile_overrides(
262    state_overrides: &StateOverride,
263    precompiles: &mut PrecompilesMap,
264) -> Result<(), EthSimulateError> {
265    let moves: Vec<_> = state_overrides
266        .iter()
267        .filter_map(|(source, account_override)| {
268            account_override.move_precompile_to.map(|dest| (*source, dest))
269        })
270        .collect();
271
272    for (source, dest) in &moves {
273        if source == dest {
274            if precompiles.get(source).is_none() {
275                return Err(EthSimulateError::NotAPrecompile(*source))
276            }
277            return Err(EthSimulateError::MovePrecompileToSelf(*source))
278        }
279    }
280
281    precompiles.move_precompiles(moves).map_err(
282        |alloy_evm::precompiles::MovePrecompileError::NotAPrecompile(addr)| {
283            EthSimulateError::NotAPrecompile(addr)
284        },
285    )?;
286
287    Ok(())
288}
289
290/// Converts all [`TransactionRequest`]s into [`Recovered`] transactions and applies them to the
291/// given [`BlockExecutor`].
292///
293/// Returns all executed transactions and the result of the execution.
294///
295/// For each call without an explicit `gas` field, the remaining block gas is used as the default.
296/// The RPC gas cap is tracked as a request-wide remaining budget and caps each call before
297/// execution. This matches the spec rule `"gasLimit: blockGasLimit - soFarUsedGasInBlock"` and
298/// geth's per-call `sanitizeCall` behavior.
299///
300/// [`TransactionRequest`]: alloy_rpc_types_eth::TransactionRequest
301#[expect(clippy::type_complexity)]
302pub fn execute_transactions<S, T>(
303    mut builder: S,
304    state_provider: impl StateProvider,
305    calls: Vec<RpcTxReq<T::Network>>,
306    remaining_call_gas_limit: &mut Option<u64>,
307    chain_id: u64,
308    compute_state_root: bool,
309    converter: &T,
310) -> Result<
311    (
312        BlockBuilderOutcome<S::Primitives>,
313        Vec<ExecutionResult<<<S::Executor as BlockExecutor>::Evm as Evm>::HaltReason>>,
314    ),
315    EthApiError,
316>
317where
318    S: BlockBuilder<Executor: BlockExecutor<Evm: Evm<DB: Database<Error: Into<EthApiError>>>>>,
319    T: RpcConvert<Primitives = S::Primitives>,
320{
321    builder.apply_pre_execution_changes()?;
322
323    let mut results = Vec::with_capacity(calls.len());
324    let mut cumulative_tx_gas_used: u64 = 0;
325    let mut block_regular_gas_used: u64 = 0;
326    let mut block_state_gas_used: u64 = 0;
327    let block_gas_limit = builder.evm().block().gas_limit();
328    let is_amsterdam = builder.evm().cfg_env().enable_amsterdam_eip8037;
329    let tx_gas_limit_cap = builder.evm().cfg_env().tx_gas_limit_cap.unwrap_or(u64::MAX);
330    for mut call in calls {
331        let block_gas_remaining = if is_amsterdam {
332            block_gas_limit
333                .saturating_sub(block_regular_gas_used)
334                .min(block_gas_limit.saturating_sub(block_state_gas_used))
335        } else {
336            block_gas_limit.saturating_sub(cumulative_tx_gas_used)
337        };
338        let mut default_gas_limit = block_gas_remaining;
339
340        if let Some(gas_limit) = call.as_ref().gas_limit() {
341            let exceeds_gas_limit = if is_amsterdam {
342                let regular_available_gas = block_gas_limit.saturating_sub(block_regular_gas_used);
343                let state_available_gas = block_gas_limit.saturating_sub(block_state_gas_used);
344                let regular_tx_gas_limit = gas_limit.min(tx_gas_limit_cap);
345
346                regular_tx_gas_limit > regular_available_gas || gas_limit > state_available_gas
347            } else {
348                gas_limit > block_gas_remaining
349            };
350
351            if exceeds_gas_limit {
352                return Err(EthApiError::other(EthSimulateError::BlockGasLimitExceeded))
353            }
354        }
355
356        if let Some(remaining_call_gas_limit) = *remaining_call_gas_limit {
357            if let Some(gas_limit) = call.as_ref().gas_limit() {
358                if gas_limit > remaining_call_gas_limit {
359                    call.as_mut().set_gas_limit(remaining_call_gas_limit);
360                }
361            } else {
362                default_gas_limit = default_gas_limit.min(remaining_call_gas_limit);
363            }
364        }
365
366        // Resolve transaction, populate missing fields and enforce calls
367        // correctness.
368        let tx = resolve_transaction(
369            call,
370            default_gas_limit,
371            builder.evm().block().basefee(),
372            chain_id,
373            builder.evm().cfg_env().disable_nonce_check,
374            builder.evm_mut().db_mut(),
375            converter,
376        )?;
377        // Create transaction with an empty envelope.
378        // The effect for a layer-2 execution client is that it does not charge L1 cost.
379        let tx = WithEncoded::new(Default::default(), tx);
380
381        let mut tx_regular_gas_used = 0;
382        let gas_output = builder.execute_transaction_with_result_closure(tx, |result| {
383            tx_regular_gas_used = result.result().result.gas().block_regular_gas_used();
384            results.push(result.result().result.clone())
385        })?;
386
387        let gas_used = gas_output.tx_gas_used();
388        if let Some(remaining_call_gas_limit) = remaining_call_gas_limit.as_mut() {
389            if gas_used > *remaining_call_gas_limit {
390                return Err(EthApiError::other(EthSimulateError::GasLimitReached))
391            }
392            *remaining_call_gas_limit -= gas_used;
393        }
394
395        cumulative_tx_gas_used = cumulative_tx_gas_used.saturating_add(gas_used);
396        block_regular_gas_used = block_regular_gas_used.saturating_add(tx_regular_gas_used);
397        block_state_gas_used = block_state_gas_used.saturating_add(gas_output.state_gas_used());
398    }
399
400    let result = if compute_state_root {
401        builder.finish(state_provider, None)?
402    } else {
403        builder.finish(NoopProvider::default(), None)?
404    };
405
406    Ok((result, results))
407}
408
409/// Goes over the list of [`TransactionRequest`]s and populates missing fields trying to resolve
410/// them into primitive transactions.
411///
412/// This will set the defaults as defined in <https://github.com/ethereum/execution-apis/blob/e56d3208789259d0b09fa68e9d8594aa4d73c725/docs/ethsimulatev1-notes.md#default-values-for-transactions>
413///
414/// [`TransactionRequest`]: alloy_rpc_types_eth::TransactionRequest
415pub fn resolve_transaction<DB: Database, Tx, T>(
416    mut tx: RpcTxReq<T::Network>,
417    default_gas_limit: u64,
418    block_base_fee_per_gas: u64,
419    chain_id: u64,
420    disable_nonce_check: bool,
421    db: &mut DB,
422    converter: &T,
423) -> Result<Recovered<Tx>, EthApiError>
424where
425    DB::Error: Into<EthApiError>,
426    T: RpcConvert<Primitives: NodePrimitives<SignedTx = Tx>>,
427{
428    // If we're missing any fields we try to fill nonce, gas and
429    // gas price.
430    let tx_type = tx.as_ref().output_tx_type();
431
432    let from = if let Some(from) = tx.as_ref().from() {
433        from
434    } else {
435        tx.as_mut().set_from(Address::ZERO);
436        Address::ZERO
437    };
438
439    if tx.as_ref().nonce().is_none() {
440        tx.as_mut().set_nonce(
441            db.basic(from).map_err(Into::into)?.map(|acc| acc.nonce).unwrap_or_default(),
442        );
443    }
444    // eth_simulateV1 validation-off mode behaves like eth_call; avoid revm's max-nonce guard.
445    if disable_nonce_check && tx.as_ref().nonce() == Some(u64::MAX) {
446        tx.as_mut().set_nonce(0);
447    }
448
449    if tx.as_ref().gas_limit().is_none() {
450        tx.as_mut().set_gas_limit(default_gas_limit);
451    }
452
453    if tx.as_ref().chain_id().is_none() {
454        tx.as_mut().set_chain_id(chain_id);
455    }
456
457    if tx.as_ref().kind().is_none() {
458        tx.as_mut().set_kind(TxKind::Create);
459    }
460
461    // if we can't build the _entire_ transaction yet, fill the fee fields.
462    //
463    // Per the eth_simulateV1 spec, unspecified fee fields default to 0 (not the block base fee),
464    // matching geth's `CallDefaults` behavior. This lets simulation behave like a free-gas
465    // `eth_call` when validation is off, and surfaces "max fee per gas less than block base fee"
466    // errors when validation is on with a real base fee.
467    let _ = block_base_fee_per_gas;
468    if tx.as_ref().output_tx_type_checked().is_none() {
469        if tx_type.is_legacy() || tx_type.is_eip2930() {
470            if tx.as_ref().gas_price().is_none() {
471                tx.as_mut().set_gas_price(0);
472            }
473        } else {
474            if tx.as_ref().max_fee_per_gas().is_none() {
475                tx.as_mut().set_max_fee_per_gas(0);
476            }
477            if tx.as_ref().max_priority_fee_per_gas().is_none() {
478                tx.as_mut().set_max_priority_fee_per_gas(0);
479            }
480        }
481    }
482
483    let tx =
484        converter.build_simulate_v1_transaction(tx).map_err(|e| EthApiError::other(e.into()))?;
485
486    Ok(Recovered::new_unchecked(tx, from))
487}
488
489/// Handles outputs of the calls execution and builds a [`SimulatedBlock`].
490pub fn build_simulated_block<Err, T>(
491    block: RecoveredBlock<BlockTy<T::Primitives>>,
492    results: Vec<ExecutionResult<HaltReasonFor<T::Evm>>>,
493    txs_kind: BlockTransactionsKind,
494    converter: &T,
495) -> Result<SimulatedBlock<RpcBlock<T::Network>>, Err>
496where
497    Err: std::error::Error
498        + FromEthApiError
499        + FromEvmError<T::Evm>
500        + From<T::Error>
501        + Into<jsonrpsee_types::ErrorObject<'static>>,
502    T: RpcConvert,
503{
504    let mut calls: Vec<SimCallResult> = Vec::with_capacity(results.len());
505
506    let mut log_index = 0;
507    for (index, (result, tx)) in results.into_iter().zip(block.body().transactions()).enumerate() {
508        let call = match result {
509            ExecutionResult::Halt { reason, gas, .. } => {
510                let error = Err::from_evm_halt(reason, tx.gas_limit());
511                SimCallResult {
512                    return_data: Bytes::new(),
513                    error: Some(SimulateError {
514                        message: error.to_string(),
515                        code: SIMULATE_VM_ERROR_CODE,
516                        ..SimulateError::invalid_params()
517                    }),
518                    gas_used: gas.tx_gas_used(),
519                    max_used_gas: Some(gas.total_gas_spent().max(gas.floor_gas())),
520                    logs: Vec::new(),
521                    status: false,
522                }
523            }
524            ExecutionResult::Revert { output, gas, .. } => {
525                let error = Err::from_revert(output.clone());
526                SimCallResult {
527                    return_data: Bytes::new(),
528                    error: Some(SimulateError {
529                        message: error.to_string(),
530                        code: SIMULATE_REVERT_CODE,
531                        data: Some(output),
532                    }),
533                    gas_used: gas.tx_gas_used(),
534                    max_used_gas: Some(gas.total_gas_spent().max(gas.floor_gas())),
535                    status: false,
536                    logs: Vec::new(),
537                }
538            }
539            ExecutionResult::Success { output, gas, logs, .. } => SimCallResult {
540                return_data: output.into_data(),
541                error: None,
542                gas_used: gas.tx_gas_used(),
543                max_used_gas: Some(gas.total_gas_spent().max(gas.floor_gas())),
544                logs: logs
545                    .into_iter()
546                    .map(|log| {
547                        log_index += 1;
548                        alloy_rpc_types_eth::Log {
549                            inner: log,
550                            log_index: Some(log_index - 1),
551                            transaction_index: Some(index as u64),
552                            transaction_hash: Some(*tx.tx_hash()),
553                            block_hash: Some(block.hash()),
554                            block_number: Some(block.header().number()),
555                            block_timestamp: Some(block.header().timestamp()),
556                            ..Default::default()
557                        }
558                    })
559                    .collect(),
560                status: true,
561            },
562        };
563
564        calls.push(call);
565    }
566
567    let block = block.into_rpc_block(
568        txs_kind,
569        |tx, tx_info| converter.fill(tx, tx_info),
570        |header, size| converter.convert_header(header, size),
571    )?;
572    Ok(SimulatedBlock { inner: block, calls })
573}
574
575#[cfg(test)]
576mod tests {
577    use super::{
578        apply_precompile_overrides, sanitize_chain, EthSimulateError, INTERNAL_ERROR_CODE,
579    };
580    use crate::{error::ToRpcError, EthApiError};
581    use alloy_chains::Chain;
582    use alloy_consensus::Header;
583    use alloy_evm::precompiles::PrecompilesMap;
584    use alloy_primitives::{address, U256};
585    use alloy_rpc_types_eth::{
586        simulate::SimBlock,
587        state::{AccountOverride, StateOverride},
588        BlockOverrides, TransactionRequest,
589    };
590    use reth_primitives_traits::SealedHeader;
591    use revm::precompile::Precompiles;
592
593    #[test]
594    fn nonce_max_value_error_uses_internal_error_code() {
595        let err = EthSimulateError::NonceMaxValue.to_rpc_error();
596
597        assert_eq!(err.code(), INTERNAL_ERROR_CODE);
598        assert_eq!(err.message(), "nonce has max value");
599    }
600
601    #[test]
602    fn block_not_found_error_uses_simulate_code() {
603        let err = EthSimulateError::BlockNotFound { block: 100000.into() }.to_rpc_error();
604
605        assert_eq!(err.code(), -32000);
606        assert_eq!(err.message(), "block not found: 0x186a0");
607    }
608
609    fn parent_at(number: u64, timestamp: u64) -> SealedHeader<Header> {
610        SealedHeader::seal_slow(Header { number, timestamp, ..Default::default() })
611    }
612
613    fn block_with_number(number: u64) -> SimBlock<TransactionRequest> {
614        SimBlock {
615            block_overrides: Some(BlockOverrides {
616                number: Some(U256::from(number)),
617                ..Default::default()
618            }),
619            ..Default::default()
620        }
621    }
622
623    #[test]
624    fn precompile_self_move_requires_existing_precompile() {
625        let address = address!("c100000000000000000000000000000000000000");
626        let mut state_overrides = StateOverride::default();
627        state_overrides.insert(
628            address,
629            AccountOverride { move_precompile_to: Some(address), ..Default::default() },
630        );
631        let mut precompiles = PrecompilesMap::from_static(Precompiles::prague());
632
633        let err = apply_precompile_overrides(&state_overrides, &mut precompiles).unwrap_err();
634
635        assert!(matches!(err, EthSimulateError::NotAPrecompile(addr) if addr == address));
636    }
637
638    #[test]
639    fn precompile_self_move_errors_for_existing_precompile() {
640        let address = address!("0000000000000000000000000000000000000001");
641        let mut state_overrides = StateOverride::default();
642        state_overrides.insert(
643            address,
644            AccountOverride { move_precompile_to: Some(address), ..Default::default() },
645        );
646        let mut precompiles = PrecompilesMap::from_static(Precompiles::prague());
647
648        let err = apply_precompile_overrides(&state_overrides, &mut precompiles).unwrap_err();
649
650        assert!(matches!(err, EthSimulateError::MovePrecompileToSelf(addr) if addr == address));
651    }
652
653    #[test]
654    fn moved_precompile_is_callable() {
655        let source = address!("0000000000000000000000000000000000000001");
656        let dest = address!("0000000000000000000000000000000000123456");
657        let mut state_overrides = StateOverride::default();
658        state_overrides.insert(
659            source,
660            AccountOverride { move_precompile_to: Some(dest), ..Default::default() },
661        );
662        let mut precompiles = PrecompilesMap::from_static(Precompiles::prague());
663
664        apply_precompile_overrides(&state_overrides, &mut precompiles).unwrap();
665
666        assert!(precompiles.get(&source).is_none());
667        assert!(precompiles.get(&dest).is_some());
668    }
669
670    #[test]
671    fn sanitize_chain_fills_gaps_with_empty_blocks() {
672        // parent at block 5; user requests one block at 8 — sanitize should insert fillers at 6
673        // and 7 before the requested block.
674        let parent = parent_at(5, 100);
675        let blocks = vec![block_with_number(8)];
676
677        let out = sanitize_chain(blocks, &parent, Chain::mainnet().id(), 256).unwrap();
678        assert_eq!(out.len(), 3);
679
680        let numbers: Vec<u64> = out
681            .iter()
682            .map(|b| b.block_overrides.as_ref().unwrap().number.unwrap().try_into().unwrap())
683            .collect();
684        assert_eq!(numbers, vec![6, 7, 8]);
685
686        assert!(out[0].calls.is_empty());
687        assert!(out[1].calls.is_empty());
688
689        // Mainnet hint is 12s, so timestamps should auto-increment from 100 by 12.
690        let times: Vec<u64> =
691            out.iter().map(|b| b.block_overrides.as_ref().unwrap().time.unwrap()).collect();
692        assert_eq!(times, vec![112, 124, 136]);
693    }
694
695    #[test]
696    fn sanitize_chain_defaults_missing_number_and_time() {
697        let parent = parent_at(10, 1000);
698        let blocks: Vec<SimBlock<TransactionRequest>> =
699            vec![SimBlock::default(), SimBlock::default()];
700
701        let out = sanitize_chain(blocks, &parent, Chain::mainnet().id(), 256).unwrap();
702        assert_eq!(out.len(), 2);
703
704        let overrides = out[0].block_overrides.as_ref().unwrap();
705        assert_eq!(overrides.number.unwrap(), U256::from(11));
706        assert_eq!(overrides.time, Some(1012));
707
708        let overrides = out[1].block_overrides.as_ref().unwrap();
709        assert_eq!(overrides.number.unwrap(), U256::from(12));
710        assert_eq!(overrides.time, Some(1024));
711    }
712
713    #[test]
714    fn sanitize_chain_uses_chain_blocktime_hint() {
715        // Optimism has a 2s blocktime hint; filler/auto timestamps should reflect that.
716        let parent = parent_at(0, 0);
717        let blocks = vec![block_with_number(3)];
718
719        let out = sanitize_chain(blocks, &parent, Chain::optimism_mainnet().id(), 256).unwrap();
720        let times: Vec<u64> =
721            out.iter().map(|b| b.block_overrides.as_ref().unwrap().time.unwrap()).collect();
722        assert_eq!(times, vec![2, 4, 6]);
723    }
724
725    #[test]
726    fn sanitize_chain_rounds_subsecond_blocktime_hint_up() {
727        // Arbitrum has a 260ms blocktime hint. Simulated timestamps are second-granular, so this
728        // rounds up to a 1s increment instead of falling back to the default.
729        let parent = parent_at(0, 0);
730        let blocks = vec![block_with_number(3)];
731
732        let out = sanitize_chain(blocks, &parent, Chain::arbitrum_mainnet().id(), 256).unwrap();
733        let times: Vec<u64> =
734            out.iter().map(|b| b.block_overrides.as_ref().unwrap().time.unwrap()).collect();
735        assert_eq!(times, vec![1, 2, 3]);
736    }
737
738    #[test]
739    fn sanitize_chain_falls_back_when_chain_has_no_hint() {
740        // An unknown chain id has no blocktime hint — fall back to the 12s default.
741        let parent = parent_at(0, 0);
742        let blocks = vec![block_with_number(2)];
743
744        let out = sanitize_chain(blocks, &parent, Chain::from_id(123_456_789).id(), 256).unwrap();
745        let times: Vec<u64> =
746            out.iter().map(|b| b.block_overrides.as_ref().unwrap().time.unwrap()).collect();
747        assert_eq!(times, vec![12, 24]);
748    }
749
750    #[test]
751    fn sanitize_chain_rejects_non_increasing_number() {
752        let parent = parent_at(10, 100);
753        let err = sanitize_chain(vec![block_with_number(10)], &parent, Chain::mainnet().id(), 256)
754            .unwrap_err();
755        assert!(matches!(err, EthApiError::Other(_)));
756    }
757
758    #[test]
759    fn sanitize_chain_enforces_max_blocks() {
760        let parent = parent_at(0, 0);
761        let err = sanitize_chain(vec![block_with_number(257)], &parent, Chain::mainnet().id(), 256)
762            .unwrap_err();
763        assert!(matches!(err, EthApiError::Other(_)));
764    }
765}