Skip to content

Commit beb239d

Browse files
fix: update linting comment length
1 parent c8fc5fa commit beb239d

File tree

6 files changed

+100
-50
lines changed

6 files changed

+100
-50
lines changed

src/ethereum_test_benchmark/benchmark_code_generator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class JumpLoopGenerator(BenchmarkCodeGenerator):
1717
def deploy_contracts(self, pre: Alloc, fork: Fork) -> Address:
1818
"""Deploy the looping contract."""
1919
# Benchmark Test Structure:
20-
# setup + JUMPDEST + attack + attack + ... + cleanup + JUMP(setup_length)
20+
# setup + JUMPDEST +
21+
# attack + attack + ... + attack +
22+
# cleanup + JUMP(setup_length)
2123
code = self.generate_repeated_code(self.attack_block, self.setup, self.cleanup, fork)
2224
self._contract_address = pre.deploy_contract(code=code)
2325
return self._contract_address

tests/benchmark/test_worst_blocks.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ def calldata_generator(
205205
# Token accounting:
206206
# tokens_in_calldata = zero_bytes + 4 * non_zero_bytes
207207
#
208-
# So we calculate how many bytes we can fit into calldata based on available gas.
208+
# So we calculate how many bytes we can fit into calldata based on
209+
# available gas.
209210
max_tokens_in_calldata = gas_amount // total_cost_floor_per_token
210211
num_of_bytes = max_tokens_in_calldata if zero_byte else max_tokens_in_calldata // 4
211212
byte_data = b"\x00" if zero_byte else b"\xff"
@@ -268,7 +269,10 @@ def test_block_full_access_list_and_data(
268269
gas_benchmark_value: int,
269270
tx_gas_limit_cap: int,
270271
):
271-
"""Test a block with access lists (60% gas) and calldata (40% gas) using random mixed bytes."""
272+
"""
273+
Test a block with access lists (60% gas) and calldata (40% gas) using
274+
random mixed bytes.
275+
"""
272276
iteration_count = math.ceil(gas_benchmark_value / tx_gas_limit_cap)
273277

274278
gas_remaining = gas_benchmark_value
@@ -305,8 +309,10 @@ def test_block_full_access_list_and_data(
305309
)
306310
]
307311

308-
# Calculate calldata with 29% of gas for zero bytes and 71% for non-zero bytes
309-
# Token accounting: tokens_in_calldata = zero_bytes + 4 * non_zero_bytes
312+
# Calculate calldata with 29% of gas for zero bytes and 71% for
313+
# non-zero bytes
314+
# Token accounting: tokens_in_calldata = zero_bytes + 4 *
315+
# non_zero_bytes
310316
# We want to split the gas budget:
311317
# - 29% of gas_for_calldata for zero bytes
312318
# - 71% of gas_for_calldata for non-zero bytes

