From f7084a22418b34015167aa0b03444cb2206aec49 Mon Sep 17 00:00:00 2001 From: pdobacz <5735525+pdobacz@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:52:36 +0200 Subject: [PATCH] Revert support for unknown opcodes in evm_bytes.py --- src/cli/eofwrap.py | 29 ++++++++++++++++++++--------- src/cli/evm_bytes.py | 14 ++++---------- src/cli/tests/test_eofwrap.py | 3 --- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/cli/eofwrap.py b/src/cli/eofwrap.py index 6b5625f085..da641db61c 100644 --- a/src/cli/eofwrap.py +++ b/src/cli/eofwrap.py @@ -36,8 +36,6 @@ from ethereum_test_types.types import Environment from ethereum_test_vm.bytecode import Bytecode -GENERATION_ERROR_MESSAGE_THRESHOLD = 30 - @click.command() @click.argument("input", type=click.Path(exists=True, dir_okay=True, file_okay=True)) @@ -180,7 +178,16 @@ def wrap_file(self, in_path: str, out_path: str, traces: bool): for address, account in fixture.pre.root.items(): if account is None or account.code is None or len(account.code) == 0: continue - wrapped = wrap_code(account.code) + + try: + wrapped = wrap_code(account.code) + except ValueError as e: + self.metrics[self.ACCOUNTS_INVALID_EOF] += 1 + _inc_counter( + self.metrics[self.VALIDATION_ERRORS], self._short_exception_msg(e) + ) + continue + if self._validate_eof(wrapped): account.code = Bytes(wrapped) wrapped_at_least_one_account = True @@ -203,11 +210,7 @@ def wrap_file(self, in_path: str, out_path: str, traces: bool): self.unique_eof.update(fixture_eof_codes) self.metrics[self.UNIQUE_ACCOUNTS_WRAPPED] = len(self.unique_eof) except Exception as e: - short = str(e) - if len(short) > GENERATION_ERROR_MESSAGE_THRESHOLD: - short = short[:GENERATION_ERROR_MESSAGE_THRESHOLD] + "..." - - _inc_counter(self.metrics[self.GENERATION_ERRORS], short) + _inc_counter(self.metrics[self.GENERATION_ERRORS], self._short_exception_msg(e)) self.metrics[self.FIXTURES_CANT_GENERATE] += 1 self.metrics[self.ACCOUNTS_CANT_GENERATE] += len(fixture_eof_codes) @@ -220,6 +223,14 @@ def wrap_file(self, in_path: str, out_path: str, traces: bool): out_fixtures.collect_into_file(Path(out_path)) self.metrics[self.FILES_GENERATED] += 1 + def _short_exception_msg(self, e: Exception): + THRESHOLD = 30 + + short = str(e) + if len(short) > THRESHOLD: + short = short[:THRESHOLD] + "..." + return short + def _wrap_fixture(self, fixture: BlockchainFixture, traces: bool): env = Environment() @@ -312,7 +323,7 @@ def wrap_code(account_code: Bytes) -> Container: """ assert len(account_code) > 0 - opcodes = process_evm_bytes(account_code, allow_unknown=True) + opcodes = process_evm_bytes(account_code) if not opcodes[-1].terminating: opcodes.append(OpcodeWithOperands(opcode=Op.STOP)) diff --git a/src/cli/evm_bytes.py b/src/cli/evm_bytes.py index b3d8e5bd80..e53feca376 100644 --- a/src/cli/evm_bytes.py +++ b/src/cli/evm_bytes.py @@ -11,7 +11,6 @@ from ethereum_test_vm import Macro from ethereum_test_vm import Opcodes as Op from ethereum_test_vm.bytecode import Bytecode -from ethereum_test_vm.opcode import Opcode OPCODES_WITH_EMPTY_LINES_AFTER = { Op.STOP, @@ -30,7 +29,7 @@ class OpcodeWithOperands: """Simple opcode with its operands.""" - opcode: Opcode | None + opcode: Op | None operands: List[int] = field(default_factory=list) def format(self, assembly: bool) -> str: @@ -73,9 +72,7 @@ def bytecode(self) -> Bytecode: return Bytecode() -def process_evm_bytes( # noqa: D103 - evm_bytes: bytes, allow_unknown: bool = False -) -> List[OpcodeWithOperands]: +def process_evm_bytes(evm_bytes: bytes) -> List[OpcodeWithOperands]: # noqa: D103 evm_bytes = bytearray(evm_bytes) opcodes: List[OpcodeWithOperands] = [] @@ -83,16 +80,13 @@ def process_evm_bytes( # noqa: D103 while evm_bytes: opcode_byte = evm_bytes.pop(0) - opcode: Opcode + opcode: Op for op in Op: if not isinstance(op, Macro) and op.int() == opcode_byte: opcode = op break else: - if allow_unknown: - opcode = Opcode(opcode_byte) - else: - raise ValueError(f"Unknown opcode: {opcode_byte}") + raise ValueError(f"Unknown opcode: {opcode_byte}") if opcode.data_portion_length > 0: signed = opcode in [Op.RJUMP, Op.RJUMPI] diff --git a/src/cli/tests/test_eofwrap.py b/src/cli/tests/test_eofwrap.py index 4646d52d65..2f554ef8a2 100644 --- a/src/cli/tests/test_eofwrap.py +++ b/src/cli/tests/test_eofwrap.py @@ -6,7 +6,6 @@ from ethereum_test_base_types.conversions import to_hex from ethereum_test_tools import Opcodes as Op from ethereum_test_types.eof.v1 import Container -from ethereum_test_vm.opcode import Opcode from ..eofwrap import wrap_code @@ -38,8 +37,6 @@ [Op.GAS + Op.RETURN(0, 0), Container.Code(Op.GAS + Op.RETURN(0, 0))], [Op.GAS + Op.REVERT(0, 0), Container.Code(Op.GAS + Op.REVERT(0, 0))], [Op.GAS + Op.INVALID, Container.Code(Op.GAS + Op.INVALID)], - # Not valid legacy opcodes, but they show up in the tests so need to somehow handle. - [Opcode(185), Container.Code(Opcode(185) + Op.STOP)], [Op.RJUMPV[1, 2, 3], Container.Code(Op.RJUMPV[1, 2, 3] + Op.STOP)], [Op.RJUMPV, Container.Code(Op.RJUMPV + Op.STOP)], [