Skip to content

Commit 3f4df0f

Browse files
committed
refactor(tests): EIP-4844, EIP-7691: Fill fork transition blob tests in newer forks
1 parent 270870b commit 3f4df0f

File tree

1 file changed

+95
-46
lines changed

1 file changed

+95
-46
lines changed

tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py

Lines changed: 95 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,29 @@
88

99
import pytest
1010

11-
from ethereum_test_forks import Cancun, Fork
11+
from ethereum_test_forks import Fork
1212
from ethereum_test_tools import (
13+
EOA,
1314
Account,
1415
Address,
16+
Alloc,
1517
Block,
1618
BlockchainTestFiller,
1719
BlockException,
1820
EngineAPIError,
1921
Environment,
2022
Hash,
2123
Header,
22-
TestAddress,
2324
Transaction,
2425
add_kzg_version,
2526
)
27+
from ethereum_test_tools import Opcodes as Op
2628

2729
from .spec import Spec, SpecHelpers, ref_spec_4844
2830

2931
REFERENCE_SPEC_GIT_PATH = ref_spec_4844.git_path
3032
REFERENCE_SPEC_VERSION = ref_spec_4844.version
3133

32-
# All tests run on the transition fork from Shanghai to Cancun
33-
pytestmark = pytest.mark.valid_at_transition_to("Cancun")
34-
35-
3634
# Timestamp of the fork
3735
FORK_TIMESTAMP = 15_000
3836

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

4442

4543
@pytest.fixture
46-
def pre() -> Mapping[Address, Account]: # noqa: D103
47-
return {
48-
TestAddress: Account(balance=10**40),
49-
}
44+
def pre_fork_blobs_per_block(fork: Fork) -> int | None:
45+
"""Amount of blobs to produce with the pre-fork rules."""
46+
if fork.supports_blobs(timestamp=0):
47+
return fork.max_blobs_per_block(timestamp=0)
48+
return None
49+
50+
51+
@pytest.fixture
52+
def sender(pre: Alloc) -> EOA:
53+
"""Sender account."""
54+
return pre.fund_eoa()
5055

5156

5257
@pytest.fixture
53-
def pre_fork_blocks():
58+
def pre_fork_blocks(
59+
pre_fork_blobs_per_block: int | None,
60+
destination_account: Address,
61+
sender: EOA,
62+
) -> List[Block]:
5463
"""Generate blocks to reach the fork."""
55-
return [Block(timestamp=t) for t in range(999, FORK_TIMESTAMP, 1_000)]
64+
if pre_fork_blobs_per_block is None:
65+
return [Block(timestamp=t) for t in range(999, FORK_TIMESTAMP, 1_000)]
66+
return [
67+
Block(
68+
txs=[
69+
Transaction(
70+
ty=Spec.BLOB_TX_TYPE,
71+
to=destination_account,
72+
value=1,
73+
gas_limit=3_000_000,
74+
max_fee_per_gas=1_000_000,
75+
max_priority_fee_per_gas=10,
76+
max_fee_per_blob_gas=100,
77+
access_list=[],
78+
blob_versioned_hashes=add_kzg_version(
79+
[Hash(x) for x in range(pre_fork_blobs_per_block)],
80+
Spec.BLOB_COMMITMENT_VERSION_KZG,
81+
),
82+
sender=sender,
83+
)
84+
if pre_fork_blobs_per_block > 0
85+
else Transaction(
86+
ty=2,
87+
to=destination_account,
88+
value=1,
89+
gas_limit=3_000_000,
90+
max_fee_per_gas=1_000_000,
91+
max_priority_fee_per_gas=10,
92+
access_list=[],
93+
sender=sender,
94+
)
95+
],
96+
timestamp=t,
97+
)
98+
for t in range(999, FORK_TIMESTAMP, 1_000)
99+
]
56100

57101

58102
@pytest.fixture
59103
def post_fork_block_count(fork: Fork) -> int:
60104
"""Amount of blocks to produce with the post-fork rules."""
61105
return SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=fork, blob_gas_price=2) // (
62-
fork.max_blobs_per_block() - fork.target_blobs_per_block()
106+
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP)
107+
- fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP)
63108
)
64109

65110

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

71116

72117
@pytest.fixture
73-
def destination_account() -> Address: # noqa: D103
74-
return Address(0x100)
118+
def destination_account(pre: Alloc) -> Address: # noqa: D103
119+
return pre.deploy_contract(Op.STOP)
75120

76121