tests/benchmark/test_worst_bytecode.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636
def deploy_initcode_template(pre: Alloc, fork: Fork) -> tuple[Address, Bytecode]:
3737
"""Deploy the initcode template contract."""
38-
# The initcode will take its address as a starting point to the input to the keccak
39-
# hash function.
40-
# It will reuse the output of the hash function in a loop to create a large amount of
41-
# seemingly random code, until it reaches the maximum contract size.
38+
# The initcode will take its address as a starting point to the input to
39+
# the keccak hash function.
40+
# It will reuse the output of the hash function in a loop to create a
41+
# large amount of random code, until it reaches the maximum code size.
4242
initcode = (
4343
Op.MSTORE(0, Op.ADDRESS)
4444
+ While(
@@ -65,8 +65,8 @@ def deploy_initcode_template(pre: Alloc, fork: Fork) -> tuple[Address, Bytecode]
6565

6666
def deploy_factory_contract(pre: Alloc, fork: Fork, initcode_address: Address) -> Address:
6767
"""Deploy the factory contract."""
68-
# The factory contract will simply use the initcode that is already deployed,
69-
# and create a new contract and return its address if successful.
68+
# The factory contract will use the initcode that is already
69+
# deployed, and create a new contract and return its address if successful.
7070
factory_code = (
7171
Op.EXTCODECOPY(
7272
address=initcode_address,
@@ -91,8 +91,8 @@ def deploy_factory_contract(pre: Alloc, fork: Fork, initcode_address: Address) -
9191

9292
def deploy_factory_caller_contract(pre: Alloc, fork: Fork, factory_address: Address) -> Address:
9393
"""Deploy the factory caller contract."""
94-
# The factory caller will call the factory contract N times, creating N new contracts.
95-
# Calldata should contain the N value.
94+
# The factory caller will call the factory contract N times, creating N
95+
# new contracts. Calldata should contain the N value.
9696
factory_caller_code = Op.CALLDATALOAD(0) + While(
9797
body=Op.POP(Op.CALL(address=factory_address)),
9898
condition=Op.PUSH1(1) + Op.SWAP1 + Op.SUB + Op.DUP1 + Op.ISZERO + Op.ISZERO,
@@ -114,7 +114,10 @@ def deploy_attack_contract(
114114
+ Op.MSTORE(64, initcode.keccak256())
115115
)
116116

117-
# setup_cost: G_VERY_LOW * 9 (PUSH) + G_VERY_LOW * 3 (MSTORE) + G_VERY_LOW (CALLDATACOPY)
117+
# setup_cost:
118+
# G_VERY_LOW * 9 (PUSH) +
119+
# G_VERY_LOW * 3 (MSTORE) +
120+
# G_VERY_LOW (CALLDATACOPY)
118121

119122
# Attack call
120123
attack_call = Bytecode()
@@ -131,7 +134,7 @@ def deploy_attack_contract(
131134

132135
# loop_cost = (
133136
# gas_costs.G_KECCAK_256 KECCAK static cost
134-
# + math.ceil(85 / 32) * gas_costs.G_KECCAK_256_WORD KECCAK dynamic cost for CREATE2
137+
# + math.ceil(85 / 32) * gas_costs.G_KECCAK_256_WORD
135138
# + gas_costs.G_VERY_LOW * ~MSTOREs+ADDs
136139
# + gas_costs.G_COLD_ACCOUNT_ACCESS Opcode cost
137140
# + 30 ~Gluing opcodes
@@ -162,24 +165,28 @@ def test_worst_bytecode_single_opcode(
162165
tx_gas_limit_cap: int,
163166
):
164167
"""
165-
Test a block execution where a single opcode execution maxes out the gas limit,
168+
Test a block execution where a single opcode execution maxes out the
169+
gas limit,
166170
and the opcodes access a huge amount of contract code.
167171
168-
We first use a single block to deploy a factory contract that will be used to deploy
172+
We first use a single block to deploy a factory contract that will be
173+
used to deploy
169174
a large number of contracts.
170175
171176
This is done to avoid having a big pre-allocation size for the test.
172177
173-
The test is performed in the last block of the test, and the entire block gas limit is
178+
The test is performed in the last block of the test, and the entire
179+
block gas limit is
174180
consumed by repeated opcode executions.
175181
"""
176182
iteration_count = gas_benchmark_value // tx_gas_limit_cap
177183

178184
# The attack gas limit is the gas limit which the target tx will use
179-
# The test will scale the block gas limit to setup the contracts accordingly to be
180-
# able to pay for the contract deposit. This has to take into account the 200 gas per byte,
181-
# but also the quadratic memory expansion costs which have to be paid each time the
182-
# memory is being setup
185+
# The test will scale the block gas limit to setup the contracts
186+
# accordingly to be able to pay for the contract deposit.
187+
# This has to take into account the 200 gas per byte,
188+
# but also the quadratic memory expansion costs which have to be paid
189+
# each time the memory is being setup
183190
max_contract_size = fork.max_code_size()
184191

185192
gas_costs = fork.gas_costs()
@@ -200,7 +207,8 @@ def test_worst_bytecode_single_opcode(
200207
for _ in range(iteration_count):
201208
gas_available = min(tx_gas_limit_cap, gas_remaining)
202209
total_contracts += (
203-
# Base available gas = GAS_LIMIT - intrinsic - (out of loop MSTOREs)
210+
# Base available gas =
211+
# GAS_LIMIT - intrinsic - (out of loop MSTOREs)
204212
gas_available - intrinsic_gas_cost_calc() - setup_cost
205213
) // loop_cost
206214
gas_remaining -= gas_available
@@ -213,7 +221,8 @@ def test_worst_bytecode_single_opcode(
213221
# Deployment Phase - Deploy N contracts
214222

215223
# Calculate the absolute minimum gas costs to deploy the contract
216-
# This does not take into account setting up the actual memory (using KECCAK256 and XOR)
224+
# This does not take into account setting up the actual memory
225+
# (using KECCAK256 and XOR)
217226
# so the actual costs of deploying the contract is higher
218227
memory_expansion_gas_calculator = fork.memory_expansion_gas_calculator()
219228
memory_gas_minimum = memory_expansion_gas_calculator(new_bytes=len(bytes(max_contract_size)))

tests/benchmark/test_worst_compute.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ def test_worst_returndatasize_zero(
212212
benchmark_test: BenchmarkTestFiller,
213213
pre: Alloc,
214214
):
215-
"""Test running a block with as many RETURNDATASIZE opcodes as possible with a zero buffer."""
215+
"""
216+
Test running a block with as many RETURNDATASIZE opcodes as possible with
217+
a zero buffer.
218+
"""
216219
benchmark_test(
217220
pre=pre,
218221
post={},
@@ -262,8 +265,10 @@ def test_worst_keccak(
262265
gsc = fork.gas_costs()
263266
mem_exp_gas_calculator = fork.memory_expansion_gas_calculator()
264267

265-
# Discover the optimal input size to maximize keccak-permutations, not keccak calls.
266-
# The complication of the discovery arises from the non-linear gas cost of memory expansion.
268+
# Discover the optimal input size to maximize keccak-permutations,
269+
# not to maximize keccak calls.
270+
# The complication of the discovery arises from
271+
# the non-linear gas cost of memory expansion.
267272
max_keccak_perm_per_block = 0
268273
optimal_input_length = 0
269274
for i in range(1, 1_000_000, 32):
@@ -294,9 +299,11 @@ def test_worst_keccak(
294299
# The loop structure is: JUMPDEST + [attack iteration] + PUSH0 + JUMP
295300
#
296301
# Now calculate available gas for [attack iteration]:
297-
# Numerator = max_code_size-3. The -3 is for the JUMPDEST, PUSH0 and JUMP.
298-
# Denominator = (PUSHN + PUSH1 + KECCAK256 + POP) + PUSH1_DATA + PUSHN_DATA
299-
# TODO: the testing framework uses PUSH1(0) instead of PUSH0 which is suboptimal for the
302+
# Numerator = max_code_size-3. (JUMPDEST, PUSH0 and JUMP)
303+
# Denominator = (PUSHN + PUSH1 + KECCAK256 + POP) + PUSH1_DATA +
304+
# PUSHN_DATA
305+
# TODO: the testing framework uses PUSH1(0) instead of PUSH0 which is
306+
# suboptimal for the
300307
# attack, whenever this is fixed adjust accordingly.
301308
benchmark_test(
302309
pre=pre,
@@ -1542,11 +1549,13 @@ def test_worst_tstore(
15421549
init_key = 42
15431550
setup = Op.PUSH1(init_key)
15441551

1545-
# If `dense_val_mut` is set, we use GAS as a cheap way of always storing a different value than
1552+
# If `dense_val_mut` is set, we use GAS as a cheap way of always
1553+
# storing a different value than
15461554
# the previous one.
15471555
attack_block = Op.TSTORE(Op.DUP2, Op.GAS if dense_val_mut else Op.DUP1)
15481556

1549-
# If `key_mut` is True, we mutate the key on every iteration of the big loop.
1557+
# If `key_mut` is True, we mutate the key on every iteration of the
1558+
# big loop.
15501559
cleanup = Op.POP + Op.GAS if key_mut else Bytecode()
15511560

15521561
benchmark_test(
@@ -1707,8 +1716,10 @@ def test_worst_mod(
17071716
The order of accessing the numerators is selected in a way the mod value
17081717
remains in the range as long as possible.
17091718
"""
1710-
# For SMOD we negate both numerator and modulus. The underlying computation is the same,
1711-
# just the SMOD implementation will have to additionally handle the sign bits.
1719+
# For SMOD we negate both numerator and modulus. The underlying
1720+
# computation is the same,
1721+
# just the SMOD implementation will have to additionally handle the
1722+
# sign bits.
17121723
# The result stays negative.
17131724
should_negate = op == Op.SMOD
17141725

@@ -1780,7 +1791,8 @@ def test_worst_mod(
17801791
seed += 1
17811792
print(f"{seed=}")
17821793

1783-
# TODO: Don't use fixed PUSH32. Let Bytecode helpers to select optimal push opcode.
1794+
# TODO: Don't use fixed PUSH32. Let Bytecode helpers to select optimal
1795+
# push opcode.
17841796
setup = sum((Op.PUSH32[n] for n in numerators), Bytecode())
17851797
attack_block = (
17861798
Op.CALLDATALOAD(0) + sum(make_dup(len(numerators) - i) + op for i in indexes) + Op.POP
@@ -1812,7 +1824,10 @@ def test_worst_memory_access(
18121824
offset_initialized: bool,
18131825
big_memory_expansion: bool,
18141826
):
1815-
"""Test running a block with as many memory access instructions as possible."""
1827+
"""
1828+
Test running a block with as many memory access instructions as
1829+
possible.
1830+
"""
18161831
mem_exp_code = Op.MSTORE8(10 * 1024, 1) if big_memory_expansion else Bytecode()
18171832
offset_set_code = Op.MSTORE(offset, 43) if offset_initialized else Bytecode()
18181833
setup = mem_exp_code + offset_set_code + Op.PUSH1(42) + Op.PUSH1(offset)
@@ -2273,7 +2288,10 @@ def test_worst_clz_diff_input(
22732288
gas_benchmark_value: int,
22742289
env: Environment,
22752290
):
2276-
"""Test running a block with as many CLZ with different input as possible."""
2291+
"""
2292+
Test running a block with as many CLZ with different input as
2293+
possible.
2294+
"""
22772295
max_code_size = fork.max_code_size()
22782296

22792297
code_prefix = Op.JUMPDEST

tests/benchmark/test_worst_memory.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ def test_worst_calldatacopy(
8282
if min_gas > gas_benchmark_value:
8383
pytest.skip("Minimum gas required for calldata ({min_gas}) is greater than the gas limit")
8484

85-
# We create the contract that will be doing the CALLDATACOPY multiple times.
85+
# We create the contract that will be doing the CALLDATACOPY multiple
86+
# times.
8687
#
87-
# If `non_zero_data` is True, we leverage CALLDATASIZE for the copy length. Otherwise, since we
88-
# don't send zero data explicitly via calldata, PUSH the target size and use DUP1 to copy it.
88+
# If `non_zero_data` is True, we leverage CALLDATASIZE for the copy
89+
# length. Otherwise, since we
90+
# don't send zero data explicitly via calldata, PUSH the target size and
91+
# use DUP1 to copy it.
8992
setup = Bytecode() if non_zero_data or size == 0 else Op.PUSH3(size)
9093
src_dst = 0 if fixed_src_dst else Op.MOD(Op.GAS, 7)
9194
attack_block = Op.CALLDATACOPY(
@@ -101,8 +104,8 @@ def test_worst_calldatacopy(
101104
# If the origin is CALL, we need to create a contract that will call the
102105
# target contract with the calldata.
103106
if origin == CallDataOrigin.CALL:
104-
# If `non_zero_data` is False we leverage just using zeroed memory. Otherwise, we
105-
# copy the calldata received from the transaction.
107+
# If `non_zero_data` is False we leverage just using zeroed memory.
108+
# Otherwise, we copy the calldata received from the transaction.
106109
setup = (
107110
Op.CALLDATACOPY(Op.PUSH0, Op.PUSH0, Op.CALLDATASIZE) if non_zero_data else Bytecode()
108111
) + Op.JUMPDEST
@@ -166,8 +169,10 @@ def test_worst_codecopy(
166169
attack_block, Bytecode(), Bytecode(), fork
167170
)
168171

169-
# The code generated above is not guaranteed to be of max_code_size, so we pad it since
170-
# a test parameter targets CODECOPYing a contract with max code size. Padded bytecode values
172+
# The code generated above is not guaranteed to be of max_code_size, so
173+
# we pad it since
174+
# a test parameter targets CODECOPYing a contract with max code size.
175+
# Padded bytecode values
171176
# are not relevant.
172177
code += Op.INVALID * (max_code_size - len(code))
173178
assert len(code) == max_code_size, (
@@ -209,8 +214,10 @@ def test_worst_returndatacopy(
209214
fixed_dst: bool,
210215
):
211216
"""Test running a block filled with RETURNDATACOPY executions."""
212-
# Create the contract that will RETURN the data that will be used for RETURNDATACOPY.
213-
# Random-ish data is injected at different points in memory to avoid making the content
217+
# Create the contract that will RETURN the data that will be used for
218+
# RETURNDATACOPY.
219+
# Random-ish data is injected at different points in memory to avoid
220+
# making the content
214221
# predictable. If `size` is 0, this helper contract won't be used.
215222
code = (
216223
Op.MSTORE8(0, Op.GAS)
@@ -223,7 +230,8 @@ def test_worst_returndatacopy(
223230
returndata_gen = Op.STATICCALL(address=helper_contract) if size > 0 else Bytecode()
224231
dst = 0 if fixed_dst else Op.MOD(Op.GAS, 7)
225232

226-
# We create the contract that will be doing the RETURNDATACOPY multiple times.
233+
# We create the contract that will be doing the RETURNDATACOPY multiple
234+
# times.
227235
returndata_gen = Op.STATICCALL(address=helper_contract) if size > 0 else Bytecode()
228236
attack_block = Op.RETURNDATACOPY(dst, Op.PUSH0, Op.RETURNDATASIZE)
229237

@@ -236,7 +244,8 @@ def test_worst_returndatacopy(
236244
# STATICCALL(address=helper_contract)
237245
# JUMP(#)
238246
# ```
239-
# The goal is that once per (big) loop iteration, the helper contract is called to
247+
# The goal is that once per (big) loop iteration, the helper contract is
248+
# called to
240249
# generate fresh returndata to continue calling RETURNDATACOPY.
241250

242251
benchmark_test(

tests/benchmark/test_worst_stateful_opcodes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ def test_worst_address_state_warm(
140140
opcode: Op,
141141
absent_target: bool,
142142
):
143-
"""Test running a block with as many stateful opcodes doing warm access for an account."""
143+
"""
144+
Test running a block with as many stateful opcodes doing warm access
145+
for an account.
146+
"""
144147
# Setup
145148
target_addr = Address(100_000)
146149
post = {}
@@ -374,7 +377,10 @@ def test_worst_storage_access_warm(
374377
gas_benchmark_value: int,
375378
env: Environment,
376379
):
377-
"""Test running a block with as many warm storage slot accesses as possible."""
380+
"""
381+
Test running a block with as many warm storage slot accesses as
382+
possible.
383+
"""
378384
blocks = []
379385

380386
# The target storage slot for the warm access is storage slot 0.

0 commit comments

Comments
 (0)