Skip to content

Commit

Permalink
feat(tests): extend Op with Op.OOG fake opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Feb 26, 2024
1 parent bf35d79 commit 7773170
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/ethereum_test_tools/vm/opcode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Ethereum Virtual Machine opcode definitions.
"""

from enum import Enum
from typing import List, Union

Expand All @@ -25,6 +26,15 @@ def _get_int_size(n: int) -> int:
_push_opcodes_byte_list = [bytes([0x5F + x]) for x in range(33)]


class SpecialCodes(Enum):
"""
Special cases opcode operation
"""

NAN = (0,)
OOG = (1,)


class Opcode(bytes):
"""
Represents a single Opcode instruction in the EVM, with extra
Expand All @@ -43,6 +53,7 @@ class Opcode(bytes):
pushed_stack_items: int
min_stack_height: int
data_portion_length: int
special_code: SpecialCodes
_name_: str

def __new__(
Expand All @@ -53,6 +64,7 @@ def __new__(
pushed_stack_items: int = 0,
min_stack_height: int = 0,
data_portion_length: int = 0,
special_code: SpecialCodes = SpecialCodes.NAN,
):
"""
Creates a new opcode instance.
Expand All @@ -67,6 +79,7 @@ def __new__(
obj.pushed_stack_items = pushed_stack_items
obj.min_stack_height = min_stack_height
obj.data_portion_length = data_portion_length
obj.special_code = special_code
return obj

def __call__(self, *args_t: Union[int, bytes, str, "Opcode", FixedSizeBytes]) -> bytes:
Expand Down Expand Up @@ -107,6 +120,12 @@ def __call__(self, *args_t: Union[int, bytes, str, "Opcode", FixedSizeBytes]) ->
pre_opcode_bytecode = bytes()
data_portion = bytes()

if self.special_code == SpecialCodes.OOG:
# This operation will result in gasprice = 19073514453125027
# Add a little more and geth report gasprice = 30 with oog exception
# Make it 0 - 1 and geth report gasprice > unsigned 64 error
return Opcodes.SHA3(0, 100000000000)

if self.data_portion_length > 0:
# For opcodes with a data portion, the first argument is the data
# and the rest of the arguments form the stack.
Expand Down Expand Up @@ -188,6 +207,14 @@ def __str__(self) -> str:
"""
return self._name_

def __eq__(self, other):
"""
Required to differentiate between SELFDESTRUCT and SENDALL type of cases
"""
if not isinstance(other, Opcode):
return NotImplemented
return self._name_ == other._name_


OpcodeCallArg = Union[int, bytes, Opcode]

Expand Down Expand Up @@ -373,3 +400,4 @@ class Opcodes(Opcode, Enum):

SELFDESTRUCT = Opcode(0xFF, popped_stack_items=1)
SENDALL = Opcode(0xFF, popped_stack_items=1)
OOG = Opcode(0xFF, popped_stack_items=2, special_code=SpecialCodes.OOG)
3 changes: 2 additions & 1 deletion whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ makepyfile
metafunc
modifyitems
nodeid
oog
optparser
originalname
parametrized
Expand Down Expand Up @@ -532,4 +533,4 @@ modexp

fi
url
gz
gz

0 comments on commit 7773170

Please sign in to comment.