Skip to content

Commit

Permalink
Revert support for unknown opcodes in evm_bytes.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pdobacz committed Oct 24, 2024
1 parent c5526f2 commit f7084a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
29 changes: 20 additions & 9 deletions src/cli/eofwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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))
Expand Down
14 changes: 4 additions & 10 deletions src/cli/evm_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -73,26 +72,21 @@ 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] = []

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]
Expand Down
3 changes: 0 additions & 3 deletions src/cli/tests/test_eofwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)],
[
Expand Down

0 comments on commit f7084a2

Please sign in to comment.