Skip to content

Commit e92ba45

Browse files
feat(benchmark): add benchmark_test test type (ethereum#1945)
* feat: wrap blockchain test for benchmark * feat: wrap state test for benchmark * feat(benchmark): add code generator to generate transaction * fix: resolve typing issue * refactor: update benchmark code generator and test wrapper * fix: udpate example changes * refactor: resolve typing and update func interface * refactor: remove benchmark state test wrapper * fix: pydantic model validation for benchmark manager * refactor synatx and parameter * refactor: remove benchmark manager feature * refactor: update logic and add benchmark tests * refactor: enforce single property requirement in blockchain test generation * refactor: update Bytecode serialization schema to use format_ser_schema * refactor: update import paths * refactor: update serialization schema * refactor: remove unused parameters * doc: add changelog entry * fix typo
1 parent 42fe3c2 commit e92ba45

File tree

3 files changed

+23
-40
lines changed

3 files changed

+23
-40
lines changed

benchmark/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
from ethereum_test_forks import Fork
8+
79
DEFAULT_BENCHMARK_FORK = "Prague"
810

911

@@ -59,3 +61,9 @@ def pytest_collection_modifyitems(config, items):
5961

6062
for i in reversed(items_for_removal):
6163
items.pop(i)
64+
65+
66+
@pytest.fixture
67+
def tx_gas_limit_cap(fork: Fork, gas_benchmark_value: int) -> int:
68+
"""Return the transaction gas limit cap."""
69+
return fork.transaction_gas_limit_cap() or gas_benchmark_value

benchmark/test_worst_blocks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
import pytest
1111

12+
from ethereum_test_base_types import Account
1213
from ethereum_test_forks import Fork
1314
from ethereum_test_tools import (
1415
AccessList,
15-
Account,
1616
Address,
1717
Alloc,
1818
Block,
1919
BlockchainTestFiller,
20+
Environment,
2021
Hash,
2122
StateTestFiller,
2223
Transaction,
@@ -112,11 +113,13 @@ def ether_transfer_case(
112113
def test_block_full_of_ether_transfers(
113114
blockchain_test: BlockchainTestFiller,
114115
pre: Alloc,
116+
env: Environment,
115117
case_id: str,
116118
ether_transfer_case,
117119
iteration_count: int,
118120
transfer_amount: int,
119121
intrinsic_cost: int,
122+
gas_benchmark_value: int,
120123
):
121124
"""
122125
Single test for ether transfer scenarios.
@@ -153,10 +156,10 @@ def test_block_full_of_ether_transfers(
153156
)
154157

155158
blockchain_test(
159+
genesis_environment=env,
156160
pre=pre,
157161
post=post_state,
158162
blocks=[Block(txs=txs)],
159-
exclude_full_post_state_in_output=True,
160163
expected_benchmark_gas_used=iteration_count * intrinsic_cost,
161164
)
162165

benchmark/test_worst_compute.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
from py_ecc.bn128 import G1, G2, multiply
1616

1717
from ethereum_test_base_types.base_types import Bytes
18+
from ethereum_test_benchmark.benchmark_code_generator import JumpLoopGenerator
1819
from ethereum_test_forks import Fork
1920
from ethereum_test_tools import (
2021
Address,
2122
Alloc,
23+
BenchmarkTestFiller,
2224
Block,
2325
BlockchainTestFiller,
2426
Bytecode,
@@ -1842,30 +1844,14 @@ def test_worst_jumpis(
18421844

18431845
@pytest.mark.slow
18441846
def test_worst_jumpdests(
1845-
state_test: StateTestFiller,
1847+
benchmark_test: BenchmarkTestFiller,
18461848
pre: Alloc,
1847-
fork: Fork,
1848-
gas_benchmark_value: int,
18491849
):
18501850
"""Test running a JUMPDEST-intensive contract."""
1851-
max_code_size = fork.max_code_size()
1852-
1853-
# Create and deploy a contract with many JUMPDESTs
1854-
code_suffix = Op.JUMP(Op.PUSH0)
1855-
code_body = Op.JUMPDEST * (max_code_size - len(code_suffix))
1856-
code = code_body + code_suffix
1857-
jumpdests_address = pre.deploy_contract(code=code)
1858-
1859-
tx = Transaction(
1860-
to=jumpdests_address,
1861-
gas_limit=gas_benchmark_value,
1862-
sender=pre.fund_eoa(),
1863-
)
1864-
1865-
state_test(
1851+
benchmark_test(
18661852
pre=pre,
18671853
post={},
1868-
tx=tx,
1854+
code_generator=JumpLoopGenerator(attack_block=Op.JUMPDEST),
18691855
)
18701856

18711857

@@ -2764,31 +2750,17 @@ def test_worst_calldataload(
27642750
],
27652751
)
27662752
def test_worst_swap(
2767-
state_test: StateTestFiller,
2753+
benchmark_test: BenchmarkTestFiller,
27682754
pre: Alloc,
2769-
fork: Fork,
27702755
opcode: Opcode,
2771-
gas_benchmark_value: int,
27722756
):
27732757
"""Test running a block with as many SWAP as possible."""
2774-
max_code_size = fork.max_code_size()
2775-
2776-
code_prefix = Op.JUMPDEST + Op.PUSH0 * opcode.min_stack_height
2777-
code_suffix = Op.PUSH0 + Op.JUMP
2778-
opcode_sequence = opcode * (max_code_size - len(code_prefix) - len(code_suffix))
2779-
code = code_prefix + opcode_sequence + code_suffix
2780-
assert len(code) <= max_code_size
2781-
2782-
tx = Transaction(
2783-
to=pre.deploy_contract(code=code),
2784-
gas_limit=gas_benchmark_value,
2785-
sender=pre.fund_eoa(),
2786-
)
2787-
2788-
state_test(
2758+
benchmark_test(
27892759
pre=pre,
27902760
post={},
2791-
tx=tx,
2761+
code_generator=JumpLoopGenerator(
2762+
attack_block=opcode, setup=Op.PUSH0 * opcode.min_stack_height
2763+
),
27922764
)
27932765

27942766

0 commit comments

Comments
 (0)