Skip to content

Commit

Permalink
refactor: don't make Spec, SpecHelpers enum classes
Browse files Browse the repository at this point in the history
This avoids confusion/possible errors regarding when or when not to use the `.value` of the enum values; just define them as normal class attributes.
  • Loading branch information
danceratopz committed Jul 14, 2023
1 parent 9363e4b commit 060b437
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 37 deletions.
21 changes: 10 additions & 11 deletions tests/cancun/eip4844_blobs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Defines EIP-4844 specification constants and functions.
"""
from dataclasses import dataclass
from enum import Enum, IntEnum
from hashlib import sha256
from typing import Optional

Expand Down Expand Up @@ -32,7 +31,7 @@ class BlockHeaderDataGasFields:

# Constants
@dataclass(frozen=True)
class Spec(IntEnum):
class Spec:
"""
Parameters from the EIP-4844 specifications as defined at
https://eips.ethereum.org/EIPS/eip-4844#parameters
Expand Down Expand Up @@ -111,22 +110,22 @@ def calc_excess_data_gas(cls, parent: BlockHeaderDataGasFields) -> int:
# """
# if tx.blob_versioned_hashes is None:
# return 0
# return cls.DATA_GAS_PER_BLOB.value * len(tx.blob_versioned_hashes)
# return cls.DATA_GAS_PER_BLOB * len(tx.blob_versioned_hashes)

@classmethod
def get_data_gasprice(cls, *, excess_data_gas: int) -> int:
"""
Calculate the data gas price from the excess.
"""
return cls.fake_exponential(
cls.MIN_DATA_GASPRICE.value,
cls.MIN_DATA_GASPRICE,
excess_data_gas,
cls.DATA_GASPRICE_UPDATE_FRACTION.value,
cls.DATA_GASPRICE_UPDATE_FRACTION,
)


