Skip to main content

revme/cmd/
blockchaintest.rs

1pub mod post_block;
2pub mod pre_block;
3
4use crate::dir_utils::find_all_json_tests;
5use clap::Parser;
6
7use revm::statetest_types::blockchain::{
8    Account, BlockchainTest, BlockchainTestCase, ForkSpec, Withdrawal,
9};
10use revm::{
11    bytecode::Bytecode,
12    context::{cfg::CfgEnv, ContextTr},
13    context_interface::{block::BlobExcessGasAndPrice, result::HaltReason},
14    database::{states::bundle_state::BundleRetention, EmptyDB, State},
15    handler::EvmTr,
16    inspector::inspectors::TracerEip3155,
17    primitives::{hardfork::SpecId, hex, Address, AddressMap, U256Map, U256},
18    state::{bal::Bal, AccountInfo},
19    Context, Database, ExecuteCommitEvm, ExecuteEvm, InspectEvm, MainBuilder, MainContext,
20};
21use serde_json::json;
22use std::{
23    collections::BTreeMap,
24    fs,
25    path::{Path, PathBuf},
26    sync::Arc,
27    time::Instant,
28};
29use thiserror::Error;
30
31/// Panics if the value cannot be serialized to JSON.
32fn print_json<T: serde::Serialize>(value: &T) {
33    println!("{}", serde_json::to_string(value).unwrap());
34}
35
36/// `blockchaintest` subcommand
37#[derive(Parser, Debug)]
38pub struct Cmd {
39    /// Path to folder or file containing the blockchain tests
40    ///
41    /// If multiple paths are specified they will be run in sequence.
42    ///
43    /// Folders will be searched recursively for files with the extension `.json`.
44    #[arg(required = true, num_args = 1..)]
45    paths: Vec<PathBuf>,
46    /// Omit progress output
47    #[arg(long)]
48    omit_progress: bool,
49    /// Keep going after a test failure
50    #[arg(long, alias = "no-fail-fast")]
51    keep_going: bool,
52    /// Print environment information (pre-state, post-state, env) when an error occurs
53    #[arg(long)]
54    print_env_on_error: bool,
55    /// Output results in JSON format
56    #[arg(long)]
57    json: bool,
58}
59
60impl Cmd {
61    /// Runs `blockchaintest` command.
62    pub fn run(&self) -> Result<(), Error> {
63        for path in &self.paths {
64            if !path.exists() {
65                return Err(Error::PathNotFound(path.clone()));
66            }
67
68            if !self.json {
69                println!("\nRunning blockchain tests in {}...", path.display());
70            }
71            let test_files = find_all_json_tests(path);
72
73            if test_files.is_empty() {
74                return Err(Error::NoJsonFiles(path.clone()));
75            }
76
77            run_tests(
78                test_files,
79                self.omit_progress,
80                self.keep_going,
81                self.print_env_on_error,
82                self.json,
83            )?;
84        }
85        Ok(())
86    }
87}
88
89/// Run all blockchain tests from the given files
90fn run_tests(
91    test_files: Vec<PathBuf>,
92    omit_progress: bool,
93    keep_going: bool,
94    print_env_on_error: bool,
95    json_output: bool,
96) -> Result<(), Error> {
97    let mut passed = 0;
98    let mut failed = 0;
99    let mut skipped = 0;
100    let mut failed_paths = Vec::new();
101
102    let start_time = Instant::now();
103    let total_files = test_files.len();
104
105    for (file_index, file_path) in test_files.into_iter().enumerate() {
106        let current_file = file_index + 1;
107        if skip_test(&file_path) {
108            skipped += 1;
109            if json_output {
110                let output = json!({
111                    "file": file_path.display().to_string(),
112                    "status": "skipped",
113                    "reason": "known_issue"
114                });
115                print_json(&output);
116            } else if !omit_progress {
117                println!(
118                    "Skipping ({}/{}): {}",
119                    current_file,
120                    total_files,
121                    file_path.display()
122                );
123            }
124            continue;
125        }
126
127        let result = run_test_file(&file_path, json_output, print_env_on_error);
128
129        match result {
130            Ok(test_count) => {
131                passed += test_count;
132                if json_output {
133                    // JSON output handled in run_test_file
134                } else if !omit_progress {
135                    println!(
136                        "āœ“ ({}/{}) {} ({} tests)",
137                        current_file,
138                        total_files,
139                        file_path.display(),
140                        test_count
141                    );
142                }
143            }
144            Err(e) => {
145                failed += 1;
146                if keep_going {
147                    failed_paths.push(file_path.clone());
148                }
149                if json_output {
150                    let output = json!({
151                        "file": file_path.display().to_string(),
152                        "error": e.to_string(),
153                        "status": "failed"
154                    });
155                    print_json(&output);
156                } else if !omit_progress {
157                    eprintln!(
158                        "āœ— ({}/{}) {} - {}",
159                        current_file,
160                        total_files,
161                        file_path.display(),
162                        e
163                    );
164                }
165
166                if !keep_going {
167                    return Err(e);
168                }
169            }
170        }
171    }
172
173    let duration = start_time.elapsed();
174
175    if json_output {
176        let results = json!({
177            "summary": {
178                "passed": passed,
179                "failed": failed,
180                "skipped": skipped,
181                "duration_secs": duration.as_secs_f64(),
182            }
183        });
184        print_json(&results);
185    } else {
186        // Print failed test paths if keep-going was enabled
187        if keep_going && !failed_paths.is_empty() {
188            println!("\nFailed test files:");
189            for path in &failed_paths {
190                println!("  {}", path.display());
191            }
192        }
193
194        println!("\nTest results:");
195        println!("  Passed:  {passed}");
196        println!("  Failed:  {failed}");
197        println!("  Skipped: {skipped}");
198        println!("  Time:    {:.2}s", duration.as_secs_f64());
199    }
200
201    if failed > 0 {
202        Err(Error::TestsFailed { failed })
203    } else {
204        Ok(())
205    }
206}
207
208/// Run tests from a single file
209fn run_test_file(
210    file_path: &Path,
211    json_output: bool,
212    print_env_on_error: bool,
213) -> Result<usize, Error> {
214    let content =
215        fs::read_to_string(file_path).map_err(|e| Error::FileRead(file_path.to_path_buf(), e))?;
216
217    let blockchain_test: BlockchainTest = serde_json::from_str(&content)
218        .map_err(|e| Error::JsonDecode(file_path.to_path_buf(), e))?;
219
220    let mut test_count = 0;
221
222    for (test_name, test_case) in blockchain_test.0 {
223        if json_output {
224            // Output test start in JSON format
225            let output = json!({
226                "test": test_name,
227                "file": file_path.display().to_string(),
228                "status": "running"
229            });
230            print_json(&output);
231        } else {
232            println!("  Running: {test_name}");
233        }
234        // Execute the blockchain test
235        let result = execute_blockchain_test(&test_case, print_env_on_error, json_output);
236
237        match result {
238            Ok(()) => {
239                if json_output {
240                    let output = json!({
241                        "test": test_name,
242                        "file": file_path.display().to_string(),
243                        "status": "passed"
244                    });
245                    print_json(&output);
246                }
247                test_count += 1;
248            }
249            Err(e) => {
250                if json_output {
251                    let output = json!({
252                        "test": test_name,
253                        "file": file_path.display().to_string(),
254                        "status": "failed",
255                        "error": e.to_string()
256                    });
257                    print_json(&output);
258                }
259                return Err(Error::TestExecution {
260                    test_name,
261                    test_path: file_path.to_path_buf(),
262                    error: e.to_string(),
263                });
264            }
265        }
266    }
267
268    Ok(test_count)
269}
270
271/// Debug information captured during test execution
272#[derive(Debug, Clone)]
273struct DebugInfo {
274    /// Initial pre-state before any execution
275    pre_state: AddressMap<(AccountInfo, U256Map<U256>)>,
276    /// Transaction environment
277    tx_env: Option<revm::context::tx::TxEnv>,
278    /// Block environment
279    block_env: revm::context::block::BlockEnv,
280    /// Configuration environment
281    cfg_env: CfgEnv,
282    /// Block index where error occurred
283    block_idx: usize,
284    /// Transaction index where error occurred
285    tx_idx: usize,
286    /// Withdrawals in the block
287    withdrawals: Option<Vec<Withdrawal>>,
288}
289
290impl DebugInfo {
291    /// Capture current state from the State database
292    fn capture_committed_state(state: &State<EmptyDB>) -> AddressMap<(AccountInfo, U256Map<U256>)> {
293        let mut committed_state = AddressMap::default();
294
295        // Access the cache state to get all accounts
296        for (address, cache_account) in &state.cache.accounts {
297            if let Some(plain_account) = &cache_account.account {
298                let mut storage = U256Map::default();
299                for (key, value) in &plain_account.storage {
300                    storage.insert(*key, *value);
301                }
302                committed_state.insert(*address, (plain_account.info.clone(), storage));
303            }
304        }
305
306        committed_state
307    }
308}
309
310/// Validate post state against expected values
311fn validate_post_state(
312    state: &mut State<EmptyDB>,
313    expected_post_state: &BTreeMap<Address, Account>,
314    debug_info: &DebugInfo,
315    print_env_on_error: bool,
316) -> Result<(), TestExecutionError> {
317    #[allow(clippy::too_many_arguments)]
318    fn make_failure(
319        state: &mut State<EmptyDB>,
320        debug_info: &DebugInfo,
321        expected_post_state: &BTreeMap<Address, Account>,
322        print_env_on_error: bool,
323        address: Address,
324        field: String,
325        expected: String,
326        actual: String,
327    ) -> Result<(), TestExecutionError> {
328        if print_env_on_error {
329            print_error_with_state(debug_info, state, Some(expected_post_state));
330        }
331        Err(TestExecutionError::PostStateValidation {
332            address,
333            field,
334            expected,
335            actual,
336        })
337    }
338
339    for (address, expected_account) in expected_post_state {
340        // Load account from final state
341        let actual_account = state
342            .load_cache_account(*address)
343            .map_err(|e| TestExecutionError::Database(format!("Account load failed: {e}")))?;
344        let info = actual_account
345            .account
346            .as_ref()
347            .map(|a| a.info.clone())
348            .unwrap_or_default();
349
350        // Validate balance
351        if info.balance != expected_account.balance {
352            return make_failure(
353                state,
354                debug_info,
355                expected_post_state,
356                print_env_on_error,
357                *address,
358                "balance".to_string(),
359                format!("{}", expected_account.balance),
360                format!("{}", info.balance),
361            );
362        }
363
364        // Validate nonce
365        let expected_nonce = expected_account.nonce.to::<u64>();
366        if info.nonce != expected_nonce {
367            return make_failure(
368                state,
369                debug_info,
370                expected_post_state,
371                print_env_on_error,
372                *address,
373                "nonce".to_string(),
374                format!("{expected_nonce}"),
375                format!("{}", info.nonce),
376            );
377        }
378
379        // Validate code if present
380        if !expected_account.code.is_empty() {
381            if let Some(actual_code) = &info.code {
382                if actual_code.original_bytes() != expected_account.code {
383                    return make_failure(
384                        state,
385                        debug_info,
386                        expected_post_state,
387                        print_env_on_error,
388                        *address,
389                        "code".to_string(),
390                        format!("0x{}", hex::encode(&expected_account.code)),
391                        format!("0x{}", hex::encode(actual_code.original_byte_slice())),
392                    );
393                }
394            } else {
395                return make_failure(
396                    state,
397                    debug_info,
398                    expected_post_state,
399                    print_env_on_error,
400                    *address,
401                    "code".to_string(),
402                    format!("0x{}", hex::encode(&expected_account.code)),
403                    "empty".to_string(),
404                );
405            }
406        }
407
408        // Check for unexpected storage entries. Avoid allocating a temporary HashMap when the account is None.
409        if let Some(acc) = actual_account.account.as_ref() {
410            for (slot, actual_value) in &acc.storage {
411                let slot = *slot;
412                let actual_value = *actual_value;
413                if !expected_account.storage.contains_key(&slot) && !actual_value.is_zero() {
414                    return make_failure(
415                        state,
416                        debug_info,
417                        expected_post_state,
418                        print_env_on_error,
419                        *address,
420                        format!("storage_unexpected[{slot}]"),
421                        "0x0".to_string(),
422                        format!("{actual_value}"),
423                    );
424                }
425            }
426        }
427
428        // Validate storage slots
429        for (slot, expected_value) in &expected_account.storage {
430            let actual_value = state.storage(*address, *slot);
431            let actual_value = actual_value.unwrap_or_default();
432
433            if actual_value != *expected_value {
434                return make_failure(
435                    state,
436                    debug_info,
437                    expected_post_state,
438                    print_env_on_error,
439                    *address,
440                    format!("storage_validation[{slot}]"),
441                    format!("{expected_value}"),
442                    format!("{actual_value}"),
443                );
444            }
445        }
446    }
447    Ok(())
448}
449
450/// Print comprehensive error information including environment and state comparison
451fn print_error_with_state(
452    debug_info: &DebugInfo,
453    current_state: &State<EmptyDB>,
454    expected_post_state: Option<&BTreeMap<Address, Account>>,
455) {
456    eprintln!("\n========== TEST EXECUTION ERROR ==========");
457
458    // Print error location
459    eprintln!(
460        "\nšŸ“ Error occurred at block {} transaction {}",
461        debug_info.block_idx, debug_info.tx_idx
462    );
463
464    // Print configuration environment
465    eprintln!("\nšŸ“‹ Configuration Environment:");
466    eprintln!("  Spec ID: {:?}", debug_info.cfg_env.spec());
467    eprintln!("  Chain ID: {}", debug_info.cfg_env.chain_id);
468    eprintln!(
469        "  Limit contract code size: {:?}",
470        debug_info.cfg_env.limit_contract_code_size
471    );
472    eprintln!(
473        "  Limit contract initcode size: {:?}",
474        debug_info.cfg_env.limit_contract_initcode_size
475    );
476
477    // Print block environment
478    eprintln!("\nšŸ”Ø Block Environment:");
479    eprintln!("  Number: {}", debug_info.block_env.number);
480    eprintln!("  Timestamp: {}", debug_info.block_env.timestamp);
481    eprintln!("  Gas limit: {}", debug_info.block_env.gas_limit);
482    eprintln!("  Base fee: {:?}", debug_info.block_env.basefee);
483    eprintln!("  Difficulty: {}", debug_info.block_env.difficulty);
484    eprintln!("  Prevrandao: {:?}", debug_info.block_env.prevrandao);
485    eprintln!("  Beneficiary: {:?}", debug_info.block_env.beneficiary);
486    let blob = debug_info.block_env.blob_excess_gas_and_price;
487    eprintln!("  Blob excess gas: {:?}", blob.map(|a| a.excess_blob_gas));
488    eprintln!("  Blob gas price: {:?}", blob.map(|a| a.blob_gasprice));
489
490    // Print withdrawals
491    if let Some(withdrawals) = &debug_info.withdrawals {
492        eprintln!("  Withdrawals: {} items", withdrawals.len());
493        if !withdrawals.is_empty() {
494            for (i, withdrawal) in withdrawals.iter().enumerate().take(3) {
495                eprintln!("    Withdrawal {i}:");
496                eprintln!("      Index: {}", withdrawal.index);
497                eprintln!("      Validator Index: {}", withdrawal.validator_index);
498                eprintln!("      Address: {:?}", withdrawal.address);
499                eprintln!(
500                    "      Amount: {} Gwei ({:.6} ETH)",
501                    withdrawal.amount,
502                    withdrawal.amount.to::<u128>() as f64 / 1_000_000_000.0
503                );
504            }
505            if withdrawals.len() > 3 {
506                eprintln!("    ... and {} more withdrawals", withdrawals.len() - 3);
507            }
508        }
509    }
510
511    // Print transaction environment if available
512    if let Some(tx_env) = &debug_info.tx_env {
513        eprintln!("\nšŸ“„ Transaction Environment:");
514        eprintln!("  Transaction type: {}", tx_env.tx_type);
515        eprintln!("  Caller: {:?}", tx_env.caller);
516        eprintln!("  Gas limit: {}", tx_env.gas_limit);
517        eprintln!("  Gas price: {}", tx_env.gas_price);
518        eprintln!("  Gas priority fee: {:?}", tx_env.gas_priority_fee);
519        eprintln!("  Transaction kind: {:?}", tx_env.kind);
520        eprintln!("  Value: {}", tx_env.value);
521        eprintln!("  Data length: {} bytes", tx_env.data.len());
522        if !tx_env.data.is_empty() {
523            let preview_len = std::cmp::min(64, tx_env.data.len());
524            eprintln!(
525                "  Data preview: 0x{}{}",
526                hex::encode(&tx_env.data[..preview_len]),
527                if tx_env.data.len() > 64 { "..." } else { "" }
528            );
529        }
530        eprintln!("  Nonce: {}", tx_env.nonce);
531        eprintln!("  Chain ID: {:?}", tx_env.chain_id);
532        eprintln!("  Access list: {} entries", tx_env.access_list.len());
533        if !tx_env.access_list.is_empty() {
534            for (i, access) in tx_env.access_list.iter().enumerate().take(3) {
535                eprintln!(
536                    "    Access {}: address={:?}, {} storage keys",
537                    i,
538                    access.address,
539                    access.storage_keys.len()
540                );
541            }
542            if tx_env.access_list.len() > 3 {
543                eprintln!(
544                    "    ... and {} more access list entries",
545                    tx_env.access_list.len() - 3
546                );
547            }
548        }
549        eprintln!("  Blob hashes: {} blobs", tx_env.blob_hashes.len());
550        if !tx_env.blob_hashes.is_empty() {
551            for (i, hash) in tx_env.blob_hashes.iter().enumerate().take(3) {
552                eprintln!("    Blob {i}: {hash:?}");
553            }
554            if tx_env.blob_hashes.len() > 3 {
555                eprintln!(
556                    "    ... and {} more blob hashes",
557                    tx_env.blob_hashes.len() - 3
558                );
559            }
560        }
561        eprintln!("  Max fee per blob gas: {}", tx_env.max_fee_per_blob_gas);
562        eprintln!(
563            "  Authorization list: {} items",
564            tx_env.authorization_list.len()
565        );
566        if !tx_env.authorization_list.is_empty() {
567            eprintln!("    (EIP-7702 authorizations present)");
568        }
569    } else {
570        eprintln!(
571            "\nšŸ“„ Transaction Environment: Not available (error occurred before tx creation)"
572        );
573    }
574
575    // Print state comparison
576    eprintln!("\nšŸ’¾ Pre-State (Initial):");
577    // Sort accounts by address for consistent output
578    let mut sorted_accounts: Vec<_> = debug_info.pre_state.iter().collect();
579    sorted_accounts.sort_by_key(|(addr, _)| *addr);
580    for (address, (info, storage)) in sorted_accounts {
581        eprintln!("  Account {address:?}:");
582        eprintln!("    Balance: 0x{:x}", info.balance);
583        eprintln!("    Nonce: {}", info.nonce);
584        eprintln!("    Code hash: {:?}", info.code_hash);
585        eprintln!(
586            "    Code size: {} bytes",
587            info.code.as_ref().map_or(0, |c| c.len())
588        );
589        if !storage.is_empty() {
590            eprintln!("    Storage ({} slots):", storage.len());
591            let mut sorted_storage: Vec<_> = storage.iter().collect();
592            sorted_storage.sort_by_key(|(key, _)| *key);
593            for (key, value) in sorted_storage.iter() {
594                eprintln!("      {key:?} => {value:?}");
595            }
596        }
597    }
598
599    eprintln!("\nšŸ“ Current State (Actual):");
600    let committed_state = DebugInfo::capture_committed_state(current_state);
601    // Sort accounts by address for consistent output
602    let mut sorted_current: Vec<_> = committed_state.iter().collect();
603    sorted_current.sort_by_key(|(addr, _)| *addr);
604    for (address, (info, storage)) in sorted_current {
605        eprintln!("  Account {address:?}:");
606        eprintln!("    Balance: 0x{:x}", info.balance);
607        eprintln!("    Nonce: {}", info.nonce);
608        eprintln!("    Code hash: {:?}", info.code_hash);
609        eprintln!(
610            "    Code size: {} bytes",
611            info.code.as_ref().map_or(0, |c| c.len())
612        );
613        if !storage.is_empty() {
614            eprintln!("    Storage ({} slots):", storage.len());
615            let mut sorted_storage: Vec<_> = storage.iter().collect();
616            sorted_storage.sort_by_key(|(key, _)| *key);
617            for (key, value) in sorted_storage.iter() {
618                eprintln!("      {key:?} => {value:?}");
619            }
620        }
621    }
622
623    // Print expected post-state if available
624    if let Some(expected_post_state) = expected_post_state {
625        eprintln!("\nāœ… Expected Post-State:");
626        for (address, account) in expected_post_state {
627            eprintln!("  Account {address:?}:");
628            eprintln!("    Balance: 0x{:x}", account.balance);
629            eprintln!("    Nonce: {}", account.nonce);
630            if !account.code.is_empty() {
631                eprintln!("    Code size: {} bytes", account.code.len());
632            }
633            if !account.storage.is_empty() {
634                eprintln!("    Storage ({} slots):", account.storage.len());
635                for (key, value) in account.storage.iter() {
636                    eprintln!("      {key:?} => {value:?}");
637                }
638            }
639        }
640    }
641
642    eprintln!("\n===========================================\n");
643}
644
645/// Execute a single blockchain test case
646fn execute_blockchain_test(
647    test_case: &BlockchainTestCase,
648    print_env_on_error: bool,
649    json_output: bool,
650) -> Result<(), TestExecutionError> {
651    // Skip all transition forks for now.
652    if matches!(
653        test_case.network,
654        ForkSpec::ByzantiumToConstantinopleAt5
655            | ForkSpec::ParisToShanghaiAtTime15k
656            | ForkSpec::ShanghaiToCancunAtTime15k
657            | ForkSpec::CancunToPragueAtTime15k
658            | ForkSpec::PragueToOsakaAtTime15k
659            | ForkSpec::BPO1ToBPO2AtTime15k
660    ) {
661        eprintln!("āš ļø  Skipping transition fork: {:?}", test_case.network);
662        return Ok(());
663    }
664
665    // Create database with initial state
666    let mut state = State::builder().with_bal_builder().build();
667
668    // Capture pre-state for debug info
669    let mut pre_state_debug = AddressMap::default();
670
671    // Insert genesis state into database
672    let genesis_state = test_case.pre.clone().into_genesis_state();
673    for (address, account) in genesis_state {
674        let account_info = AccountInfo {
675            balance: account.balance,
676            nonce: account.nonce,
677            code_hash: revm::primitives::keccak256(&account.code),
678            code: Some(Bytecode::new_raw(account.code.clone())),
679            account_id: None,
680        };
681
682        // Store for debug info
683        if print_env_on_error {
684            pre_state_debug.insert(address, (account_info.clone(), account.storage.clone()));
685        }
686
687        state.insert_account_with_storage(address, account_info, account.storage);
688    }
689
690    // insert genesis hash
691    state
692        .block_hashes
693        .insert(0, test_case.genesis_block_header.hash);
694
695    // Setup configuration based on fork
696    let spec_id = fork_to_spec_id(test_case.network);
697    let mut cfg = CfgEnv::default();
698    cfg.set_spec_and_mainnet_gas_params(spec_id);
699
700    // Genesis block is not used yet.
701    let mut parent_block_hash = Some(test_case.genesis_block_header.hash);
702    let mut parent_excess_blob_gas = test_case
703        .genesis_block_header
704        .excess_blob_gas
705        .unwrap_or_default()
706        .to::<u64>();
707    let mut block_env = test_case.genesis_block_env();
708
709    // Process each block in the test
710    for (block_idx, block) in test_case.blocks.iter().enumerate() {
711        println!("Run block {block_idx}/{}", test_case.blocks.len());
712
713        // Check if this block should fail
714        let should_fail = block.expect_exception.is_some();
715
716        let transactions = block.transactions.as_deref().unwrap_or_default();
717
718        // Update block environment for this blockk
719
720        let mut block_hash = None;
721        let mut beacon_root = None;
722        let this_excess_blob_gas;
723
724        if let Some(block_header) = block.block_header.as_ref() {
725            block_hash = Some(block_header.hash);
726            beacon_root = block_header.parent_beacon_block_root;
727            block_env = block_header.to_block_env(Some(BlobExcessGasAndPrice::new_with_spec(
728                parent_excess_blob_gas,
729                spec_id,
730            )));
731            this_excess_blob_gas = block_header.excess_blob_gas.map(|i| i.to::<u64>());
732        } else {
733            this_excess_blob_gas = None;
734        }
735
736        let bal_test = block
737            .block_access_list
738            .as_ref()
739            .and_then(|bal| Bal::try_from(bal.clone()).ok())
740            .map(Arc::new);
741
742        //state.set_bal(bal_test);
743        state.reset_bal_index();
744
745        // Create EVM context for each transaction to ensure fresh state access
746        let evm_context = Context::mainnet()
747            .with_block(&block_env)
748            .with_cfg(cfg.clone())
749            .with_db(&mut state);
750
751        // Build and execute with EVM - always use inspector when JSON output is enabled
752        let mut evm = evm_context.build_mainnet_with_inspector(TracerEip3155::new_stdout());
753
754        // Pre block system calls
755        pre_block::pre_block_transition(&mut evm, spec_id, parent_block_hash, beacon_root)
756            .map_err(|e| TestExecutionError::PreBlockSystemCall {
757                block_idx,
758                error: format!("{e:?}"),
759            })?;
760
761        // Track cumulative gas used across all transactions in this block
762        let mut cumulative_gas_used: u64 = 0;
763        let mut block_completed = true;
764
765        // Execute each transaction in the block
766        for (tx_idx, tx) in transactions.iter().enumerate() {
767            if tx.sender.is_none() {
768                if print_env_on_error {
769                    let debug_info = DebugInfo {
770                        pre_state: pre_state_debug.clone(),
771                        tx_env: None,
772                        block_env: block_env.clone(),
773                        cfg_env: cfg.clone(),
774                        block_idx,
775                        tx_idx,
776                        withdrawals: block.withdrawals.clone(),
777                    };
778                    print_error_with_state(
779                        &debug_info,
780                        evm.ctx().db_ref(),
781                        test_case.post_state.as_ref(),
782                    );
783                }
784                if json_output {
785                    let output = json!({
786                        "block": block_idx,
787                        "tx": tx_idx,
788                        "error": "missing sender",
789                        "status": "skipped"
790                    });
791                    print_json(&output);
792                } else {
793                    eprintln!("āš ļø  Skipping block {block_idx} due to missing sender");
794                }
795                block_completed = false;
796                break; // Skip to next block
797            }
798
799            let tx_env = match tx.to_tx_env() {
800                Ok(env) => env,
801                Err(e) => {
802                    if should_fail {
803                        // Expected failure during tx env creation
804                        continue;
805                    }
806                    if print_env_on_error {
807                        let debug_info = DebugInfo {
808                            pre_state: pre_state_debug.clone(),
809                            tx_env: None,
810                            block_env: block_env.clone(),
811                            cfg_env: cfg.clone(),
812                            block_idx,
813                            tx_idx,
814                            withdrawals: block.withdrawals.clone(),
815                        };
816                        print_error_with_state(
817                            &debug_info,
818                            evm.ctx().db_ref(),
819                            test_case.post_state.as_ref(),
820                        );
821                    }
822                    if json_output {
823                        let output = json!({
824                            "block": block_idx,
825                            "tx": tx_idx,
826                            "error": format!("tx env creation error: {e}"),
827                            "status": "skipped"
828                        });
829                        print_json(&output);
830                    } else {
831                        eprintln!(
832                            "āš ļø  Skipping block {block_idx} due to transaction env creation error: {e}"
833                        );
834                    }
835                    block_completed = false;
836                    break; // Skip to next block
837                }
838            };
839
840            // bump bal index
841            evm.db_mut().bump_bal_index();
842
843            // If JSON output requested, output transaction details
844            let execution_result = if json_output {
845                evm.inspect_tx(tx_env.clone())
846            } else {
847                evm.transact(tx_env.clone())
848            };
849
850            match execution_result {
851                Ok(result) => {
852                    if should_fail {
853                        // Unexpected success - should have failed but didn't
854                        // If not expected to fail, use inspector to trace the transaction
855                        if print_env_on_error {
856                            // Re-run with inspector to get detailed trace
857                            if json_output {
858                                eprintln!("=== Transaction trace (unexpected success) ===");
859                            }
860                            let _ = evm.inspect_tx(tx_env.clone());
861                        }
862
863                        if print_env_on_error {
864                            let debug_info = DebugInfo {
865                                pre_state: pre_state_debug.clone(),
866                                tx_env: Some(tx_env.clone()),
867                                block_env: block_env.clone(),
868                                cfg_env: cfg.clone(),
869                                block_idx,
870                                tx_idx,
871                                withdrawals: block.withdrawals.clone(),
872                            };
873                            print_error_with_state(
874                                &debug_info,
875                                evm.ctx().db_ref(),
876                                test_case.post_state.as_ref(),
877                            );
878                        }
879                        let expected_exception = block.expect_exception.clone().unwrap_or_default();
880                        if json_output {
881                            let output = json!({
882                                "block": block_idx,
883                                "tx": tx_idx,
884                                "expected_exception": expected_exception,
885                                "gas_used": result.result.gas_used(),
886                                "status": "unexpected_success"
887                            });
888                            print_json(&output);
889                        } else {
890                            eprintln!(
891                                "āš ļø  Skipping block {block_idx}: transaction unexpectedly succeeded (expected failure: {expected_exception})"
892                            );
893                        }
894                        block_completed = false;
895                        break; // Skip to next block
896                    }
897                    // EIP-7778: Block gas accounting without refunds.
898                    // For Amsterdam+, block gas = max(spent, floor_gas).
899                    // For pre-Amsterdam, block gas = used() = max(spent - refunded, floor_gas).
900                    let gas = result.result.gas();
901                    cumulative_gas_used += if spec_id.is_enabled_in(SpecId::AMSTERDAM) {
902                        gas.spent().max(gas.floor_gas())
903                    } else {
904                        gas.used()
905                    };
906                    evm.commit(result.state);
907                }
908                Err(e) => {
909                    if !should_fail {
910                        // Unexpected error - use inspector to trace the transaction
911                        if print_env_on_error {
912                            if json_output {
913                                eprintln!("=== Transaction trace (unexpected failure) ===");
914                            }
915                            let _ = evm.inspect_tx(tx_env.clone());
916                        }
917
918                        if print_env_on_error {
919                            let debug_info = DebugInfo {
920                                pre_state: pre_state_debug.clone(),
921                                tx_env: Some(tx_env.clone()),
922                                block_env: block_env.clone(),
923                                cfg_env: cfg.clone(),
924                                block_idx,
925                                tx_idx,
926                                withdrawals: block.withdrawals.clone(),
927                            };
928                            print_error_with_state(
929                                &debug_info,
930                                evm.ctx().db_ref(),
931                                test_case.post_state.as_ref(),
932                            );
933                        }
934                        if json_output {
935                            let output = json!({
936                                "block": block_idx,
937                                "tx": tx_idx,
938                                "error": format!("{e:?}"),
939                                "status": "unexpected_failure"
940                            });
941                            print_json(&output);
942                        } else {
943                            eprintln!(
944                                "āš ļø  Skipping block {block_idx} due to unexpected failure: {e:?}"
945                            );
946                        }
947                        block_completed = false;
948                        break; // Skip to next block
949                    } else if json_output {
950                        // Expected failure
951                        let output = json!({
952                            "block": block_idx,
953                            "tx": tx_idx,
954                            "error": format!("{e:?}"),
955                            "status": "expected_failure"
956                        });
957                        print_json(&output);
958                    }
959                }
960            }
961        }
962
963        // Validate block gas used against header
964        if block_completed && !should_fail {
965            if let Some(block_header) = block.block_header.as_ref() {
966                let expected_gas_used = block_header.gas_used.to::<u64>();
967                if cumulative_gas_used != expected_gas_used {
968                    if print_env_on_error {
969                        eprintln!(
970                            "Block gas used mismatch at block {block_idx}: expected {expected_gas_used}, got {cumulative_gas_used}"
971                        );
972                    }
973                    return Err(TestExecutionError::BlockGasUsedMismatch {
974                        block_idx,
975                        expected: expected_gas_used,
976                        actual: cumulative_gas_used,
977                    });
978                }
979            }
980        }
981
982        // bump bal index
983        evm.db_mut().bump_bal_index();
984
985        // uncle rewards are not implemented yet
986        post_block::post_block_transition(
987            &mut evm,
988            &block_env,
989            block.withdrawals.as_deref().unwrap_or_default(),
990            spec_id,
991        )
992        .map_err(|e| TestExecutionError::PostBlockSystemCall {
993            block_idx,
994            error: format!("{e:?}"),
995        })?;
996
997        // insert present block hash.
998        state
999            .block_hashes
1000            .insert(block_env.number.to::<u64>(), block_hash.unwrap_or_default());
1001
1002        if let Some(bal) = state.bal_state.bal_builder.take() {
1003            if let Some(state_bal) = bal_test {
1004                if &bal != state_bal.as_ref() {
1005                    println!("Bal mismatch");
1006                    println!("Test bal");
1007                    state_bal.pretty_print();
1008                    println!("Bal:");
1009                    bal.pretty_print();
1010                    return Err(TestExecutionError::BalMismatchError);
1011                }
1012            }
1013        }
1014
1015        parent_block_hash = block_hash;
1016        if let Some(excess_blob_gas) = this_excess_blob_gas {
1017            parent_excess_blob_gas = excess_blob_gas;
1018        }
1019
1020        state.merge_transitions(BundleRetention::Reverts);
1021    }
1022
1023    // Validate post state if present
1024    if let Some(expected_post_state) = &test_case.post_state {
1025        // Create debug info for post-state validation
1026        let debug_info = DebugInfo {
1027            pre_state: pre_state_debug.clone(),
1028            tx_env: None, // Last transaction is done
1029            block_env: block_env.clone(),
1030            cfg_env: cfg.clone(),
1031            block_idx: test_case.blocks.len(),
1032            tx_idx: 0,
1033            withdrawals: test_case.blocks.last().and_then(|b| b.withdrawals.clone()),
1034        };
1035        validate_post_state(
1036            &mut state,
1037            expected_post_state,
1038            &debug_info,
1039            print_env_on_error,
1040        )?;
1041    }
1042
1043    Ok(())
1044}
1045
1046/// Convert ForkSpec to SpecId
1047fn fork_to_spec_id(fork: ForkSpec) -> SpecId {
1048    match fork {
1049        ForkSpec::Frontier => SpecId::FRONTIER,
1050        ForkSpec::Homestead | ForkSpec::FrontierToHomesteadAt5 => SpecId::HOMESTEAD,
1051        ForkSpec::EIP150 | ForkSpec::HomesteadToDaoAt5 | ForkSpec::HomesteadToEIP150At5 => {
1052            SpecId::TANGERINE
1053        }
1054        ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON,
1055        ForkSpec::Byzantium
1056        | ForkSpec::EIP158ToByzantiumAt5
1057        | ForkSpec::ByzantiumToConstantinopleFixAt5 => SpecId::BYZANTIUM,
1058        ForkSpec::Constantinople | ForkSpec::ByzantiumToConstantinopleAt5 => SpecId::PETERSBURG,
1059        ForkSpec::ConstantinopleFix => SpecId::PETERSBURG,
1060        ForkSpec::Istanbul => SpecId::ISTANBUL,
1061        ForkSpec::Berlin => SpecId::BERLIN,
1062        ForkSpec::London | ForkSpec::BerlinToLondonAt5 => SpecId::LONDON,
1063        ForkSpec::Paris | ForkSpec::ParisToShanghaiAtTime15k => SpecId::MERGE,
1064        ForkSpec::Shanghai => SpecId::SHANGHAI,
1065        ForkSpec::Cancun | ForkSpec::ShanghaiToCancunAtTime15k => SpecId::CANCUN,
1066        ForkSpec::Prague | ForkSpec::CancunToPragueAtTime15k => SpecId::PRAGUE,
1067        ForkSpec::Osaka | ForkSpec::PragueToOsakaAtTime15k => SpecId::OSAKA,
1068        ForkSpec::Amsterdam => SpecId::AMSTERDAM,
1069        _ => SpecId::AMSTERDAM, // For any unknown forks, use latest available
1070    }
1071}
1072
1073/// Check if a test should be skipped based on its filename
1074fn skip_test(path: &Path) -> bool {
1075    let path_str = path.to_str().unwrap_or_default();
1076    // blobs excess gas calculation is not supported or osaka BPO configuration
1077    if path_str.contains("paris/eip7610_create_collision")
1078        || path_str.contains("cancun/eip4844_blobs")
1079        || path_str.contains("prague/eip7251_consolidations")
1080        || path_str.contains("prague/eip7685_general_purpose_el_requests")
1081        || path_str.contains("prague/eip7002_el_triggerable_withdrawals")
1082        || path_str.contains("osaka/eip7918_blob_reserve_price")
1083    {
1084        return true;
1085    }
1086
1087    let name = path.file_name().unwrap().to_str().unwrap_or_default();
1088    // Add any problematic tests here that should be skipped
1089    matches!(
1090        name,
1091        // Test check if gas price overflows, we handle this correctly but does not match tests specific exception.
1092        "CreateTransactionHighNonce.json"
1093
1094        // Test with some storage check.
1095        | "RevertInCreateInInit_Paris.json"
1096        | "RevertInCreateInInit.json"
1097        | "dynamicAccountOverwriteEmpty.json"
1098        | "dynamicAccountOverwriteEmpty_Paris.json"
1099        | "RevertInCreateInInitCreate2Paris.json"
1100        | "create2collisionStorage.json"
1101        | "RevertInCreateInInitCreate2.json"
1102        | "create2collisionStorageParis.json"
1103        | "InitCollision.json"
1104        | "InitCollisionParis.json"
1105
1106        // Malformed value.
1107        | "ValueOverflow.json"
1108        | "ValueOverflowParis.json"
1109
1110        // These tests are passing, but they take a lot of time to execute so we are going to skip them.
1111        | "Call50000_sha256.json"
1112        | "static_Call50000_sha256.json"
1113        | "loopMul.json"
1114        | "CALLBlake2f_MaxRounds.json"
1115        // TODO tests not checked, maybe related to parent block hashes as it is currently not supported in test.
1116        | "scenarios.json"
1117        // IT seems that post state is wrong, we properly handle max blob gas and state should stay the same.
1118        | "invalid_tx_max_fee_per_blob_gas.json"
1119        | "correct_increasing_blob_gas_costs.json"
1120        | "correct_decreasing_blob_gas_costs.json"
1121
1122        // test-fixtures/main/develop/blockchain_tests/prague/eip2935_historical_block_hashes_from_state/block_hashes/block_hashes_history.json
1123        | "block_hashes_history.json"
1124    )
1125}
1126
1127#[derive(Debug, Error)]
1128pub enum TestExecutionError {
1129    #[error("Database error: {0}")]
1130    Database(String),
1131
1132    #[error("Skipped fork: {0}")]
1133    SkippedFork(String),
1134
1135    #[error("Sender is required")]
1136    SenderRequired,
1137
1138    #[error("Expected failure at block {block_idx}, tx {tx_idx}: {message}")]
1139    ExpectedFailure {
1140        block_idx: usize,
1141        tx_idx: usize,
1142        message: String,
1143    },
1144
1145    #[error("Unexpected failure at block {block_idx}, tx {tx_idx}: {error}")]
1146    UnexpectedFailure {
1147        block_idx: usize,
1148        tx_idx: usize,
1149        error: String,
1150    },
1151
1152    #[error("Transaction env creation failed at block {block_idx}, tx {tx_idx}: {error}")]
1153    TransactionEnvCreation {
1154        block_idx: usize,
1155        tx_idx: usize,
1156        error: String,
1157    },
1158
1159    #[error("Unexpected revert at block {block_idx}, tx {tx_idx}, gas used: {gas_used}")]
1160    UnexpectedRevert {
1161        block_idx: usize,
1162        tx_idx: usize,
1163        gas_used: u64,
1164    },
1165
1166    #[error("Unexpected halt at block {block_idx}, tx {tx_idx}: {reason:?}, gas used: {gas_used}")]
1167    UnexpectedHalt {
1168        block_idx: usize,
1169        tx_idx: usize,
1170        reason: HaltReason,
1171        gas_used: u64,
1172    },
1173
1174    #[error("Block gas used mismatch at block {block_idx}: expected {expected}, got {actual}")]
1175    BlockGasUsedMismatch {
1176        block_idx: usize,
1177        expected: u64,
1178        actual: u64,
1179    },
1180
1181    #[error("Pre-block system call failed at block {block_idx}: {error}")]
1182    PreBlockSystemCall { block_idx: usize, error: String },
1183
1184    #[error("Post-block system call failed at block {block_idx}: {error}")]
1185    PostBlockSystemCall { block_idx: usize, error: String },
1186
1187    #[error("BAL error")]
1188    BalMismatchError,
1189
1190    #[error(
1191        "Post-state validation failed for {address:?}.{field}: expected {expected}, got {actual}"
1192    )]
1193    PostStateValidation {
1194        address: Address,
1195        field: String,
1196        expected: String,
1197        actual: String,
1198    },
1199}
1200
1201#[derive(Debug, Error)]
1202pub enum Error {
1203    #[error("Path not found: {0}")]
1204    PathNotFound(PathBuf),
1205
1206    #[error("No JSON files found in: {0}")]
1207    NoJsonFiles(PathBuf),
1208
1209    #[error("Failed to read file {0}: {1}")]
1210    FileRead(PathBuf, std::io::Error),
1211
1212    #[error("Failed to decode JSON from {0}: {1}")]
1213    JsonDecode(PathBuf, serde_json::Error),
1214
1215    #[error("Test execution failed for {test_name} in {test_path}: {error}")]
1216    TestExecution {
1217        test_name: String,
1218        test_path: PathBuf,
1219        error: String,
1220    },
1221
1222    #[error("Directory traversal error: {0}")]
1223    WalkDir(#[from] walkdir::Error),
1224
1225    #[error("{failed} tests failed")]
1226    TestsFailed { failed: usize },
1227}