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

REFUND_AUTH_PER_EXISTING_ACCOUNT

32
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500

NULL_ADDRESS

35
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:
39
    """
40
    Whether the code is a valid delegation designation.
41
42
    Parameters
43
    ----------
44
    code: `bytes`
45
        The code to check.
46
47
    Returns
48
    -------
49
    valid : `bool`
50
        True if the code is a valid delegation designation,
51
        False otherwise.
52
53
    """
54
    if (
55
        len(code) == EOA_DELEGATED_CODE_LENGTH
56
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
57
    ):
58
        return True
59
    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]:
63
    """
64
    Get the address to which the code delegates.
65
66
    Parameters
67
    ----------
68
    code: `bytes`
69
        The code to get the address from.
70
71
    Returns
72
    -------
73
    address : `Optional[Address]`
74
        The address of the delegated code.
75
76
    """
77
    if is_valid_delegation(code):
78
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
79
    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:
83
    """
84
    Recover the authority address from the authorization.
85
86
    Parameters
87
    ----------
88
    authorization
89
        The authorization to recover the authority from.
90
91
    Raises
92
    ------
93
    InvalidSignatureError
94
        If the signature is invalid.
95
96
    Returns
97
    -------
98
    authority : `Address`
99
        The recovered authority address.
100
101
    """
102
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
103
    if y_parity not in (0, 1):
104
        raise InvalidSignatureError("Invalid y_parity in authorization")
105
    if U256(0) >= r or r >= SECP256K1N:
106
        raise InvalidSignatureError("Invalid r value in authorization")
107
    if U256(0) >= s or s > SECP256K1N // U256(2):
108
        raise InvalidSignatureError("Invalid s value in authorization")
109
110
    signing_hash = keccak256(
111
        SET_CODE_TX_MAGIC
112
        + rlp.encode(
113
            (
114
                authorization.chain_id,
115
                authorization.address,
116
                authorization.nonce,
117
            )
118
        )
119
    )
120
121
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
122
    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]:
126
    """
127
    Get the delegation address, code, and the cost of access from the address.
128
129
    Parameters
130
    ----------
131
    evm : `Evm`
132
        The execution frame.
133
    address : `Address`
134
        The address to get the delegation from.
135
136
    Returns
137
    -------
138
    delegation : `Tuple[bool, Address, Bytes, Uint]`
139
        The delegation address, code, and access gas cost.
140
141
    """
142
    state = evm.message.block_env.state
143
    code = get_code(state, get_account(state, address).code_hash)
144
    if not is_valid_delegation(code):
145
        return False, address, code, Uint(0)
146
147
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
148
    if address in evm.accessed_addresses:
149
        access_gas_cost = GasCosts.WARM_ACCESS
150
    else:
151
        evm.accessed_addresses.add(address)
152
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
153
    code = get_code(state, get_account(state, address).code_hash)
154
155
    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]:
128
    """
129
    Get the delegation address and the cost of access from the address.
130
131
    Parameters
132
    ----------
133
    evm : `Evm`
134
        The execution frame.
135
    address : `Address`
136
        The address to check for delegation.
137
138
    Returns
139
    -------
140
    delegation : `Tuple[bool, Address, Uint]`
141
        The delegation address and access gas cost.
142
143
    """
144
    tx_state = evm.message.tx_env.state
145
146
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
147
148
    if not is_valid_delegation(code):
149
        return False, address, Uint(0)
150
151
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
152
153
    if delegated_address in evm.accessed_addresses:
154
        delegation_gas_cost = GasCosts.WARM_ACCESS
155
    else:
156
        delegation_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
157
158
    return True, delegated_address, delegation_gas_cost

set_delegation

Set the delegation code for the authorities in the message.

For existing accounts, refunds the account-creation component of state gas to the reservoir (no mutation of intrinsic_state_gas).

Parameters

message : Transaction specific items.

Returns

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

def set_delegation(message: Message) -> U256None:
162
    """
163
    Set the delegation code for the authorities in the message.
164
165
    For existing accounts, refunds the account-creation component of
166
    state gas to the reservoir (no mutation of intrinsic_state_gas).
167
168
    Parameters
169
    ----------
170
    message :
171
        Transaction specific items.
172
167
    Returns
168
    -------
169
    refund_counter: `U256`
170
        Refund from authority which already exists in state.
171
173
    """
173
    state = message.block_env.state
174
    refund_counter = U256(0)
174
    tx_state = message.tx_env.state
175
    for auth in message.tx_env.authorizations:
176
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
177
            continue
178
179
        if auth.nonce >= U64.MAX_VALUE:
180
            continue
181
182
        try:
183
            authority = recover_authority(auth)
184
        except InvalidSignatureError:
185
            continue
186
187
        message.accessed_addresses.add(authority)
188
189
        authority_account = get_account(state, authority)
190
        authority_code = get_code(state, authority_account.code_hash)
189
        authority_account = get_account(tx_state, authority)
190
        authority_code = get_code(tx_state, authority_account.code_hash)
191
192
        if authority_code and not is_valid_delegation(authority_code):
193
            continue
194
195
        authority_nonce = authority_account.nonce
196
        if authority_nonce != auth.nonce:
197
            continue
198
199
        if account_exists(state, authority):
200
            refund_counter += U256(
201
                GasCosts.AUTH_PER_EMPTY_ACCOUNT
202
                - REFUND_AUTH_PER_EXISTING_ACCOUNT
203
            )
199
        # For existing accounts, no account creation needed.
200
        # Refund the account creation state gas to the reservoir.
201
        # intrinsic_state_gas is immutable after validation.
202
        if account_exists(tx_state, authority):
203
            refund = STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE
204
            message.state_gas_reservoir += refund
205
206
        if auth.address == NULL_ADDRESS:
207
            code_to_set = b""
208
        else:
209
            code_to_set = EOA_DELEGATION_MARKER + auth.address
209
        set_code(state, authority, code_to_set)
210
211
        increment_nonce(state, authority)
211
        set_code(tx_state, authority, code_to_set)
212
        increment_nonce(tx_state, authority)
213
214
    if message.code_address is None:
215
        raise InvalidBlock("Invalid type 4 transaction: no target")
216
216
    message.code = get_code(
217
        state, get_account(state, message.code_address).code_hash
218
    )
219
220
    return refund_counter
217
    message.code = get_code(
218
        tx_state,
219
        get_account(tx_state, message.code_address).code_hash,
220
    )