-
Notifications
You must be signed in to change notification settings - Fork 90
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
β¨ feat(tests): PUSH opcode tests added #1018
Open
felix314159
wants to merge
2
commits into
ethereum:main
Choose a base branch
from
felix314159:main
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.
+94
β0
Open
Changes from 1 commit
Commits
Show all changes
2 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 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,124 @@ | ||
""" | ||
abstract: Test PUSH | ||
Test the PUSH opcodes. | ||
|
||
""" | ||
|
||
import pytest | ||
|
||
from ethereum_test_forks import Frontier, Homestead | ||
from ethereum_test_tools import Account, Alloc, Environment | ||
from ethereum_test_tools import Opcodes as Op | ||
from ethereum_test_tools import StateTestFiller, Storage, Transaction | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"push_opcode", | ||
[ | ||
Op.PUSH0, | ||
Op.PUSH1, | ||
Op.PUSH2, | ||
Op.PUSH3, | ||
Op.PUSH4, | ||
Op.PUSH5, | ||
Op.PUSH6, | ||
Op.PUSH7, | ||
Op.PUSH8, | ||
Op.PUSH9, | ||
Op.PUSH10, | ||
Op.PUSH11, | ||
Op.PUSH12, | ||
Op.PUSH13, | ||
Op.PUSH14, | ||
Op.PUSH15, | ||
Op.PUSH16, | ||
Op.PUSH17, | ||
Op.PUSH18, | ||
Op.PUSH19, | ||
Op.PUSH20, | ||
Op.PUSH21, | ||
Op.PUSH22, | ||
Op.PUSH23, | ||
Op.PUSH24, | ||
Op.PUSH25, | ||
Op.PUSH26, | ||
Op.PUSH27, | ||
Op.PUSH28, | ||
Op.PUSH29, | ||
Op.PUSH30, | ||
Op.PUSH31, | ||
Op.PUSH32, | ||
], | ||
ids=lambda op: str(op), | ||
) | ||
@pytest.mark.with_all_evm_code_types | ||
def test_push( | ||
state_test: StateTestFiller, | ||
fork: str, | ||
push_opcode: Op, | ||
pre: Alloc, | ||
): | ||
""" | ||
Test the PUSH0-PUSH32 opcodes. | ||
""" | ||
|
||
env = Environment() | ||
sender = pre.fund_eoa() | ||
post = {} | ||
|
||
# 0x5F is PUSH0, so from the current opcode's int we can derive which iteration we are in | ||
# PUSH0 = iteration 0 | ||
# PUSH1 = iteration 1 | ||
# ... | ||
# PUSH32 = iteration 32 | ||
current_iter = ( | ||
push_opcode.int() - 0x5F | ||
) # also equal to the amount of bytes required to store value chosen in current iteration | ||
|
||
account_code = b"".join( | ||
# first pushX (X changes each iteration) a value, then push1 key=1, then sstore | ||
[ | ||
bytes([push_opcode.int()]), | ||
values[current_iter].to_bytes(current_iter, byteorder="big"), | ||
bytes([Op.PUSH1.int()]), | ||
bytes([0x01]), | ||
bytes([Op.SSTORE.int()]), | ||
] | ||
) | ||
|
||
account = pre.deploy_contract(account_code) | ||
|
||
tx = Transaction( | ||
ty=0x0, | ||
nonce=0, | ||
to=account, | ||
gas_limit=500000, | ||
gas_price=10, | ||
protected=False if fork in [Frontier, Homestead] else True, | ||
data="", | ||
sender=sender, | ||
) | ||
|
||
s: Storage.StorageDictType = {1: values[current_iter]} | ||
|
||
post[account] = Account(storage=s) | ||
|
||
state_test(env=env, pre=pre, post=post, tx=tx) | ||
|
||
|
||
def get_largest_value_that_requires_at_least_x_bytes(bytes_required): | ||
"""Returns the largest value that can be stored with the specified amount of bytes | ||
|
||
Args: | ||
bytes_required (int): Amount of bytes that are available | ||
|
||
Returns: | ||
int: Largest possible value that can be stored with specified amount of bytes | ||
""" | ||
if bytes_required > 0: | ||
return (2 ** (8 * bytes_required)) - 1 | ||
|
||
|
||
# in global scope so that it doesn't get re-created every iteration | ||
# generate list of values that require x bytes, x+1 bytes, etc. to store | ||
values = [0] + [get_largest_value_that_requires_at_least_x_bytes(i) for i in range(1, 33)] | ||
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.
I think what you need here is a module-scoped fixture: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session
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.
Thanks for the feedback, I pushed an improved version. Let me know if this still does not do what I originally intended it to do