Skip to content

Commit

Permalink
fix: missing trace frames during creates (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 31, 2023
1 parent 47463fe commit e3f2cac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
32 changes: 20 additions & 12 deletions evm_trace/geth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import math
from itertools import tee
from typing import Dict, Iterator, List, Optional

from eth_utils import to_int
Expand Down Expand Up @@ -90,28 +89,37 @@ def create_trace_frames(data: Iterator[Dict]) -> Iterator[TraceFrame]:
frames = iter(data)

for frame in frames:
frame_obj = TraceFrame(**frame)

if CallType.CREATE.value in frame.get("op", ""):
# Before yielding, find the address of the CREATE.
frames, frames_copy = tee(frames)
# Look ahead to find the address.

create_frames = [frame_obj]
start_depth = frame.get("depth", 0)
for next_frame in frames_copy:
depth = next_frame.get("depth", 0)
if depth == start_depth:
for next_frame in frames:
next_frame_obj = TraceFrame.parse_obj(next_frame)
depth = next_frame_obj.depth

if depth <= start_depth:
# Extract the address for the original CREATE using
# the first frame after the CREATE with an equal depth.
stack = next_frame.get("stack", [])
if len(stack) > 0:
raw_addr = HexBytes(stack[-1][-40:])
if len(next_frame_obj.stack) > 0:
raw_addr = HexBytes(next_frame_obj.stack[-1][-40:])
try:
frame["contract_address"] = HexBytes(to_address(raw_addr))
frame_obj.contract_address = HexBytes(to_address(raw_addr))
except Exception:
# Potentially, a transaction was made with poor data.
frame["contract_address"] = raw_addr
frame_obj.contract_address = raw_addr

create_frames.append(next_frame_obj)
yield from create_frames
break

yield TraceFrame(**frame)
elif depth > start_depth:
create_frames.append(next_frame_obj)

else:
yield TraceFrame(**frame)


def get_calltree_from_geth_call_trace(data: Dict) -> CallTreeNode:
Expand Down
1 change: 1 addition & 0 deletions tests/test_geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def test_create_trace_frames_from_geth_create2_struct_logs(
geth_create2_struct_logs, geth_create2_trace_frames
):
frames = list(create_trace_frames(geth_create2_struct_logs))
assert len(frames) == len(geth_create2_trace_frames)
assert frames != geth_create2_trace_frames

assert "CREATE2" in [f.op for f in frames]
Expand Down

0 comments on commit e3f2cac

Please sign in to comment.