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        if !json_output {
712            println!("Run block {block_idx}/{}", test_case.blocks.len());
713        }
714
715        // Check if this block should fail
716        let should_fail = block.expect_exception.is_some();
717
718        let transactions = block.transactions.as_deref().unwrap_or_default();
719
720        // Update block environment for this blockk
721
722        let mut block_hash = None;
723        let mut beacon_root = None;
724        let this_excess_blob_gas;
725
726        if let Some(block_header) = block.block_header.as_ref() {
727            block_hash = Some(block_header.hash);
728            beacon_root = block_header.parent_beacon_block_root;
729            block_env = block_header.to_block_env(Some(BlobExcessGasAndPrice::new_with_spec(
730                parent_excess_blob_gas,
731                spec_id,
732            )));
733            this_excess_blob_gas = block_header.excess_blob_gas.map(|i| i.to::<u64>());
734        } else {
735            this_excess_blob_gas = None;
736        }
737
738        let bal_test = block
739            .block_access_list
740            .as_ref()
741            .and_then(|bal| Bal::try_from(bal.clone()).ok())
742            .map(Arc::new);
743
744        //state.set_bal(bal_test);
745        state.reset_bal_index();
746
747        // Create EVM context for each transaction to ensure fresh state access
748        let evm_context = Context::mainnet()
749            .with_block(&block_env)
750            .with_cfg(cfg.clone())
751            .with_db(&mut state);
752
753        // Build and execute with EVM - always use inspector when JSON output is enabled
754        let mut evm = evm_context.build_mainnet_with_inspector(TracerEip3155::new_stdout());
755
756        // Pre block system calls
757        pre_block::pre_block_transition(&mut evm, spec_id, parent_block_hash, beacon_root)
758            .map_err(|e| TestExecutionError::PreBlockSystemCall {
759                block_idx,
760                error: format!("{e:?}"),
761            })?;
762
763        // Track cumulative gas used across all transactions in this block
764        let mut cumulative_gas_used: u64 = 0;
765        let mut block_completed = true;
766
767        // Execute each transaction in the block
768        for (tx_idx, tx) in transactions.iter().enumerate() {
769            if tx.sender.is_none() {
770                if print_env_on_error {
771                    let debug_info = DebugInfo {
772                        pre_state: pre_state_debug.clone(),
773                        tx_env: None,
774                        block_env: block_env.clone(),
775                        cfg_env: cfg.clone(),
776                        block_idx,
777                        tx_idx,
778                        withdrawals: block.withdrawals.clone(),
779                    };
780                    print_error_with_state(
781                        &debug_info,
782                        evm.ctx().db_ref(),
783                        test_case.post_state.as_ref(),
784                    );
785                }
786                if json_output {
787                    let output = json!({
788                        "block": block_idx,
789                        "tx": tx_idx,
790                        "error": "missing sender",
791                        "status": "skipped"
792                    });
793                    print_json(&output);
794                } else {
795                    eprintln!("āš ļø  Skipping block {block_idx} due to missing sender");
796                }
797                block_completed = false;
798                break; // Skip to next block
799            }
800
801            let tx_env = match tx.to_tx_env() {
802                Ok(env) => env,
803                Err(e) => {
804                    if should_fail {
805                        // Expected failure during tx env creation
806                        continue;
807                    }
808                    if print_env_on_error {
809                        let debug_info = DebugInfo {
810                            pre_state: pre_state_debug.clone(),
811                            tx_env: None,
812                            block_env: block_env.clone(),
813                            cfg_env: cfg.clone(),
814                            block_idx,
815                            tx_idx,
816                            withdrawals: block.withdrawals.clone(),
817                        };
818                        print_error_with_state(
819                            &debug_info,
820                            evm.ctx().db_ref(),
821                            test_case.post_state.as_ref(),
822                        );
823                    }
824                    if json_output {
825                        let output = json!({
826                            "block": block_idx,
827                            "tx": tx_idx,
828                            "error": format!("tx env creation error: {e}"),
829                            "status": "skipped"
830                        });
831                        print_json(&output);
832                    } else {
833                        eprintln!(
834                            "āš ļø  Skipping block {block_idx} due to transaction env creation error: {e}"
835                        );
836                    }
837                    block_completed = false;
838                    break; // Skip to next block
839                }
840            };
841
842            // bump bal index
843            evm.db_mut().bump_bal_index();
844
845            // If JSON output requested, output transaction details
846            let execution_result = if json_output {
847                evm.inspect_tx(tx_env.clone())
848            } else {
849                evm.transact(tx_env.clone())
850            };
851
852            match execution_result {
853                Ok(result) => {
854                    if should_fail {
855                        // Unexpected success - should have failed but didn't
856                        // If not expected to fail, use inspector to trace the transaction
857                        if print_env_on_error {
858                            // Re-run with inspector to get detailed trace
859                            if json_output {
860                                eprintln!("=== Transaction trace (unexpected success) ===");
861                            }
862                            let _ = evm.inspect_tx(tx_env.clone());
863                        }
864
865                        if print_env_on_error {
866                            let debug_info = DebugInfo {
867                                pre_state: pre_state_debug.clone(),
868                                tx_env: Some(tx_env.clone()),
869                                block_env: block_env.clone(),
870                                cfg_env: cfg.clone(),
871                                block_idx,
872                                tx_idx,
873                                withdrawals: block.withdrawals.clone(),
874                            };
875                            print_error_with_state(
876                                &debug_info,
877                                evm.ctx().db_ref(),
878                                test_case.post_state.as_ref(),
879                            );
880                        }
881                        let expected_exception = block.expect_exception.clone().unwrap_or_default();
882                        if json_output {
883                            let output = json!({
884                                "block": block_idx,
885                                "tx": tx_idx,
886                                "expected_exception": expected_exception,
887                                "gas_used": result.result.gas_used(),
888                                "status": "unexpected_success"
889                            });
890                            print_json(&output);
891                        } else {
892                            eprintln!(
893                                "āš ļø  Skipping block {block_idx}: transaction unexpectedly succeeded (expected failure: {expected_exception})"
894                            );
895                        }
896                        block_completed = false;
897                        break; // Skip to next block
898                    }
899                    // EIP-7778: Block gas accounting without refunds.
900                    // For Amsterdam+, block gas = max(spent, floor_gas).
901                    // For pre-Amsterdam, block gas = used() = max(spent - refunded, floor_gas).
902                    let gas = result.result.gas();
903                    cumulative_gas_used += if spec_id.is_enabled_in(SpecId::AMSTERDAM) {
904                        gas.spent().max(gas.floor_gas())
905                    } else {
906                        gas.used()
907                    };
908                    evm.commit(result.state);
909                }
910                Err(e) => {
911                    if !should_fail {
912                        // Unexpected error - use inspector to trace the transaction
913                        if print_env_on_error {
914                            if json_output {
915                                eprintln!("=== Transaction trace (unexpected failure) ===");
916                            }
917                            let _ = evm.inspect_tx(tx_env.clone());
918                        }
919
920                        if print_env_on_error {
921                            let debug_info = DebugInfo {
922                                pre_state: pre_state_debug.clone(),
923                                tx_env: Some(tx_env.clone()),
924                                block_env: block_env.clone(),
925                                cfg_env: cfg.clone(),
926                                block_idx,
927                                tx_idx,
928                                withdrawals: block.withdrawals.clone(),
929                            };
930                            print_error_with_state(
931                                &debug_info,
932                                evm.ctx().db_ref(),
933                                test_case.post_state.as_ref(),
934                            );
935                        }
936                        if json_output {
937                            let output = json!({
938                                "block": block_idx,
939                                "tx": tx_idx,
940                                "error": format!("{e:?}"),
941                                "status": "unexpected_failure"
942                            });
943                            print_json(&output);
944                        } else {
945                            eprintln!(
946                                "āš ļø  Skipping block {block_idx} due to unexpected failure: {e:?}"
947                            );
948                        }
949                        block_completed = false;
950                        break; // Skip to next block
951                    } else if json_output {
952                        // Expected failure
953                        let output = json!({
954                            "block": block_idx,
955                            "tx": tx_idx,
956                            "error": format!("{e:?}"),
957                            "status": "expected_failure"
958                        });
959                        print_json(&output);
960                    }
961                }
962            }
963        }
964
965        // Validate block gas used against header
966        if block_completed && !should_fail {
967            if let Some(block_header) = block.block_header.as_ref() {
968                let expected_gas_used = block_header.gas_used.to::<u64>();
969                if cumulative_gas_used != expected_gas_used {
970                    if print_env_on_error {
971                        eprintln!(
972                            "Block gas used mismatch at block {block_idx}: expected {expected_gas_used}, got {cumulative_gas_used}"
973                        );
974                    }
975                    return Err(TestExecutionError::BlockGasUsedMismatch {
976                        block_idx,
977                        expected: expected_gas_used,
978                        actual: cumulative_gas_used,
979                    });
980                }
981            }
982        }
983
984        // bump bal index
985        evm.db_mut().bump_bal_index();
986
987        // uncle rewards are not implemented yet
988        post_block::post_block_transition(
989            &mut evm,
990            &block_env,
991            block.withdrawals.as_deref().unwrap_or_default(),
992            spec_id,
993        )
994        .map_err(|e| TestExecutionError::PostBlockSystemCall {
995            block_idx,
996            error: format!("{e:?}"),
997        })?;
998
999        // insert present block hash.
1000        state
1001            .block_hashes
1002            .insert(block_env.number.to::<u64>(), block_hash.unwrap_or_default());
1003
1004        if let Some(bal) = state.bal_state.bal_builder.take() {
1005            if let Some(state_bal) = bal_test {
1006                if &bal != state_bal.as_ref() {
1007                    println!("Bal mismatch");
1008                    println!("Test bal");
1009                    state_bal.pretty_print();
1010                    println!("Bal:");
1011                    bal.pretty_print();
1012                    return Err(TestExecutionError::BalMismatchError);
1013                }
1014            }
1015        }
1016
1017        parent_block_hash = block_hash;
1018        if let Some(excess_blob_gas) = this_excess_blob_gas {
1019            parent_excess_blob_gas = excess_blob_gas;
1020        }
1021
1022        state.merge_transitions(BundleRetention::Reverts);
1023    }
1024
1025    // Validate post state if present
1026    if let Some(expected_post_state) = &test_case.post_state {
1027        // Create debug info for post-state validation
1028        let debug_info = DebugInfo {
1029            pre_state: pre_state_debug.clone(),
1030            tx_env: None, // Last transaction is done
1031            block_env: block_env.clone(),
1032            cfg_env: cfg.clone(),
1033            block_idx: test_case.blocks.len(),
1034            tx_idx: 0,
1035            withdrawals: test_case.blocks.last().and_then(|b| b.withdrawals.clone()),
1036        };
1037        validate_post_state(
1038            &mut state,
1039            expected_post_state,
1040            &debug_info,
1041            print_env_on_error,
1042        )?;
1043    }
1044
1045    Ok(())
1046}
1047
1048/// Convert ForkSpec to SpecId
1049fn fork_to_spec_id(fork: ForkSpec) -> SpecId {
1050    match fork {
1051        ForkSpec::Frontier => SpecId::FRONTIER,
1052        ForkSpec::Homestead | ForkSpec::FrontierToHomesteadAt5 => SpecId::HOMESTEAD,
1053        ForkSpec::EIP150 | ForkSpec::HomesteadToDaoAt5 | ForkSpec::HomesteadToEIP150At5 => {
1054            SpecId::TANGERINE
1055        }
1056        ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON,
1057        ForkSpec::Byzantium
1058        | ForkSpec::EIP158ToByzantiumAt5
1059        | ForkSpec::ByzantiumToConstantinopleFixAt5 => SpecId::BYZANTIUM,
1060        ForkSpec::Constantinople | ForkSpec::ByzantiumToConstantinopleAt5 => SpecId::PETERSBURG,
1061        ForkSpec::ConstantinopleFix => SpecId::PETERSBURG,
1062        ForkSpec::Istanbul => SpecId::ISTANBUL,
1063        ForkSpec::Berlin => SpecId::BERLIN,
1064        ForkSpec::London | ForkSpec::BerlinToLondonAt5 => SpecId::LONDON,
1065        ForkSpec::Paris | ForkSpec::ParisToShanghaiAtTime15k => SpecId::MERGE,
1066        ForkSpec::Shanghai => SpecId::SHANGHAI,
1067        ForkSpec::Cancun | ForkSpec::ShanghaiToCancunAtTime15k => SpecId::CANCUN,
1068        ForkSpec::Prague | ForkSpec::CancunToPragueAtTime15k => SpecId::PRAGUE,
1069        ForkSpec::Osaka | ForkSpec::PragueToOsakaAtTime15k => SpecId::OSAKA,
1070        ForkSpec::Amsterdam => SpecId::AMSTERDAM,
1071        _ => SpecId::AMSTERDAM, // For any unknown forks, use latest available
1072    }
1073}
1074
1075/// Check if a test should be skipped based on its filename
1076fn skip_test(path: &Path) -> bool {
1077    let path_str = path.to_str().unwrap_or_default();
1078    // blobs excess gas calculation is not supported or osaka BPO configuration
1079    if path_str.contains("paris/eip7610_create_collision")
1080        || path_str.contains("cancun/eip4844_blobs")
1081        || path_str.contains("prague/eip7251_consolidations")
1082        || path_str.contains("prague/eip7685_general_purpose_el_requests")
1083        || path_str.contains("prague/eip7002_el_triggerable_withdrawals")
1084        || path_str.contains("osaka/eip7918_blob_reserve_price")
1085    {
1086        return true;
1087    }
1088
1089    let name = path.file_name().unwrap().to_str().unwrap_or_default();
1090    // Add any problematic tests here that should be skipped
1091    matches!(
1092        name,
1093        // Test check if gas price overflows, we handle this correctly but does not match tests specific exception.
1094        "CreateTransactionHighNonce.json"
1095
1096        // Test with some storage check.
1097        | "RevertInCreateInInit_Paris.json"
1098        | "RevertInCreateInInit.json"
1099        | "dynamicAccountOverwriteEmpty.json"
1100        | "dynamicAccountOverwriteEmpty_Paris.json"
1101        | "RevertInCreateInInitCreate2Paris.json"
1102        | "create2collisionStorage.json"
1103        | "RevertInCreateInInitCreate2.json"
1104        | "create2collisionStorageParis.json"
1105        | "InitCollision.json"
1106        | "InitCollisionParis.json"
1107
1108        // Malformed value.
1109        | "ValueOverflow.json"
1110        | "ValueOverflowParis.json"
1111
1112        // These tests are passing, but they take a lot of time to execute so we are going to skip them.
1113        | "Call50000_sha256.json"
1114        | "static_Call50000_sha256.json"
1115        | "loopMul.json"
1116        | "CALLBlake2f_MaxRounds.json"
1117        // TODO tests not checked, maybe related to parent block hashes as it is currently not supported in test.
1118        | "scenarios.json"
1119        // IT seems that post state is wrong, we properly handle max blob gas and state should stay the same.
1120        | "invalid_tx_max_fee_per_blob_gas.json"
1121        | "correct_increasing_blob_gas_costs.json"
1122        | "correct_decreasing_blob_gas_costs.json"
1123
1124        // test-fixtures/main/develop/blockchain_tests/prague/eip2935_historical_block_hashes_from_state/block_hashes/block_hashes_history.json
1125        | "block_hashes_history.json"
1126    )
1127}
1128
1129#[derive(Debug, Error)]
1130pub enum TestExecutionError {
1131    #[error("Database error: {0}")]
1132    Database(String),
1133
1134    #[error("Skipped fork: {0}")]
1135    SkippedFork(String),
1136
1137    #[error("Sender is required")]
1138    SenderRequired,
1139
1140    #[error("Expected failure at block {block_idx}, tx {tx_idx}: {message}")]
1141    ExpectedFailure {
1142        block_idx: usize,
1143        tx_idx: usize,
1144        message: String,
1145    },
1146
1147    #[error("Unexpected failure at block {block_idx}, tx {tx_idx}: {error}")]
1148    UnexpectedFailure {
1149        block_idx: usize,
1150        tx_idx: usize,
1151        error: String,
1152    },
1153
1154    #[error("Transaction env creation failed at block {block_idx}, tx {tx_idx}: {error}")]
1155    TransactionEnvCreation {
1156        block_idx: usize,
1157        tx_idx: usize,
1158        error: String,
1159    },
1160
1161    #[error("Unexpected revert at block {block_idx}, tx {tx_idx}, gas used: {gas_used}")]
1162    UnexpectedRevert {
1163        block_idx: usize,
1164        tx_idx: usize,
1165        gas_used: u64,
1166    },
1167
1168    #[error("Unexpected halt at block {block_idx}, tx {tx_idx}: {reason:?}, gas used: {gas_used}")]
1169    UnexpectedHalt {
1170        block_idx: usize,
1171        tx_idx: usize,
1172        reason: HaltReason,
1173        gas_used: u64,
1174    },
1175
1176    #[error("Block gas used mismatch at block {block_idx}: expected {expected}, got {actual}")]
1177    BlockGasUsedMismatch {
1178        block_idx: usize,
1179        expected: u64,
1180        actual: u64,
1181    },
1182
1183    #[error("Pre-block system call failed at block {block_idx}: {error}")]
1184    PreBlockSystemCall { block_idx: usize, error: String },
1185
1186    #[error("Post-block system call failed at block {block_idx}: {error}")]
1187    PostBlockSystemCall { block_idx: usize, error: String },
1188
1189    #[error("BAL error")]
1190    BalMismatchError,
1191
1192    #[error(
1193        "Post-state validation failed for {address:?}.{field}: expected {expected}, got {actual}"
1194    )]
1195    PostStateValidation {
1196        address: Address,
1197        field: String,
1198        expected: String,
1199        actual: String,
1200    },
1201}
1202
1203#[derive(Debug, Error)]
1204pub enum Error {
1205    #[error("Path not found: {0}")]
1206    PathNotFound(PathBuf),
1207
1208    #[error("No JSON files found in: {0}")]
1209    NoJsonFiles(PathBuf),
1210
1211    #[error("Failed to read file {0}: {1}")]
1212    FileRead(PathBuf, std::io::Error),
1213
1214    #[error("Failed to decode JSON from {0}: {1}")]
1215    JsonDecode(PathBuf, serde_json::Error),
1216
1217    #[error("Test execution failed for {test_name} in {test_path}: {error}")]
1218    TestExecution {
1219        test_name: String,
1220        test_path: PathBuf,
1221        error: String,
1222    },
1223
1224    #[error("Directory traversal error: {0}")]
1225    WalkDir(#[from] walkdir::Error),
1226
1227    #[error("{failed} tests failed")]
1228    TestsFailed { failed: usize },
1229}