-
Notifications
You must be signed in to change notification settings - Fork 153
feat(tests): port ethereum/tests swap opcode tests #1163
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
Open
bomanaps
wants to merge
5
commits into
ethereum:main
Choose a base branch
from
bomanaps:Port-SwapFiller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
""" | ||
A State test for the set of `SWAP*` opcodes. | ||
Ported from: https://github.com/ethereum/tests/ | ||
blob/develop/src/GeneralStateTestsFiller/VMTests/vmTests/swapFiller.yml. | ||
""" | ||
|
||
import pytest # noqa: I001 | ||
|
||
from ethereum_test_forks import Fork, Frontier, Homestead | ||
from ethereum_test_tools import Account, Alloc, Bytecode, Environment | ||
from ethereum_test_tools import Opcodes as Op | ||
from ethereum_test_tools import StateTestFiller, Storage, Transaction | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"swap_opcode", | ||
[getattr(Op, f"SWAP{i}") for i in range(1, 17)], | ||
ids=lambda op: str(op), | ||
) | ||
@pytest.mark.valid_from("Frontier") | ||
def test_swap( | ||
state_test: StateTestFiller, | ||
fork: Fork, | ||
pre: Alloc, | ||
swap_opcode: Op | ||
): | ||
""" | ||
The set of `SWAP*` opcodes swaps the top of the stack with a specific | ||
element. | ||
|
||
In this test, we ensure that the set of `SWAP*` opcodes correctly swaps | ||
the top element with the nth element and stores the result in storage. | ||
""" | ||
env = Environment() | ||
|
||
# Calculate which position we're swapping with (1-based index) | ||
swap_pos = swap_opcode.int() - 0x90 + 1 | ||
|
||
# Generate stack values | ||
stack_values = list(range(swap_pos + 1)) | ||
|
||
# Push the stack values onto the stack (in reverse order). | ||
contract_code = Bytecode() | ||
for value in reversed(stack_values): | ||
contract_code += Op.PUSH1(value) | ||
|
||
# Perform the SWAP operation. | ||
contract_code += swap_opcode | ||
|
||
# Store the top of the stack in storage slot 0. | ||
contract_code += Op.PUSH1(0) + Op.SSTORE | ||
|
||
# Deploy the contract with the generated bytecode. | ||
contract = pre.deploy_contract(contract_code) | ||
bomanaps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Create a transaction to execute the contract. | ||
tx = Transaction( | ||
sender=pre.fund_eoa(), | ||
to=contract, | ||
gas_limit=500_000, | ||
protected=False if fork in [Frontier, Homestead] else True, | ||
bomanaps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
# After SWAP, the top value will be the one at swap_pos | ||
expected_value = swap_pos | ||
|
||
# Define the expected post-state. | ||
post = {} | ||
storage = Storage() | ||
storage.store_next(expected_value, f"SWAP{swap_pos} result") | ||
post[contract] = Account(storage=storage) | ||
# Run the state test. | ||
state_test(env=env, pre=pre, post=post, tx=tx) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"swap_opcode", | ||
[getattr(Op, f"SWAP{i}") for i in range(1, 17)], | ||
ids=lambda op: str(op), | ||
) | ||
@pytest.mark.valid_from("Frontier") | ||
def test_stack_underflow( | ||
state_test: StateTestFiller, | ||
fork: Fork, | ||
pre: Alloc, | ||
swap_opcode: Op, | ||
): | ||
""" | ||
A test to ensure that the stack underflow when there are not enough | ||
elements for the `SWAP*` opcode to operate. | ||
|
||
For each SWAPn operation, we push exactly (n-1) elements to cause an | ||
underflow when trying to swap with the nth element. | ||
""" | ||
env = Environment() | ||
|
||
# Calculate which position we're swapping with (1-based index) | ||
swap_pos = swap_opcode.int() - 0x90 + 1 | ||
|
||
# Push exactly (n-1) elements for SWAPn to cause underflow | ||
contract_code = Bytecode() | ||
for i in range(swap_pos - 1): | ||
contract_code += Op.PUSH1(i % 256) | ||
|
||
# Attempt to perform the SWAP operation | ||
contract_code += swap_opcode | ||
|
||
# Store the top of the stack in storage slot 0 | ||
contract_code += Op.PUSH1(0) + Op.SSTORE | ||
bomanaps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Deploy the contract with the generated bytecode. | ||
contract = pre.deploy_contract(contract_code) | ||
|
||
# Create a transaction to execute the contract. | ||
tx = Transaction( | ||
sender=pre.fund_eoa(), | ||
to=contract, | ||
gas_limit=500_000, | ||
protected=False if fork in [Frontier, Homestead] else True, | ||
) | ||
|
||
# Define the expected post-state. | ||
post = {} | ||
storage = Storage() | ||
storage.store_next(0, f"SWAP{swap_pos} failed due to stack underflow") | ||
post[contract] = Account(storage=storage) | ||
|
||
# Run the state test. | ||
state_test(env=env, pre=pre, post=post, tx=tx) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add now path to original fillers. as ported marker.
rebase as converted-ethereum-tests.txt now is removed
remove the corresponding test filler .json from static_test folder in execution-spec-repo