-
Notifications
You must be signed in to change notification settings - Fork 152
new(tests): EOF: Validate EOF only opcodes are invalid in legacy #768
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
786cc2b
Ensure gas_test uses EOF
shemnon 99d4c17
Test all EOF opcodes in legacy
shemnon 60b2036
Merge remote-tracking branch 'upstream/main' into eof/legacy-opcodes
shemnon a27a610
Merge branch 'main' of github.com:ethereum/execution-spec-tests into β¦
shemnon 9748d75
Merge branch 'main' of github.com:ethereum/execution-spec-tests into β¦
shemnon a57a208
review changes
shemnon 2856479
Apply suggestions from code review
marioevz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
tests/prague/eip7692_eof_v1/eip3540_eof_v1/test_opcodes_in_legacy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
""" | ||
Tests all EOF-only opcodes in legacy contracts and expects failure. | ||
""" | ||
import pytest | ||
|
||
from ethereum_test_base_types import Account | ||
from ethereum_test_specs import StateTestFiller | ||
from ethereum_test_tools import Initcode | ||
from ethereum_test_tools import Opcodes as Op | ||
from ethereum_test_tools.eof.v1 import Container, Section | ||
from ethereum_test_types import Alloc, Environment, Transaction | ||
from ethereum_test_vm import Opcodes | ||
|
||
from .. import EOF_FORK_NAME | ||
|
||
REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7692.md" | ||
REFERENCE_SPEC_VERSION = "f0e7661ee0d16e612e0931ec88b4c9f208e9d513" | ||
|
||
pytestmark = pytest.mark.valid_from(EOF_FORK_NAME) | ||
|
||
slot_code_executed = b"EXEC" | ||
slot_code_worked = b"WORK" | ||
slot_create_address = b"ADDR" | ||
|
||
value_code_executed = b"exec" | ||
value_code_worked = b"work" | ||
value_non_execution_canary = b"brid" | ||
value_create_failed = 0 | ||
|
||
eof_opcode_blocks = [ | ||
pytest.param(Op.PUSH0 + Op.DUPN[0], id="DUPN"), | ||
pytest.param(Op.PUSH0 + Op.PUSH0 + Op.SWAPN[0], id="SWAPN"), | ||
pytest.param(Op.PUSH0 + Op.PUSH0 + Op.PUSH0 + Op.EXCHANGE[2, 3], id="EXCHANGE"), | ||
pytest.param(Op.RJUMP[0], id="RJUMP"), | ||
pytest.param(Op.PUSH0 + Op.RJUMPI[0], id="RJUMPI"), | ||
pytest.param(Op.PUSH0 + Op.RJUMPV[0, 0], id="RJUMPI"), | ||
pytest.param(Op.CALLF[1], id="CALLF"), | ||
pytest.param(Op.RETF, id="RETF"), | ||
pytest.param(Op.JUMPF[0], id="JUMPF"), | ||
pytest.param(Op.PUSH0 + Op.PUSH0 + Op.PUSH0 + Op.PUSH1(2) + Op.EXTCALL, id="EXTCALL"), | ||
pytest.param( | ||
Op.PUSH0 + Op.PUSH0 + Op.PUSH0 + Op.PUSH1(2) + Op.EXTDELEGATECALL, id="EXTDELEGATECALL" | ||
), | ||
pytest.param( | ||
Op.PUSH0 + Op.PUSH0 + Op.PUSH0 + Op.PUSH1(2) + Op.EXTSTATICCALL, id="EXTSTATICCALL" | ||
), | ||
pytest.param(Op.DATALOAD(0), id="DATALOAD"), | ||
pytest.param(Op.DATALOADN[0], id="DATALOADN"), | ||
pytest.param(Op.DATASIZE, id="DATASIZE"), | ||
pytest.param(Op.DATACOPY(0, 0, 32), id="DATACOPY"), | ||
pytest.param(Op.EOFCREATE[0](0, 0, 0, 0), id="EOFCREATE"), | ||
pytest.param(Op.RETURNCONTRACT[0], id="RETURNCONTRACT"), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"code", | ||
eof_opcode_blocks, | ||
) | ||
def test_opcodes_in_legacy(state_test: StateTestFiller, pre: Alloc, code: Opcodes): | ||
""" | ||
Test all EOF only opcodes in legacy contracts and expects failure. | ||
""" | ||
env = Environment() | ||
|
||
address_test_contract = pre.deploy_contract( | ||
code=code + Op.SSTORE(slot_code_executed, value_code_executed), | ||
storage={slot_code_executed: value_non_execution_canary}, | ||
) | ||
|
||
post = { | ||
# assert the canary is not over-written. If it was written then the EOF opcode was valid | ||
address_test_contract: Account(storage={slot_code_executed: value_non_execution_canary}), | ||
} | ||
|
||
sender = pre.fund_eoa() | ||
|
||
tx = Transaction( | ||
sender=sender, | ||
to=address_test_contract, | ||
gas_limit=5_000_000, | ||
gas_price=10, | ||
protected=False, | ||
data="", | ||
) | ||
|
||
state_test( | ||
env=env, | ||
pre=pre, | ||
post=post, | ||
tx=tx, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"code", | ||
eof_opcode_blocks, | ||
) | ||
def test_opcodes_in_create_tx(state_test: StateTestFiller, pre: Alloc, code: Opcodes): | ||
""" | ||
Test all EOF only opcodes in legacy contracts and expects failure. | ||
""" | ||
env = Environment() | ||
|
||
sender = pre.fund_eoa() | ||
|
||
tx = Transaction( | ||
sender=sender, | ||
to=None, | ||
gas_limit=5_000_000, | ||
gas_price=10, | ||
protected=False, | ||
data=code, | ||
) | ||
|
||
post = { | ||
# Should revert in initcode, which results in no contract created | ||
tx.created_contract: Account.NONEXISTENT | ||
} | ||
|
||
state_test( | ||
env=env, | ||
pre=pre, | ||
post=post, | ||
tx=tx, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"legacy_create_opcode", | ||
[ | ||
pytest.param(Op.CREATE, id="CREATE"), | ||
pytest.param(Op.CREATE2, id="CREATE2"), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"code", | ||
eof_opcode_blocks, | ||
) | ||
def test_opcodes_in_create_operation( | ||
state_test: StateTestFiller, | ||
pre: Alloc, | ||
code: Opcodes, | ||
legacy_create_opcode: Opcodes, | ||
): | ||
""" | ||
Test all EOF only opcodes in legacy contracts and expects failure. | ||
""" | ||
env = Environment() | ||
|
||
init_code = Initcode(initcode_prefix=code, deploy_code=Op.RETURN(0, 0)) | ||
factory_code = ( | ||
Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) | ||
+ Op.SSTORE(slot_create_address, legacy_create_opcode(size=Op.CALLDATASIZE)) | ||
+ Op.SSTORE(slot_code_worked, value_code_worked) | ||
) | ||
|
||
sender = pre.fund_eoa() | ||
contract_address = pre.deploy_contract(code=factory_code) | ||
|
||
post = { | ||
contract_address: Account( | ||
storage={slot_create_address: value_create_failed, slot_code_worked: value_code_worked} | ||
) | ||
} | ||
tx = Transaction( | ||
to=contract_address, | ||
gas_limit=10_000_000, | ||
gas_price=10, | ||
protected=False, | ||
data=init_code, | ||
sender=sender, | ||
) | ||
|
||
state_test(env=env, pre=pre, post=post, tx=tx) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("ext_call_opcode"), | ||
[ | ||
pytest.param(Op.EXTCALL, id="EXTCALL"), | ||
pytest.param(Op.EXTDELEGATECALL, id="EXTDELEGATECALL"), | ||
pytest.param(Op.EXTSTATICCALL, id="EXTSTATICCALL"), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"code", | ||
eof_opcode_blocks, | ||
) | ||
def test_opcodes_in_eof_calling_legacy( | ||
state_test: StateTestFiller, | ||
pre: Alloc, | ||
code: Opcodes, | ||
ext_call_opcode: Op, | ||
): | ||
""" | ||
Test all EOF only opcodes in legacy contracts and expects failure. | ||
""" | ||
env = Environment() | ||
|
||
address_test_contract = pre.deploy_contract( | ||
code=code + Op.SSTORE(slot_code_executed, value_code_executed), | ||
storage={slot_code_executed: value_non_execution_canary}, | ||
) | ||
|
||
address_entry_contract = pre.deploy_contract( | ||
code=Container( | ||
sections=[ | ||
Section.Code( | ||
ext_call_opcode(address=address_test_contract) | ||
+ Op.SSTORE(slot_code_worked, value_code_worked) | ||
+ Op.STOP | ||
) | ||
] | ||
), | ||
storage={slot_code_executed: value_non_execution_canary}, | ||
) | ||
|
||
post = { | ||
# assert the canary is not over-written. If it was written then the EOF opcode was valid | ||
address_test_contract: Account(storage={slot_code_executed: value_non_execution_canary}), | ||
address_entry_contract: Account( | ||
storage={ | ||
slot_code_executed: value_non_execution_canary, | ||
slot_code_worked: value_code_worked, | ||
} | ||
), | ||
} | ||
|
||
sender = pre.fund_eoa() | ||
|
||
tx = Transaction( | ||
sender=sender, | ||
to=address_entry_contract, | ||
gas_limit=5_000_000, | ||
gas_price=10, | ||
protected=False, | ||
data="", | ||
) | ||
|
||
state_test( | ||
env=env, | ||
pre=pre, | ||
post=post, | ||
tx=tx, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.