Skip to content

Commit b8e7811

Browse files
committed
[WIP] Implement subroutine opcodes
1 parent c88becc commit b8e7811

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

eth/vm/logic/flow.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ def gas(computation: BaseComputation) -> None:
6363

6464

6565
def beginsub(computation: BaseComputation) -> None:
66-
raise OutOfGas
66+
raise OutOfGas("Error: at pc={}, op=BEGINSUB: invalid subroutine entry")
6767

6868

6969
def jumpsub(computation: BaseComputation) -> None:
7070
sub_loc = computation.stack_pop1_int()
71+
code_range_length = computation.code._length_cache
72+
73+
if sub_loc >= code_range_length:
74+
raise InvalidJumpDestination("Error: at pc={}, op=JUMPSUB: invalid jump destination".format(computation.code.program_counter))
7175

7276
if computation.code.is_valid_opcode(sub_loc):
7377

tests/core/opcodes/test_opcodes.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,6 @@ def test_blake2b_f_compression(vm_class, input_hex, output_hex, expect_exception
11271127
result = comp.output
11281128
assert result.hex() == output_hex
11291129

1130-
11311130
@pytest.mark.parametrize(
11321131
'vm_class, code, expect_gas_used',
11331132
(
@@ -1143,32 +1142,45 @@ def test_blake2b_f_compression(vm_class, input_hex, output_hex, expect_exception
11431142
),
11441143
(
11451144
BerlinVM,
1146-
'0x6801000000000000000c5e005c60115e5d5c5d',
1145+
'0x6005565c5d5b60035e',
11471146
0,
11481147
),
1148+
)
1149+
)
1150+
def test_jumpsub(vm_class, code, expect_gas_used):
1151+
computation = setup_computation(vm_class, CANONICAL_ADDRESS_B, decode_hex(code))
1152+
comp = computation.apply_message(
1153+
computation.state,
1154+
computation.msg,
1155+
computation.transaction_context,
1156+
)
1157+
assert comp.is_success
1158+
assert comp.get_gas_used() == expect_gas_used
1159+
1160+
@pytest.mark.xfail(reason="invalid subroutines")
1161+
@pytest.mark.parametrize(
1162+
'vm_class, code',
1163+
(
11491164
(
11501165
BerlinVM,
11511166
'0x5d5858',
1152-
0,
11531167
),
11541168
(
11551169
BerlinVM,
1156-
'0x6005565c5d5b60035e',
1157-
30,
1170+
'0x6801000000000000000c5e005c60115e5d5c5d',
11581171
),
11591172
(
11601173
BerlinVM,
11611174
'0x5c5d00',
1162-
0,
11631175
),
11641176
)
11651177
)
1166-
def test_jumpsub(vm_class, code, expect_gas_used):
1178+
def test_jumpsub(vm_class, code):
11671179
computation = setup_computation(vm_class, CANONICAL_ADDRESS_B, decode_hex(code))
11681180
comp = computation.apply_message(
11691181
computation.state,
11701182
computation.msg,
11711183
computation.transaction_context,
11721184
)
11731185
assert comp.is_success
1174-
assert comp.get_gas_used() == expect_gas_used
1186+

0 commit comments

Comments
 (0)