Skip to content

Commit

Permalink
tests: dynamic target blob count for 4844 tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed Oct 30, 2024
1 parent a29ec05 commit 98dabae
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tests/cancun/eip4844_blobs/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def non_zero_blob_gas_used_genesis_block(
if parent_blobs == 0:
return None

parent_excess_blob_gas += Spec.TARGET_BLOB_GAS_PER_BLOCK
parent_excess_blob_gas += Spec.target_blob_gas_per_block()
excess_blob_gas = Spec.calc_excess_blob_gas(
BlockHeaderBlobGasFields(parent_excess_blob_gas, 0)
)
Expand Down
23 changes: 18 additions & 5 deletions tests/cancun/eip4844_blobs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass
from hashlib import sha256
from typing import Optional
from ethereum_test_forks import Fork, Cancun

from ethereum_test_tools import Transaction

Expand Down Expand Up @@ -62,6 +63,16 @@ class Spec:
HASH_OPCODE_BYTE = 0x49
HASH_GAS_COST = 3

@classmethod
def target_blob_gas_per_block(cls, fork: Optional[Fork] = Cancun, cl_target_blob_count: Optional[int] = None) -> int:
"""
Returns the target blob gas per block, using a dynamic target for forks after Cancun.
For Cancun the target blob count is 3, hence the blob gas per block is 393216.
"""
if fork and fork > Cancun and cl_target_blob_count is not None:
return cl_target_blob_count * cls.GAS_PER_BLOB
return Spec.TARGET_BLOB_GAS_PER_BLOCK

@classmethod
def kzg_to_versioned_hash(
cls,
Expand Down Expand Up @@ -99,10 +110,10 @@ def calc_excess_blob_gas(cls, parent: BlockHeaderBlobGasFields) -> int:
Calculate the excess blob gas for a block given the excess blob gas
and blob gas used from the parent block header.
"""
if parent.excess_blob_gas + parent.blob_gas_used < cls.TARGET_BLOB_GAS_PER_BLOCK:
if parent.excess_blob_gas + parent.blob_gas_used < cls.target_blob_gas_per_block():
return 0
else:
return parent.excess_blob_gas + parent.blob_gas_used - cls.TARGET_BLOB_GAS_PER_BLOCK
return parent.excess_blob_gas + parent.blob_gas_used - cls.target_blob_gas_per_block()

@classmethod
def get_total_blob_gas(cls, tx: Transaction) -> int:
Expand All @@ -124,7 +135,6 @@ def get_blob_gasprice(cls, *, excess_blob_gas: int) -> int:
cls.BLOB_GASPRICE_UPDATE_FRACTION,
)


@dataclass(frozen=True)
class SpecHelpers:
"""
Expand All @@ -146,7 +156,7 @@ def target_blobs_per_block(cls) -> int:
"""
Returns the target number of blobs per block.
"""
return Spec.TARGET_BLOB_GAS_PER_BLOCK // Spec.GAS_PER_BLOB
return Spec.target_blob_gas_per_block() // Spec.GAS_PER_BLOB

@classmethod
def calc_excess_blob_gas_from_blob_count(
Expand Down Expand Up @@ -180,4 +190,7 @@ def get_min_excess_blobs_for_blob_gas_price(cls, blob_gas_price: int) -> int:
"""
Gets the minimum required excess blobs to get a given blob gas cost in a block
"""
return cls.get_min_excess_blob_gas_for_blob_gas_price(blob_gas_price) // Spec.GAS_PER_BLOB
return (
cls.get_min_excess_blob_gas_for_blob_gas_price(blob_gas_price)
// Spec.GAS_PER_BLOB
)
2 changes: 1 addition & 1 deletion tests/cancun/eip4844_blobs/test_blob_txs.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def env(
# We increase the excess blob gas of the genesis because
# we cannot include blobs in the genesis, so the
# test blobs are actually in block 1.
excess_blob_gas += Spec.TARGET_BLOB_GAS_PER_BLOCK
excess_blob_gas += Spec.target_blob_gas_per_block()
return Environment(
excess_blob_gas=excess_blob_gas,
blob_gas_used=0,
Expand Down
2 changes: 1 addition & 1 deletion tests/cancun/eip4844_blobs/test_excess_blob_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def env( # noqa: D103
excess_blob_gas=(
parent_excess_blob_gas
if parent_blobs == 0
else parent_excess_blob_gas + Spec.TARGET_BLOB_GAS_PER_BLOCK
else parent_excess_blob_gas + Spec.target_blob_gas_per_block()
),
base_fee_per_gas=block_base_fee,
)
Expand Down

0 comments on commit 98dabae

Please sign in to comment.