Skip to content

Commit

Permalink
new(tests): EOF - EIP-4750: add fibonacci and factorial tests for CAL…
Browse files Browse the repository at this point in the history
…LF (#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.
  • Loading branch information
chfast authored Oct 28, 2024
1 parent db60338 commit d20b3c4
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""
EOF CALLF execution tests
"""
import math

import pytest

from ethereum_test_base_types import Hash
from ethereum_test_specs import StateTestFiller
from ethereum_test_tools import Account, EOFStateTestFiller
from ethereum_test_tools import Opcodes as Op
Expand All @@ -22,6 +25,92 @@
pytestmark = pytest.mark.valid_from(EOF_FORK_NAME)


@pytest.mark.parametrize(
"n,result",
((0, 1), (1, 1), (5, 120), (57, math.factorial(57)), (58, math.factorial(58) % 2**256)),
)
def test_callf_factorial(eof_state_test: EOFStateTestFiller, n, result):
"""Test factorial implementation with recursive CALLF instructions"""
eof_state_test(
data=Container(
sections=[
Section.Code(
Op.CALLDATALOAD(0) + Op.SSTORE(0, Op.CALLF[1]) + Op.STOP,
max_stack_height=2,
),
Section.Code(
Op.PUSH1[1]
+ Op.DUP2
+ Op.GT
+ Op.RJUMPI[4]
+ Op.POP
+ Op.PUSH1[1]
+ Op.RETF
+ Op.PUSH1[1]
+ Op.DUP2
+ Op.SUB
+ Op.CALLF[1]
+ Op.DUP2
+ Op.MUL
+ Op.SWAP1
+ Op.POP
+ Op.RETF,
code_inputs=1,
code_outputs=1,
max_stack_height=3,
),
]
),
tx_data=Hash(n),
container_post=Account(storage={0: result}),
)


@pytest.mark.parametrize(
"n,result",
((0, 1), (1, 1), (13, 233), (27, 196418)),
)
def test_callf_fibonacci(eof_state_test: EOFStateTestFiller, n, result):
"""Test fibonacci sequence implementation with recursive CALLF instructions"""
eof_state_test(
data=Container(
sections=[
Section.Code(
Op.CALLDATALOAD(0) + Op.SSTORE(0, Op.CALLF[1]) + Op.STOP,
max_stack_height=2,
),
Section.Code(
Op.PUSH1[2]
+ Op.DUP2
+ Op.GT
+ Op.RJUMPI[4]
+ Op.POP
+ Op.PUSH1[1]
+ Op.RETF
+ Op.PUSH1[2]
+ Op.DUP2
+ Op.SUB
+ Op.CALLF[1]
+ Op.PUSH1[1]
+ Op.DUP3
+ Op.SUB
+ Op.CALLF[1]
+ Op.ADD
+ Op.SWAP1
+ Op.POP
+ Op.RETF,
code_inputs=1,
code_outputs=1,
max_stack_height=4,
),
]
),
tx_gas_limit=15_000_000,
tx_data=Hash(n),
container_post=Account(storage={0: result}),
)


@pytest.mark.parametrize(
"container",
(
Expand Down

0 comments on commit d20b3c4

Please sign in to comment.