Skip to content

Commit c720a28

Browse files
refactor: apply suggested changes
1 parent ac154c4 commit c720a28

File tree

7 files changed

+58
-61
lines changed

7 files changed

+58
-61
lines changed

packages/testing/src/execution_testing/specs/benchmark.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class BenchmarkCodeGenerator(ABC):
5353
setup: Bytecode = field(default_factory=Bytecode)
5454
cleanup: Bytecode = field(default_factory=Bytecode)
5555
tx_kwargs: Dict[str, Any] = field(default_factory=dict)
56+
code_padding_opcode: Op | None = None
5657
_contract_address: Address | None = None
5758

5859
@abstractmethod
@@ -105,7 +106,8 @@ def generate_repeated_code(
105106
code = setup + Op.JUMPDEST + repeated_code * max_iterations + cleanup
106107
code += Op.JUMP(len(setup)) if len(setup) > 0 else Op.PUSH0 + Op.JUMP
107108
# Pad the code to the maximum code size.
108-
code += Op.STOP * (max_code_size - len(code))
109+
if self.code_padding_opcode is not None:
110+
code += self.code_padding_opcode * (max_code_size - len(code))
109111
self._validate_code_size(code, fork)
110112

111113
return code

tests/benchmark/compute/instruction/test_account_query.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import math
7-
from typing import Dict
7+
from typing import Any
88

99
import pytest
1010
from execution_testing import (
@@ -25,7 +25,6 @@
2525
While,
2626
compute_create2_address,
2727
)
28-
from execution_testing.base_types.conversions import NumberConvertible
2928

3029
from tests.benchmark.compute.helpers import (
3130
XOR_TABLE,
@@ -89,7 +88,9 @@ def test_codecopy(
8988

9089
benchmark_test(
9190
code_generator=JumpLoopGenerator(
92-
setup=setup, attack_block=attack_block
91+
setup=setup,
92+
attack_block=attack_block,
93+
code_padding_opcode=Op.STOP,
9394
)
9495
)
9596

@@ -348,7 +349,7 @@ def test_extcodecopy_warm(
348349
],
349350
)
350351
@pytest.mark.parametrize(
351-
"empty_account",
352+
"empty_code",
352353
[
353354
True,
354355
False,
@@ -372,7 +373,7 @@ def test_ext_account_query_warm(
372373
benchmark_test: BenchmarkTestFiller,
373374
pre: Alloc,
374375
opcode: Op,
375-
empty_account: bool,
376+
empty_code: bool,
376377
initial_balance: bool,
377378
initial_storage: bool,
378379
) -> None:
@@ -381,32 +382,24 @@ def test_ext_account_query_warm(
381382
for an account.
382383
"""
383384
# Setup
384-
target_addr = Address()
385-
if not initial_balance and not initial_storage:
386-
target_addr = pre.empty_account()
387-
elif initial_balance or initial_storage:
388-
target_addr = pre.fund_eoa(
389-
storage={0: 0x1337} if initial_storage else {0: 0}
390-
)
391-
392385
post = {}
393-
if not empty_account:
394-
code = Op.STOP + Op.JUMPDEST * 100
395386

396-
storage: Dict[NumberConvertible, NumberConvertible] = (
397-
{0: 0x1337} if initial_storage else {0: 0}
398-
)
399-
target_addr = pre.deploy_contract(
400-
balance=initial_balance,
401-
code=code,
402-
storage=storage,
403-
)
404-
405-
post[target_addr] = Account(
406-
balance=initial_balance,
407-
code=code,
408-
storage=storage,
409-
)
387+
if not initial_balance and not initial_storage and empty_code:
388+
target_addr = pre.empty_account()
389+
else:
390+
kwargs: dict[str, Any] = {}
391+
if initial_balance:
392+
kwargs["balance"] = 100
393+
if initial_storage:
394+
kwargs["storage"] = {0: 0x1337}
395+
396+
if empty_code:
397+
target_addr = pre.fund_eoa(**kwargs)
398+
else:
399+
code = Op.STOP + Op.JUMPDEST * 100
400+
kwargs["code"] = code
401+
target_addr = pre.deploy_contract(**kwargs)
402+
post[target_addr] = Account(**kwargs)
410403

411404
benchmark_test(
412405
post=post,

tests/benchmark/compute/instruction/test_block_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_block_context_ops(
4343
1,
4444
256,
4545
257,
46-
None,
46+
pytest.param(None, id="random"),
4747
],
4848
)
4949
def test_blockhash(

tests/benchmark/compute/instruction/test_keccak.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_keccak_max_permutations(
6161

6262
benchmark_test(
6363
code_generator=JumpLoopGenerator(
64-
setup=Op.PUSH20(optimal_input_length),
64+
setup=Op.PUSH20[optimal_input_length],
6565
attack_block=Op.POP(Op.SHA3(Op.PUSH0, Op.DUP1)),
6666
),
6767
)

tests/benchmark/compute/instruction/test_storage.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,55 @@
2323
# SLOAD, SSTORE, TLOAD, TSTORE
2424

2525

26-
# `key_mut` indicates the key isn't fixed.
27-
@pytest.mark.parametrize("key_mut", [True, False])
28-
# `val_mut` indicates that at the end of each big-loop, the value of the target
29-
# key changes.
30-
@pytest.mark.parametrize("val_mut", [True, False])
26+
@pytest.mark.parametrize("fixed_key", [True, False])
27+
@pytest.mark.parametrize("fixed_value", [True, False])
3128
def test_tload(
3229
benchmark_test: BenchmarkTestFiller,
33-
key_mut: bool,
34-
val_mut: bool,
30+
fixed_key: bool,
31+
fixed_value: bool,
3532
) -> None:
3633
"""Benchmark TLOAD instruction."""
3734
setup = Bytecode()
38-
if key_mut and val_mut:
35+
if not fixed_key and not fixed_value:
3936
setup = Op.GAS + Op.TSTORE(Op.DUP2, Op.GAS)
4037
attack_block = Op.TLOAD(Op.DUP1)
41-
if key_mut and not val_mut:
38+
if not fixed_key and fixed_value:
4239
attack_block = Op.TLOAD(Op.GAS)
43-
if not key_mut and val_mut:
44-
setup = Op.TSTORE(Op.CALLVALUE, Op.GAS)
45-
attack_block = Op.TLOAD(Op.CALLVALUE)
46-
if not key_mut and not val_mut:
47-
attack_block = Op.TLOAD(Op.CALLVALUE)
40+
if fixed_key and not fixed_value:
41+
setup = Op.TSTORE(Op.CALLDATASIZE, Op.GAS)
42+
attack_block = Op.TLOAD(Op.CALLDATASIZE)
43+
if fixed_key and fixed_value:
44+
attack_block = Op.TLOAD(Op.CALLDATASIZE)
4845

49-
tx_value = 42 if not key_mut and val_mut else 0
46+
tx_data = b"42" if fixed_key and not fixed_value else 0
5047

5148
benchmark_test(
5249
code_generator=ExtCallGenerator(
5350
setup=setup,
5451
attack_block=attack_block,
55-
contract_balance=tx_value,
52+
tx_kwargs={"data": tx_data},
5653
),
5754
)
5855

5956

60-
@pytest.mark.parametrize("key_mut", [True, False])
61-
@pytest.mark.parametrize("dense_val_mut", [True, False])
57+
@pytest.mark.parametrize("fixed_key", [True, False])
58+
@pytest.mark.parametrize("fixed_value", [True, False])
6259
def test_tstore(
6360
benchmark_test: BenchmarkTestFiller,
64-
key_mut: bool,
65-
dense_val_mut: bool,
61+
fixed_key: bool,
62+
fixed_value: bool,
6663
) -> None:
6764
"""Benchmark TSTORE instruction."""
6865
init_key = 42
6966
setup = Op.PUSH1(init_key)
7067

71-
# If `dense_val_mut` is set, we use GAS as a cheap way of always
72-
# storing a different value than
73-
# the previous one.
74-
attack_block = Op.TSTORE(Op.DUP2, Op.GAS if dense_val_mut else Op.DUP1)
68+
# If fixed_value is False, we use GAS as a cheap way of always
69+
# storing a different value than the previous one.
70+
attack_block = Op.TSTORE(Op.DUP2, Op.GAS if not fixed_value else Op.DUP1)
7571

76-
# If `key_mut` is True, we mutate the key on every iteration of the
72+
# If fixed_key is False, we mutate the key on every iteration of the
7773
# big loop.
78-
cleanup = Op.POP + Op.GAS if key_mut else Bytecode()
74+
cleanup = Op.POP + Op.GAS if not fixed_key else Bytecode()
7975

8076
benchmark_test(
8177
code_generator=JumpLoopGenerator(

tests/benchmark/compute/instruction/test_system.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ def test_return_revert(
453453
)
454454
benchmark_test(
455455
code_generator=ExtCallGenerator(
456-
setup=mem_preparation, attack_block=opcode(size=return_size)
456+
setup=mem_preparation,
457+
attack_block=opcode(size=return_size),
458+
code_padding_opcode=Op.INVALID,
457459
),
458460
)
459461

tests/benchmark/compute/scenario/test_transaction_types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ def get_single_receiver_list(
8383
yield receiver
8484

8585

86+
@pytest.fixture
8687
def ether_transfer_case(
8788
case_id: str, pre: Alloc, balance: int
8889
) -> Tuple[Generator[Address, None, None], Generator[Address, None, None]]:
89-
"""Generate the test parameters based on the case ID."""
90+
"""Generate sender and receiver generators based on the test case."""
9091
if case_id == "a_to_a":
9192
"""Sending to self."""
9293
senders = get_single_sender_list(pre)
@@ -137,6 +138,9 @@ def test_block_full_of_ether_transfers(
137138
iteration_count: int,
138139
transfer_amount: int,
139140
intrinsic_cost: int,
141+
ether_transfer_case: Tuple[
142+
Generator[Address, None, None], Generator[Address, None, None]
143+
],
140144
) -> None:
141145
"""
142146
Single test for ether transfer scenarios.
@@ -148,7 +152,7 @@ def test_block_full_of_ether_transfers(
148152
- a_to_diff_acc: one sender → multiple receivers
149153
- diff_acc_to_diff_acc: multiple senders → multiple receivers
150154
"""
151-
senders, receivers = ether_transfer_case(case_id, pre, balance)
155+
senders, receivers = ether_transfer_case
152156

153157
# Create a single block with all transactions
154158
txs = []

0 commit comments

Comments
 (0)