-
Notifications
You must be signed in to change notification settings - Fork 72
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(exception mapper): class to verify exception strings #795
Open
winsvega
wants to merge
2
commits into
main
Choose a base branch
from
exception_mapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,99 @@ | ||
""" | ||
Evmone eof exceptions ENUM -> str mapper | ||
Evmone exceptions ENUM -> str mapper | ||
""" | ||
|
||
from dataclasses import dataclass | ||
from .exception_mapper import ExceptionMapper, ExceptionMessage | ||
from .exceptions import EOFException, TransactionException | ||
|
||
from bidict import frozenbidict | ||
|
||
from .exceptions import EOFException | ||
|
||
|
||
@dataclass | ||
class ExceptionMessage: | ||
"""Defines a mapping between an exception and a message.""" | ||
|
||
exception: EOFException | ||
message: str | ||
|
||
|
||
class EvmoneExceptionMapper: | ||
class EvmoneExceptionMapper(ExceptionMapper): | ||
""" | ||
Translate between EEST exceptions and error strings returned by evmone. | ||
""" | ||
|
||
_mapping_data = ( | ||
# TODO EVMONE needs to differentiate when the section is missing in the header or body | ||
ExceptionMessage(EOFException.MISSING_STOP_OPCODE, "err: no_terminating_instruction"), | ||
ExceptionMessage(EOFException.MISSING_CODE_HEADER, "err: code_section_missing"), | ||
ExceptionMessage(EOFException.MISSING_TYPE_HEADER, "err: type_section_missing"), | ||
# TODO EVMONE these exceptions are too similar, this leeds to ambiguity | ||
ExceptionMessage(EOFException.MISSING_TERMINATOR, "err: header_terminator_missing"), | ||
ExceptionMessage( | ||
EOFException.MISSING_HEADERS_TERMINATOR, "err: section_headers_not_terminated" | ||
), | ||
ExceptionMessage(EOFException.INVALID_VERSION, "err: eof_version_unknown"), | ||
ExceptionMessage( | ||
EOFException.INVALID_NON_RETURNING_FLAG, "err: invalid_non_returning_flag" | ||
), | ||
ExceptionMessage(EOFException.INVALID_MAGIC, "err: invalid_prefix"), | ||
ExceptionMessage( | ||
EOFException.INVALID_FIRST_SECTION_TYPE, "err: invalid_first_section_type" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_SECTION_BODIES_SIZE, "err: invalid_section_bodies_size" | ||
), | ||
ExceptionMessage(EOFException.INVALID_TYPE_SECTION_SIZE, "err: invalid_type_section_size"), | ||
ExceptionMessage(EOFException.INCOMPLETE_SECTION_SIZE, "err: incomplete_section_size"), | ||
ExceptionMessage(EOFException.INCOMPLETE_SECTION_NUMBER, "err: incomplete_section_number"), | ||
ExceptionMessage(EOFException.TOO_MANY_CODE_SECTIONS, "err: too_many_code_sections"), | ||
ExceptionMessage(EOFException.ZERO_SECTION_SIZE, "err: zero_section_size"), | ||
ExceptionMessage(EOFException.MISSING_DATA_SECTION, "err: data_section_missing"), | ||
ExceptionMessage(EOFException.UNDEFINED_INSTRUCTION, "err: undefined_instruction"), | ||
ExceptionMessage( | ||
EOFException.INPUTS_OUTPUTS_NUM_ABOVE_LIMIT, "err: inputs_outputs_num_above_limit" | ||
), | ||
ExceptionMessage(EOFException.UNREACHABLE_INSTRUCTIONS, "err: unreachable_instructions"), | ||
ExceptionMessage(EOFException.INVALID_RJUMP_DESTINATION, "err: invalid_rjump_destination"), | ||
ExceptionMessage(EOFException.UNREACHABLE_CODE_SECTIONS, "err: unreachable_code_sections"), | ||
ExceptionMessage(EOFException.STACK_UNDERFLOW, "err: stack_underflow"), | ||
ExceptionMessage( | ||
EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT, "err: max_stack_height_above_limit" | ||
), | ||
ExceptionMessage( | ||
EOFException.STACK_HIGHER_THAN_OUTPUTS, "err: stack_higher_than_outputs_required" | ||
), | ||
ExceptionMessage( | ||
EOFException.JUMPF_DESTINATION_INCOMPATIBLE_OUTPUTS, | ||
"err: jumpf_destination_incompatible_outputs", | ||
), | ||
ExceptionMessage(EOFException.INVALID_MAX_STACK_HEIGHT, "err: invalid_max_stack_height"), | ||
ExceptionMessage(EOFException.INVALID_DATALOADN_INDEX, "err: invalid_dataloadn_index"), | ||
ExceptionMessage(EOFException.TRUNCATED_INSTRUCTION, "err: truncated_instruction"), | ||
ExceptionMessage( | ||
EOFException.TOPLEVEL_CONTAINER_TRUNCATED, "err: toplevel_container_truncated" | ||
), | ||
ExceptionMessage(EOFException.ORPHAN_SUBCONTAINER, "err: unreferenced_subcontainer"), | ||
ExceptionMessage( | ||
EOFException.CONTAINER_SIZE_ABOVE_LIMIT, "err: container_size_above_limit" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_CONTAINER_SECTION_INDEX, "err: invalid_container_section_index" | ||
), | ||
ExceptionMessage( | ||
EOFException.INCOMPATIBLE_CONTAINER_KIND, "err: incompatible_container_kind" | ||
), | ||
ExceptionMessage(EOFException.STACK_HEIGHT_MISMATCH, "err: stack_height_mismatch"), | ||
ExceptionMessage(EOFException.TOO_MANY_CONTAINERS, "err: too_many_container_sections"), | ||
ExceptionMessage( | ||
EOFException.INVALID_CODE_SECTION_INDEX, "err: invalid_code_section_index" | ||
), | ||
) | ||
|
||
def __init__(self) -> None: | ||
assert len(set(entry.exception for entry in self._mapping_data)) == len( | ||
self._mapping_data | ||
), "Duplicate exception in _mapping_data" | ||
assert len(set(entry.message for entry in self._mapping_data)) == len( | ||
self._mapping_data | ||
), "Duplicate message in _mapping_data" | ||
self.exception_to_message_map: frozenbidict = frozenbidict( | ||
{entry.exception: entry.message for entry in self._mapping_data} | ||
) | ||
|
||
def exception_to_message(self, exception: EOFException) -> str: | ||
"""Takes an EOFException and returns a formatted string.""" | ||
message = self.exception_to_message_map.get( | ||
exception, | ||
f"No message defined for {exception}; please add it to {self.__class__.__name__}", | ||
) | ||
return message | ||
|
||
def message_to_exception(self, exception_string: str) -> EOFException: | ||
"""Takes a string and tries to find matching exception""" | ||
# TODO inform tester where to add the missing exception if get uses default | ||
exception = self.exception_to_message_map.inverse.get( | ||
exception_string, EOFException.UNDEFINED_EXCEPTION | ||
) | ||
return exception | ||
@property | ||
def _mapping_data(self): | ||
return [ | ||
ExceptionMessage( | ||
TransactionException.TYPE_4_TX_CONTRACT_CREATION, | ||
"set code transaction must ", | ||
), | ||
# TODO EVMONE needs to differentiate when the section is missing in the header or body | ||
ExceptionMessage(EOFException.MISSING_STOP_OPCODE, "err: no_terminating_instruction"), | ||
ExceptionMessage(EOFException.MISSING_CODE_HEADER, "err: code_section_missing"), | ||
ExceptionMessage(EOFException.MISSING_TYPE_HEADER, "err: type_section_missing"), | ||
# TODO EVMONE these exceptions are too similar, this leeds to ambiguity | ||
ExceptionMessage(EOFException.MISSING_TERMINATOR, "err: header_terminator_missing"), | ||
ExceptionMessage( | ||
EOFException.MISSING_HEADERS_TERMINATOR, "err: section_headers_not_terminated" | ||
), | ||
ExceptionMessage(EOFException.INVALID_VERSION, "err: eof_version_unknown"), | ||
ExceptionMessage( | ||
EOFException.INVALID_NON_RETURNING_FLAG, "err: invalid_non_returning_flag" | ||
), | ||
ExceptionMessage(EOFException.INVALID_MAGIC, "err: invalid_prefix"), | ||
ExceptionMessage( | ||
EOFException.INVALID_FIRST_SECTION_TYPE, "err: invalid_first_section_type" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_SECTION_BODIES_SIZE, "err: invalid_section_bodies_size" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_TYPE_SECTION_SIZE, "err: invalid_type_section_size" | ||
), | ||
ExceptionMessage(EOFException.INCOMPLETE_SECTION_SIZE, "err: incomplete_section_size"), | ||
ExceptionMessage( | ||
EOFException.INCOMPLETE_SECTION_NUMBER, "err: incomplete_section_number" | ||
), | ||
ExceptionMessage(EOFException.TOO_MANY_CODE_SECTIONS, "err: too_many_code_sections"), | ||
ExceptionMessage(EOFException.ZERO_SECTION_SIZE, "err: zero_section_size"), | ||
ExceptionMessage(EOFException.MISSING_DATA_SECTION, "err: data_section_missing"), | ||
ExceptionMessage(EOFException.UNDEFINED_INSTRUCTION, "err: undefined_instruction"), | ||
ExceptionMessage( | ||
EOFException.INPUTS_OUTPUTS_NUM_ABOVE_LIMIT, "err: inputs_outputs_num_above_limit" | ||
), | ||
ExceptionMessage( | ||
EOFException.UNREACHABLE_INSTRUCTIONS, "err: unreachable_instructions" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_RJUMP_DESTINATION, "err: invalid_rjump_destination" | ||
), | ||
ExceptionMessage( | ||
EOFException.UNREACHABLE_CODE_SECTIONS, "err: unreachable_code_sections" | ||
), | ||
ExceptionMessage(EOFException.STACK_UNDERFLOW, "err: stack_underflow"), | ||
ExceptionMessage( | ||
EOFException.MAX_STACK_HEIGHT_ABOVE_LIMIT, "err: max_stack_height_above_limit" | ||
), | ||
ExceptionMessage( | ||
EOFException.STACK_HIGHER_THAN_OUTPUTS, "err: stack_higher_than_outputs_required" | ||
), | ||
ExceptionMessage( | ||
EOFException.JUMPF_DESTINATION_INCOMPATIBLE_OUTPUTS, | ||
"err: jumpf_destination_incompatible_outputs", | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_MAX_STACK_HEIGHT, "err: invalid_max_stack_height" | ||
), | ||
ExceptionMessage(EOFException.INVALID_DATALOADN_INDEX, "err: invalid_dataloadn_index"), | ||
ExceptionMessage(EOFException.TRUNCATED_INSTRUCTION, "err: truncated_instruction"), | ||
ExceptionMessage( | ||
EOFException.TOPLEVEL_CONTAINER_TRUNCATED, "err: toplevel_container_truncated" | ||
), | ||
ExceptionMessage(EOFException.ORPHAN_SUBCONTAINER, "err: unreferenced_subcontainer"), | ||
ExceptionMessage( | ||
EOFException.CONTAINER_SIZE_ABOVE_LIMIT, "err: container_size_above_limit" | ||
), | ||
ExceptionMessage( | ||
EOFException.INVALID_CONTAINER_SECTION_INDEX, | ||
"err: invalid_container_section_index", | ||
), | ||
ExceptionMessage( | ||
EOFException.INCOMPATIBLE_CONTAINER_KIND, "err: incompatible_container_kind" | ||
), | ||
ExceptionMessage(EOFException.STACK_HEIGHT_MISMATCH, "err: stack_height_mismatch"), | ||
ExceptionMessage(EOFException.TOO_MANY_CONTAINERS, "err: too_many_container_sections"), | ||
ExceptionMessage( | ||
EOFException.INVALID_CODE_SECTION_INDEX, "err: invalid_code_section_index" | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
EEST Exception mapper | ||
""" | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
|
||
from bidict import frozenbidict | ||
|
||
from .exceptions import ExceptionBase, TransactionException, UndefinedException | ||
|
||
|
||
@dataclass | ||
class ExceptionMessage: | ||
"""Defines a mapping between an exception and a message.""" | ||
|
||
exception: ExceptionBase | ||
message: str | ||
|
||
|
||
class ExceptionMapper(ABC): | ||
""" | ||
Translate between EEST exceptions and error strings returned by client's t8n or other tools. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
# Ensure that the subclass has properly defined _mapping_data before accessing it | ||
assert self._mapping_data is not None, "_mapping_data must be defined in subclass" | ||
|
||
assert len(set(entry.exception for entry in self._mapping_data)) == len( | ||
self._mapping_data | ||
), "Duplicate exception in _mapping_data" | ||
assert len(set(entry.message for entry in self._mapping_data)) == len( | ||
self._mapping_data | ||
), "Duplicate message in _mapping_data" | ||
self.exception_to_message_map: frozenbidict = frozenbidict( | ||
{entry.exception: entry.message for entry in self._mapping_data} | ||
) | ||
|
||
@property | ||
@abstractmethod | ||
def _mapping_data(self): | ||
"""This method should be overridden in the subclass to provide mapping data.""" | ||
pass | ||
|
||
def exception_to_message(self, exception: ExceptionBase) -> str: | ||
"""Takes an exception and returns a formatted string.""" | ||
message = self.exception_to_message_map.get(exception, "Undefined") | ||
return message | ||
|
||
def message_to_exception(self, exception_string: str) -> ExceptionBase: | ||
"""Takes a string and tries to find matching exception""" | ||
# TODO inform tester where to add the missing exception if get uses default | ||
exception = self.exception_to_message_map.inverse.get( | ||
exception_string, UndefinedException.UNDEFINED_EXCEPTION | ||
) | ||
return exception | ||
|
||
def check_transaction(self, tx_error_message: str | None, tx, tx_pos: int): | ||
"""Verify transaction exception""" | ||
exception_info = f"TransactionException (pos={tx_pos}, nonce={tx.nonce})\n" | ||
|
||
if tx.error and not tx_error_message: | ||
raise Exception(f"{exception_info} Error: tx expected to fail succeeded") | ||
elif not tx.error and tx_error_message: | ||
raise Exception(f"{exception_info} Error: tx unexpectedly failed: {tx_error_message}") | ||
# TODO check exception list case | ||
elif isinstance(tx.error, TransactionException) and tx_error_message: | ||
expected_error_message = self.exception_to_message(tx.error) | ||
error_exception = self.message_to_exception(tx_error_message) | ||
|
||
define_message_hint = ( | ||
f"No message defined for {tx.error}, please add it to {self.__class__.__name__}" | ||
if expected_error_message == "Undefined" | ||
else "" | ||
) | ||
define_exception_hint = ( | ||
f"No exception defined for error message got, " | ||
f"please add it to {self.__class__.__name__}" | ||
if error_exception == UndefinedException.UNDEFINED_EXCEPTION | ||
else "" | ||
) | ||
|
||
if expected_error_message not in tx_error_message: | ||
raise Exception( | ||
f"{exception_info}" | ||
f"Error: exception mismatch:\n want = '{expected_error_message}' ({tx.error})," | ||
f"\n got = '{tx_error_message}' ({error_exception})" | ||
f"\n {define_message_hint}" | ||
f"\n {define_exception_hint}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Transaction
here would cause a cycle import, and I think that is because this method should not be defined here because not all tests depend on transaction exceptions (onlystate_test
).Probably better to be in
ethereum_test_specs/state.py
.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.
After looking through all the changes, I think what we could do is have a subclass called
TransitionToolExceptionMapper
that is specifically dedicated to map exceptions from the transition tool, and this can be used to subclass specificGethTransaitionToolExceptionMapper
and so on.Another approach could be that we don't need the subclass
GethTransaitionToolExceptionMapper
and onlyTransitionToolExceptionMapper
, and then insrc/evm_transition_tool/geth.py
we create an instance ofTransitionToolExceptionMapper
that contains the map, so we avoid creating too many classes and also avoid instantiating the sameGethTransaitionToolExceptionMapper
every time we instantiate the t8n tool.