From 2c712f23133287d2bc24cf0d789be9645ee31f2a Mon Sep 17 00:00:00 2001 From: Mario Vega Date: Tue, 26 Mar 2024 21:28:22 +0000 Subject: [PATCH] feat: Add meaningful description to the relevant traces --- src/ethereum_test_tools/common/types.py | 44 +++++++++---------------- src/evm_transition_tool/traces.py | 17 ++++++---- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/ethereum_test_tools/common/types.py b/src/ethereum_test_tools/common/types.py index f18dfac714..5a61cf67da 100644 --- a/src/ethereum_test_tools/common/types.py +++ b/src/ethereum_test_tools/common/types.py @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) diff --git a/src/evm_transition_tool/traces.py b/src/evm_transition_tool/traces.py index 83216d17bf..dd9e217c5b 100644 --- a/src/evm_transition_tool/traces.py +++ b/src/evm_transition_tool/traces.py @@ -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) @@ -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. """ @@ -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): @@ -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): @@ -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): @@ -200,6 +203,7 @@ class RelevantTraceContext(BaseModel): transaction_index: int transaction_hash: str traces: List[EVMCallFrameEnter | EVMTraceLine | EVMCallFrameExit] + description: str class TraceableException(Exception, ABC): @@ -232,7 +236,7 @@ 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, @@ -240,4 +244,5 @@ def get_relevant_traces( traces=tx_trace.get_trace_line_with_context( line_number=line_number, previous_lines=context_previous_lines ), + description=trace._mark_info, )