Skip to content

Commit

Permalink
refactor(tests): EIP-4844, EIP-7691: Fill fork transition blob tests …
Browse files Browse the repository at this point in the history
…in newer forks
  • Loading branch information
marioevz committed Jan 16, 2025
1 parent 270870b commit 3f4df0f
Showing 1 changed file with 95 additions and 46 deletions.
141 changes: 95 additions & 46 deletions tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@

import pytest

from ethereum_test_forks import Cancun, Fork
from ethereum_test_forks import Fork
from ethereum_test_tools import (
EOA,
Account,
Address,
Alloc,
Block,
BlockchainTestFiller,
BlockException,
EngineAPIError,
Environment,
Hash,
Header,
TestAddress,
Transaction,
add_kzg_version,
)
from ethereum_test_tools import Opcodes as Op

from .spec import Spec, SpecHelpers, ref_spec_4844

REFERENCE_SPEC_GIT_PATH = ref_spec_4844.git_path
REFERENCE_SPEC_VERSION = ref_spec_4844.version

# All tests run on the transition fork from Shanghai to Cancun
pytestmark = pytest.mark.valid_at_transition_to("Cancun")


# Timestamp of the fork
FORK_TIMESTAMP = 15_000

Expand All @@ -43,89 +41,137 @@ def env() -> Environment: # noqa: D103


@pytest.fixture
def pre() -> Mapping[Address, Account]: # noqa: D103
return {
TestAddress: Account(balance=10**40),
}
def pre_fork_blobs_per_block(fork: Fork) -> int | None:
"""Amount of blobs to produce with the pre-fork rules."""
if fork.supports_blobs(timestamp=0):
return fork.max_blobs_per_block(timestamp=0)
return None


@pytest.fixture
def sender(pre: Alloc) -> EOA:
"""Sender account."""
return pre.fund_eoa()


@pytest.fixture
def pre_fork_blocks():
def pre_fork_blocks(
pre_fork_blobs_per_block: int | None,
destination_account: Address,
sender: EOA,
) -> List[Block]:
"""Generate blocks to reach the fork."""
return [Block(timestamp=t) for t in range(999, FORK_TIMESTAMP, 1_000)]
if pre_fork_blobs_per_block is None:
return [Block(timestamp=t) for t in range(999, FORK_TIMESTAMP, 1_000)]
return [
Block(
txs=[
Transaction(
ty=Spec.BLOB_TX_TYPE,
to=destination_account,
value=1,
gas_limit=3_000_000,
max_fee_per_gas=1_000_000,
max_priority_fee_per_gas=10,
max_fee_per_blob_gas=100,
access_list=[],
blob_versioned_hashes=add_kzg_version(
[Hash(x) for x in range(pre_fork_blobs_per_block)],
Spec.BLOB_COMMITMENT_VERSION_KZG,
),
sender=sender,
)
if pre_fork_blobs_per_block > 0
else Transaction(
ty=2,
to=destination_account,
value=1,
gas_limit=3_000_000,
max_fee_per_gas=1_000_000,
max_priority_fee_per_gas=10,
access_list=[],
sender=sender,
)
],
timestamp=t,
)
for t in range(999, FORK_TIMESTAMP, 1_000)
]


@pytest.fixture
def post_fork_block_count(fork: Fork) -> int:
"""Amount of blocks to produce with the post-fork rules."""
return SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=fork, blob_gas_price=2) // (
fork.max_blobs_per_block() - fork.target_blobs_per_block()
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP)
- fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP)
)


@pytest.fixture
def blob_count_per_block() -> int:
def post_fork_blobs_per_block(fork: Fork) -> int:
"""Amount of blocks to produce with the post-fork rules."""
return 4
return fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP) + 1


@pytest.fixture
def destination_account() -> Address: # noqa: D103
return Address(0x100)
def destination_account(pre: Alloc) -> Address: # noqa: D103
return pre.deploy_contract(Op.STOP)


