Skip to content

Commit b8edede

Browse files
chfastmarioevz
andauthored
feat(fw): allow adding verbatim bytes to Bytecode (#1119)
* feat(fw): allow adding verbatim bytes to Bytecode * fix: linting, tests --------- Co-authored-by: Mario Vega <[email protected]>
1 parent 2aa8e12 commit b8edede

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/ethereum_test_vm/bytecode.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __eq__(self, other):
118118
and self.max_stack_height == other.max_stack_height
119119
and self.min_stack_height == other.min_stack_height
120120
)
121-
if isinstance(other, SupportsBytes):
121+
if isinstance(other, SupportsBytes) or isinstance(other, bytes):
122122
return bytes(self) == bytes(other)
123123
raise NotImplementedError(f"Unsupported type for comparison: {type(other)}")
124124

@@ -134,11 +134,18 @@ def __hash__(self):
134134
)
135135
)
136136

137-
def __add__(self, other: "Bytecode | int | None") -> "Bytecode":
137+
def __add__(self, other: "Bytecode | bytes | int | None") -> "Bytecode":
138138
"""Concatenate the bytecode representation with another bytecode object."""
139139
if other is None or (isinstance(other, int) and other == 0):
140140
# Edge case for sum() function
141141
return self
142+
143+
if isinstance(other, bytes):
144+
c = Bytecode(self)
145+
c._bytes_ += other
146+
c._name_ = ""
147+
return c
148+
142149
assert isinstance(other, Bytecode), "Can only concatenate Bytecode instances"
143150
# Figure out the stack height after executing the two opcodes.
144151
a_pop, a_push = self.popped_stack_items, self.pushed_stack_items

src/ethereum_test_vm/tests/test_vm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,22 @@ def test_opcode_comparison():
412412
assert Op.ADD == Op.ADD
413413
assert Op.ADD != Op.STOP
414414
assert Op.ADD > Op.STOP
415+
416+
417+
def test_bytecode_concatenation_with_bytes():
418+
"""
419+
Test that the bytecode can be concatenated with bytes.
420+
Bytes work as verbatim code and don't affect the bytecode properties.
421+
"""
422+
base = Op.PUSH1[0xFF] + Op.NOT
423+
assert str(base) == ""
424+
425+
code = base + b"\x01\x02"
426+
assert code == bytes([0x60, 0xFF, 0x19, 0x01, 0x02])
427+
428+
assert str(code) == ""
429+
assert code.popped_stack_items == code.popped_stack_items
430+
assert code.pushed_stack_items == code.pushed_stack_items
431+
assert code.max_stack_height == code.max_stack_height
432+
assert code.min_stack_height == code.min_stack_height
433+
assert code.terminating == code.terminating

tests/osaka/eip7692_eof_v1/eip4200_relative_jumps/test_rjump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_rjump_truncated_rjump_2(
116116
):
117117
"""EOF1I4200_0002 (Invalid) EOF code containing truncated RJUMP."""
118118
eof_test(
119-
data=Container.Code(Op.RJUMP + Op.STOP),
119+
data=Container.Code(Op.RJUMP + b"\x00"),
120120
expect_exception=EOFException.TRUNCATED_INSTRUCTION,
121121
)
122122

tests/osaka/eip7692_eof_v1/eip4200_relative_jumps/test_rjumpi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_rjumpi_truncated_2(
300300
data=Container(
301301
sections=[
302302
Section.Code(
303-
code=Op.PUSH1(0) + Op.RJUMPI + Op.STOP,
303+
code=Op.PUSH1(0) + Op.RJUMPI + b"\x00",
304304
)
305305
],
306306
),

0 commit comments

Comments
 (0)