ethereum.forks.prague.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

28
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

30
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

31
EOA_DELEGATED_CODE_LENGTH = 23

NULL_ADDRESS

32
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:
36
    <snip>
51
    return (
52
        len(code) == EOA_DELEGATED_CODE_LENGTH
53
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
54
    )

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]:
58
    <snip>
72
    if is_valid_delegation(code):
73
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
74
    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:
78
    <snip>
97
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
98
    if y_parity not in (0, 1):
99
        raise InvalidSignatureError("Invalid y_parity in authorization")
100
    if U256(0) >= r or r >= SECP256K1N:
101
        raise InvalidSignatureError("Invalid r value in authorization")
102
    if U256(0) >= s or s > SECP256K1N // U256(2):
103
        raise InvalidSignatureError("Invalid s value in authorization")
104
105
    signing_hash = keccak256(
106
        SET_CODE_TX_MAGIC
107
        + rlp.encode(
108
            (
109
                authorization.chain_id,
110
                authorization.address,
111
                authorization.nonce,
112
            )
113
        )
114
    )
115
116
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
117
    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]:
123
    <snip>
139
    tx_state = evm.message.tx_env.state
140
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
141
    if not is_valid_delegation(code):
142
        return False, address, code, Uint(0)
143
144
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
145
    if address in evm.accessed_addresses:
146
        access_gas_cost = GasCosts.WARM_ACCESS
147
    else:
148
        evm.accessed_addresses.add(address)
149
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
150
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
151
152
    return True, address, code, access_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(message: Message, ​​auth: Authorization) -> None | Address:
158
    <snip>
164
    tx_state = message.tx_env.state
165
166
    if auth.chain_id not in (message.block_env.chain_id, U256(0)):
167
        return None
168
169
    if auth.nonce >= U64.MAX_VALUE:
170
        return None
171
172
    try:
173
        authority = recover_authority(auth)
174
    except InvalidSignatureError:
175
        return None
176
177
    message.accessed_addresses.add(authority)
178
179
    authority_account = get_account(tx_state, authority)
180
    authority_code = get_code(tx_state, authority_account.code_hash)
181
182
    if authority_code and not is_valid_delegation(authority_code):
183
        return None
184
185
    authority_nonce = authority_account.nonce
186
    if authority_nonce != auth.nonce:
187
        return None
188
189
    return authority

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:
193
    <snip>
207
    tx_state = message.tx_env.state
208
    refund_counter = U256(0)
209
    for auth in message.tx_env.authorizations:
210
        match validate_authorization(message, auth):
211
            case None:
212
                continue
213
            case authority:
214
                pass
215
216
        if account_exists(tx_state, authority):
217
            refund_counter += U256(
218
                GasCosts.AUTH_PER_EMPTY_ACCOUNT
219
                - GasCosts.REFUND_AUTH_PER_EXISTING_ACCOUNT
220
            )
221
222
        if auth.address == NULL_ADDRESS:
223
            code_to_set = b""
224
        else:
225
            code_to_set = EOA_DELEGATION_MARKER + auth.address
226
        set_code(tx_state, authority, code_to_set)
227
228
        increment_nonce(tx_state, authority)
229
230
    if message.code_address is None:
231
        raise InvalidBlock("Invalid type 4 transaction: no target")
232
233
    message.code = get_code(
234
        tx_state, get_account(tx_state, message.code_address).code_hash
235
    )
236
237
    return refund_counter