@@ -202,13 +202,20 @@ pub struct Cheatcodes {
202202pub  breakpoints :  Breakpoints , 
203203
204204    /// Track if cool cheatcode was called on each address 
205- pub  addresses :  HashMap < Address ,  AddressState > , 
206-     /// Track if an addresses storage slot is cool or not 
207- pub  address_storage :  HashMap < Address ,  HashMap < U256 ,  StorageSlotState > > , 
205+ pub  addresses :  HashMap < Address ,  CoolState > , 
208206    /// How much gas to charge in the next step (op code) based on cool cheatcode calculations 
209207pub  additional_gas_next_op :  u64 , 
210208} 
211209
210+ /// Tracking if an address and it's slots are cool 
211+ #[ derive( Debug ,  Clone ) ]  
212+ pub  struct  CoolState  { 
213+     /// Track if address is cool 
214+ pub  address :  AddressState , 
215+     /// Track if each storage slot is cool or not 
216+ pub  slots :  HashMap < U256 ,  StorageSlotState > , 
217+ } 
218+ 
212219/// Whether an Address is accessed or not 
213220#[ derive( Clone ,  PartialEq ,  Debug ) ]  
214221pub  enum  AddressState  { 
@@ -235,7 +242,7 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
235242    interpreter :  & mut  Interpreter , 
236243    data :  & mut  EVMData < ' _ ,  DB > , 
237244)  -> InstructionResult  { 
238-     if  state. addresses . len ( )  ==  0  { 
245+     if  state. addresses . is_empty ( )  { 
239246        return  InstructionResult :: Continue 
240247    } 
241248
@@ -268,9 +275,11 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
268275                    // COLD_ACCOUNT_ACCESS_COST is 2600 
269276                    // check this is done once per address, unless cheatcode is called again 
270277                    // ignore if same as contract address 
271-                     if  state. addresses . get ( & addr)  == Some ( & AddressState :: Cool )  { 
272-                         state. additional_gas_next_op  = 2500 ; 
273-                         state. addresses . insert ( addr,  AddressState :: Warm ) ; 
278+                     if  let  Some ( cool_state)  = state. addresses . get_mut ( & addr)  { 
279+                         if  cool_state. address  == AddressState :: Cool  { 
280+                             state. additional_gas_next_op  = 2500 ; 
281+                             cool_state. address  = AddressState :: Warm ; 
282+                         } 
274283                    } 
275284                } 
276285            } 
@@ -283,32 +292,34 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
283292                    // COLD_ACCOUNT_ACCESS_COST is 2600 
284293                    // check this is done once per address, unless cheatcode is called again 
285294                    // ignore if same as contract address 
286-                     if  state. addresses . get ( & addr)  == Some ( & AddressState :: Cool )  { 
287-                         state. additional_gas_next_op  = 2500 ; 
288-                         state. addresses . insert ( addr,  AddressState :: Warm ) ; 
295+                     if  let  Some ( cool_state)  = state. addresses . get_mut ( & addr)  { 
296+                         if  cool_state. address  == AddressState :: Cool  { 
297+                             state. additional_gas_next_op  = 2500 ; 
298+                             cool_state. address  = AddressState :: Warm ; 
299+                         } 
289300                    } 
290301                } 
291302            } 
292303            _ => { } 
293304        } 
294305        // check target's slots 
295-         if  let  Some ( contract_storage )  = state. address_storage . get_mut ( & contract_address)  { 
306+         if  let  Some ( cool_state )  = state. addresses . get_mut ( & contract_address)  { 
296307            match  interpreter. current_opcode ( )  { 
297308                opcode:: SLOAD  => { 
298309                    let  key = try_or_continue ! ( interpreter. stack( ) . peek( 0 ) ) ; 
299310
300311                    let  account = data. journaled_state . state ( ) . get ( & contract_address) . unwrap ( ) ; 
301312                    if  account. storage . get ( & key) . is_some ( )  { 
302-                         match  contract_storage . get ( & key)  { 
313+                         match  cool_state . slots . get ( & key)  { 
303314                            None  | Some ( StorageSlotState :: Cool )  => { 
304315                                // COLD_SLOAD_COST is 2100 
305316                                state. additional_gas_next_op  = 2000 ; 
306-                                 contract_storage . insert ( key,  StorageSlotState :: WarmWithSLOAD ) ; 
317+                                 cool_state . slots . insert ( key,  StorageSlotState :: WarmWithSLOAD ) ; 
307318                            } 
308319                            Some ( _)  => { } 
309320                        } 
310321                    }  else  { 
311-                         contract_storage . insert ( key,  StorageSlotState :: WarmWithSLOAD ) ; 
322+                         cool_state . slots . insert ( key,  StorageSlotState :: WarmWithSLOAD ) ; 
312323                    } 
313324                } 
314325                opcode:: SSTORE  => { 
@@ -318,7 +329,7 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
318329                    let  account = data. journaled_state . state ( ) . get ( & contract_address) . unwrap ( ) ; 
319330                    if  account. storage . get ( & key) . is_some ( )  { 
320331                        // only add gas the first time the storage is touched again 
321-                         match  contract_storage . get ( & key)  { 
332+                         match  cool_state . slots . get ( & key)  { 
322333                            Some ( StorageSlotState :: WarmWithSLOAD )  => { 
323334                                // cool keeps the slot value changes 
324335                                // as if the previous_or_original_value = present_value` 
@@ -337,7 +348,7 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
337348                                } 
338349
339350                                // set slot is_warm to true 
340-                                 contract_storage . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
351+                                 cool_state . slots . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
341352                            } 
342353                            None  | Some ( StorageSlotState :: Cool )  => { 
343354                                // Means SSTORE was called without SLOAD before 
@@ -359,12 +370,12 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
359370                                        state. additional_gas_next_op  += 2900  - 100 
360371                                    } 
361372                                } 
362-                                 contract_storage . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
373+                                 cool_state . slots . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
363374                            } 
364375                            Some ( StorageSlotState :: WarmWithSSTORE )  => { } 
365376                        } 
366377                    }  else  { 
367-                         contract_storage . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
378+                         cool_state . slots . insert ( key,  StorageSlotState :: WarmWithSSTORE ) ; 
368379                    } 
369380                } 
370381                _ => { } 
0 commit comments