ethereum.forks.bpo5.vm.eoa_delegationethereum.forks.amsterdam.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

31
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

32
EOA_DELEGATION_MARKER = b"\xef\x01\x00"

EOA_DELEGATION_MARKER_LENGTH

33
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

34
EOA_DELEGATED_CODE_LENGTH = 23

PER_EMPTY_ACCOUNT_COST

35
PER_EMPTY_ACCOUNT_COST = 25000

PER_AUTH_BASE_COST

36
PER_AUTH_BASE_COST = 12500

NULL_ADDRESS

37
NULL_ADDRESS = hex_to_address("0x0000000000000000000000000000000000000000")

is_valid_delegation

Whether the code is a valid delegation designation.

Parameters

code: bytes The code to check.

Returns

valid : bool True if the code is a valid delegation designation, False otherwise.

def is_valid_delegation(code: bytes) -> bool:
41
    """
42
    Whether the code is a valid delegation designation.
43
44
    Parameters
45
    ----------
46
    code: `bytes`
47
        The code to check.
48
49
    Returns
50
    -------
51
    valid : `bool`
52
        True if the code is a valid delegation designation,
53
        False otherwise.
54
55
    """
56
    if (
57
        len(code) == EOA_DELEGATED_CODE_LENGTH
58
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
59
    ):
60
        return True
61
    return False

get_delegated_code_address

Get the address to which the code delegates.

Parameters

code: bytes The code to get the address from.

Returns

address : Optional[Address] The address of the delegated code.

def get_delegated_code_address(code: bytes) -> Optional[Address]:
65
    """
66
    Get the address to which the code delegates.
67
68
    Parameters
69
    ----------
70
    code: `bytes`
71
        The code to get the address from.
72
73
    Returns
74
    -------
75
    address : `Optional[Address]`
76
        The address of the delegated code.
77
78
    """
79
    if is_valid_delegation(code):
80
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
81
    return None

recover_authority

Recover the authority address from the authorization.

Parameters

authorization The authorization to recover the authority from.

Raises

InvalidSignatureError If the signature is invalid.

Returns

authority : Address The recovered authority address.

def recover_authority(authorization: Authorization) -> Address:
85
    """
86
    Recover the authority address from the authorization.
87
88
    Parameters
89
    ----------
90
    authorization
91
        The authorization to recover the authority from.
92
93
    Raises
94
    ------
95
    InvalidSignatureError
96
        If the signature is invalid.
97
98
    Returns
99
    -------
100
    authority : `Address`
101
        The recovered authority address.
102
103
    """
104
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
105
    if y_parity not in (0, 1):
106
        raise InvalidSignatureError("Invalid y_parity in authorization")
107
    if U256(0) >= r or r >= SECP256K1N:
108
        raise InvalidSignatureError("Invalid r value in authorization")
109
    if U256(0) >= s or s > SECP256K1N // U256(2):
110
        raise InvalidSignatureError("Invalid s value in authorization")
111
112
    signing_hash = keccak256(
113
        SET_CODE_TX_MAGIC
114
        + rlp.encode(
115
            (
116
                authorization.chain_id,
117
                authorization.address,
118
                authorization.nonce,
119
            )
120
        )
121
    )
122
123
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
124
    return Address(keccak256(public_key)[12:32])

access_delegation

Get the delegation address, code, and the cost of access from the address.

Parameters

evm : Evm The execution frame. address : Address The address to get the delegation from.

Returns

delegation : Tuple[bool, Address, Bytes, Uint] The delegation address, code, and access gas cost.

def access_delegation(evm: Evm, ​​address: Address) -> Tuple[bool, Address, Bytes, Uint]:
120
    """
121
    Get the delegation address, code, and the cost of access from the address.
122
123
    Parameters
124
    ----------
125
    evm : `Evm`
126
        The execution frame.
127
    address : `Address`
128
        The address to get the delegation from.
129
130
    Returns
131
    -------
132
    delegation : `Tuple[bool, Address, Bytes, Uint]`
133
        The delegation address, code, and access gas cost.
134
135
    """
