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 a31871d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ethereum_test_tools/vm/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def _get_int_size(n: int) -> int:

_push_opcodes_byte_list = [bytes([0x5F + x]) for x in range(33)]

class Special_Codes(Enum):
"""
"""
NAN = 0,
OOG = 1,


class Opcode(bytes):
"""
Expand All @@ -43,6 +49,7 @@ class Opcode(bytes):
pushed_stack_items: int
min_stack_height: int
data_portion_length: int
special_code: Special_Codes
_name_: str

def __new__(
Expand All @@ -53,6 +60,7 @@ def __new__(
pushed_stack_items: int = 0,
min_stack_height: int = 0,
data_portion_length: int = 0,
special_code: Special_Codes = Special_Codes.NAN,
):
"""
Creates a new opcode instance.
Expand All @@ -67,6 +75,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 +116,12 @@ def __call__(self, *args_t: Union[int, bytes, str, "Opcode", FixedSizeBytes]) ->
pre_opcode_bytecode = bytes()
data_portion = bytes()

if self.special_code == Special_Codes.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 > u64 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 +203,13 @@ 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 +395,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=Special_Codes.OOG)

0 comments on commit a31871d

Please sign in to comment.