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

Set EOA account code.

SET_CODE_TX_MAGIC

32
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

34
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

35
EOA_DELEGATED_CODE_LENGTH = 23

REFUND_AUTH_PER_EXISTING_ACCOUNT

32
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500

NULL_ADDRESS

36
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:
40
    <snip>
55
    return (
56
        len(code) == EOA_DELEGATED_CODE_LENGTH
57
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
58
    )

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]:
62
    <snip>
76
    if is_valid_delegation(code):
77
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
78
    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:
82
    <snip>
101
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
102
    if y_parity not in (0, 1):
103
        raise InvalidSignatureError("Invalid y_parity in authorization")
104
    if U256(0) >= r or r >= SECP256K1N:
105
        raise InvalidSignatureError("Invalid r value in authorization")
106
    if U256(0) >= s or s > SECP256K1N // U256(2):
107
        raise InvalidSignatureError("Invalid s value in authorization")
108
109
    signing_hash = keccak256(
110
        SET_CODE_TX_MAGIC
111
        + rlp.encode(
112
            (
113
                authorization.chain_id,
114
                authorization.address,
115
                authorization.nonce,
116
            )
117
        )
118
    )
119
120
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
121
    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]:
124
    <snip>
140
    tx_state = evm.message.tx_env.state
141
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
142
    if not is_valid_delegation(code):
143
        return False, address, code, Uint(0)
144
145
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
146
    if address in evm.accessed_addresses:
147
        access_gas_cost = GasCosts.WARM_ACCESS
148
    else:
149
        evm.accessed_addresses.add(address)
150
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
151
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
152
153
    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]:
127
    <snip>
143
    tx_state = evm.message.tx_env.state
144
145
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
146
147
    if not is_valid_delegation(code):
148
        return False, address, Uint(0)
149
150
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
151
152
    if delegated_address in evm.accessed_addresses:
153
        delegation_gas_cost = GasCosts.WARM_ACCESS
154
    else:
155
        delegation_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
156
157
    return True, delegated_address, delegation_gas_cost

validate_authorization

Check if the given Authorization is valid against the current state.

Returns the authority address or address and its code, or None if the validation was if the unsuccessful.validation was unsuccessful.

def validate_authorization(message: Message, ​​auth: Authorization) -> None | AddressTuple[Address, Bytes]:
163
    <snip>
169
    tx_state = message.tx_env.state
170
171
    if auth.chain_id not in (message.block_env.chain_id, U256(0)):
172
        return None
173
174
    if auth.nonce >= U64.MAX_VALUE:
175
        return None
176
177
    try:
178
        authority = recover_authority(auth)
179
    except InvalidSignatureError:
180
        return None
181
182
    message.accessed_addresses.add(authority)
183
184
    authority_account = get_account(tx_state, authority)
185
    authority_code = get_code(tx_state, authority_account.code_hash)
186
187
    if authority_code and not is_valid_delegation(authority_code):
188
        return None
189
190
    authority_nonce = authority_account.nonce
191
    if authority_nonce != auth.nonce:
192
        return None
193
190
    return authority
194
    return (authority, authority_code)

set_delegation

Set the delegation code for the authorities in the message.

Refills StateGasCosts.NEW_ACCOUNT when the authority's account leaf already exists, and StateGasCosts.AUTH_BASE when its code slot already holds a delegation indicator. When the authority leaf already exists, the worst-case GasCosts.ACCOUNT_WRITE charged in the intrinsic cost is also refunded to the regular-gas refund counter. The totals are returned so block accounting can subtract the state refill from tx_state_gas and apply the regular refund.

Parameters

message : Transaction specific items.

Returns

refund_counter: state_refund : U256Uint Refund from authority which already exists in state.Total state gas refunded across all processed authorizations. regular_refund : Uint Total regular gas (ACCOUNT_WRITE) refunded for authorities whose account leaf already existed.

def set_delegation(message: Message) -> U256Tuple[Uint, Uint]:
198
    <snip>
223
    tx_state = message.tx_env.state
209
    refund_counter = U256(0)
224
    state_refund = Uint(0)
225
    regular_refund = Uint(0)
226
    for auth in message.tx_env.authorizations:
227
        match validate_authorization(message, auth):
228
            case None:
213
                continue
214
            case authority:
229
                refund = StateGasCosts.AUTH_BASE + StateGasCosts.NEW_ACCOUNT
230
                message.state_gas_reservoir += refund
231
                state_refund += refund
232
                regular_refund += GasCosts.ACCOUNT_WRITE
233
                continue
234
            case (authority, authority_code):
235
                pass
236
237
        refund = StateGas(Uint(0))
238
239
        if account_exists(tx_state, authority):
218
            refund_counter += U256(
219
                GasCosts.AUTH_PER_EMPTY_ACCOUNT
220
                - REFUND_AUTH_PER_EXISTING_ACCOUNT
221
            )
240
            refund += StateGasCosts.NEW_ACCOUNT
241
            # The new-account ACCOUNT_WRITE charged at intrinsic time is
242
            # not needed: refund it to the regular refund counter.
243
            regular_refund += GasCosts.ACCOUNT_WRITE
244
245
        pre_state_authority_account = get_pre_state_account(
246
            tx_state, authority
247
        )
248
        pre_state_authority_code = get_code(
249
            tx_state, pre_state_authority_account.code_hash
250
        )
251
252
        delegated_before_tx = is_valid_delegation(pre_state_authority_code)
253
        delegated_now = is_valid_delegation(authority_code)
254
255
        if auth.address == NULL_ADDRESS:
224
            code_to_set = b""
256
            refund += StateGasCosts.AUTH_BASE
257
258
            if delegated_now and not delegated_before_tx:
259
                refund += StateGasCosts.AUTH_BASE
260
261
            code_to_set = b""
262
        else:
226
            code_to_set = EOA_DELEGATION_MARKER + auth.address
263
            code_to_set = EOA_DELEGATION_MARKER + auth.address
264
265
            if delegated_now or delegated_before_tx:
266
                refund += StateGasCosts.AUTH_BASE
267
268
        set_code(tx_state, authority, code_to_set)
269
        increment_nonce(tx_state, authority)
270
229
        increment_nonce(tx_state, authority)
271
        message.state_gas_reservoir += refund
272
        state_refund += refund
273
274
    if message.code_address is None:
275
        raise InvalidBlock("Invalid type 4 transaction: no target")
276
277
    message.code = get_code(
235
        tx_state, get_account(tx_state, message.code_address).code_hash
278
        tx_state,
279
        get_account(tx_state, message.code_address).code_hash,
280
    )
281
238
    return refund_counter
282
    return state_refund, regular_refund