Skip to content

Commit 00745da

Browse files
authored
new(tests): EOF - EIP-4750: add fibonacci and factorial tests for CALLF (ethereum#915)
Add tests implementing fibonacci sequence and factorial using recursive CALLF instructions. They were originally contributed to ethereum/tests by @hugo-dc in ethereum/tests@89c2147. Tests were additionally parametrized.
1 parent 1155288 commit 00745da

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

osaka/eip7692_eof_v1/eip4750_functions/test_callf_execution.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
22
EOF CALLF execution tests
33
"""
4+
import math
5+
46
import pytest
57

8+
from ethereum_test_base_types import Hash
69
from ethereum_test_specs import StateTestFiller
710
from ethereum_test_tools import Account, EOFStateTestFiller
811
from ethereum_test_tools import Opcodes as Op
@@ -22,6 +25,92 @@
2225
pytestmark = pytest.mark.valid_from(EOF_FORK_NAME)
2326

2427

28+
@pytest.mark.parametrize(
29+
"n,result",
30+
((0, 1), (1, 1), (5, 120), (57, math.factorial(57)), (58, math.factorial(58) % 2**256)),
31+
)
32+
def test_callf_factorial(eof_state_test: EOFStateTestFiller, n, result):
33+
"""Test factorial implementation with recursive CALLF instructions"""
34+
eof_state_test(
35+
data=Container(
36+
sections=[
37+
Section.Code(
38+
Op.CALLDATALOAD(0) + Op.SSTORE(0, Op.CALLF[1]) + Op.STOP,
39+
max_stack_height=2,
40+
),
41+
Section.Code(
42+
Op.PUSH1[1]
43+
+ Op.DUP2
44+
+ Op.GT
45+
+ Op.RJUMPI[4]
46+
+ Op.POP
47+
+ Op.PUSH1[1]
48+
+ Op.RETF
49+
+ Op.PUSH1[1]
50+
+ Op.DUP2
51+
+ Op.SUB
52+
+ Op.CALLF[1]
53+
+ Op.DUP2
54+
+ Op.MUL
55+
+ Op.SWAP1
56+
+ Op.POP
57+
+ Op.RETF,
58+
code_inputs=1,
59+
code_outputs=1,
60+
max_stack_height=3,
61+
),
62+
]
63+
),
64+
tx_data=Hash(n),
65+
container_post=Account(storage={0: result}),
66+
)
67+
68+
69+
@pytest.mark.parametrize(
70+
"n,result",
71+
((0, 1), (1, 1), (13, 233), (27, 196418)),
72+
)
73+
def test_callf_fibonacci(eof_state_test: EOFStateTestFiller, n, result):
74+
"""Test fibonacci sequence implementation with recursive CALLF instructions"""
75+
eof_state_test(
76+
data=Container(
77+
sections=[
78+
Section.Code(
79+
Op.CALLDATALOAD(0) + Op.SSTORE(0, Op.CALLF[1]) + Op.STOP,
80+
max_stack_height=2,
81+
),
82+
Section.Code(
83+
Op.PUSH1[2]
84+
+ Op.DUP2
85+
+ Op.GT
86+
+ Op.RJUMPI[4]
87+
+ Op.POP
88+
+ Op.PUSH1[1]
89+
+ Op.RETF
90+
+ Op.PUSH1[2]
91+
+ Op.DUP2
92+
+ Op.SUB
93+
+ Op.CALLF[1]
94+
+ Op.PUSH1[1]
95+
+ Op.DUP3
96+
+ Op.SUB
97+
+ Op.CALLF[1]
98+
+ Op.ADD
99+
+ Op.SWAP1
100+
+ Op.POP
101+
+ Op.RETF,
102+
code_inputs=1,
103+
code_outputs=1,
104+
max_stack_height=4,
105+
),
106+
]
107+
),
108+
tx_gas_limit=15_000_000,
109+
tx_data=Hash(n),
110+
container_post=Account(storage={0: result}),
111+
)
112+
113+
25114
@pytest.mark.parametrize(
26115
"container",
27116
(

0 commit comments

Comments
 (0)