ethereum.forks.bogota.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

35
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

37
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

38
EOA_DELEGATED_CODE_LENGTH = 23

NULL_ADDRESS

39
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:
43
    <snip>
58
    return (
59
        len(code) == EOA_DELEGATED_CODE_LENGTH
60
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
61
    )

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
    <snip>
79
    if is_valid_delegation(code):
80
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
81
    return None

resolve_delegated_code_address

Resolve the address of the code a call target executes.

If target_address carries a delegation designation, charge the warm or cold access for the delegated address, warm it, and return it with precompiles disabled; otherwise return target_address unchanged.

def resolve_delegated_code_address(​state: TransactionState, ​​gas_meter: GasMeter, ​​accessed_addresses: Set[Address], ​​target_address: Address​) -> Tuple[Address, bool]:
90
    <snip>
98
    code = get_code(state, get_account(state, target_address).code_hash)
99
    delegated_address = get_delegated_code_address(code)
100
    if delegated_address is None:
101
        return target_address, False
102
103
    if delegated_address in accessed_addresses:
104
        charge_gas_from_meter(gas_meter, GasCosts.WARM_ACCESS)
105
    else:
106
        charge_gas_from_meter(gas_meter, GasCosts.COLD_ACCOUNT_ACCESS)
107
        accessed_addresses.add(delegated_address)
108
109
    return delegated_address, True

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:
113
    <snip>
132
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
133
    if y_parity not in (0, 1):
134
        raise InvalidSignatureError("Invalid y_parity in authorization")
135
    if U256(0) >= r or r >= SECP256K1N:
136
        raise InvalidSignatureError("Invalid r value in authorization")
137
    if U256(0) >= s or s > SECP256K1N // U256(2):
138
        raise InvalidSignatureError("Invalid s value in authorization")
139
140
    signing_hash = keccak256(
141
        SET_CODE_TX_MAGIC
142
        + rlp.encode(
143
            (
144
                authorization.chain_id,
145
                authorization.address,
146
                authorization.nonce,
147
            )
148
        )
149
    )
150
151
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
152
    return Address(keccak256(public_key)[12:32])

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, ExecutionGas] The delegation address and access gas cost.

def calculate_delegation_cost(​evm: Evm, ​​address: Address​) -> Tuple[bool, Address, ExecutionGas]:
158
    <snip>
174
    tx_state = evm.tx_env.state
175
176
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
177
178
    if not is_valid_delegation(code):
179
        return False, address, GasCosts.ZERO
180
181
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
182
183
    if delegated_address in evm.accessed_addresses:
184
        delegation_gas_cost = GasCosts.WARM_ACCESS
185
    else:
186
        delegation_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
187
188
    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 None if the validation was unsuccessful.

def validate_authorization(​block_env: BlockEnvironment, ​​tx_env: TransactionEnvironment, ​​accessed_authorities: Set[Address], ​​auth: Authorization​) -> Optional[Address]:
197
    <snip>
203
    tx_state = tx_env.state
204
205
    if auth.chain_id not in (block_env.chain_id, U256(0)):
206
        return None
207
208
    if auth.nonce >= U64.MAX_VALUE:
209
        return None
210
211
    try:
212
        authority = recover_authority(auth)
213
    except InvalidSignatureError:
214
        return None
215
216
    accessed_authorities.add(authority)
217
218
    authority_account = get_account(tx_state, authority)
219
    authority_code = get_code(tx_state, authority_account.code_hash)
220
221
    if authority_code and not is_valid_delegation(authority_code):
222
        return None
223
224
    authority_nonce = authority_account.nonce
225
    if authority_nonce != auth.nonce:
226
        return None
227
228
    return authority

set_delegation

Apply the EIP-7702 authorizations and charge their state-dependent costs at the top frame.

Each valid authorization is charged, on top of the state-independent GasCosts.EXECUTION_PER_AUTH_BASE_COST already paid in the intrinsic cost:

  • StateGasCosts.NEW_ACCOUNT (state) when the authority's account leaf does not yet exist.

  • GasCosts.ACCOUNT_WRITE (execution) when applying the authorization is the transaction's first write to the authority's leaf. Writes the transaction already prices elsewhere are exempt: the sender's, covered by TX_BASE, and, for a value-bearing transaction, the recipient's, covered by TX_VALUE_COST. Repeated authorizations on one authority pay it once.

  • StateGasCosts.AUTH_BASE (state) when a net-new delegation indicator is written: the authority held no delegation before the transaction, none was set for it earlier in the transaction, and this authorization sets one. It is charged at most once per authority and is never credited back -- a delegation set and then cleared in the same transaction keeps its charge.

These costs depend on the authority's current state and so cannot be charged in the intrinsic cost. Insufficient gas raises an OutOfGasError; the caller rolls back the authorizations applied so far and halts the top frame.

Parameters

block_env : Environment for the Ethereum Virtual Machine. tx_env : Environment for the transaction. gas_meter : Gas meter of the top-level frame.

Returns

accessed_authorities : Set[Address] Authorities recovered from the authorizations, warmed for the transaction.

def set_delegation(​block_env: BlockEnvironment, ​​tx_env: TransactionEnvironment, ​​gas_meter: GasMeter​) -> Set[Address]:
236
    <snip>
281
    top_level_context = tx_env.top_level_context
282
    assert top_level_context is not None and not top_level_context.is_create
283
    tx_state = tx_env.state
284
    # Authorities a delegation was set for earlier in this transaction.
285
    accessed_authorities: Set[Address] = set()
286
    delegation_set_for: Set[Address] = set()
287
    for auth in tx_env.authorizations:
288
        match validate_authorization(
289
            block_env, tx_env, accessed_authorities, auth
290
        ):
291
            case None:
292
                continue
293
            case authority:
294
                pass
295
296
        if not account_exists(tx_state, authority):
297
            charge_state_gas_from_meter(gas_meter, StateGasCosts.NEW_ACCOUNT)
298
299
        if authority not in tx_env.accounts_with_paid_writes:
300
            charge_gas_from_meter(gas_meter, GasCosts.ACCOUNT_WRITE)
301
            tx_env.accounts_with_paid_writes.add(authority)
302
303
        pre_state_authority_account = get_pre_state_account(
304
            tx_state, authority
305
        )
306
        pre_state_authority_code = get_code(
307
            tx_state, pre_state_authority_account.code_hash
308
        )
309
        delegated_before_tx = is_valid_delegation(pre_state_authority_code)
310
311
        if auth.address == NULL_ADDRESS:
312
            code_to_set = b""
313
        else:
314
            if not delegated_before_tx and authority not in delegation_set_for:
315
                charge_state_gas_from_meter(gas_meter, StateGasCosts.AUTH_BASE)
316
            delegation_set_for.add(authority)
317
            code_to_set = EOA_DELEGATION_MARKER + auth.address
318
        set_code(tx_state, authority, code_to_set)
319
320
        increment_nonce(tx_state, authority)
321
322
    return accessed_authorities