@@ -5,17 +5,19 @@ use std::str::FromStr;
55use alloy_primitives:: { Address , B256 , U256 } ;
66use clap:: { Args , Parser } ;
77use mega_evm:: {
8- alloy_evm:: Database ,
98 revm:: {
109 context:: { block:: BlockEnv , cfg:: CfgEnv } ,
10+ database:: { DatabaseRef , WrapDatabaseRef } ,
1111 primitives:: eip4844,
1212 } ,
13- BucketId , MegaContext , MegaSpecId , TestExternalEnvs ,
13+ BucketId , MegaContext , MegaSpecId , TestDatabaseWrapper , TestExternalEnvs ,
1414} ;
1515use tracing:: { debug, trace} ;
1616
1717use super :: { EvmeError , Result } ;
1818
19+ type ConfiguredRefDatabase < DB > = TestDatabaseWrapper < WrapDatabaseRef < DB > > ;
20+
1921/// Chain configuration arguments (spec and chain ID)
2022#[ derive( Args , Debug , Clone ) ]
2123#[ command( next_help_heading = "Chain Options" ) ]
@@ -127,19 +129,20 @@ pub struct ExtEnvArgs {
127129}
128130
129131impl ExtEnvArgs {
130- /// Creates [`TestExternalEnvs`] and returns bucket capacity configuration.
131- pub fn create_external_envs ( & self ) -> Result < ( TestExternalEnvs , Vec < ( BucketId , u64 ) > ) > {
132- let external_envs = TestExternalEnvs :: new ( ) ;
132+ /// Parses CLI-provided bucket capacity overrides.
133+ pub fn bucket_capacities ( & self ) -> Result < Vec < ( BucketId , u64 ) > > {
134+ self . bucket_capacity
135+ . iter ( )
136+ . map ( |bucket_capacity| parse_bucket_capacity ( bucket_capacity) )
137+ . collect ( )
138+ }
133139
134- // Parse bucket capacities
135- let mut bucket_capacities = Vec :: new ( ) ;
136- for bucket_capacity_str in & self . bucket_capacity {
137- let ( bucket_id, capacity) = parse_bucket_capacity ( bucket_capacity_str) ?;
138- bucket_capacities. push ( ( bucket_id, capacity) ) ;
139- }
140- debug ! ( external_envs = ?external_envs, bucket_capacities = ?bucket_capacities, "Evm TestExternalEnvs created" ) ;
140+ /// Creates [`TestExternalEnvs`].
141+ pub fn create_external_envs ( & self ) -> Result < TestExternalEnvs > {
142+ let external_envs = TestExternalEnvs :: new ( ) ;
143+ debug ! ( external_envs = ?external_envs, "Evm TestExternalEnvs created" ) ;
141144
142- Ok ( ( external_envs, bucket_capacities ) )
145+ Ok ( external_envs)
143146 }
144147}
145148
@@ -175,19 +178,26 @@ impl EnvArgs {
175178 self . block . create_block_env ( )
176179 }
177180
178- /// Creates [`TestExternalEnvs`] and returns bucket capacity configuration .
179- pub fn create_external_envs ( & self ) -> Result < ( TestExternalEnvs , Vec < ( BucketId , u64 ) > ) > {
181+ /// Creates [`TestExternalEnvs`].
182+ pub fn create_external_envs ( & self ) -> Result < TestExternalEnvs > {
180183 self . ext . create_external_envs ( )
181184 }
182185
183186 /// Creates a [`MegaContext`] with all environment configurations.
184- pub fn create_evm_context < DB : Database > (
187+ pub fn create_evm_context < DB > (
185188 & self ,
186189 db : DB ,
187- ) -> Result < MegaContext < DB , TestExternalEnvs > > {
190+ ) -> Result < MegaContext < ConfiguredRefDatabase < DB > , TestExternalEnvs > >
191+ where
192+ DB : DatabaseRef ,
193+ <DB as DatabaseRef >:: Error : Send + Sync + ' static ,
194+ {
188195 let cfg = self . create_cfg_env ( ) ?;
189196 let block = self . create_block_env ( ) ?;
190- let ( external_envs, _bucket_capacities) = self . create_external_envs ( ) ?;
197+ let external_envs = self . create_external_envs ( ) ?;
198+ let bucket_capacities = self . ext . bucket_capacities ( ) ?;
199+ let db = TestDatabaseWrapper :: new ( WrapDatabaseRef :: from ( db) )
200+ . with_bucket_capacities ( bucket_capacities) ;
191201
192202 Ok ( MegaContext :: new ( db, cfg. spec )
193203 . with_cfg ( cfg)
@@ -218,3 +228,25 @@ pub fn parse_bucket_capacity(s: &str) -> Result<(u32, u64)> {
218228 trace ! ( string = %s, bucket_id = %bucket_id, capacity = %capacity, "Parsed bucket capacity" ) ;
219229 Ok ( ( bucket_id, capacity) )
220230}
231+
232+ #[ cfg( test) ]
233+ mod tests {
234+ use super :: * ;
235+ use mega_evm:: { revm:: Database as _, test_utils:: MemoryDatabase , MIN_BUCKET_SIZE } ;
236+
237+ #[ test]
238+ fn bucket_capacities_apply_to_wrapped_database ( ) {
239+ let address = Address :: repeat_byte ( 0x11 ) ;
240+ let bucket_id = TestDatabaseWrapper :: < MemoryDatabase > :: bucket_id_for_account ( address) ;
241+ let capacity = MIN_BUCKET_SIZE as u64 * 4 ;
242+ let args = ExtEnvArgs { bucket_capacity : vec ! [ format!( "{bucket_id}:{capacity}" ) ] } ;
243+
244+ let wrapped = TestDatabaseWrapper :: new ( MemoryDatabase :: default ( ) )
245+ . with_bucket_capacities ( args. bucket_capacities ( ) . unwrap ( ) ) ;
246+
247+ assert_eq ! (
248+ wrapped. salt_bucket_capacity( address, None ) . unwrap( ) ,
249+ ( bucket_id as usize , capacity)
250+ ) ;
251+ }
252+ }
0 commit comments