Skip to content

Commit ee35f1b

Browse files
committed
refactor to use a tuple
1 parent 5b9728e commit ee35f1b

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

crates/cheatcodes/src/evm.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ impl Cheatcode for coolCall {
310310
let Self { target } = *self;
311311
ensure_not_precompile!(&target, ccx);
312312
// TODO: prevent or warn about cooling the to/from address in a tx
313-
ccx.state
314-
.addresses
315-
.insert(target, CoolState { address: AddressState::Cool, slots: HashMap::new() });
313+
ccx.state.addresses.insert(target, (AddressState::Cool, HashMap::new()));
316314
Ok(Default::default())
317315
}
318316
}

crates/cheatcodes/src/inspector.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,12 @@ pub struct Cheatcodes {
202202
pub breakpoints: Breakpoints,
203203

204204
/// Track if cool cheatcode was called on each address
205-
pub addresses: HashMap<Address, CoolState>,
205+
/// Mapping tracks if the address itself is cool and if each of it's slots are cool
206+
pub addresses: HashMap<Address, (AddressState, HashMap<U256, StorageSlotState>)>,
206207
/// How much gas to charge in the next step (op code) based on cool cheatcode calculations
207208
pub additional_gas_next_op: u64,
208209
}
209210

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-
219211
/// Whether an Address is accessed or not
220212
#[derive(Clone, PartialEq, Debug)]
221213
pub enum AddressState {
@@ -275,10 +267,10 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
275267
// COLD_ACCOUNT_ACCESS_COST is 2600
276268
// check this is done once per address, unless cheatcode is called again
277269
// ignore if same as contract address
278-
if let Some(cool_state) = state.addresses.get_mut(&addr) {
279-
if cool_state.address == AddressState::Cool {
270+
if let Some((ref mut address, _)) = state.addresses.get_mut(&addr) {
271+
if *address == AddressState::Cool {
280272
state.additional_gas_next_op = 2500;
281-
cool_state.address = AddressState::Warm;
273+
*address = AddressState::Warm;
282274
}
283275
}
284276
}
@@ -292,34 +284,34 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
292284
// COLD_ACCOUNT_ACCESS_COST is 2600
293285
// check this is done once per address, unless cheatcode is called again
294286
// ignore if same as contract address
295-
if let Some(cool_state) = state.addresses.get_mut(&addr) {
296-
if cool_state.address == AddressState::Cool {
287+
if let Some((ref mut address, _)) = state.addresses.get_mut(&addr) {
288+
if *address == AddressState::Cool {
297289
state.additional_gas_next_op = 2500;
298-
cool_state.address = AddressState::Warm;
290+
*address = AddressState::Warm;
299291
}
300292
}
301293
}
302294
}
303295
_ => {}
304296
}
305297
// check target's slots
306-
if let Some(cool_state) = state.addresses.get_mut(&contract_address) {
298+
if let Some((_, slots)) = state.addresses.get_mut(&contract_address) {
307299
match interpreter.current_opcode() {
308300
opcode::SLOAD => {
309301
let key = try_or_continue!(interpreter.stack().peek(0));
310302

311303
let account = data.journaled_state.state().get(&contract_address).unwrap();
312304
if account.storage.get(&key).is_some() {
313-
match cool_state.slots.get(&key) {
305+
match slots.get(&key) {
314306
None | Some(StorageSlotState::Cool) => {
315307
// COLD_SLOAD_COST is 2100
316308
state.additional_gas_next_op = 2000;
317-
cool_state.slots.insert(key, StorageSlotState::WarmWithSLOAD);
309+
slots.insert(key, StorageSlotState::WarmWithSLOAD);
318310
}
319311
Some(_) => {}
320312
}
321313
} else {
322-
cool_state.slots.insert(key, StorageSlotState::WarmWithSLOAD);
314+
slots.insert(key, StorageSlotState::WarmWithSLOAD);
323315
}
324316
}
325317
opcode::SSTORE => {
@@ -329,7 +321,7 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
329321
let account = data.journaled_state.state().get(&contract_address).unwrap();
330322
if account.storage.get(&key).is_some() {
331323
// only add gas the first time the storage is touched again
332-
match cool_state.slots.get(&key) {
324+
match slots.get(&key) {
333325
Some(StorageSlotState::WarmWithSLOAD) => {
334326
// cool keeps the slot value changes
335327
// as if the previous_or_original_value = present_value`
@@ -348,7 +340,7 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
348340
}
349341

350342
// set slot is_warm to true
351-
cool_state.slots.insert(key, StorageSlotState::WarmWithSSTORE);
343+
slots.insert(key, StorageSlotState::WarmWithSSTORE);
352344
}
353345
None | Some(StorageSlotState::Cool) => {
354346
// Means SSTORE was called without SLOAD before
@@ -370,12 +362,12 @@ fn add_gas_from_cool_cheatcode<DB: DatabaseExt>(
370362
state.additional_gas_next_op += 2900 - 100
371363
}
372364
}
373-
cool_state.slots.insert(key, StorageSlotState::WarmWithSSTORE);
365+
slots.insert(key, StorageSlotState::WarmWithSSTORE);
374366
}
375367
Some(StorageSlotState::WarmWithSSTORE) => {}
376368
}
377369
} else {
378-
cool_state.slots.insert(key, StorageSlotState::WarmWithSSTORE);
370+
slots.insert(key, StorageSlotState::WarmWithSSTORE);
379371
}
380372
}
381373
_ => {}

0 commit comments

Comments
 (0)