136
    state = evm.message.block_env.state
137
    code = get_account(state, address).code
138
    if not is_valid_delegation(code):
139
        return False, address, code, Uint(0)
140
141
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
142
    if address in evm.accessed_addresses:
143
        access_gas_cost = GAS_WARM_ACCESS
144
    else:
145
        evm.accessed_addresses.add(address)
146
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
147
    code = get_account(state, address).code
148
149
    return True, address, code, access_gas_cost

calculate_delegation_cost

Get the delegation address and the cost of access from the address.

Parameters

evm : Evm The execution frame. address : Address The address to check for delegation.

Returns

delegation : Tuple[bool, Address, Uint] The delegation address and access gas cost.

def calculate_delegation_cost(evm: Evm, ​​address: Address) -> Tuple[bool, Address, Uint]:
130
    """
131
    Get the delegation address and the cost of access from the address.
132
133
    Parameters
134
    ----------
135
    evm : `Evm`
136
        The execution frame.
137
    address : `Address`
138
        The address to check for delegation.
139
140
    Returns
141
    -------
142
    delegation : `Tuple[bool, Address, Uint]`
143
        The delegation address and access gas cost.
144
145
    """
146
    state = evm.message.block_env.state
147
148
    code = get_account(state, address).code
149
    track_address(evm.state_changes, address)
150
151
    if not is_valid_delegation(code):
152
        return False, address, Uint(0)
153
154
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
155
156
    if delegated_address in evm.accessed_addresses:
157
        delegation_gas_cost = GAS_WARM_ACCESS
158
    else:
159
        delegation_gas_cost = GAS_COLD_ACCOUNT_ACCESS
160
161
    return True, delegated_address, delegation_gas_cost

set_delegation

Set the delegation code for the authorities in the message.

Parameters

message : Transaction specific items.

Returns

refund_counter: U256 Refund from authority which already exists in state.

def set_delegation(message: Message) -> U256:
165
    """
166
    Set the delegation code for the authorities in the message.
167
168
    Parameters
169
    ----------
170
    message :
171
        Transaction specific items.
172
173
    Returns
174
    -------
175
    refund_counter: `U256`
176
        Refund from authority which already exists in state.
177
178
    """
179
    state = message.block_env.state
180
    refund_counter = U256(0)
181
    for auth in message.tx_env.authorizations:
182
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
183
            continue
184
185
        if auth.nonce >= U64.MAX_VALUE:
186
            continue
187
188
        try:
189
            authority = recover_authority(auth)
190
        except InvalidSignatureError:
191
            continue
192
193
        message.accessed_addresses.add(authority)
194
195
        authority_account = get_account(state, authority)
196
        authority_code = authority_account.code
197
        track_address(message.tx_env.state_changes, authority)
198
199
        if authority_code and not is_valid_delegation(authority_code):
200
            continue
201
202
        authority_nonce = authority_account.nonce
203
        if authority_nonce != auth.nonce:
204
            continue
205
206
        if account_exists(state, authority):
207
            refund_counter += U256(PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST)
208
209
        if auth.address == NULL_ADDRESS:
210
            code_to_set = b""
211
        else:
212
            code_to_set = EOA_DELEGATION_MARKER + auth.address
200
        set_code(state, authority, code_to_set)
213
202
        increment_nonce(state, authority)
214
        tx_frame = message.tx_env.state_changes
215
        # EIP-7928: Capture pre-code before any changes
216
        capture_pre_code(tx_frame, authority, authority_code)
217
218
        set_authority_code(state, authority, code_to_set)
219
220
        if authority_code != code_to_set:
221
            # Track code change if different from current
222
            track_code_change(tx_frame, authority, code_to_set)
223
224
        increment_nonce(state, authority)
225
        nonce_after = get_account(state, authority).nonce
226
        track_nonce_change(tx_frame, authority, U64(nonce_after))
227
228
    if message.code_address is None:
229
        raise InvalidBlock("Invalid type 4 transaction: no target")
230
231
    message.code = get_account(state, message.code_address).code
232
233
    return refund_counter