Skip to content

Commit b4ea57b

Browse files
committed
zkvm: full bytecode attack enabling
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
1 parent 6a23d32 commit b4ea57b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tests/zkevm/test_worst_bytecode.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
REFERENCE_SPEC_VERSION = "TODO"
2828

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

3431
XOR_TABLE_SIZE = 256
3532
XOR_TABLE = [Hash(i).sha256() for i in range(XOR_TABLE_SIZE)]
@@ -41,12 +38,19 @@
4138
Op.EXTCODESIZE,
4239
],
4340
)
41+
@pytest.mark.parametrize(
42+
"attack_gas_limit",
43+
[
44+
Environment().gas_limit,
45+
],
46+
)
4447
@pytest.mark.valid_from("Cancun")
4548
def test_worst_bytecode_single_opcode(
4649
blockchain_test: BlockchainTestFiller,
4750
pre: Alloc,
4851
fork: Fork,
4952
opcode: Op,
53+
attack_gas_limit: int,
5054
):
5155
"""
5256
Test a block execution where a single opcode execution maxes out the gas limit,
@@ -60,7 +64,9 @@ def test_worst_bytecode_single_opcode(
6064
The test is performed in the last block of the test, and the entire block gas limit is
6165
consumed by repeated opcode executions.
6266
"""
63-
env = Environment(gas_limit=BLOCK_GAS_LIMIT)
67+
# We use 100G gas limit to be able to deploy a large number of contracts in a single block,
68+
# avoiding bloating the number of preparing blocks in the test.
69+
env = Environment(gas_limit=100_000_000_000)
6470

6571
# The initcode will take its address as a starting point to the input to the keccak
6672
# hash function.
@@ -126,19 +132,19 @@ def test_worst_bytecode_single_opcode(
126132
)
127133
max_number_of_contract_calls = (
128134
# Base available gas = GAS_LIMIT - intrinsic - (out of loop MSTOREs)
129-
OPCODE_GAS_LIMIT - intrinsic_gas_cost_calc() - gas_costs.G_VERY_LOW * 4
135+
attack_gas_limit - intrinsic_gas_cost_calc() - gas_costs.G_VERY_LOW * 4
130136
) // loop_cost
131137

132138
total_contracts_to_deploy = max_number_of_contract_calls
133139
approximate_gas_per_deployment = 4_970_000 # Obtained from evm tracing
134-
contracts_deployed_per_tx = BLOCK_GAS_LIMIT // approximate_gas_per_deployment
140+
contracts_deployed_per_tx = env.gas_limit // approximate_gas_per_deployment
135141

136142
deploy_txs = []
137143

138144
def generate_deploy_tx(contracts_to_deploy: int):
139145
return Transaction(
140146
to=factory_caller_address,
141-
gas_limit=BLOCK_GAS_LIMIT,
147+
gas_limit=env.gas_limit,
142148
gas_price=10**9, # Bump required due to the amount of full blocks
143149
data=Hash(contracts_deployed_per_tx),
144150
sender=pre.fund_eoa(),
@@ -171,8 +177,7 @@ def generate_deploy_tx(contracts_to_deploy: int):
171177
+ Op.MSTORE(64, initcode.keccak256())
172178
# Main loop
173179
+ While(
174-
body=Op.POP(Op.EXTCODESIZE(Op.SHA3(32 - 20 - 1, 85)))
175-
+ Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)),
180+
body=Op.POP(opcode(Op.SHA3(32 - 20 - 1, 85))) + Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)),
176181
)
177182
)
178183

@@ -185,7 +190,7 @@ def generate_deploy_tx(contracts_to_deploy: int):
185190
opcode_address = pre.deploy_contract(code=opcode_code)
186191
opcode_tx = Transaction(
187192
to=opcode_address,
188-
gas_limit=OPCODE_GAS_LIMIT,
193+
gas_limit=attack_gas_limit,
189194
gas_price=10**9, # Bump required due to the amount of full blocks
190195
sender=pre.fund_eoa(),
191196
)
@@ -198,4 +203,5 @@ def generate_deploy_tx(contracts_to_deploy: int):
198203
*[Block(txs=[deploy_tx]) for deploy_tx in deploy_txs],
199204
Block(txs=[opcode_tx]),
200205
],
206+
exclude_full_post_state_in_output=True,
201207
)

0 commit comments

Comments
 (0)