ethereum.state

Shared state types and the PreState protocol used by the state transition function.

The PreState protocol specifies the operations that any pre-execution state provider must support, allowing multiple backing implementations (in-memory dict, on-disk database, witness, etc.).

Address

20
Address = Bytes20

Root

21
Root = Hash32

Account

State associated with an address.

24
@slotted_freezable
25
@dataclass
class Account:

nonce

31
    nonce: Uint

balance

32
    balance: U256

code

33
    code: Bytes

EMPTY_ACCOUNT

36
EMPTY_ACCOUNT = Account(
37
    nonce=Uint(0),
38
    balance=U256(0),
39
    code=b"",
40
)

LeafNode

Leaf node in the Merkle Trie.

43
@slotted_freezable
44
@dataclass
class LeafNode:

rest_of_key

48
    rest_of_key: Bytes

value

49
    value: Extended

ExtensionNode

Extension node in the Merkle Trie.

52
@slotted_freezable
53
@dataclass
class ExtensionNode:

key_segment

57
    key_segment: Bytes

subnode

58
    subnode: Extended

BranchSubnodes

61
BranchSubnodes = Tuple[
62
    Extended,
63
    Extended,
64
    Extended,
65
    Extended,
66
    Extended,
67
    Extended,
68
    Extended,
69
    Extended,
70
    Extended,
71
    Extended,
72
    Extended,
73
    Extended,
74
    Extended,
75
    Extended,
76
    Extended,
77
    Extended,
78
]

BranchNode

Branch node in the Merkle Trie.

81
@slotted_freezable
82
@dataclass
class BranchNode:

subnodes

86
    subnodes: BranchSubnodes

value

87
    value: Extended

InternalNode

90
InternalNode = LeafNode | ExtensionNode | BranchNode

PreState

Protocol for providing pre-execution state.

Specify the operations that any pre-state provider (dict, database, witness, etc.) must support for the EELS state transition.

class PreState:

get_account_optional

Get the account at an address.

Return None if there is no account at the address.

def get_account_optional(self, ​​address: Address) -> Optional[Account]:
102
        """
103
        Get the account at an address.
104
105
        Return ``None`` if there is no account at the address.
106
        """
107
        ...

get_storage

Get a storage value.

Return U256(0) if the key has not been set.

def get_storage(self, ​​address: Address, ​​key: Bytes32) -> U256:
110
        """
111
        Get a storage value.
112
113
        Return ``U256(0)`` if the key has not been set.
114
        """
115
        ...

account_has_storage

Check whether an account has any storage.

Only needed for EIP-7610.

def account_has_storage(self, ​​address: Address) -> bool:
118
        """
119
        Check whether an account has any storage.
120
121
        Only needed for EIP-7610.
122
        """
123
        ...

compute_state_root_and_trie_changes

Compute the state root after applying changes to the pre-state.

Return the new state root together with the internal trie nodes that were created or modified.

def compute_state_root_and_trie_changes(self, ​​account_changes: Dict[Address, Optional[Account]], ​​storage_changes: Dict[Address, Dict[Bytes32, U256]]) -> Tuple[Root, List[InternalNode]]:
130
        """
131
        Compute the state root after applying changes to the pre-state.
132
133
        Return the new state root together with the internal trie nodes
134
        that were created or modified.
135
        """
136
        ...