From 98dabaee3f4632d723c3484afa1e987c3c4e2097 Mon Sep 17 00:00:00 2001 From: spencer-tb Date: Wed, 30 Oct 2024 20:54:06 +0700 Subject: [PATCH] tests: dynamic target blob count for 4844 tests. --- tests/cancun/eip4844_blobs/conftest.py | 2 +- tests/cancun/eip4844_blobs/spec.py | 23 +++++++++++++++---- tests/cancun/eip4844_blobs/test_blob_txs.py | 2 +- .../eip4844_blobs/test_excess_blob_gas.py | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/cancun/eip4844_blobs/conftest.py b/tests/cancun/eip4844_blobs/conftest.py index e8c6c5505d..7858bbba45 100644 --- a/tests/cancun/eip4844_blobs/conftest.py +++ b/tests/cancun/eip4844_blobs/conftest.py @@ -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) ) diff --git a/tests/cancun/eip4844_blobs/spec.py b/tests/cancun/eip4844_blobs/spec.py index c253aee0a2..1a7f52b6c7 100644 --- a/tests/cancun/eip4844_blobs/spec.py +++ b/tests/cancun/eip4844_blobs/spec.py @@ -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 @@ -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, @@ -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: @@ -124,7 +135,6 @@ def get_blob_gasprice(cls, *, excess_blob_gas: int) -> int: cls.BLOB_GASPRICE_UPDATE_FRACTION, ) - @dataclass(frozen=True) class SpecHelpers: """ @@ -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( @@ -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 + ) diff --git a/tests/cancun/eip4844_blobs/test_blob_txs.py b/tests/cancun/eip4844_blobs/test_blob_txs.py index 188b87bcb8..2757df5ee9 100644 --- a/tests/cancun/eip4844_blobs/test_blob_txs.py +++ b/tests/cancun/eip4844_blobs/test_blob_txs.py @@ -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, diff --git a/tests/cancun/eip4844_blobs/test_excess_blob_gas.py b/tests/cancun/eip4844_blobs/test_excess_blob_gas.py index 07d8bf9e95..87410d5a1d 100644 --- a/tests/cancun/eip4844_blobs/test_excess_blob_gas.py +++ b/tests/cancun/eip4844_blobs/test_excess_blob_gas.py @@ -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, )