|
| 1 | +use revm_trace::{create_evm_with_tracer,TransactionTrace, TxInspector, SimulationBatch, SimulationTx}; |
| 2 | +use alloy::primitives::{address, U256, TxKind}; |
| 3 | + |
| 4 | +/// Test to verify that inspector state is properly reset between transactions in batch processing |
| 5 | +/// |
| 6 | +/// This test creates a SINGLE BATCH containing two ETH transfers and verifies that: |
| 7 | +/// 1. Both transactions are processed in the same batch call |
| 8 | +/// 2. Inspector state is properly reset between transactions within the batch |
| 9 | +/// 3. Simple check: if asset_transfers lengths are equal but contents differ, reset worked |
| 10 | +#[tokio::main] |
| 11 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 12 | + println!("🧪 Testing Inspector Reset in Single Batch Processing"); |
| 13 | + println!("{}", "=".repeat(60)); |
| 14 | + |
| 15 | + // Create EVM with tracer |
| 16 | + let trace_inspector = TxInspector::new(); |
| 17 | + let mut evm = create_evm_with_tracer("https://eth.llamarpc.com", trace_inspector).await?; |
| 18 | + |
| 19 | + // Define test addresses |
| 20 | + let from = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"); // Vitalik's address |
| 21 | + let to1 = address!("0000000000000000000000000000000000000001"); // First recipient |
| 22 | + let to2 = address!("0000000000000000000000000000000000000002"); // Second recipient |
| 23 | + let value = U256::from(1000000000000000u64); // 0.001 ETH |
| 24 | + |
| 25 | + // Create single batch containing both transactions using SimulationTx |
| 26 | + let tx1 = SimulationTx { |
| 27 | + caller: from, |
| 28 | + transact_to: TxKind::Call(to1), |
| 29 | + value, |
| 30 | + data: vec![].into(), |
| 31 | + }; |
| 32 | + |
| 33 | + let tx2 = SimulationTx { |
| 34 | + caller: from, |
| 35 | + transact_to: TxKind::Call(to2), |
| 36 | + value, |
| 37 | + data: vec![].into(), |
| 38 | + }; |
| 39 | + |
| 40 | + let single_batch = SimulationBatch { |
| 41 | + block_env: None, |
| 42 | + transactions: vec![tx1, tx2], |
| 43 | + is_stateful: false, // stateless mode |
| 44 | + }; |
| 45 | + |
| 46 | + println!("📦 Processing SINGLE BATCH containing {} transactions:", single_batch.transactions.len()); |
| 47 | + println!(" 📤 TX1: {} -> {} (value: {})", from, to1, value); |
| 48 | + println!(" 📤 TX2: {} -> {} (value: {})", from, to2, value); |
| 49 | + println!(" 🎯 Key Test: Inspector should reset between TX1 and TX2 within this batch"); |
| 50 | + |
| 51 | + // Execute the single batch containing both transactions |
| 52 | + let results: Vec<_> = evm.trace_transactions(single_batch) |
| 53 | + .into_iter() |
| 54 | + .map(|r| r.unwrap()) |
| 55 | + .collect(); |
| 56 | + |
| 57 | + println!("\n✅ Single batch execution completed with {} results", results.len()); |
| 58 | + |
| 59 | + // Extract outputs from each transaction |
| 60 | + if results.len() != 2 { |
| 61 | + return Err(format!("Expected 2 results, got {}", results.len()).into()); |
| 62 | + } |
| 63 | + |
| 64 | + let (_result1, output1) = &results[0]; |
| 65 | + let (_result2, output2) = &results[1]; |
| 66 | + |
| 67 | + println!("\n📊 Transaction 1 Results:"); |
| 68 | + println!(" 💰 Asset transfers: {}", output1.asset_transfers.len()); |
| 69 | + if let Some(transfer) = output1.asset_transfers.first() { |
| 70 | + println!(" 🎯 First transfer to: {:?}", transfer.to); |
| 71 | + } |
| 72 | + |
| 73 | + println!("\n📊 Transaction 2 Results:"); |
| 74 | + println!(" 💰 Asset transfers: {}", output2.asset_transfers.len()); |
| 75 | + if let Some(transfer) = output2.asset_transfers.first() { |
| 76 | + println!(" 🎯 First transfer to: {:?}", transfer.to); |
| 77 | + } |
| 78 | + |
| 79 | + // Simplified verification: lengths equal but contents different = reset worked |
| 80 | + println!("\n🔍 Verification Results:"); |
| 81 | + println!("{}", "=".repeat(60)); |
| 82 | + |
| 83 | + if output1.asset_transfers.len() == output2.asset_transfers.len() { |
| 84 | + println!("✅ PASS: Asset transfer lengths are equal ({} each)", output1.asset_transfers.len()); |
| 85 | + |
| 86 | + // Check if contents are different (which proves reset worked) |
| 87 | + let contents_different = if let (Some(transfer1), Some(transfer2)) = ( |
| 88 | + output1.asset_transfers.first(), |
| 89 | + output2.asset_transfers.first() |
| 90 | + ) { |
| 91 | + transfer1.to != transfer2.to |
| 92 | + } else { |
| 93 | + false |
| 94 | + }; |
| 95 | + |
| 96 | + if contents_different { |
| 97 | + println!("✅ PASS: Asset transfer contents are different"); |
| 98 | + println!(" 🎉 Inspector was properly reset between transactions!"); |
| 99 | + println!(" ✨ TX1 and TX2 have independent outputs despite being in same batch"); |
| 100 | + } else { |
| 101 | + println!("❌ FAIL: Asset transfer contents are the same"); |
| 102 | + println!(" 🚨 Inspector was NOT reset between transactions!"); |
| 103 | + return Err("Inspector reset test failed - contents identical".into()); |
| 104 | + } |
| 105 | + } else { |
| 106 | + println!("❌ FAIL: Asset transfer lengths differ ({} vs {})", |
| 107 | + output1.asset_transfers.len(), output2.asset_transfers.len()); |
| 108 | + println!(" 🚨 This suggests state accumulation or other issues"); |
| 109 | + return Err("Inspector reset test failed - length mismatch".into()); |
| 110 | + } |
| 111 | + |
| 112 | + println!("\n🎉 All tests PASSED! Inspector reset is working correctly within single batch!"); |
| 113 | + println!(" ✨ Both transactions were processed in the same batch call"); |
| 114 | + println!(" ✨ Equal lengths + different contents = proper reset"); |
| 115 | + println!(" ✨ No state accumulation between TX1 and TX2 within the batch"); |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
0 commit comments