Skip to content

Commit

Permalink
refactor exception mapper
Browse files Browse the repository at this point in the history
parse expected exception lists
  • Loading branch information
winsvega committed Oct 7, 2024
1 parent e5b5571 commit db0b38b
Show file tree
Hide file tree
Showing 14 changed files with 550 additions and 342 deletions.
9 changes: 4 additions & 5 deletions src/ethereum_test_exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"""

from .engine_api import EngineAPIError
from .evmone_exceptions import EvmoneExceptionMapper
from .exception_mapper import ExceptionMapper
from .exception_mapper import ExceptionMapper, ExceptionMessage
from .exceptions import (
BlockException,
BlockExceptionInstanceOrList,
Expand All @@ -13,8 +12,8 @@
ExceptionInstanceOrList,
TransactionException,
TransactionExceptionInstanceOrList,
UndefinedException,
)
from .geth_exceptions import GethExceptionMapper

__all__ = [
"BlockException",
Expand All @@ -23,9 +22,9 @@
"EOFExceptionInstanceOrList",
"EngineAPIError",
"ExceptionMapper",
"EvmoneExceptionMapper",
"GethExceptionMapper",
"ExceptionMessage",
"ExceptionInstanceOrList",
"TransactionException",
"UndefinedException",
"TransactionExceptionInstanceOrList",
]
99 changes: 0 additions & 99 deletions src/ethereum_test_exceptions/evmone_exceptions.py

This file was deleted.

37 changes: 2 additions & 35 deletions src/ethereum_test_exceptions/exception_mapper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""
EEST Exception mapper
"""

from abc import ABC, abstractmethod
from dataclasses import dataclass

from bidict import frozenbidict

from .exceptions import ExceptionBase, TransactionException, UndefinedException
from .exceptions import ExceptionBase, UndefinedException


@dataclass
Expand Down Expand Up @@ -54,37 +55,3 @@ def message_to_exception(self, exception_string: str) -> ExceptionBase:
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}"
)
6 changes: 5 additions & 1 deletion src/ethereum_test_exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ class TransactionException(ExceptionBase):
"""
TYPE_4_INVALID_AUTHORITY_SIGNATURE = auto()
"""
Transaction is type 4, but has an empty authorization list.
Transaction authority signature is invalid
"""
TYPE_4_INVALID_AUTHORITY_SIGNATURE_S_TOO_HIGH = auto()
"""
Transaction authority signature is invalid
"""
TYPE_4_TX_CONTRACT_CREATION = auto()
"""
Expand Down
140 changes: 0 additions & 140 deletions src/ethereum_test_exceptions/geth_exceptions.py

This file was deleted.

23 changes: 2 additions & 21 deletions src/ethereum_test_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
from itertools import count
from os import path
from pathlib import Path
from typing import Callable, ClassVar, Dict, Generator, Iterator, List, Optional
from typing import Callable, ClassVar, Generator, Iterator, List, Optional

import pytest
from pydantic import BaseModel, Field

from ethereum_test_base_types import to_hex
from ethereum_test_exceptions import ExceptionMapper, TransactionException
from ethereum_test_fixtures import BaseFixture, FixtureFormat
from ethereum_test_forks import Fork
from ethereum_test_types import Environment, Transaction, Withdrawal
from ethereum_test_types import Environment, Withdrawal
from evm_transition_tool import Result, TransitionTool


Expand All @@ -33,24 +32,6 @@ def __str__(self): # noqa: D105
return f"{self.message}: Expected {self.expected_hash}, got {self.actual_hash}"


def verify_transactions(
exception_mapper: ExceptionMapper, txs: List[Transaction], result: Result
) -> List[int]:
"""
Verify rejected transactions (if any) against the expected outcome.
Raises exception on unexpected rejections or unexpected successful txs.
"""
rejected_txs: Dict[int, str] = {
rejected_tx.index: rejected_tx.error for rejected_tx in result.rejected_transactions
}

for i, tx in enumerate(txs):
error_message = rejected_txs[i] if i in rejected_txs else None
exception_mapper.check_transaction(error_message, tx, i)

return list(rejected_txs.keys())


def verify_result(result: Result, env: Environment):
"""
Verify that values in the t8n result match the expected values.
Expand Down
Loading

0 comments on commit db0b38b

Please sign in to comment.