77122
@pytest.fixture
78123
def post_fork_blocks(
79124
destination_account: Address,
80125
post_fork_block_count: int,
81-
blob_count_per_block: int,
126+
post_fork_blobs_per_block: int,
127+
sender: EOA,
82128
):
83129
"""Generate blocks past the fork."""
84130
return [
85131
Block(
86132
txs=[
87133
Transaction(
88134
ty=Spec.BLOB_TX_TYPE,
89-
nonce=b,
90135
to=destination_account,
91136
value=1,
92-
gas_limit=3000000,
93-
max_fee_per_gas=1000000,
137+
gas_limit=3_000_000,
138+
max_fee_per_gas=1_000_000,
94139
max_priority_fee_per_gas=10,
95140
max_fee_per_blob_gas=100,
96-
access_list=[],
97141
blob_versioned_hashes=add_kzg_version(
98-
[Hash(x) for x in range(blob_count_per_block)],
142+
[Hash(x) for x in range(post_fork_blobs_per_block)],
99143
Spec.BLOB_COMMITMENT_VERSION_KZG,
100144
),
145+
sender=sender,
101146
)
102-
if blob_count_per_block > 0
147+
if post_fork_blobs_per_block > 0
103148
else Transaction(
104149
ty=2,
105-
nonce=b,
106150
to=destination_account,
107151
value=1,
108-
gas_limit=3000000,
109-
max_fee_per_gas=1000000,
152+
gas_limit=3_000_000,
153+
max_fee_per_gas=1_000_000,
110154
max_priority_fee_per_gas=10,
111-
access_list=[],
155+
sender=sender,
112156
)
113157
],
114158
)
115-
for b in range(post_fork_block_count)
159+
for _ in range(post_fork_block_count)
116160
]
117161

118162

119163
@pytest.fixture
120164
def post( # noqa: D103
165+
pre_fork_blocks: List[Block],
121166
post_fork_block_count: int,
122167
destination_account: Address,
123168
) -> Mapping[Address, Account]:
124169
return {
125-
destination_account: Account(balance=post_fork_block_count),
170+
destination_account: Account(balance=post_fork_block_count + len(pre_fork_blocks)),
126171
}
127172

128173

174+
@pytest.mark.valid_at_transition_to("Cancun")
129175
@pytest.mark.parametrize(
130176
"excess_blob_gas_present,blob_gas_used_present",
131177
[
@@ -137,7 +183,7 @@ def post( # noqa: D103
137183
def test_invalid_pre_fork_block_with_blob_fields(
138184
blockchain_test: BlockchainTestFiller,
139185
env: Environment,
140-
pre: Mapping[Address, Account],
186+
pre: Alloc,
141187
pre_fork_blocks: List[Block],
142188
excess_blob_gas_present: bool,
143189
blob_gas_used_present: bool,
@@ -166,10 +212,10 @@ def test_invalid_pre_fork_block_with_blob_fields(
166212
)
167213
],
168214
genesis_environment=env,
169-
tag="invalid_pre_fork_blob_fields",
170215
)
171216

172217

218+
@pytest.mark.valid_at_transition_to("Cancun")
173219
@pytest.mark.parametrize(
174220
"excess_blob_gas_missing,blob_gas_used_missing",
175221
[
@@ -181,7 +227,7 @@ def test_invalid_pre_fork_block_with_blob_fields(
181227
def test_invalid_post_fork_block_without_blob_fields(
182228
blockchain_test: BlockchainTestFiller,
183229
env: Environment,
184-
pre: Mapping[Address, Account],
230+
pre: Alloc,
185231
pre_fork_blocks: List[Block],
186232
excess_blob_gas_missing: bool,
187233
blob_gas_used_missing: bool,
@@ -211,28 +257,32 @@ def test_invalid_post_fork_block_without_blob_fields(
211257
)
212258
],
213259
genesis_environment=env,
214-
tag="blob_fields_missing_post_fork",
215260
)
216261

217262

218-
@pytest.mark.parametrize(
219-
"post_fork_block_count,blob_count_per_block",
220-
[
221-
(
222-
SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=Cancun, blob_gas_price=2)
223-
// (Cancun.max_blobs_per_block() - Cancun.target_blobs_per_block())
263+
@pytest.mark.valid_from("Cancun")
264+
@pytest.mark.fork_transition_test()
265+
@pytest.mark.parametrize_by_fork(
266+
"post_fork_block_count,post_fork_blobs_per_block",
267+
lambda fork: [
268+
pytest.param(
269+
SpecHelpers.get_min_excess_blobs_for_blob_gas_price(fork=fork, blob_gas_price=2)
270+
// (
271+
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP)
272+
- fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP)
273+
)
224274
+ 2,
225-
Cancun.max_blobs_per_block(),
275+
fork.max_blobs_per_block(timestamp=FORK_TIMESTAMP),
276+
id="max_blobs",
226277
),
227-
(10, 0),
228-
(10, Cancun.target_blobs_per_block()),
278+
pytest.param(10, 0, id="no_blobs"),
279+
pytest.param(10, fork.target_blobs_per_block(timestamp=FORK_TIMESTAMP), id="target_blobs"),
229280
],
230-
ids=["max_blobs", "no_blobs", "target_blobs"],
231281
)
232282
def test_fork_transition_excess_blob_gas(
233283
blockchain_test: BlockchainTestFiller,
234284
env: Environment,
235-
pre: Mapping[Address, Account],
285+
pre: Alloc,
236286
pre_fork_blocks: List[Block],
237287
post_fork_blocks: List[Block],
238288
post: Mapping[Address, Account],
@@ -248,5 +298,4 @@ def test_fork_transition_excess_blob_gas(
248298
post=post,
249299
blocks=pre_fork_blocks + post_fork_blocks,
250300
genesis_environment=env,
251-
tag="correct_initial_blob_gas_calc",
252301
)

0 commit comments

Comments
 (0)