Skip to content

Commit 7315523

Browse files
feat: change is_after_fork method (#1448)
1 parent 78240b6 commit 7315523

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

src/ethereum_spec_tools/evm_tools/loaders/fork_loader.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ForkLoad:
1414
"""
1515

1616
_fork_module: str
17-
_forks: Any
17+
_forks: list[Hardfork]
1818

1919
def __init__(self, fork_module: str):
2020
self._fork_module = fork_module
@@ -40,12 +40,17 @@ def proof_of_stake(self) -> bool:
4040
raise Exception(f"fork {self._fork_module} not discovered")
4141

4242
def is_after_fork(self, target_fork_name: str) -> bool:
43-
"""Check if the fork is after the target fork."""
43+
"""
44+
Check if the fork is after the target fork.
45+
46+
Accepts short fork names (e.g. "cancun") instead of full module paths.
47+
"""
4448
return_value = False
4549
for fork in self._forks:
46-
if fork.name == target_fork_name:
50+
short_name = fork.short_name
51+
if short_name == target_fork_name:
4752
return_value = True
48-
if fork.name == "ethereum.forks." + self._fork_module:
53+
if short_name == self._fork_module:
4954
break
5055
return return_value
5156

src/ethereum_spec_tools/evm_tools/t8n/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ def block_environment(self) -> Any:
174174
"chain_id": self.chain_id,
175175
}
176176

177-
if self.fork.is_after_fork("ethereum.forks.london"):
177+
if self.fork.is_after_fork("london"):
178178
kw_arguments["base_fee_per_gas"] = self.env.base_fee_per_gas
179179

180-
if self.fork.is_after_fork("ethereum.forks.paris"):
180+
if self.fork.is_after_fork("paris"):
181181
kw_arguments["prev_randao"] = self.env.prev_randao
182182
else:
183183
kw_arguments["difficulty"] = self.env.block_difficulty
184184

185-
if self.fork.is_after_fork("ethereum.forks.cancun"):
185+
if self.fork.is_after_fork("cancun"):
186186
kw_arguments["parent_beacon_block_root"] = (
187187
self.env.parent_beacon_block_root
188188
)
@@ -252,14 +252,14 @@ def run_state_test(self) -> Any:
252252
self.result.rejected = self.txs.rejected_txs
253253

254254
def _run_blockchain_test(self, block_env: Any, block_output: Any) -> None:
255-
if self.fork.is_after_fork("ethereum.forks.prague"):
255+
if self.fork.is_after_fork("prague"):
256256
self.fork.process_unchecked_system_transaction(
257257
block_env=block_env,
258258
target_address=self.fork.HISTORY_STORAGE_ADDRESS,
259259
data=block_env.block_hashes[-1], # The parent hash
260260
)
261261

262-
if self.fork.is_after_fork("ethereum.forks.cancun"):
262+
if self.fork.is_after_fork("cancun"):
263263
self.fork.process_unchecked_system_transaction(
264264
block_env=block_env,
265265
target_address=self.fork.BEACON_ROOTS_ADDRESS,
@@ -281,20 +281,20 @@ def _run_blockchain_test(self, block_env: Any, block_output: Any) -> None:
281281
self.restore_state()
282282
self.logger.warning(f"Transaction {i} failed: {e!r}")
283283

284-
if not self.fork.is_after_fork("ethereum.forks.paris"):
284+
if not self.fork.is_after_fork("paris"):
285285
if self.options.state_reward is None:
286286
self.pay_block_rewards(self.fork.BLOCK_REWARD, block_env)
287287
elif self.options.state_reward != -1:
288288
self.pay_block_rewards(
289289
U256(self.options.state_reward), block_env
290290
)
291291

292-
if self.fork.is_after_fork("ethereum.forks.shanghai"):
292+
if self.fork.is_after_fork("shanghai"):
293293
self.fork.process_withdrawals(
294294
block_env, block_output, self.env.withdrawals
295295
)
296296

297-
if self.fork.is_after_fork("ethereum.forks.prague"):
297+
if self.fork.is_after_fork("prague"):
298298
self.fork.process_general_purpose_requests(block_env, block_output)
299299

300300
def run_blockchain_test(self) -> None:

src/ethereum_spec_tools/evm_tools/t8n/env.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, t8n: "T8N", stdin: Optional[Dict] = None):
7777
self.read_withdrawals(data, t8n)
7878

7979
self.parent_beacon_block_root = None
80-
if t8n.fork.is_after_fork("ethereum.forks.cancun"):
80+
if t8n.fork.is_after_fork("cancun"):
8181
if not t8n.options.state_test:
8282
parent_beacon_block_root_hex = data["parentBeaconBlockRoot"]
8383
self.parent_beacon_block_root = (
@@ -96,7 +96,7 @@ def read_excess_blob_gas(self, data: Any, t8n: "T8N") -> None:
9696
self.parent_excess_blob_gas = U64(0)
9797
self.excess_blob_gas = None
9898

99-
if not t8n.fork.is_after_fork("ethereum.forks.cancun"):
99+
if not t8n.fork.is_after_fork("cancun"):
100100
return
101101

102102
if "currentExcessBlobGas" in data:
@@ -131,7 +131,7 @@ def read_excess_blob_gas(self, data: Any, t8n: "T8N") -> None:
131131
else:
132132
self.excess_blob_gas = parent_blob_gas - target_blob_gas_per_block
133133

134-
if t8n.fork.is_after_fork("ethereum.forks.osaka"):
134+
if t8n.fork.is_after_fork("osaka"):
135135
# Under certain conditions specified in EIP-7918, the
136136
# the excess_blob_gas is calculated differently in osaka
137137
assert self.parent_base_fee_per_gas is not None
@@ -170,7 +170,7 @@ def read_base_fee_per_gas(self, data: Any, t8n: "T8N") -> None:
170170
self.parent_base_fee_per_gas = None
171171
self.base_fee_per_gas = None
172172

173-
if t8n.fork.is_after_fork("ethereum.forks.london"):
173+
if t8n.fork.is_after_fork("london"):
174174
if "currentBaseFee" in data:
175175
self.base_fee_per_gas = parse_hex_or_int(
176176
data["currentBaseFee"], Uint
@@ -212,7 +212,7 @@ def read_randao(self, data: Any, t8n: "T8N") -> None:
212212
Read the randao from the data.
213213
"""
214214
self.prev_randao = None
215-
if t8n.fork.is_after_fork("ethereum.forks.paris"):
215+
if t8n.fork.is_after_fork("paris"):
216216
# tf tool might not always provide an
217217
# even number of nibbles in the randao
218218
# This could create issues in the
@@ -233,7 +233,7 @@ def read_withdrawals(self, data: Any, t8n: "T8N") -> None:
233233
Read the withdrawals from the data.
234234
"""
235235
self.withdrawals = None
236-
if t8n.fork.is_after_fork("ethereum.forks.shanghai"):
236+
if t8n.fork.is_after_fork("shanghai"):
237237
self.withdrawals = tuple(
238238
t8n.json_to_withdrawals(wd) for wd in data["withdrawals"]
239239
)
@@ -248,7 +248,7 @@ def read_block_difficulty(self, data: Any, t8n: "T8N") -> None:
248248
self.parent_timestamp = None
249249
self.parent_difficulty = None
250250
self.parent_ommers_hash = None
251-
if t8n.fork.is_after_fork("ethereum.forks.paris"):
251+
if t8n.fork.is_after_fork("paris"):
252252
return
253253
elif "currentDifficulty" in data:
254254
self.block_difficulty = parse_hex_or_int(
@@ -267,7 +267,7 @@ def read_block_difficulty(self, data: Any, t8n: "T8N") -> None:
267267
self.parent_timestamp,
268268
self.parent_difficulty,
269269
]
270-
if t8n.fork.is_after_fork("ethereum.forks.byzantium"):
270+
if t8n.fork.is_after_fork("byzantium"):
271271
if "parentUncleHash" in data:
272272
EMPTY_OMMER_HASH = keccak256(rlp.encode([])) # noqa N806
273273
self.parent_ommers_hash = Hash32(
@@ -286,10 +286,7 @@ def read_block_hashes(self, data: Any, t8n: "T8N") -> None:
286286
Read the block hashes. Returns a maximum of 256 block hashes.
287287
"""
288288
self.parent_hash = None
289-
if (
290-
t8n.fork.is_after_fork("ethereum.forks.prague")
291-
and not t8n.options.state_test
292-
):
289+
if t8n.fork.is_after_fork("prague") and not t8n.options.state_test:
293290
self.parent_hash = Hash32(hex_to_bytes(data["parentHash"]))
294291

295292
# Read the block hashes

src/ethereum_spec_tools/evm_tools/t8n/t8n_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def parse_rlp_tx(self, raw_tx: Any) -> Any:
137137
t8n = self.t8n
138138

139139
tx_rlp = rlp.encode(raw_tx)
140-
if t8n.fork.is_after_fork("ethereum.forks.berlin"):
140+
if t8n.fork.is_after_fork("berlin"):
141141
if isinstance(raw_tx, Bytes):
142142
transaction = t8n.fork.decode_transaction(raw_tx)
143143
self.all_txs.append(raw_tx)
@@ -180,7 +180,7 @@ def parse_json_tx(self, raw_tx: Any) -> Any:
180180
tx = TransactionLoad(raw_tx, t8n.fork).read()
181181
self.all_txs.append(tx)
182182

183-
if t8n.fork.is_after_fork("ethereum.forks.berlin"):
183+
if t8n.fork.is_after_fork("berlin"):
184184
transaction = t8n.fork.decode_transaction(tx)
185185
else:
186186
transaction = tx
@@ -205,14 +205,14 @@ def sign_transaction(self, json_tx: Any) -> None:
205205
tx_decoded = tx
206206

207207
secret_key = hex_to_uint(json_tx["secretKey"][2:])
208-
if t8n.fork.is_after_fork("ethereum.forks.berlin"):
208+
if t8n.fork.is_after_fork("berlin"):
209209
Transaction = t8n.fork.LegacyTransaction # noqa N806
210210
else:
211211
Transaction = t8n.fork.Transaction # noqa N806
212212

213213
v_addend: U256
214214
if isinstance(tx_decoded, Transaction):
215-
if t8n.fork.is_after_fork("ethereum.forks.spurious_dragon"):
215+
if t8n.fork.is_after_fork("spurious_dragon"):
216216
if protected:
217217
signing_hash = t8n.fork.signing_hash_155(
218218
tx_decoded, U64(1)

0 commit comments

Comments
 (0)