Skip to content

Commit bb8dcc3

Browse files
committed
feat(tests): Add tests for OOGing in EXP variable gas
1 parent e917eb5 commit bb8dcc3

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

tests/frontier/opcodes/test_all_opcodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ def test_constant_gas(
306306
+ (code_increment_counter if opcode == Op.CREATE2 else Bytecode())
307307
)
308308
gas_test(
309+
fork,
309310
state_test,
310311
env,
311312
pre,

tests/frontier/opcodes/test_exp.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Test EXP opcode.
3+
"""
4+
5+
import pytest
6+
from execution_testing import (
7+
Alloc,
8+
Environment,
9+
StateTestFiller,
10+
)
11+
from execution_testing.base_types.base_types import ZeroPaddedHexNumber
12+
from execution_testing.forks.helpers import Fork
13+
from execution_testing.vm import Opcodes as Op
14+
15+
from tests.unscheduled.eip7692_eof_v1.gas_test import gas_test
16+
17+
REFERENCE_SPEC_GIT_PATH = "N/A"
18+
REFERENCE_SPEC_VERSION = "N/A"
19+
20+
21+
def exp_gas(fork: Fork, exponent: int) -> int:
22+
"""Calculate gas cost for EXP opcode given the exponent."""
23+
byte_len = (exponent.bit_length() + 7) // 8
24+
return fork.gas_costs().G_EXP + fork.gas_costs().G_EXP_BYTE * byte_len
25+
26+
27+
@pytest.mark.valid_from("Berlin")
28+
@pytest.mark.parametrize(
29+
"a", [0, 1, pytest.param(2**256 - 1, id="a2to256minus1")]
30+
)
31+
@pytest.mark.parametrize(
32+
"exponent",
33+
[
34+
0,
35+
1,
36+
2,
37+
1023,
38+
1024,
39+
pytest.param(2**255, id="exponent2to255"),
40+
pytest.param(2**256 - 1, id="exponent2to256minus1"),
41+
],
42+
)
43+
def test_gas(
44+
state_test: StateTestFiller,
45+
pre: Alloc,
46+
a: int,
47+
exponent: int,
48+
fork: Fork,
49+
env: Environment,
50+
) -> None:
51+
"""Test that EXP gas works as expected."""
52+
warm_gas = exp_gas(fork, exponent)
53+
54+
if cap := fork.transaction_gas_limit_cap():
55+
env.gas_limit = ZeroPaddedHexNumber(cap)
56+
57+
gas_test(
58+
fork,
59+
state_test,
60+
env,
61+
pre,
62+
setup_code=Op.PUSH32(exponent) + Op.PUSH32(a),
63+
subject_code=Op.EXP,
64+
tear_down_code=Op.STOP,
65+
cold_gas=warm_gas,
66+
warm_gas=warm_gas,
67+
eof=False,
68+
)

tests/unscheduled/eip7692_eof_v1/eip7069_extcall/test_gas.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def test_ext_calls_gas(
143143
)
144144
cost_memory_bytes = fork.memory_expansion_gas_calculator()
145145
gas_test(
146+
fork,
146147
state_test,
147148
state_env,
148149
pre,
@@ -164,6 +165,7 @@ def test_ext_calls_gas(
164165
def test_transfer_gas_is_cleared(
165166
state_test: StateTestFiller,
166167
pre: Alloc,
168+
fork: Fork,
167169
state_env: Environment,
168170
opcode: Op,
169171
value: int,
@@ -185,6 +187,7 @@ def test_transfer_gas_is_cleared(
185187
push_gas = (4 if opcode == Op.EXTCALL else 3) * 3
186188

187189
gas_test(
190+
fork,
188191
state_test,
189192
state_env,
190193
pre,
@@ -212,6 +215,7 @@ def test_transfer_gas_is_cleared(
212215
def test_late_account_create(
213216
state_test: StateTestFiller,
214217
pre: Alloc,
218+
fork: Fork,
215219
state_env: Environment,
216220
opcode: Op,
217221
) -> None:
@@ -222,6 +226,7 @@ def test_late_account_create(
222226
empty_address = Address(0xDECAFC0DE)
223227

224228
gas_test(
229+
fork,
225230
state_test,
226231
state_env,
227232
pre,

tests/unscheduled/eip7692_eof_v1/eip7620_eof_create/test_gas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def test_eofcreate_gas(
149149
)
150150
cost_memory_bytes = fork.memory_expansion_gas_calculator()
151151
gas_test(
152+
fork,
152153
state_test,
153154
Environment(),
154155
pre,

tests/unscheduled/eip7692_eof_v1/gas_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
StateTestFiller,
1414
Transaction,
1515
)
16+
from execution_testing.forks.forks.forks import Berlin
17+
from execution_testing.forks.helpers import Fork
1618
from execution_testing.test_types.eof.v1 import Container, Section
1719

1820
from .eip7069_extcall.spec import (
@@ -31,6 +33,7 @@
3133

3234

3335
def gas_test(
36+
fork: Fork,
3437
state_test: StateTestFiller,
3538
env: Environment,
3639
pre: Alloc,
@@ -55,6 +58,11 @@ def gas_test(
5558
test, and MUST NOT have any side-effects which persist across message
5659
calls, and in particular, any effects on the gas usage of `subject_code`.
5760
"""
61+
if fork < Berlin:
62+
raise ValueError(
63+
"Gas tests before Berlin are not supported due to CALL gas changes"
64+
)
65+
5866
if cold_gas <= 0:
5967
raise ValueError(
6068
f"Target gas allocations (cold_gas) must be > 0, got {cold_gas}"

0 commit comments

Comments
 (0)