Skip to content

Commit 610d538

Browse files
authored
refactor(specs): make State fork agnostic (#2381)
* refactor(spec): make Trie fork-agnostic for amsterdam Move all trie logic to a shared `src/ethereum/trie.py`, removing the fork-specific `src/ethereum/forks/amsterdam/trie.py`. Also move trie internal node types (LeafNode, ExtensionNode, BranchNode, InternalNode) from `ethereum.state` into `ethereum.trie`, and trim `ethereum.state` to just account types and the PreState protocol. - New `ethereum/trie.py`: Trie, trie_set/get/copy, root, patricialize, encode_node/account/internal_node, EMPTY_TRIE_ROOT, and node types - `ethereum/state.py`: remove trie node types; import InternalNode from ethereum.trie under TYPE_CHECKING to break the runtime cycle - `amsterdam/trie.py`: deleted; consumers updated to import directly from ethereum.trie - `fork_loader.py`: fall back to ethereum.trie when a fork has no fork-specific trie module * refactor(spec): make State fork-agnostic for amsterdam Move the `State` class and all state helper functions from `ethereum/forks/amsterdam/state.py` into the shared `ethereum/state.py`, making the state implementation fork-agnostic for amsterdam. To break the circular import that would arise between `state.py` and `trie.py`, introduce `ethereum/types.py` as a thin module holding the base account types (`Account`, `Address`, `Root`, `EMPTY_CODE_HASH`, `EMPTY_ACCOUNT`). `trie.py` now imports from `types.py`, allowing `state.py` to import from both `types.py` and `trie.py` without a cycle. `ethereum/state.py` re-exports the base types so that the ~260 existing `from ethereum.state import ...` call sites across all forks require no changes. `fork_loader.py` and `t8n_types.py` are updated to fall back to the shared `ethereum.state` module when the fork-specific `state` module no longer exists. * refactor(tests): fall back to ethereum.state in VmTestLoader Add `_state_module()` helper to `VmTestLoader` that tries the fork-specific state module first and falls back to the shared `ethereum.state` when the fork no longer ships its own `state.py` (e.g. after the fork-agnostic-state refactor). TODO: remove the fallback once the state module is ported over to the older forks. * fix(tests): fix other json_infra tests * fix(docs): fix docs references * refactor(spec): address review feedback on encode_node Add a note explaining why `EMPTY_TRIE_ROOT` uses `Hash32` rather than `Root` (circular import), unify the `storage_root` annotation on `encode_node` to use `Bytes | None` for consistency with the `node` parameter, and tighten `encode_node`'s `node` parameter to `Extended` (non-nullable) now that `_prepare_trie` narrows `value` away from `None` before calling in.
1 parent 49b882e commit 610d538

12 files changed

Lines changed: 434 additions & 359 deletions

File tree

src/ethereum/forks/amsterdam/blocks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class Header:
112112
Ethereum world state after applying the block's state changes.
113113
114114
[`keccak256`]: ref:ethereum.crypto.hash.keccak256
115-
[changes]: ref:ethereum.forks.amsterdam.state.State.compute_state_root_and_trie_changes
116-
[Trie]: ref:ethereum.forks.amsterdam.trie.Trie
115+
[changes]: ref:ethereum.state.State.compute_state_root_and_trie_changes
116+
[Trie]: ref:ethereum.merkle_patricia_trie.Trie
117117
""" # noqa: E501
118118

119119
transactions_root: Root
@@ -124,8 +124,8 @@ class Header:
124124
transactions as the parameter.
125125
126126
[`keccak256`]: ref:ethereum.crypto.hash.keccak256
127-
[`root()`]: ref:ethereum.forks.amsterdam.trie.root
128-
[Trie]: ref:ethereum.forks.amsterdam.trie.Trie
127+
[`root()`]: ref:ethereum.merkle_patricia_trie.root
128+
[Trie]: ref:ethereum.merkle_patricia_trie.Trie
129129
"""
130130

131131
receipt_root: Root
@@ -135,8 +135,8 @@ class Header:
135135
function over the Merkle-Patricia [trie] constructed from the receipts.
136136
137137
[`keccak256`]: ref:ethereum.crypto.hash.keccak256
138-
[`root()`]: ref:ethereum.forks.amsterdam.trie.root
139-
[Trie]: ref:ethereum.forks.amsterdam.trie.Trie
138+
[`root()`]: ref:ethereum.merkle_patricia_trie.root
139+
[Trie]: ref:ethereum.merkle_patricia_trie.Trie
140140
"""
141141

142142
bloom: Bloom

src/ethereum/forks/amsterdam/fork.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
NonceMismatchError,
3030
)
3131
from ethereum.forks.bpo5.blocks import Header as PreviousHeader
32-
from ethereum.state import EMPTY_CODE_HASH, Address, BlockDiff, PreState
32+
from ethereum.merkle_patricia_trie import root, trie_set
33+
from ethereum.state import (
34+
EMPTY_CODE_HASH,
35+
Address,
36+
BlockDiff,
37+
State,
38+
apply_changes_to_state,
39+
)
3340

3441
from . import vm
3542
from .block_access_lists import (
@@ -60,10 +67,6 @@
6067
compute_requests_hash,
6168
parse_deposit_requests,
6269
)
63-
from .state import (
64-
State,
65-
apply_changes_to_state,
66-
)
6770
from .state_tracker import (
6871
BlockState,
6972
TransactionState,
@@ -89,7 +92,6 @@
8992
recover_sender,
9093
validate_transaction,
9194
)
92-
from .trie import root, trie_set
9395
from .utils.hexadecimal import hex_to_address
9496
from .utils.message import prepare_message
9597
from .vm import Message
@@ -263,7 +265,7 @@ def state_transition(chain: BlockChain, block: Block) -> None:
263265

264266
def execute_block(
265267
block: Block,
266-
pre_state: PreState,
268+
pre_state: State,
267269
chain_context: ChainContext,
268270
) -> BlockDiff:
269271
"""

src/ethereum/forks/amsterdam/requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
from ethereum_types.numeric import Uint, ulen
3636

3737
from ethereum.exceptions import InvalidBlock
38+
from ethereum.merkle_patricia_trie import trie_get
3839
from ethereum.utils.hexadecimal import hex_to_bytes32
3940

4041
from .blocks import decode_receipt
41-
from .trie import trie_get
4242
from .utils.hexadecimal import hex_to_address
4343
from .vm import BlockOutput
4444

src/ethereum/forks/amsterdam/state.py

Lines changed: 0 additions & 222 deletions
This file was deleted.

src/ethereum/forks/amsterdam/vm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
from ethereum.crypto.hash import Hash32
2222
from ethereum.exceptions import EthereumException
23+
from ethereum.merkle_patricia_trie import Trie
2324
from ethereum.state import Address
2425

2526
from ..block_access_lists import BlockAccessList, BlockAccessListBuilder
2627
from ..blocks import Log, Receipt, Withdrawal
2728
from ..fork_types import Authorization, VersionedHash
2829
from ..state_tracker import BlockState, TransactionState
2930
from ..transactions import LegacyTransaction
30-
from ..trie import Trie
3131

3232
__all__ = ("Environment", "Evm", "Message")
3333

0 commit comments

Comments
 (0)