ethereum.forks.amsterdam.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 |
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 |
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 byTX_BASE, and, for a value-bearing transaction, the recipient's, covered byTX_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 | assert not tx_env.is_create |
| 282 | tx_state = tx_env.state |
| 283 | # Authorities a delegation was set for earlier in this transaction. |
| 284 | accessed_authorities: Set[Address] = set() |
| 285 | delegation_set_for: Set[Address] = set() |
| 286 | for auth in tx_env.authorizations: |
| 287 | match validate_authorization( |
| 288 | block_env, tx_env, accessed_authorities, auth |
| 289 | ): |
| 290 | case None: |
| 291 | continue |
| 292 | case authority: |
| 293 | pass |
| 294 | |
| 295 | if not account_exists(tx_state, authority): |
| 296 | charge_state_gas_from_meter(gas_meter, StateGasCosts.NEW_ACCOUNT) |
| 297 | |
| 298 | if authority not in tx_env.accounts_with_paid_writes: |
| 299 | charge_gas_from_meter(gas_meter, GasCosts.ACCOUNT_WRITE) |
| 300 | tx_env.accounts_with_paid_writes.add(authority) |
| 301 | |
| 302 | pre_state_authority_account = get_pre_state_account( |
| 303 | tx_state, authority |
| 304 | ) |
| 305 | pre_state_authority_code = get_code( |
| 306 | tx_state, pre_state_authority_account.code_hash |
| 307 | ) |
| 308 | delegated_before_tx = is_valid_delegation(pre_state_authority_code) |
| 309 | |
| 310 | if auth.address == NULL_ADDRESS: |
| 311 | code_to_set = b"" |
| 312 | else: |
| 313 | if not delegated_before_tx and authority not in delegation_set_for: |
| 314 | charge_state_gas_from_meter(gas_meter, StateGasCosts.AUTH_BASE) |
| 315 | delegation_set_for.add(authority) |
| 316 | code_to_set = EOA_DELEGATION_MARKER + auth.address |
| 317 | |
| 318 | set_code(tx_state, authority, code_to_set) |
| 319 | increment_nonce(tx_state, authority) |
| 320 | |
| 321 | return accessed_authorities |