Skip to content

Commit

Permalink
feat: Add meaningful description to the relevant traces
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Mar 26, 2024
1 parent ea68703 commit 2c712f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
44 changes: 15 additions & 29 deletions src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,17 @@ class MissingKey(TraceableException):

@property
def markers(self) -> Generator[TraceMarkerDescriptor, None, None]:
"""
Mark the traces that are relevant to the exception.
"""
# Record an SSTORE event in the expected address and key
"""Mark the traces that are relevant to the exception."""
yield TraceMarkerDescriptor(
trace_type=EVMTraceLine,
description=f"SSTORE event on address {self.address} and key {hex(self.key)}",
context_address=self.address,
op_name="SSTORE",
stack=[self.key], # top element of the stack must be the key that caused the error
)
# Record an exit frame from the expected address with an error
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameExit,
description=f"Exit frame from address {self.address} with error",
from_address=self.address,
error=lambda e: e is not None,
)
Expand All @@ -182,20 +180,17 @@ class KeyValueMismatch(TraceableException):

@property
def markers(self) -> Generator[TraceMarkerDescriptor, None, None]:
"""
Mark the traces that are relevant to the exception.
"""
# Record an SSTORE event in the expected address and key
"""Mark the traces that are relevant to the exception."""
yield TraceMarkerDescriptor(
trace_type=EVMTraceLine,
description=f"SSTORE event on address {self.address} and key {hex(self.key)}",
context_address=self.address,
op_name="SSTORE",
stack=[self.key], # top element of the stack must be the key that caused the error
)

# Record an exit frame from the expected address with an error
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameExit,
description=f"Exit frame from address {self.address} with error",
from_address=self.address,
error=lambda e: e is not None,
)
Expand Down Expand Up @@ -427,19 +422,16 @@ class NonceMismatch(TraceableException):

@property
def markers(self) -> Generator[TraceMarkerDescriptor, None, None]:
"""
Mark the traces that are relevant to the exception.
"""
# Record CREATE events in the expected address
"""Mark the traces that are relevant to the exception."""
yield TraceMarkerDescriptor(
trace_type=EVMTraceLine,
description=f"CREATE event from address {self.address}",
context_address=self.address,
op_name="CREATE",
)

# Record an exit frame from the expected address with an error
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameExit,
description=f"Exit frame from address {self.address} with error",
from_address=self.address,
error=lambda e: e is not None,
)
Expand All @@ -464,12 +456,10 @@ class BalanceMismatch(TraceableException):

@property
def markers(self) -> Generator[TraceMarkerDescriptor, None, None]:
"""
Mark the traces that are relevant to the exception.
"""
# Record an enter frame to the expected address
"""Mark the traces that are relevant to the exception."""
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameEnter,
description=f"Enter frame to address {self.address}",
to_address=self.address,
)

Expand All @@ -493,26 +483,22 @@ class CodeMismatch(TraceableException):

@property
def markers(self) -> Generator[TraceMarkerDescriptor, None, None]:
"""
Mark the traces that are relevant to the exception.
"""
# Mark creation of the expected address
"""Mark the traces that are relevant to the exception."""
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameEnter,
description=f"CREATE/CREATE2 event to address {self.address}",
to_address=self.address,
op_name=lambda op: op in ["CREATE", "CREATE2"],
)

# Mark selfdestruct of the expected address
yield TraceMarkerDescriptor(
trace_type=EVMTraceLine,
description=f"SELFDESTRUCT event from address {self.address}",
context_address=self.address,
op_name="SELFDESTRUCT",
)

# Record an exit frame from the expected address with an error
yield TraceMarkerDescriptor(
trace_type=EVMCallFrameExit,
description=f"Exit frame from address {self.address} with error",
from_address=self.address,
error=lambda e: e is not None,
)
Expand Down
17 changes: 11 additions & 6 deletions src/evm_transition_tool/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

class TraceModel(BaseModel):
"""
Base class for models that use camel case.
Base class all trace line types.
"""

_marked: bool = False
_mark_info: str = ""

model_config = ConfigDict(alias_generator=to_camel)

Expand All @@ -45,7 +45,7 @@ def match_stack(self, other: List[HexNumber | None]) -> bool:

return True

def mark(self, **kwargs):
def mark(self, description: str, **kwargs):
"""
Check if the trace call frame enter matches the given values.
"""
Expand All @@ -58,7 +58,7 @@ def mark(self, **kwargs):
return
elif value != getattr(self, key):
return
self._marked = True
self._mark_info = description


class EVMCallFrameEnter(TraceModel):
Expand Down Expand Up @@ -166,15 +166,18 @@ class TraceMarkerDescriptor:
"""

trace_type: Type[TraceModel]
description: str
kwargs: Dict[str, Any] = {}

def __init__(
self,
*,
trace_type: Type[TraceModel],
description: str,
**kwargs,
):
self.trace_type = trace_type
self.description = description
self.kwargs = kwargs

def mark_traces(self, traces: List[List[EVMTransactionTrace]] | None):
Expand All @@ -188,7 +191,7 @@ def mark_traces(self, traces: List[List[EVMTransactionTrace]] | None):
for tx_trace in execution_trace:
for trace in tx_trace.trace:
if isinstance(trace, self.trace_type):
trace.mark(**self.kwargs)
trace.mark(description=self.description, **self.kwargs)


class RelevantTraceContext(BaseModel):
Expand All @@ -200,6 +203,7 @@ class RelevantTraceContext(BaseModel):
transaction_index: int
transaction_hash: str
traces: List[EVMCallFrameEnter | EVMTraceLine | EVMCallFrameExit]
description: str


class TraceableException(Exception, ABC):
Expand Down Expand Up @@ -232,12 +236,13 @@ def get_relevant_traces(
for execution_index, execution_trace in enumerate(traces):
for tx_index, tx_trace in enumerate(execution_trace):
for line_number, trace in enumerate(tx_trace.trace):
if trace._marked:
if trace._mark_info:
yield RelevantTraceContext(
execution_index=execution_index,
transaction_index=tx_index,
transaction_hash=tx_trace.transaction_hash,
traces=tx_trace.get_trace_line_with_context(
line_number=line_number, previous_lines=context_previous_lines
),
description=trace._mark_info,
)

0 comments on commit 2c712f2

Please sign in to comment.