Skip to main content

ef_tests/cases/
blockchain_test.rs

1//! Test runners for `BlockchainTests` in <https://github.com/ethereum/tests>
2
3use crate::{
4    models::{BlockchainTest, ForkSpec},
5    Case, Error, Suite,
6};
7use alloy_rlp::Decodable;
8use rayon::iter::{IndexedParallelIterator, ParallelIterator};
9use reth_chainspec::ChainSpec;
10use reth_consensus::{Consensus, HeaderValidator};
11use reth_db_common::init::{insert_genesis_hashes, insert_genesis_history, insert_genesis_state};
12use reth_ethereum_consensus::{validate_block_post_execution, EthBeaconConsensus};
13use reth_ethereum_primitives::Block;
14use reth_evm::{execute::Executor, ConfigureEvm};
15use reth_evm_ethereum::EthEvmConfig;
16use reth_primitives_traits::{ParallelBridgeBuffered, RecoveredBlock, SealedBlock};
17use reth_provider::{
18    test_utils::create_test_provider_factory_with_chain_spec, BlockWriter, DatabaseProviderFactory,
19    ExecutionOutcome, HistoryWriter, OriginalValuesKnown, StateWriteConfig, StateWriter,
20    StaticFileProviderFactory, StaticFileSegment, StaticFileWriter,
21};
22use reth_revm::database::StateProviderDatabase;
23use reth_trie::{HashedPostState, KeccakKeyHasher, StateRoot};
24use reth_trie_db::DatabaseStateRoot;
25use std::{
26    collections::BTreeMap,
27    fs,
28    path::{Path, PathBuf},
29    sync::Arc,
30};
31
32/// A handler for the blockchain test suite.
33#[derive(Debug)]
34pub struct BlockchainTests {
35    suite_path: PathBuf,
36}
37
38impl BlockchainTests {
39    /// Create a new suite for tests with blockchain tests format.
40    pub const fn new(suite_path: PathBuf) -> Self {
41        Self { suite_path }
42    }
43}
44
45impl Suite for BlockchainTests {
46    type Case = BlockchainTestCase;
47
48    fn suite_path(&self) -> &Path {
49        &self.suite_path
50    }
51}
52
53/// An Ethereum blockchain test.
54#[derive(Debug, PartialEq, Eq)]
55pub struct BlockchainTestCase {
56    /// The tests within this test case.
57    pub tests: BTreeMap<String, BlockchainTest>,
58    /// Whether to skip this test case.
59    pub skip: bool,
60}
61
62impl BlockchainTestCase {
63    /// Returns `true` if the fork is not supported.
64    const fn excluded_fork(network: ForkSpec) -> bool {
65        matches!(
66            network,
67            ForkSpec::ByzantiumToConstantinopleAt5 |
68                ForkSpec::Constantinople |
69                ForkSpec::ConstantinopleFix |
70                ForkSpec::MergeEOF |
71                ForkSpec::MergeMeterInitCode |
72                ForkSpec::MergePush0
73        )
74    }
75
76    /// Checks if the test case is a particular test called `UncleFromSideChain`
77    ///
78    /// This fixture fails as expected, however it fails at the wrong block number.
79    /// Given we no longer have uncle blocks, this test case was pulled out such
80    /// that we ensure it still fails as expected, however we do not check the block number.
81    #[inline]
82    fn is_uncle_sidechain_case(name: &str) -> bool {
83        name.contains("UncleFromSideChain")
84    }
85
86    /// If the test expects an exception, return the block number
87    /// at which it must occur together with the original message.
88    ///
89    /// Note: There is a +1 here because the genesis block is not included
90    /// in the set of blocks, so the first block is actually block number 1
91    /// and not block number 0.
92    #[inline]
93    fn expected_failure(case: &BlockchainTest) -> Option<(u64, String)> {
94        case.blocks.iter().enumerate().find_map(|(idx, blk)| {
95            blk.expect_exception.as_ref().map(|msg| ((idx + 1) as u64, msg.clone()))
96        })
97    }
98
99    /// Execute a single `BlockchainTest`, validating the outcome against the
100    /// expectations encoded in the JSON file.
101    pub fn run_single_case(name: &str, case: &BlockchainTest) -> Result<(), Error> {
102        let expectation = Self::expected_failure(case);
103        match run_case(case) {
104            // All blocks executed successfully.
105            Ok(()) => {
106                // Check if the test case specifies that it should have failed
107                if let Some((block, msg)) = expectation {
108                    Err(Error::Assertion(format!(
109                        "Test case: {name}\nExpected failure at block {block} - {msg}, but all blocks succeeded",
110                    )))
111                } else {
112                    Ok(())
113                }
114            }
115
116            // A block processing failure occurred.
117            Err(Error::BlockProcessingFailed { block_number, err }) => {
118                match expectation {
119                    // It happened on exactly the block we were told to fail on
120                    Some((expected, _)) if block_number == expected => Ok(()),
121
122                    // Uncle side‑chain edge case, we accept as long as it failed.
123                    // But we don't check the exact block number.
124                    _ if Self::is_uncle_sidechain_case(name) => Ok(()),
125
126                    // Expected failure, but block number does not match
127                    Some((expected, _)) => Err(Error::Assertion(format!(
128                        "Test case: {name}\nExpected failure at block {expected}\nGot failure at block {block_number}",
129                    ))),
130
131                    // No failure expected at all - bubble up original error.
132                    None => Err(Error::BlockProcessingFailed { block_number, err }),
133                }
134            }
135
136            // Non‑processing error – forward as‑is.
137            //
138            // This should only happen if we get an unexpected error from processing the block.
139            // Since it is unexpected, we treat it as a test failure.
140            //
141            // One reason for this happening is when one forgets to wrap the error from `run_case`
142            // so that it produces an `Error::BlockProcessingFailed`
143            Err(other) => Err(other),
144        }
145    }
146}
147
148impl Case for BlockchainTestCase {
149    fn load(path: &Path) -> Result<Self, Error> {
150        Ok(Self {
151            tests: {
152                let s = fs::read_to_string(path)
153                    .map_err(|error| Error::Io { path: path.into(), error })?;
154                serde_json::from_str(&s)
155                    .map_err(|error| Error::CouldNotDeserialize { path: path.into(), error })?
156            },
157            skip: should_skip(path),
158        })
159    }
160
161    /// Runs the test cases for the Ethereum Forks test suite.
162    ///
163    /// # Errors
164    /// Returns an error if the test is flagged for skipping or encounters issues during execution.
165    fn run(self) -> Result<(), Error> {
166        // If the test is marked for skipping, return a Skipped error immediately.
167        if self.skip {
168            return Err(Error::Skipped);
169        }
170
171        // Iterate through test cases, filtering by the network type to exclude specific forks.
172        self.tests
173            .into_iter()
174            .filter(|(_, case)| !Self::excluded_fork(case.network))
175            .par_bridge_buffered()
176            .with_min_len(64)
177            .try_for_each(|(name, case)| Self::run_single_case(&name, &case).map(|_| ()))
178    }
179}
180
181/// Executes a single `BlockchainTest` returning an error as soon as any block has a consensus
182/// validation failure.
183///
184/// A `BlockchainTest` represents a self-contained scenario:
185/// - It initializes a fresh blockchain state.
186/// - It sequentially decodes, executes, and inserts a predefined set of blocks.
187/// - It then verifies that the resulting blockchain state (post-state) matches the expected
188///   outcome.
189///
190/// Returns:
191/// - `Ok(())` if all blocks execute successfully.
192/// - `Err(Error)` if any block fails to execute correctly.
193fn run_case(case: &BlockchainTest) -> Result<(), Error> {
194    // Create a new test database and initialize a provider for the test case.
195    let chain_spec = case.network.to_chain_spec();
196    let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone());
197    let provider = factory.database_provider_rw().unwrap();
198
199    // Insert initial test state into the provider.
200    let genesis_block = SealedBlock::<Block>::from_sealed_parts(
201        case.genesis_block_header.clone().into(),
202        Default::default(),
203    )
204    .try_recover()
205    .unwrap();
206
207    provider.insert_block(&genesis_block).map_err(|err| Error::block_failed(0, err))?;
208
209    // Increment block number for receipts static file
210    provider
211        .static_file_provider()
212        .latest_writer(StaticFileSegment::Receipts)
213        .and_then(|mut writer| writer.increment_block(0))
214        .map_err(|err| Error::block_failed(0, err))?;
215
216    let genesis_state = case.pre.clone().into_genesis_state();
217    insert_genesis_state(&provider, genesis_state.iter())
218        .map_err(|err| Error::block_failed(0, err))?;
219    insert_genesis_hashes(&provider, genesis_state.iter())
220        .map_err(|err| Error::block_failed(0, err))?;
221    insert_genesis_history(&provider, genesis_state.iter())
222        .map_err(|err| Error::block_failed(0, err))?;
223
224    // Decode blocks
225    let blocks = decode_blocks(&case.blocks)?;
226
227    let executor_provider = EthEvmConfig::ethereum(chain_spec.clone());
228    let mut parent = genesis_block;
229
230    for (block_index, block) in blocks.iter().enumerate() {
231        // Note: same as the comment on `decode_blocks` as to why we cannot use block.number
232        let block_number = (block_index + 1) as u64;
233
234        // Insert the block into the database
235        provider.insert_block(block).map_err(|err| Error::block_failed(block_number, err))?;
236        provider
237            .static_file_provider()
238            .commit()
239            .map_err(|err| Error::block_failed(block_number, err))?;
240
241        // Consensus checks before block execution
242        pre_execution_checks(chain_spec.clone(), &parent, block)
243            .map_err(|err| Error::block_failed(block_number, err))?;
244
245        // Execute the block
246        let state_provider = provider.latest();
247        let state_db = StateProviderDatabase(&state_provider);
248        let executor = executor_provider.batch_executor(state_db);
249
250        let output = executor
251            .execute(&(*block).clone())
252            .map_err(|err| Error::block_failed(block_number, err))?;
253
254        // Consensus checks after block execution
255        validate_block_post_execution(block, &chain_spec, &output.receipts, &output.requests, None)
256            .map_err(|err| Error::block_failed(block_number, err))?;
257
258        // Compute and check the post state root
259        let hashed_state =
260            HashedPostState::from_bundle_state::<KeccakKeyHasher>(output.state.state());
261        let (computed_state_root, _) = StateRoot::overlay_root_with_updates(
262            provider.tx_ref(),
263            &hashed_state.clone_into_sorted(),
264        )
265        .map_err(|err| Error::block_failed(block_number, err))?;
266        if computed_state_root != block.state_root {
267            return Err(Error::block_failed(
268                block_number,
269                Error::Assertion("state root mismatch".to_string()),
270            ));
271        }
272
273        // Commit the post state/state diff to the database
274        provider
275            .write_state(
276                &ExecutionOutcome::single(block.number, output),
277                OriginalValuesKnown::Yes,
278                StateWriteConfig::default(),
279            )
280            .map_err(|err| Error::block_failed(block_number, err))?;
281
282        provider
283            .write_hashed_state(&hashed_state.into_sorted())
284            .map_err(|err| Error::block_failed(block_number, err))?;
285        provider
286            .update_history_indices(block.number..=block.number)
287            .map_err(|err| Error::block_failed(block_number, err))?;
288
289        // Since there were no errors, update the parent block
290        parent = block.clone()
291    }
292
293    match &case.post_state {
294        Some(expected_post_state) => {
295            // Validate the post-state for the test case.
296            //
297            // If we get here then it means that the post-state root checks
298            // made after we execute each block was successful.
299            //
300            // If an error occurs here, then it is:
301            // - Either an issue with the test setup
302            // - Possibly an error in the test case where the post-state root in the last block does
303            //   not match the post-state values.
304            for (address, account) in expected_post_state {
305                account.assert_db(*address, provider.tx_ref())?;
306            }
307        }
308        None => {
309            // Some tests may not have post-state (e.g., state-heavy benchmark tests).
310            // In this case, we can skip the post-state validation.
311        }
312    }
313
314    Ok(())
315}
316
317fn decode_blocks(
318    test_case_blocks: &[crate::models::Block],
319) -> Result<Vec<RecoveredBlock<Block>>, Error> {
320    let mut blocks = Vec::with_capacity(test_case_blocks.len());
321    for (block_index, block) in test_case_blocks.iter().enumerate() {
322        // The blocks do not include the genesis block which is why we have the plus one.
323        // We also cannot use block.number because for invalid blocks, this may be incorrect.
324        let block_number = (block_index + 1) as u64;
325
326        let decoded = SealedBlock::<Block>::decode(&mut block.rlp.as_ref())
327            .map_err(|err| Error::block_failed(block_number, err))?;
328
329        let recovered_block =
330            decoded.clone().try_recover().map_err(|err| Error::block_failed(block_number, err))?;
331
332        blocks.push(recovered_block);
333    }
334
335    Ok(blocks)
336}
337
338fn pre_execution_checks(
339    chain_spec: Arc<ChainSpec>,
340    parent: &RecoveredBlock<Block>,
341    block: &RecoveredBlock<Block>,
342) -> Result<(), Error> {
343    let consensus: EthBeaconConsensus<ChainSpec> = EthBeaconConsensus::new(chain_spec);
344
345    let sealed_header = block.sealed_header();
346
347    <EthBeaconConsensus<ChainSpec> as Consensus<Block>>::validate_body_against_header(
348        &consensus,
349        block.body(),
350        sealed_header,
351    )?;
352    consensus.validate_header_against_parent(sealed_header, parent.sealed_header())?;
353    consensus.validate_header(sealed_header)?;
354    consensus.validate_block_pre_execution(block)?;
355
356    Ok(())
357}
358
359/// Returns whether the test at the given path should be skipped.
360///
361/// Some tests are edge cases that cannot happen on mainnet, while others are skipped for
362/// convenience (e.g. they take a long time to run) or are temporarily disabled.
363///
364/// The reason should be documented in a comment above the file name(s).
365pub fn should_skip(path: &Path) -> bool {
366    let path_str = path.to_str().expect("Path is not valid UTF-8");
367    let name = path.file_name().unwrap().to_str().unwrap();
368    matches!(
369        name,
370        // funky test with `bigint 0x00` value in json :) not possible to happen on mainnet and require
371        // custom json parser. https://github.com/ethereum/tests/issues/971
372        | "ValueOverflow.json"
373        | "ValueOverflowParis.json"
374
375        // txbyte is of type 02 and we don't parse tx bytes for this test to fail.
376        | "typeTwoBerlin.json"
377
378        // Test checks if nonce overflows. We are handling this correctly but we are not parsing
379        // exception in testsuite. There are more nonce overflow tests that are internal
380        // call/create, and those tests are passing and are enabled.
381        | "CreateTransactionHighNonce.json"
382
383        // Test check if gas price overflows, we handle this correctly but does not match tests specific
384        // exception.
385        | "HighGasPrice.json"
386        | "HighGasPriceParis.json"
387
388        // Skip test where basefee/accesslist/difficulty is present but it shouldn't be supported in
389        // London/Berlin/TheMerge. https://github.com/ethereum/tests/blob/5b7e1ab3ffaf026d99d20b17bb30f533a2c80c8b/GeneralStateTests/stExample/eip1559.json#L130
390        // It is expected to not execute these tests.
391        | "accessListExample.json"
392        | "basefeeExample.json"
393        | "eip1559.json"
394        | "mergeTest.json"
395
396        // These tests are passing, but they take a lot of time to execute so we are going to skip them.
397        | "loopExp.json"
398        | "Call50000_sha256.json"
399        | "static_Call50000_sha256.json"
400        | "loopMul.json"
401        | "CALLBlake2f_MaxRounds.json"
402        | "shiftCombinations.json"
403
404        // Skipped by revm as well: <https://github.com/bluealloy/revm/blob/be92e1db21f1c47b34c5a58cfbf019f6b97d7e4b/bins/revme/src/cmd/statetest/runner.rs#L115-L125>
405        | "RevertInCreateInInit_Paris.json"
406        | "RevertInCreateInInit.json"
407        | "dynamicAccountOverwriteEmpty.json"
408        | "dynamicAccountOverwriteEmpty_Paris.json"
409        | "RevertInCreateInInitCreate2Paris.json"
410        | "create2collisionStorage.json"
411        | "RevertInCreateInInitCreate2.json"
412        | "create2collisionStorageParis.json"
413        | "InitCollision.json"
414        | "InitCollisionParis.json"
415    )
416    // Ignore outdated EOF tests that haven't been updated for Cancun yet.
417    || path_contains(path_str, &["EIPTests", "stEOF"])
418}
419
420/// `str::contains` but for a path. Takes into account the OS path separator (`/` or `\`).
421fn path_contains(path_str: &str, rhs: &[&str]) -> bool {
422    let rhs = rhs.join(std::path::MAIN_SEPARATOR_STR);
423    path_str.contains(&rhs)
424}