Skip to content

Commit e872f2e

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

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

tests/zkevm/test_worst_bytecode.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,15 @@
1010
import pytest
1111

1212
from ethereum_test_forks import Fork
13-
from ethereum_test_tools import (
14-
Account,
15-
Alloc,
16-
Block,
17-
BlockchainTestFiller,
18-
Environment,
19-
Hash,
20-
Transaction,
21-
While,
22-
compute_create2_address,
23-
)
13+
from ethereum_test_tools import (Account, Alloc, Block, BlockchainTestFiller,
14+
Environment, Hash, Transaction, While,
15+
compute_create2_address)
2416
from ethereum_test_tools.vm.opcode import Opcodes as Op
2517

2618
REFERENCE_SPEC_GIT_PATH = "TODO"
2719
REFERENCE_SPEC_VERSION = "TODO"
2820

2921
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
3322

3423
XOR_TABLE_SIZE = 256
3524
XOR_TABLE = [Hash(i).sha256() for i in range(XOR_TABLE_SIZE)]
@@ -41,12 +30,19 @@
4130
Op.EXTCODESIZE,
4231
],
4332
)
33+
@pytest.mark.parametrize(
34+
"attack_gas_limit",
35+
[
36+
Environment().gas_limit,
37+
],
38+
)
4439
@pytest.mark.valid_from("Cancun")
4540
def test_worst_bytecode_single_opcode(
4641
blockchain_test: BlockchainTestFiller,
4742
pre: Alloc,
4843
fork: Fork,
4944
opcode: Op,
45+
attack_gas_limit: int,
5046
):
5147
"""
5248
Test a block execution where a single opcode execution maxes out the gas limit,
@@ -60,7 +56,9 @@ def test_worst_bytecode_single_opcode(
6056
The test is performed in the last block of the test, and the entire block gas limit is
6157
consumed by repeated opcode executions.
6258
"""
63-
env = Environment(gas_limit=BLOCK_GAS_LIMIT)
59+
# We use 100G gas limit to be able to deploy a large number of contracts in a single block,
60+
# avoiding bloating the number of preparing blocks in the test.
61+
env = Environment(gas_limit=100_000_000_000)
6462

6563
# The initcode will take its address as a starting point to the input to the keccak
6664
# hash function.
@@ -126,7 +124,7 @@ def test_worst_bytecode_single_opcode(
126124
)
127125
max_number_of_contract_calls = (
128126
# Base available gas = GAS_LIMIT - intrinsic - (out of loop MSTOREs)
129-
OPCODE_GAS_LIMIT - intrinsic_gas_cost_calc() - gas_costs.G_VERY_LOW * 4
127+
attack_gas_limit - intrinsic_gas_cost_calc() - gas_costs.G_VERY_LOW * 4
130128
) // loop_cost
131129

132130
total_contracts_to_deploy = max_number_of_contract_calls
@@ -171,8 +169,7 @@ def generate_deploy_tx(contracts_to_deploy: int):
171169
+ Op.MSTORE(64, initcode.keccak256())
172170
# Main loop
173171
+ While(
174-
body=Op.POP(Op.EXTCODESIZE(Op.SHA3(32 - 20 - 1, 85)))
175-
+ Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)),
172+
body=Op.POP(opcode(Op.SHA3(32 - 20 - 1, 85))) + Op.MSTORE(32, Op.ADD(Op.MLOAD(32), 1)),
176173
)
177174
)
178175

@@ -185,7 +182,7 @@ def generate_deploy_tx(contracts_to_deploy: int):
185182
opcode_address = pre.deploy_contract(code=opcode_code)
186183
opcode_tx = Transaction(
187184
to=opcode_address,
188-
gas_limit=OPCODE_GAS_LIMIT,
185+
gas_limit=attack_gas_limit,
189186
gas_price=10**9, # Bump required due to the amount of full blocks
190187
sender=pre.fund_eoa(),
191188
)
@@ -198,4 +195,5 @@ def generate_deploy_tx(contracts_to_deploy: int):
198195
*[Block(txs=[deploy_tx]) for deploy_tx in deploy_txs],
199196
Block(txs=[opcode_tx]),
200197
],
198+
exclude_full_post_state_in_output=True,
201199
)

0 commit comments

Comments
 (0)