Skip to content

Commit

Permalink
new(tests): Add precompile-absence test
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Dec 19, 2024
1 parent 6446ed4 commit 368388e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/frontier/precompiles/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Test for precompiles that apply for all forks starting from Frontier.
"""
71 changes: 71 additions & 0 deletions tests/frontier/precompiles/test_precompile_absence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
abstract: Test Calling Precompile Range (close to zero)
"""

import pytest

from ethereum_test_forks import Fork
from ethereum_test_tools import Account, Address, Alloc, Bytecode
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools import StateTestFiller, Storage, Transaction

UPPER_BOUND = 0x101
RETURNDATASIZE_OFFSET = 0x10000000000000000 # Must be greater than UPPER_BOUND


@pytest.mark.parametrize(
"calldata_size",
[
pytest.param(0, id="empty_calldata"),
pytest.param(32, id="32_bytes"),
],
)
@pytest.mark.valid_from("Byzantium")
def test_precompile_absence(
state_test: StateTestFiller,
pre: Alloc,
fork: Fork,
calldata_size: int,
):
"""
Test that addresses close to zero are not precompiles unless active in the fork.
"""
active_precompiles = fork.precompiles()
storage = Storage()
call_code = Bytecode()
for address in range(1, UPPER_BOUND + 1):
if Address(address) in active_precompiles:
continue
call_code += Op.SSTORE(
address,
Op.CALL(address=address, args_size=calldata_size),
)
storage[address] = 1
if Op.RETURNDATASIZE in fork.valid_opcodes():
call_code += Op.SSTORE(
address + RETURNDATASIZE_OFFSET,
Op.RETURNDATASIZE,
)
storage[address + RETURNDATASIZE_OFFSET] = 0

call_code += Op.STOP

entry_point_address = pre.deploy_contract(call_code, storage=storage.canary())

tx = Transaction(
to=entry_point_address,
gas_limit=10_000_000,
sender=pre.fund_eoa(),
protected=True,
)

state_test(
pre=pre,
tx=tx,
post={
entry_point_address: Account(
storage=storage,
)
},
)

0 comments on commit 368388e

Please sign in to comment.