Skip to content

Commit 097783e

Browse files
committed
add AccessSlot records in trace_call
1 parent 558362b commit 097783e

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ Desktop.ini
2929
.env.local
3030
TxInspector.md
3131

32+
examples/proxy_attack.rs
33+
3234
# Backup files
3335
*~

src/evm/processor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::collections::HashMap;
99
use crate::{
1010
evm::TraceEvm,
1111
traits::{ResetDB, TraceOutput, TransactionTrace, StorageDiff},
12-
types::{SimulationBatch, SimulationTx, SlotChange},
12+
types::{SimulationBatch, SimulationTx, SlotAccess},
1313
};
1414

1515
use crate::errors::{EvmError, RuntimeError};
@@ -87,11 +87,12 @@ where
8787
for (slot, value) in account.storage.iter() {
8888
if value.original_value != value.present_value {
8989
// Store slot changes for diff output
90-
diffs.entry(*address).or_insert_with(Vec::new).push(SlotChange {
90+
diffs.entry(*address).or_insert_with(Vec::new).push(SlotAccess {
9191
address: *address,
9292
slot: *slot,
9393
old_value: value.original_value,
9494
new_value: value.present_value,
95+
is_write: true,
9596
});
9697
}
9798
}

src/inspectors/tx_inspector/inspector.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ where
9494
error_origin: false,
9595
subtraces: Vec::new(),
9696
trace_address,
97-
slot_changes: Vec::new(), // Initialize empty slot changes
97+
slot_accesses: Vec::new(), // Initialize empty slot accesses
9898
};
9999

100100
self.call_traces.push(trace);
@@ -156,7 +156,7 @@ where
156156
error_origin: false,
157157
subtraces: Vec::new(),
158158
trace_address,
159-
slot_changes: Vec::new(), // Initialize empty slot changes
159+
slot_accesses: Vec::new(), // Initialize empty slot accesses
160160
};
161161

162162
self.call_traces.push(trace);
@@ -288,17 +288,39 @@ where
288288
// Store the slot change in the current call trace
289289
let index = self.call_stack.last().unwrap();
290290
let call_trace = &mut self.call_traces[*index];
291-
call_trace.slot_changes.push(SlotChange {
291+
call_trace.slot_accesses.push(SlotAccess {
292292
address: target,
293293
slot,
294294
old_value: old,
295295
new_value: value,
296+
is_write: true, // This is a write operation
296297
});
297298
// Update the slot cache
298299
self.slot_cache.insert((target, slot), value);
299300
}
300301
_ => {}
301302
}
303+
} else if opcode == 0x54 && self.call_stack.last().is_some() {
304+
let slot = interp.stack.pop();
305+
if let Some(slot) = slot {
306+
let _ = interp.stack.push(slot);
307+
let target = interp.input.target_address();
308+
let cached = self.slot_cache.get(&(target, slot));
309+
let value = if let Some(old) = cached {
310+
*old
311+
} else {
312+
context.db().storage(target, slot).unwrap_or_default()
313+
};
314+
let index = self.call_stack.last().unwrap();
315+
let call_trace = &mut self.call_traces[*index];
316+
call_trace.slot_accesses.push(SlotAccess {
317+
address: target,
318+
slot,
319+
old_value: value,
320+
new_value: value,
321+
is_write: false, // This is a read operation
322+
});
323+
}
302324
}
303325
}
304326
}

src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::errors::EvmError;
2-
use crate::types::{SimulationBatch, SlotChange};
2+
use crate::types::{SimulationBatch, SlotAccess};
33
use revm::context_interface::result::ExecutionResult;
44
use revm::inspector::{Inspector, NoOpInspector};
55
use std::collections::HashMap;
@@ -220,7 +220,7 @@ impl TraceOutput for () {
220220
// No output for unit type
221221
}
222222
}
223-
pub type StorageDiff = HashMap<Address, Vec<SlotChange>>;
223+
pub type StorageDiff = HashMap<Address, Vec<SlotAccess>>;
224224
pub type TraceResult<T> = Result<(ExecutionResult, StorageDiff, T), EvmError>;
225225

226226
/// Defines standard transaction processing capabilities

src/types.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use revm::{
1212
database::AlloyDB,
1313
interpreter::{CallScheme, CreateScheme},
1414
};
15-
use serde::Serialize;
15+
use serde::{Serialize, Deserialize};
1616

1717
pub const ERC20_TRANSFER_EVENT_SIGNATURE: FixedBytes<32> =
1818
fixed_bytes!("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");
@@ -187,14 +187,16 @@ impl CallStatus {
187187
}
188188

189189
/// Storage slot change during a contract call
190-
#[derive(Debug, Clone, Serialize, Default)]
191-
pub struct SlotChange {
190+
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
191+
pub struct SlotAccess {
192192
pub address: Address,
193193
pub slot: U256,
194194
pub old_value: U256,
195195
pub new_value: U256,
196+
pub is_write: bool, // true=write, false=read
196197
}
197198

199+
198200
/// Detailed trace of a contract call
199201
#[derive(Debug, Clone, Serialize, Default)]
200202
pub struct CallTrace {
@@ -222,8 +224,8 @@ pub struct CallTrace {
222224
pub subtraces: Vec<CallTrace>,
223225
/// Position in the call tree
224226
pub trace_address: Vec<usize>,
225-
/// Changes to contract storage slots during this call
226-
pub slot_changes: Vec<SlotChange>,
227+
/// Access to contract storage slots during this call
228+
pub slot_accesses: Vec<SlotAccess>,
227229
}
228230

229231
impl TokenTransfer {

0 commit comments

Comments
 (0)