55//!
66//! Every test runs at O0, O1, O2, and O3 to catch optimizer bugs.
77
8- use std:: path:: PathBuf ;
9-
10- use edge_driver:: {
11- compiler:: Compiler ,
12- config:: { CompilerConfig , EmitKind } ,
13- } ;
148use revm:: {
159 context:: { Context , TxEnv } ,
1610 database:: { CacheDB , EmptyDB } ,
17- handler:: { MainBuilder , MainnetContext } ,
11+ handler:: MainnetContext ,
1812 primitives:: { Address , Bytes , TxKind , U256 } ,
1913 state:: AccountInfo ,
20- ExecuteCommitEvm , MainContext , MainnetEvm ,
14+ ExecuteCommitEvm , MainBuilder , MainContext , MainnetEvm ,
2115} ;
2216use tiny_keccak:: { Hasher , Keccak } ;
2317
18+ use crate :: helpers:: { calldata, compile_contract_opt, decode_u256, encode_u256, selector, CALLER } ;
19+
2420// =============================================================================
25- // Shared helpers
21+ // Shared helpers (features-specific — EvmHandle returns CallResult with logs)
2622// =============================================================================
2723
28- fn workspace_root ( ) -> PathBuf {
29- PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( "../.." )
30- }
31-
32- fn compile_contract_opt ( relative_path : & str , opt_level : u8 ) -> Vec < u8 > {
33- let path = workspace_root ( ) . join ( relative_path) ;
34- let mut config = CompilerConfig :: new ( path) ;
35- config. emit = EmitKind :: Bytecode ;
36- config. optimization_level = opt_level;
37- let mut compiler = Compiler :: new ( config) . expect ( "compiler init failed" ) ;
38- let output = compiler. compile ( ) . expect ( "compile failed" ) ;
39- output. bytecode . expect ( "no bytecode produced" )
40- }
41-
42- fn selector ( sig : & str ) -> [ u8 ; 4 ] {
43- let mut h = Keccak :: v256 ( ) ;
44- h. update ( sig. as_bytes ( ) ) ;
45- let mut out = [ 0u8 ; 32 ] ;
46- h. finalize ( & mut out) ;
47- [ out[ 0 ] , out[ 1 ] , out[ 2 ] , out[ 3 ] ]
48- }
49-
5024fn event_sig ( sig : & str ) -> [ u8 ; 32 ] {
5125 let mut h = Keccak :: v256 ( ) ;
5226 h. update ( sig. as_bytes ( ) ) ;
@@ -55,30 +29,12 @@ fn event_sig(sig: &str) -> [u8; 32] {
5529 out
5630}
5731
58- fn encode_u256 ( val : u64 ) -> [ u8 ; 32 ] {
59- let mut out = [ 0u8 ; 32 ] ;
60- out[ 24 ..] . copy_from_slice ( & val. to_be_bytes ( ) ) ;
61- out
62- }
63-
6432const fn encode_addr ( suffix : u8 ) -> [ u8 ; 32 ] {
6533 let mut out = [ 0u8 ; 32 ] ;
6634 out[ 31 ] = suffix;
6735 out
6836}
6937
70- fn decode_u256 ( output : & [ u8 ] ) -> u64 {
71- assert ! (
72- output. len( ) >= 32 ,
73- "return value too short: {} bytes" ,
74- output. len( )
75- ) ;
76- assert_eq ! ( & output[ 0 ..24 ] , & [ 0u8 ; 24 ] , "u256 too large for u64" ) ;
77- u64:: from_be_bytes ( output[ 24 ..32 ] . try_into ( ) . unwrap ( ) )
78- }
79-
80- const CALLER : Address = Address :: ZERO ;
81-
8238type TestDb = CacheDB < EmptyDB > ;
8339type TestEvm = MainnetEvm < MainnetContext < TestDb > > ;
8440
@@ -164,20 +120,23 @@ struct CallResult {
164120 logs : Vec < LogEntry > ,
165121}
166122
167- fn calldata ( sel : [ u8 ; 4 ] , args : & [ [ u8 ; 32 ] ] ) -> Vec < u8 > {
168- let mut cd = sel. to_vec ( ) ;
169- for a in args {
170- cd. extend_from_slice ( a) ;
171- }
172- cd
173- }
174-
175- fn for_all_opt_levels ( contract_path : & str , test_fn : impl Fn ( & mut EvmHandle , u8 ) ) {
176- for opt in 0 ..=3 {
177- let bc = compile_contract_opt ( contract_path, opt) ;
178- let mut h = EvmHandle :: new ( bc) ;
179- test_fn ( & mut h, opt) ;
180- }
123+ fn for_all_opt_levels ( contract_path : & str , test_fn : impl Fn ( & mut EvmHandle , u8 ) + Sync ) {
124+ std:: thread:: scope ( |s| {
125+ let handles: Vec < _ > = ( 0 ..=3 )
126+ . map ( |opt| {
127+ let test_fn = & test_fn;
128+ let path = contract_path;
129+ s. spawn ( move || {
130+ let bc = compile_contract_opt ( path, opt) ;
131+ let mut h = EvmHandle :: new ( bc) ;
132+ test_fn ( & mut h, opt) ;
133+ } )
134+ } )
135+ . collect ( ) ;
136+ for h in handles {
137+ h. join ( ) . unwrap ( ) ;
138+ }
139+ } ) ;
181140}
182141
183142// =============================================================================
0 commit comments