Skip to content

Commit

Permalink
refactor(tests): EIP-7623: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Dec 11, 2024
1 parent f6fb3cf commit c6ae49e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
42 changes: 2 additions & 40 deletions tests/prague/eip7623_increase_calldata_cost/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Fixtures for the EIP-7623 tests.
"""

from typing import Callable, List, Sequence
from typing import List, Sequence

import pytest

Expand All @@ -20,7 +20,7 @@
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools import Transaction, TransactionException

from .helpers import DataTestType
from .helpers import DataTestType, floor_cost_find


@pytest.fixture
Expand Down Expand Up @@ -119,44 +119,6 @@ def contract_creating_tx(to: Address | None) -> bool:
return to is None


def floor_cost_find(
floor_data_gas_cost_calculator: Callable[[int], int],
intrinsic_gas_cost_calculator: Callable[[int], int],
) -> int:
"""
Find the minimum amount of tokens that will trigger the floor gas cost, by using a binary
search and the intrinsic gas cost and floor data calculators.
"""
# Start with 1000 tokens and if the intrinsic gas cost is greater than the floor gas cost,
# multiply the number of tokens by 2 until it's not.
tokens = 1000
while floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
tokens *= 2

# Binary search to find the minimum number of tokens that will trigger the floor gas cost.
left = 0
right = tokens
while left < right:
tokens = (left + right) // 2
if floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
left = tokens + 1
else:
right = tokens
tokens = left

if floor_data_gas_cost_calculator(tokens) > intrinsic_gas_cost_calculator(tokens):
tokens -= 1

# Verify that increasing the tokens by one would always trigger the floor gas cost.
assert (
floor_data_gas_cost_calculator(tokens) <= intrinsic_gas_cost_calculator(tokens)
) and floor_data_gas_cost_calculator(tokens + 1) > intrinsic_gas_cost_calculator(
tokens + 1
), "invalid case"

return tokens


@pytest.fixture
def intrinsic_gas_data_floor_minimum_delta() -> int:
"""
Expand Down
39 changes: 34 additions & 5 deletions tests/prague/eip7623_increase_calldata_cost/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from enum import Enum, auto
from typing import Callable


class DataTestType(Enum):
Expand All @@ -14,11 +15,39 @@ class DataTestType(Enum):
FLOOR_GAS_COST_GREATER_THAN_INTRINSIC_GAS = auto()


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

CONSUME_ZERO_GAS = auto()
CONSUME_ALL_GAS = auto()
CONSUME_ALL_GAS_WITH_REFUND = auto()
# Binary search to find the minimum number of tokens that will trigger the floor gas cost.
left = 0
right = tokens
while left < right:
tokens = (left + right) // 2
if floor_data_gas_cost_calculator(tokens) < intrinsic_gas_cost_calculator(tokens):
left = tokens + 1
else:
right = tokens
tokens = left

if floor_data_gas_cost_calculator(tokens) > intrinsic_gas_cost_calculator(tokens):
tokens -= 1

# Verify that increasing the tokens by one would always trigger the floor gas cost.
assert (
floor_data_gas_cost_calculator(tokens) <= intrinsic_gas_cost_calculator(tokens)
) and floor_data_gas_cost_calculator(tokens + 1) > intrinsic_gas_cost_calculator(
tokens + 1
), "invalid case"

return tokens

0 comments on commit c6ae49e

Please sign in to comment.