@pytest.fixture
def post_fork_blocks(
destination_account: Address,
post_fork_block_count: int,
blob_count_per_block: int,
post_fork_blobs_per_block: int,
sender: EOA,
):
"""Generate blocks past the fork."""
return [
Block(
txs=[
Transaction(
ty=Spec.BLOB_TX_TYPE,
nonce=b,
to=destination_account,
value=1,
gas_limit=3000000,
max_fee_per_gas=1000000,
gas_limit=3_000_000,
max_fee_per_gas=1_000_000,
max_priority_fee_per_gas=10,
max_fee_per_blob_gas=100,
access_list=[],
blob_versioned_hashes=add_kzg_version(
[Hash(x) for x in range(blob_count_per_block)],
[Hash(x) for x in range(post_fork_blobs_per_block)],
Spec.BLOB_COMMITMENT_VERSION_KZG,
),
sender=sender,
)
if blob_count_per_block > 0
if post_fork_blobs_per_block > 0
else Transaction(
ty=2,
nonce=b,
to=destination_account,
value=1,
gas_limit=3000000,
max_fee_per_gas=1000000,
gas_limit=3_000_000,
max_fee_per_gas=1_000_000,
max_priority_fee_per_gas=10,
access_list=[],
sender=sender,
)
],
)
for b in range(post_fork_block_count)
for _ in range(post_fork_block_count)
]


@pytest.fixture
def post( # noqa: D103
pre_fork_blocks: List[Block],
post_fork_block_count: int,
destination_account: Address,
) -> Mapping[Address, Account]:
return {
destination_account: Account(balance=post_fork_block_count),
destination_account: Account(balance=post_fork_block_count + len(pre_fork_blocks)),
}


@pytest.mark.valid_at_transition_to("Cancun")
@pytest.mark.parametrize(
"excess_blob_gas_present,blob_gas_used_present",
[
Expand All @@ -137,7 +183,7 @@ def post( # noqa: D103
def test_invalid_pre_fork_block_with_blob_fields(
blockchain_test: BlockchainTestFiller,
env: Environment,
pre: Mapping[Address, Account],
pre: Alloc,
pre_fork_blocks: List[Block],
excess_blob_gas_present: bool,
blob_gas_used_present: bool,
Expand Down Expand Up @@ -166,10 +212,10 @@ def test_invalid_pre_fork_block_with_blob_fields(
)
],
genesis_environment=env,
tag="invalid_pre_fork_blob_fields",
)


@pytest.mark.valid_at_transition_to("Cancun")
@pytest.mark.parametrize(
"excess_blob_gas_missing,blob_gas_used_missing",
[
Expand All @@ -181,7 +227,7 @@ def test_invalid_pre_fork_block_with_blob_fields(
def test_invalid_post_fork_block_without_blob_fields(
blockchain_test: BlockchainTestFiller,
env: Environment,
pre: Mapping[Address, Account],
pre: Alloc,
pre_fork_blocks: List[Block],
excess_blob_gas_missing: bool,
blob_gas_used_missing: bool,
Expand Down Expand Up @@ -211,28 +257,32 @@ def test_invalid_post_fork_block_without_blob_fields(
)
],
genesis_environment=env,
tag="blob_fields_missing_post_fork",
)


@pytest.mark.parametrize(
"post_fork_block_count,blob_count_per_block",
[
(
SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=Cancun, blob_gas_price=2)
// (Cancun.max_blobs_per_block() - Cancun.target_blobs_per_block())
@pytest.mark.valid_from("Cancun")
@pytest.mark.fork_transition_test()
@pytest.mark.parametrize_by_fork(
"post_fork_block_count,post_fork_blobs_per_block",
lambda fork: [
pytest.param(
SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=fork, blob_gas_price=2)
// (
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP)
- fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP)
)
+ 2,
Cancun.max_blobs_per_block(),
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP),
id="max_blobs",
),
(10, 0),
(10, Cancun.target_blobs_per_block()),
pytest.param(10, 0, id="no_blobs"),
pytest.param(10, fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP), id="target_blobs"),
],
ids=["max_blobs", "no_blobs", "target_blobs"],
)
def test_fork_transition_excess_blob_gas(
blockchain_test: BlockchainTestFiller,
env: Environment,
pre: Mapping[Address, Account],
pre: Alloc,
pre_fork_blocks: List[Block],
post_fork_blocks: List[Block],
post: Mapping[Address, Account],
Expand All @@ -248,5 +298,4 @@ def test_fork_transition_excess_blob_gas(
post=post,
blocks=pre_fork_blocks + post_fork_blocks,
genesis_environment=env,
tag="correct_initial_blob_gas_calc",
)

0 comments on commit 3f4df0f

Please sign in to comment.