-
Notifications
You must be signed in to change notification settings - Fork 70
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(fw): Gas test generator #719
Open
marioevz
wants to merge
4
commits into
main
Choose a base branch
from
gas-test-generator
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
4 commits
Select commit
Hold shift + click to select a range
e3d7ba2
feat(fw): Add gas test generator
marioevz e944a8b
refactor(tests): EIP-5656: Use gas test generator
marioevz b1430c0
refactor(tools): rename test generator
marioevz 5f35889
new(tests): EIP-5656 add eof marker to memory expansion test
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 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
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,10 @@ | ||
""" | ||
Ethereum test generators package. | ||
""" | ||
|
||
from .state import GasTestType, exact_gas_test | ||
|
||
__all__ = [ | ||
"GasTestType", | ||
"exact_gas_test", | ||
] |
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,163 @@ | ||
""" | ||
Includes state test generators to use in Ethereum tests. | ||
""" | ||
from enum import Enum | ||
from typing import Any, Callable, List | ||
|
||
import pytest | ||
|
||
from ethereum_test_base_types import Account | ||
from ethereum_test_specs import StateTestFiller | ||
from ethereum_test_types import Alloc, Environment, Transaction, eip_2028_transaction_data_cost | ||
from ethereum_test_vm import Bytecode | ||
from ethereum_test_vm import Opcodes as Op | ||
|
||
Decorator = Callable[[Callable[..., Any]], Callable[..., Any]] | ||
|
||
|
||
class GasTestType(str, Enum): | ||
""" | ||
Enum to list the types of gas tests that can be generated. | ||
""" | ||
|
||
EXACT_GAS = "exact_gas" | ||
OOG = "oog" | ||
|
||
|
||
def exact_gas_test( | ||
*, | ||
gas_test_types: List[GasTestType] | GasTestType = list(GasTestType), | ||
with_data: bool = False, | ||
) -> Decorator: | ||
""" | ||
Used to generate a parametrized test that checks that specified gas is | ||
exactly the same as the gas used by the EVM. | ||
|
||
Automatically generates an exact-gas test, where the transaction is sent | ||
with the exact amount of gas required, and an out-of-gas test, where the | ||
transaction is sent with one less gas than required. | ||
|
||
Body of the decorated test is ignored. | ||
|
||
Required fixtures: | ||
pre: Alloc - Automatically provided by filler. | ||
state_test: StateTestFiller - Automatically provided by filler. | ||
exact_gas_cost: int - Exact gas cost required to execute the bytecode. | ||
bytecode: Bytecode, the bytecode to be tested. | ||
|
||
Optional fixtures: | ||
data: bytes - if `with_data` is True, is the transaction calldata. | ||
|
||
Args: | ||
gas_test_types: List[GasTestType] | GasTestType = list(GasTestType) - List of gas test types | ||
to generate. | ||
with_data: bool = False - If True, a `data` fixture needs to be defined and will be | ||
included. | ||
""" | ||
if type(gas_test_types) is GasTestType: | ||
gas_test_types = [gas_test_types] | ||
|
||
assert type(gas_test_types) is list | ||
|
||
def generator( | ||
pre: Alloc, | ||
state_test: StateTestFiller, | ||
exact_gas_cost: int, | ||
gas_test_type: GasTestType, | ||
bytecode: Bytecode, | ||
data: bytes = b"", | ||
env: Environment = Environment(), | ||
): | ||
""" | ||
Generate a test case that can be further parametrized to verify gas comsumption of a | ||
specific EVM bytecode. | ||
""" | ||
test_code = Op.SSTORE(0xBA5E, 1) + bytecode + Op.STOP() | ||
test_code_address = pre.deploy_contract(code=test_code) | ||
total_gas = ( | ||
# Intrinsic tx cost | ||
21_000 | ||
+ 2_100 # Cold account access cost | ||
+ eip_2028_transaction_data_cost(data) | ||
+ 20_000 # Op.SSTORE | ||
+ 3 # Op.PUSH2 | ||
+ 3 # Op.PUSH1(1) | ||
+ exact_gas_cost | ||
) | ||
tx = Transaction( | ||
sender=pre.fund_eoa(), | ||
to=test_code_address, | ||
data=data, | ||
gas_limit=total_gas if gas_test_type == GasTestType.EXACT_GAS else total_gas - 1, | ||
) | ||
post = { | ||
test_code_address: Account( | ||
storage={0xBA5E: 1} if gas_test_type == GasTestType.EXACT_GAS else {} | ||
) | ||
} | ||
state_test( | ||
env=env, | ||
pre=pre, | ||
post=post, | ||
tx=tx, | ||
) | ||
|
||
if with_data: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this still works if we move the def decorator(func: Callable[..., Any], with_data: bool) -> Callable[..., Any]:
@pytest.mark.parametrize(
"gas_test_type",
gas_test_types,
ids=lambda x: x.value,
)
def generated_gas_test(
pre: Alloc,
state_test: StateTestFiller,
exact_gas_cost: int,
gas_test_type: GasTestType,
bytecode: Bytecode,
data: bytes = None,
):
if with_data:
generator(
pre=pre,
state_test=state_test,
exact_gas_cost=exact_gas_cost,
gas_test_type=gas_test_type,
bytecode=bytecode,
data=data,
)
else:
generator(
pre=pre,
state_test=state_test,
exact_gas_cost=exact_gas_cost,
gas_test_type=gas_test_type,
bytecode=bytecode,
)
generated_gas_test.__name__ = func.__name__
generated_gas_test.__doc__ = func.__doc__
return generated_gas_test |
||
|
||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
@pytest.mark.parametrize( | ||
"gas_test_type", | ||
gas_test_types, | ||
ids=lambda x: x.value, | ||
) | ||
def generated_gas_test( | ||
pre: Alloc, | ||
state_test: StateTestFiller, | ||
exact_gas_cost: int, | ||
gas_test_type: GasTestType, | ||
bytecode: Bytecode, | ||
data: bytes, | ||
): | ||
generator( | ||
pre=pre, | ||
state_test=state_test, | ||
exact_gas_cost=exact_gas_cost, | ||
gas_test_type=gas_test_type, | ||
bytecode=bytecode, | ||
data=data, | ||
) | ||
|
||
generated_gas_test.__name__ = func.__name__ | ||
generated_gas_test.__doc__ = func.__doc__ | ||
|
||
return generated_gas_test | ||
|
||
else: | ||
|
||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
@pytest.mark.parametrize( | ||
"gas_test_type", | ||
gas_test_types, | ||
ids=lambda x: x.value, | ||
) | ||
def generated_gas_test( | ||
pre: Alloc, | ||
state_test: StateTestFiller, | ||
exact_gas_cost: int, | ||
gas_test_type: GasTestType, | ||
bytecode: Bytecode, | ||
): | ||
generator( | ||
pre=pre, | ||
state_test=state_test, | ||
exact_gas_cost=exact_gas_cost, | ||
gas_test_type=gas_test_type, | ||
bytecode=bytecode, | ||
) | ||
|
||
generated_gas_test.__name__ = func.__name__ | ||
generated_gas_test.__doc__ = func.__doc__ | ||
|
||
return generated_gas_test | ||
|
||
return decorator |
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.
Should we assert all the required fixtures?