-
Notifications
You must be signed in to change notification settings - Fork 545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: TracerEip3155 optionally traces memory #1234
Changes from 1 commit
138ad5b
1279c02
24a90c8
8a9194f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ pub struct TracerEip3155 { | |
refunded: i64, | ||
mem_size: usize, | ||
skip: bool, | ||
include_memory: bool, | ||
memory: Option<Vec<u8>>, | ||
} | ||
|
||
// # Output | ||
|
@@ -58,7 +60,7 @@ struct Output { | |
error: Option<String>, | ||
/// Array of all allocated values | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
memory: Option<Vec<String>>, | ||
memory: Option<String>, | ||
rakita marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Array of all stored values | ||
#[serde(default, skip_serializing_if = "Option::is_none")] | ||
storage: Option<HashMap<String, String>>, | ||
|
@@ -98,12 +100,14 @@ impl TracerEip3155 { | |
} | ||
|
||
impl TracerEip3155 { | ||
pub fn new(output: Box<dyn Write>, print_summary: bool) -> Self { | ||
pub fn new(output: Box<dyn Write>, print_summary: bool, include_memory: bool) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would not add include_memor flag here but a new function This would allow setting Would be good to add same for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, I've implemented it for both now. For the memory I've defaulted to false, as it's not mandatory and often not wanted due to performance issues. |
||
Self { | ||
output, | ||
gas_inspector: GasInspector::default(), | ||
print_summary, | ||
include_memory, | ||
stack: Default::default(), | ||
memory: Default::default(), | ||
pc: 0, | ||
opcode: 0, | ||
gas: 0, | ||
|
@@ -128,6 +132,11 @@ impl<DB: Database> Inspector<DB> for TracerEip3155 { | |
fn step(&mut self, interp: &mut Interpreter, context: &mut EvmContext<DB>) { | ||
self.gas_inspector.step(interp, context); | ||
self.stack = interp.stack.data().clone(); | ||
self.memory = if self.include_memory { | ||
Some(interp.shared_memory.context_memory().to_owned()) | ||
} else { | ||
None | ||
}; | ||
self.pc = interp.program_counter(); | ||
self.opcode = interp.current_opcode(); | ||
self.mem_size = interp.shared_memory.len(); | ||
|
@@ -159,7 +168,13 @@ impl<DB: Database> Inspector<DB> for TracerEip3155 { | |
} else { | ||
None | ||
}, | ||
memory: None, | ||
memory: match &self.memory { | ||
Some(memory) => { | ||
let hex_memory = hex::encode(memory); | ||
Some(format!("0x{hex_memory}")) | ||
} | ||
None => None, | ||
}, | ||
storage: None, | ||
return_stack: None, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Onee more thing, it would be good to have
String
here so we don't copy memory multiple times.Now we are doing clone in
step
and clone into String instep_end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it to String now, however my change did not improve the performance. I guess the
self.memory.take()
method also copies it internally. Without the.take()
I've got the error "cannot move out ofself.memory
which is behind a mutable reference" and didn't manage to fix it in a better way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
take()
does not copy full string, it is the right approach. Copying memory on every step is a heavy operation, not a lot we could do there. There are few optimisation that we could do but they require introspection of opcodes that change memory, definitely out of scope for this PR.