Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(tests): Identity precompile test #1047

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ethereum_test_forks/forks/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,11 @@ def valid_opcodes(
cls,
) -> List[Opcodes]:
"""Return list of Opcodes that are valid to work on this fork."""
return [Opcodes.RETURNDATASIZE, Opcodes.STATICCALL] + super(Byzantium, cls).valid_opcodes()
return [
Opcodes.RETURNDATASIZE,
Opcodes.RETURNDATACOPY,
Opcodes.STATICCALL,
] + super(Byzantium, cls).valid_opcodes()


class Constantinople(Byzantium):
Expand Down
8 changes: 5 additions & 3 deletions tests/frontier/opcodes/test_all_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def prepare_stack(opcode: Opcode) -> Bytecode:
return Op.PUSH1(1) + Op.PUSH1(5)
if opcode == Op.JUMP:
return Op.PUSH1(3)
if opcode == Op.RETURNDATACOPY:
return Op.PUSH1(0x00) * 32
return Op.PUSH1(0x01) * 32


Expand Down Expand Up @@ -65,8 +67,8 @@ def test_all_opcodes(state_test: StateTestFiller, pre: Alloc, fork: Fork):
contract_address = pre.deploy_contract(
code=sum(
Op.SSTORE(
Op.PUSH1(opcode.int()),
Op.CALL(1_000_000, opcode_address, 0, 0, 0, 0, 0),
opcode.int(),
Op.CALL(gas=100_000, address=opcode_address),
)
for opcode, opcode_address in code_contract.items()
)
Expand All @@ -82,7 +84,7 @@ def test_all_opcodes(state_test: StateTestFiller, pre: Alloc, fork: Fork):

tx = Transaction(
sender=pre.fund_eoa(),
gas_limit=500_000_000,
gas_limit=50_000_000,
to=contract_address,
data=b"",
value=0,
Expand Down
1 change: 1 addition & 0 deletions tests/homestead/identity_precompile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""abstract: EIP-2: Homestead Precompile Identity Test Cases."""
103 changes: 103 additions & 0 deletions tests/homestead/identity_precompile/test_identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""abstract: EIP-2: Homestead Identity Precompile Test Cases."""

import pytest

from ethereum_test_tools import (
Account,
Alloc,
Environment,
StateTestFiller,
Transaction,
keccak256,
)
from ethereum_test_tools import Opcodes as Op


@pytest.mark.with_all_call_opcodes(
selector=lambda call_opcode: call_opcode
not in [Op.EXTCALL, Op.EXTDELEGATECALL, Op.EXTSTATICCALL]
# EXT*CALL opcodes do not contain accept the return data size/offset as an argument
)
@pytest.mark.valid_from("Byzantium")
def test_identity_return_overwrite(
state_test: StateTestFiller,
pre: Alloc,
call_opcode: Op,
):
"""Test the return data of the identity precompile overwriting its input."""
env = Environment()
code = (
sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible I prefer to have concrete looking memory input like
0x000102030405 because when reading the code we will have to stop and calculate or sometimes we don't see what exactly the result of a calculation and when its not obvious we can have a bug and difficulty to find it.

+ call_opcode(
address=4,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we have global space constants for the precompiles?

args_offset=0,
args_size=4, # args = [1, 2, 3, 4]
ret_offset=1,
ret_size=4,
) # memory = [1, 1, 2, 3, 4]
+ Op.RETURNDATACOPY(
dest_offset=0, offset=0, size=Op.RETURNDATASIZE()
) # memory correct = [1, 2, 3, 4, 4], corrupt = [1, 1, 2, 3, 4]
+ Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE))
)
contract_address = pre.deploy_contract(
code=code,
)
tx = Transaction(
sender=pre.fund_eoa(),
to=contract_address,
gas_limit=100_000,
)

post = {
contract_address: Account(
storage={
1: keccak256(bytes([1, 2, 3, 4, 4]).ljust(32, b"\0")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this benefit from leftpadding?

},
),
}

state_test(env=env, pre=pre, post=post, tx=tx)


@pytest.mark.with_all_call_opcodes()
@pytest.mark.valid_from("Byzantium")
def test_identity_return_buffer_modify(
state_test: StateTestFiller,
pre: Alloc,
call_opcode: Op,
):
"""Test the modification of the input range to attempt to modify the return buffer."""
env = Environment()
code = (
sum(Op.MSTORE8(offset=i, value=(i + 1)) for i in range(4)) # memory = [1, 2, 3, 4]
+ call_opcode(
address=4,
args_offset=0,
args_size=4, # args = [1, 2, 3, 4]
) # memory = [1, 2, 3, 4]
+ Op.MSTORE8(offset=0, value=5) # memory = [5, 2, 3, 4]
+ Op.MSTORE8(offset=4, value=5) # memory = [5, 2, 3, 4, 5]
+ Op.RETURNDATACOPY(
dest_offset=0, offset=0, size=Op.RETURNDATASIZE()
) # memory correct = [1, 2, 3, 4, 5], corrupt = [5, 2, 3, 4, 5]
+ Op.SSTORE(1, Op.SHA3(offset=0, size=Op.MSIZE))
)
contract_address = pre.deploy_contract(
code=code,
)
tx = Transaction(
sender=pre.fund_eoa(),
to=contract_address,
gas_limit=100_000,
)

post = {
contract_address: Account(
storage={
1: keccak256(bytes([1, 2, 3, 4, 5]).ljust(32, b"\0")),
},
),
}

state_test(env=env, pre=pre, post=post, tx=tx)
Loading