Skip to content

Commit 55853d2

Browse files
committed
fix: review comments
1 parent dfd9334 commit 55853d2

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

tests/zkevm/test_worst_bytecode.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
REFERENCE_SPEC_GIT_PATH = "TODO"
2525
REFERENCE_SPEC_VERSION = "TODO"
2626

27-
MAX_CONTRACT_SIZE = 24 * 1024
28-
GAS_LIMIT = 36_000_000 # TODO: Parametrize using the (yet to be implemented) block gas limit
27+
MAX_CONTRACT_SIZE = 24 * 1024 # TODO: This could be a fork property
28+
BLOCK_GAS_LIMIT = 36_000_000 # TODO: Parametrize using the (yet to be implemented) block gas limit
29+
# OPCODE_GAS_LIMIT = BLOCK_GAS_LIMIT # TODO: Reduced in order to run the test in a reasonable time
30+
OPCODE_GAS_LIMIT = 100_000
2931

3032
XOR_TABLE_SIZE = 256
3133
XOR_TABLE = [Hash(i).sha256() for i in range(XOR_TABLE_SIZE)]
@@ -37,7 +39,6 @@
3739
"opcode",
3840
[
3941
Op.EXTCODESIZE,
40-
Op.EXTCODEHASH,
4142
],
4243
)
4344
@pytest.mark.valid_from("Cancun")
@@ -59,7 +60,7 @@ def test_worst_bytecode_single_opcode(
5960
The test is performed in the last block of the test, and the entire block gas limit is
6061
consumed by repeated opcode executions.
6162
"""
62-
env = Environment(gas_limit=GAS_LIMIT)
63+
env = Environment(gas_limit=BLOCK_GAS_LIMIT)
6364

6465
# The initcode will take its address as a starting point to the input to the keccak
6566
# hash function.
@@ -112,25 +113,21 @@ def test_worst_bytecode_single_opcode(
112113
)
113114
factory_caller_address = pre.deploy_contract(code=factory_caller_code)
114115

115-
# TODO: The correct way to calculate this is to use the gas costs and the transaction
116-
# intrinsic cost calculator, but this is generating approx 13,822 calls, and deploying this
117-
# many different contracts requires 1,974 blocks.
118-
# gas_costs = fork.gas_costs()
119-
# intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator()
120-
# max_number_of_contract_calls = (GAS_LIMIT - intrinsic_gas_cost_calc()) // (
121-
# gas_costs.G_VERY_LOW + gas_costs.G_BASE + gas_costs.G_COLD_ACCOUNT_ACCESS
122-
# )
123-
max_number_of_contract_calls = 10
116+
gas_costs = fork.gas_costs()
117+
intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator()
118+
max_number_of_contract_calls = (OPCODE_GAS_LIMIT - intrinsic_gas_cost_calc()) // (
119+
gas_costs.G_VERY_LOW + gas_costs.G_BASE + gas_costs.G_COLD_ACCOUNT_ACCESS
120+
)
124121
total_contracts_to_deploy = max_number_of_contract_calls
125122
approximate_gas_per_deployment = 4_970_000 # Obtained from evm tracing
126-
contracts_deployed_per_tx = GAS_LIMIT // approximate_gas_per_deployment
123+
contracts_deployed_per_tx = BLOCK_GAS_LIMIT // approximate_gas_per_deployment
127124

128125
deploy_txs = []
129126

130127
def generate_deploy_tx(contracts_to_deploy: int):
131128
return Transaction(
132129
to=factory_caller_address,
133-
gas_limit=GAS_LIMIT,
130+
gas_limit=BLOCK_GAS_LIMIT,
134131
gas_price=10**9, # Bump required due to the amount of full blocks
135132
data=Hash(contracts_deployed_per_tx),
136133
sender=pre.fund_eoa(),
@@ -154,11 +151,19 @@ def generate_deploy_tx(contracts_to_deploy: int):
154151
post[deployed_contract_address] = Account(nonce=1)
155152
deployed_contract_addresses.append(deployed_contract_address)
156153

157-
opcode_code = sum(opcode(address=address) for address in deployed_contract_addresses) + Op.STOP
154+
opcode_code = (
155+
sum(Op.POP(opcode(address=address)) for address in deployed_contract_addresses) + Op.STOP
156+
)
157+
if len(opcode_code) > MAX_CONTRACT_SIZE:
158+
# TODO: A workaround could be to split the opcode code into multiple contracts
159+
# and call them in sequence.
160+
raise ValueError(
161+
f"Code size {len(opcode_code)} exceeds maximum code size {MAX_CONTRACT_SIZE}"
162+
)
158163
opcode_address = pre.deploy_contract(code=opcode_code)
159164
opcode_tx = Transaction(
160165
to=opcode_address,
161-
gas_limit=GAS_LIMIT,
166+
gas_limit=OPCODE_GAS_LIMIT,
162167
gas_price=10**9, # Bump required due to the amount of full blocks
163168
sender=pre.fund_eoa(),
164169
)

0 commit comments

Comments
 (0)