Skip to content

Commit 460469d

Browse files
committed
refactor(tests): EIP-7623: minor refactor
1 parent a7d550d commit 460469d

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed

tests/prague/eip7623_increase_calldata_cost/conftest.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Fixtures for the EIP-7623 tests.
33
"""
44

5-
from typing import Callable, List, Sequence
5+
from typing import List, Sequence
66

77
import pytest
88

@@ -20,7 +20,7 @@
2020
from ethereum_test_tools import Opcodes as Op
2121
from ethereum_test_tools import Transaction, TransactionException
2222

23-
from .helpers import DataTestType
23+
from .helpers import DataTestType, floor_cost_find
2424

2525

2626
@pytest.fixture
@@ -119,44 +119,6 @@ def contract_creating_tx(to: Address | None) -> bool:
119119
return to is None
120120

121121

122-
def floor_cost_find(
123-
floor_data_gas_cost_calculator: Callable[[int], int],
124-
intrinsic_gas_cost_calculator: Callable[[int], int],
125-
) -> int:
126-
"""
127-
Find the minimum amount of tokens that will trigger the floor gas cost, by using a binary
128-
search and the intrinsic gas cost and floor data calculators.
129-
"""
130-
# Start with 1000 tokens and if the intrinsic gas cost is greater than the floor gas cost,
131-
# multiply the number of tokens by 2 until it's not.
132-
tokens = 1000
133-
while floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
134-
tokens *= 2
135-
136-
# Binary search to find the minimum number of tokens that will trigger the floor gas cost.
137-
left = 0
138-
right = tokens
139-
while left < right:
140-
tokens = (left + right) // 2
141-
if floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
142-
left = tokens + 1
143-
else:
144-
right = tokens
145-
tokens = left
146-
147-
if floor_data_gas_cost_calculator(tokens) > intrinsic_gas_cost_calculator(tokens):
148-
tokens -= 1
149-
150-
# Verify that increasing the tokens by one would always trigger the floor gas cost.
151-
assert (
152-
floor_data_gas_cost_calculator(tokens) <= intrinsic_gas_cost_calculator(tokens)
153-
) and floor_data_gas_cost_calculator(tokens + 1) > intrinsic_gas_cost_calculator(
154-
tokens + 1
155-
), "invalid case"
156-
157-
return tokens
158-
159-
160122
@pytest.fixture
161123
def intrinsic_gas_data_floor_minimum_delta() -> int:
162124
"""

tests/prague/eip7623_increase_calldata_cost/helpers.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from enum import Enum, auto
6+
from typing import Callable
67

78

89
class DataTestType(Enum):
@@ -14,11 +15,39 @@ class DataTestType(Enum):
1415
FLOOR_GAS_COST_GREATER_THAN_INTRINSIC_GAS = auto()
1516

1617

17-
class GasTestType(Enum):
18+
def floor_cost_find(
19+
floor_data_gas_cost_calculator: Callable[[int], int],
20+
intrinsic_gas_cost_calculator: Callable[[int], int],
21+
) -> int:
1822
"""
19-
Enum for the different types of gas tests.
23+
Find the minimum amount of tokens that will trigger the floor gas cost, by using a binary
24+
search and the intrinsic gas cost and floor data calculators.
2025
"""
26+
# Start with 1000 tokens and if the intrinsic gas cost is greater than the floor gas cost,
27+
# multiply the number of tokens by 2 until it's not.
28+
tokens = 1000
29+
while floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
30+
tokens *= 2
2131

22-
CONSUME_ZERO_GAS = auto()
23-
CONSUME_ALL_GAS = auto()
24-
CONSUME_ALL_GAS_WITH_REFUND = auto()
32+
# Binary search to find the minimum number of tokens that will trigger the floor gas cost.
33+
left = 0
34+
right = tokens
35+
while left < right:
36+
tokens = (left + right) // 2
37+
if floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
38+
left = tokens + 1
39+
else:
40+
right = tokens
41+
tokens = left
42+
43+
if floor_data_gas_cost_calculator(tokens) > intrinsic_gas_cost_calculator(tokens):
44+
tokens -= 1
45+
46+
# Verify that increasing the tokens by one would always trigger the floor gas cost.
47+
assert (
48+
floor_data_gas_cost_calculator(tokens) <= intrinsic_gas_cost_calculator(tokens)
49+
) and floor_data_gas_cost_calculator(tokens + 1) > intrinsic_gas_cost_calculator(
50+
tokens + 1
51+
), "invalid case"
52+
53+
return tokens

0 commit comments

Comments
 (0)