@dataclass(frozen=True)
class SpecHelpers(Enum):
class SpecHelpers:
"""
Define parameters and helper functions that are tightly coupled to the 4844
spec but not strictly part of it.
Expand All @@ -139,14 +138,14 @@ def max_blobs_per_block(cls) -> int: # MAX_BLOBS_PER_BLOCK =
"""
Returns the maximum number of blobs per block.
"""
return Spec.MAX_DATA_GAS_PER_BLOCK.value // Spec.DATA_GAS_PER_BLOB.value
return Spec.MAX_DATA_GAS_PER_BLOCK // Spec.DATA_GAS_PER_BLOB

@classmethod
def target_blobs_per_block(cls) -> int:
"""
Returns the target number of blobs per block.
"""
return Spec.TARGET_DATA_GAS_PER_BLOCK.value // Spec.DATA_GAS_PER_BLOB.value
return Spec.TARGET_DATA_GAS_PER_BLOCK // Spec.DATA_GAS_PER_BLOB

@classmethod
def calc_excess_data_gas_from_blob_count(
Expand All @@ -156,7 +155,7 @@ def calc_excess_data_gas_from_blob_count(
Calculate the excess data gas for a block given the parent excess data gas
and the number of blobs in the block.
"""
parent_consumed_data_gas = parent_blob_count * Spec.DATA_GAS_PER_BLOB.value
parent_consumed_data_gas = parent_blob_count * Spec.DATA_GAS_PER_BLOB
return Spec.calc_excess_data_gas(
BlockHeaderDataGasFields(parent_excess_data_gas, parent_consumed_data_gas)
)
Expand All @@ -169,7 +168,7 @@ def get_min_excess_data_gas_for_data_gas_price(cls, data_gas_price: int) -> int:
current_excess_data_gas = 0
current_data_gas_price = 1
while current_data_gas_price < data_gas_price:
current_excess_data_gas += Spec.DATA_GAS_PER_BLOB.value
current_excess_data_gas += Spec.DATA_GAS_PER_BLOB
current_data_gas_price = Spec.get_data_gasprice(
excess_data_gas=current_excess_data_gas
)
Expand All @@ -182,5 +181,5 @@ def get_min_excess_data_blobs_for_data_gas_price(cls, data_gas_price: int) -> in
"""
return (
cls.get_min_excess_data_gas_for_data_gas_price(data_gas_price)
// Spec.DATA_GAS_PER_BLOB.value
// Spec.DATA_GAS_PER_BLOB
)
4 changes: 2 additions & 2 deletions tests/cancun/eip4844_blobs/test_blob_txs.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def blob_hashes_per_tx(blobs_per_tx: List[int]) -> List[List[bytes]]:
return [
add_kzg_version(
[to_hash_bytes(x) for x in range(blob_count)],
Spec.BLOB_COMMITMENT_VERSION_KZG.value,
Spec.BLOB_COMMITMENT_VERSION_KZG,
)
for blob_count in blobs_per_tx
]
Expand Down Expand Up @@ -255,7 +255,7 @@ def txs( # noqa: D103
"""
return [
Transaction(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=tx_i,
to=destination_account,
value=tx_value,
Expand Down
11 changes: 4 additions & 7 deletions tests/cancun/eip4844_blobs/test_blob_txs_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def txs( # noqa: D103
blobs_info = Blob.blobs_to_transaction_input(tx_blobs)
txs.append(
Transaction(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=nonce,
to=destination_account,
value=tx_value,
Expand Down Expand Up @@ -276,8 +276,7 @@ def blocks(
[ # Blobs per transaction
Blob(
blob=bytes(
Spec.FIELD_ELEMENTS_PER_BLOB.value
* SpecHelpers.BYTES_PER_FIELD_ELEMENT.value
Spec.FIELD_ELEMENTS_PER_BLOB * SpecHelpers.BYTES_PER_FIELD_ELEMENT
),
kzg_commitment=INF_POINT,
kzg_proof=INF_POINT,
Expand All @@ -291,8 +290,7 @@ def blocks(
[ # Blobs per transaction
Blob(
blob=bytes(
Spec.FIELD_ELEMENTS_PER_BLOB.value
* SpecHelpers.BYTES_PER_FIELD_ELEMENT.value
Spec.FIELD_ELEMENTS_PER_BLOB * SpecHelpers.BYTES_PER_FIELD_ELEMENT
),
kzg_commitment=INF_POINT,
kzg_proof=INF_POINT,
Expand All @@ -307,8 +305,7 @@ def blocks(
[ # Blobs per transaction
Blob(
blob=bytes(
Spec.FIELD_ELEMENTS_PER_BLOB.value
* SpecHelpers.BYTES_PER_FIELD_ELEMENT.value
Spec.FIELD_ELEMENTS_PER_BLOB * SpecHelpers.BYTES_PER_FIELD_ELEMENT
),
kzg_commitment=INF_POINT,
kzg_proof=INF_POINT,
Expand Down
6 changes: 3 additions & 3 deletions tests/cancun/eip4844_blobs/test_blobhash_opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_blobhash_gas_cost(
]
)
)
post[address] = Account(storage={0: Spec.HASH_GAS_COST.value})
post[address] = Account(storage={0: Spec.HASH_GAS_COST})
blockchain_test(
pre=pre,
blocks=blocks,
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_blobhash_scenarios(
Block(
txs=[
template_tx.with_fields(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=i,
to=address,
access_list=[],
Expand Down Expand Up @@ -241,7 +241,7 @@ def test_blobhash_invalid_blob_index(
Block(
txs=[
template_tx.with_fields(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=i,
to=address,
access_list=[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# Blob transaction template
tx_type_3 = Transaction(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
data=to_hash_bytes(0),
gas_limit=3000000,
max_fee_per_gas=10,
Expand Down
2 changes: 1 addition & 1 deletion tests/cancun/eip4844_blobs/test_excess_data_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def tx( # noqa: D103
)
else:
return Transaction(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=0,
to=destination_account,
value=1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def post_fork_blocks(
Block(
txs=[
Transaction(
ty=Spec.BLOB_TX_TYPE.value,
ty=Spec.BLOB_TX_TYPE,
nonce=b,
to=destination_account,
value=1,
Expand Down
12 changes: 6 additions & 6 deletions tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ def post(
# CALL operation success
expected_storage[0] = 1
# Success return values
expected_storage[1] = Spec.FIELD_ELEMENTS_PER_BLOB.value
expected_storage[2] = Spec.BLS_MODULUS.value
expected_storage[1] = Spec.FIELD_ELEMENTS_PER_BLOB
expected_storage[2] = Spec.BLS_MODULUS
# Success return values size
expected_storage[3] = 64
# Success return values from RETURNDATACOPY
expected_storage[4] = Spec.FIELD_ELEMENTS_PER_BLOB.value
expected_storage[5] = Spec.BLS_MODULUS.value
expected_storage[4] = Spec.FIELD_ELEMENTS_PER_BLOB
expected_storage[5] = Spec.BLS_MODULUS

else:
# CALL operation failure
Expand Down Expand Up @@ -540,7 +540,7 @@ def test_point_evaluation_precompile_gas_tx_to(
ty=2,
nonce=0,
data=precompile_input,
to=to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS.value),
to=to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS),
value=0,
gas_limit=call_gas + intrinsic_gas_cost,
max_fee_per_gas=7,
Expand Down Expand Up @@ -622,7 +622,7 @@ def tx_generator() -> Iterator[Transaction]:
storage={b: 1 for b in range(1, len(PRE_FORK_BLOCK_RANGE) + 1)},
# The tx in last block succeeds; storage 0 by default.
),
to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS.value): Account(
to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account(
balance=len(PRE_FORK_BLOCK_RANGE),
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def post(
if proof == "correct":
expected_gas_usage = (
call_gas
if call_gas < Spec.POINT_EVALUATION_PRECOMPILE_GAS.value
else Spec.POINT_EVALUATION_PRECOMPILE_GAS.value
if call_gas < Spec.POINT_EVALUATION_PRECOMPILE_GAS
else Spec.POINT_EVALUATION_PRECOMPILE_GAS
)
else:
expected_gas_usage = call_gas
Expand All @@ -204,9 +204,9 @@ def post(
@pytest.mark.parametrize(
"call_gas",
[
Spec.POINT_EVALUATION_PRECOMPILE_GAS.value,
Spec.POINT_EVALUATION_PRECOMPILE_GAS.value - 1,
Spec.POINT_EVALUATION_PRECOMPILE_GAS.value + 1,
Spec.POINT_EVALUATION_PRECOMPILE_GAS,
Spec.POINT_EVALUATION_PRECOMPILE_GAS - 1,
Spec.POINT_EVALUATION_PRECOMPILE_GAS + 1,
],
ids=["exact_gas", "insufficient_gas", "extra_gas"],
)
Expand Down

0 comments on commit 060b437

Please sign in to comment.