Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason committed Jul 24, 2023
1 parent 9e07be9 commit ab36616
Showing 1 changed file with 107 additions and 59 deletions.
166 changes: 107 additions & 59 deletions core/executor/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ macro_rules! trace_type {
self.0.len()
}

#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn into_raw(self) -> Vec<[u8; 32]> {
self.0
}
Expand All @@ -48,15 +43,6 @@ macro_rules! trace_type {
#[derive(Default, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct $name(pub std::collections::BTreeMap<$key, $val>);

impl IntoIterator for $name {
type Item = ($key, $val);
type IntoIter = std::collections::btree_map::IntoIter<$key, $val>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl $name {
pub fn insert(&mut self, key: $key, value: $val) {
self.0.insert(key, value);
Expand Down Expand Up @@ -414,8 +400,18 @@ impl TryFrom<TransactionTrace> for Web3TransactionTrace {

if log_len > 1 {
let logs = &value.logs().0;
let (mut index, current_depth) = (0usize, logs[0].depth);
ret.calls = Some(build_subcalls(logs, &mut index, current_depth));

if !logs.windows(2).all(|pair| {
let (prev, next) = (&pair[0], &pair[1]);

prev.depth == next.depth
|| prev.depth + 1 == next.depth
|| prev.depth == next.depth + 1
}) {
return Err("Log depth variation is not smooth".to_string());
}

ret.calls = Some(build_subcalls(logs, &mut 0, logs[0].depth));
}

Ok(ret)
Expand Down Expand Up @@ -450,11 +446,8 @@ fn build_subcalls(

*index += 1;
} else if t_log.depth == (current_depth + 1) {
let sub_calls = build_subcalls(traces, index, current_depth + 1);

println!("\n{:?}\n", sub_calls);

stack.last_mut().unwrap().calls = Some(sub_calls);
stack.last_mut().unwrap().calls =
Some(build_subcalls(traces, index, current_depth + 1));
} else {
return stack;
}
Expand Down Expand Up @@ -491,31 +484,6 @@ impl TransactionTrace {
}
}

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct StepTransactionTrace {
inner: TransactionTrace,
step: usize,
}

#[allow(dead_code)]
impl StepTransactionTrace {
pub fn new(transaction_trace: TransactionTrace) -> Self {
Self {
inner: transaction_trace,
step: 0,
}
}

pub fn step(&mut self) -> Option<&TraceLog> {
if self.step > self.inner.struct_logs.len() {
None
} else {
self.step += 1;
Some(&self.inner.struct_logs[self.step])
}
}
}

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Logs(pub Vec<TraceLog>);

Expand Down Expand Up @@ -570,24 +538,12 @@ mod wrapped_opcode {
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub struct Opcode(u8);

impl PartialEq<evm::Opcode> for Opcode {
fn eq(&self, other: &evm::Opcode) -> bool {
self.0 == other.as_u8()
}
}

impl From<evm::Opcode> for Opcode {
fn from(value: evm::Opcode) -> Self {
Opcode::new(value.as_u8())
}
}

impl From<revm_interpreter::opcode::OpCode> for Opcode {
fn from(value: revm_interpreter::opcode::OpCode) -> Self {
Opcode::new(value.u8())
}
}

impl Display for Opcode {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Display::fmt(
Expand Down Expand Up @@ -749,6 +705,98 @@ mod tests {
let mut i = 0usize;

let res = build_subcalls(&logs, &mut i, 1);
println!("{:?}", serde_json::to_string(&res).unwrap());

let data = r#"[
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x1",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x"
},
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x1",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x",
"calls": [
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x2",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x",
"calls": [
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x3",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x"
}
]
},
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x2",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x"
}
]
},
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x1",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x",
"calls": [
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x2",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x"
}
]
},
{
"type_": "",
"from": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x1",
"gas": "0x0",
"gas_used": "0x0",
"input": "0x",
"output": "0x"
}]"#;

assert_eq!(
res,
serde_json::from_str::<Vec<Web3TransactionTrace>>(data).unwrap()
);
}
}

0 comments on commit ab36616

Please sign in to comment.