ethereum.forks.amsterdam.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

27
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

29
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

30
EOA_DELEGATED_CODE_LENGTH = 23

REFUND_AUTH_PER_EXISTING_ACCOUNT

31
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500

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
    """
37
    Whether the code is a valid delegation designation.
38
39
    Parameters
40
    ----------
41
    code: `bytes`
42
        The code to check.
43
44
    Returns
45
    -------
46
    valid : `bool`
47
        True if the code is a valid delegation designation,
48
        False otherwise.
49
50
    """
51
    if (
52
        len(code) == EOA_DELEGATED_CODE_LENGTH
53
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
54
    ):
55
        return True
56
    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]:
60
    """
61
    Get the address to which the code delegates.
62
63
    Parameters
64
    ----------
65
    code: `bytes`
66
        The code to get the address from.
67
68
    Returns
69
    -------
70
    address : `Optional[Address]`
71
        The address of the delegated code.
72
73
    """
74
    if is_valid_delegation(code):
75
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
76
    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:
80
    """
81
    Recover the authority address from the authorization.
82
83
    Parameters
84
    ----------
85
    authorization
86
        The authorization to recover the authority from.
87
88
    Raises
89
    ------
90
    InvalidSignatureError
91
        If the signature is invalid.
92
93
    Returns
94
    -------
95
    authority : `Address`
96
        The recovered authority address.
97
98
    """
99
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
100
    if y_parity not in (0, 1):
101
        raise InvalidSignatureError("Invalid y_parity in authorization")
102
    if U256(0) >= r or r >= SECP256K1N:
103
        raise InvalidSignatureError("Invalid r value in authorization")
104
    if U256(0) >= s or s > SECP256K1N // U256(2):
105
        raise InvalidSignatureError("Invalid s value in authorization")
106
107
    signing_hash = keccak256(
108
        SET_CODE_TX_MAGIC
109
        + rlp.encode(
110
            (
111
                authorization.chain_id,
112
                authorization.address,
113
                authorization.nonce,
114
            )
115
        )
116
    )
117
118
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
119
    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, Uint] The delegation address and access gas cost.

def calculate_delegation_cost(evm: Evm, ​​address: Address) -> Tuple[bool, Address, Uint]:
125
    """
126
    Get the delegation address and the cost of access from the address.
127
128
    Parameters
129
    ----------
130
    evm : `Evm`
131
        The execution frame.
132
    address : `Address`
133
        The address to check for delegation.
134
135
    Returns
136
    -------
137
    delegation : `Tuple[bool, Address, Uint]`
138
        The delegation address and access gas cost.
139
140
    """
141
    tx_state = evm.message.tx_env.state
142
143
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
144
145
    if not is_valid_delegation(code):
146
        return False, address, Uint(0)
147
148
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
149
150
    if delegated_address in evm.accessed_addresses:
151
        delegation_gas_cost = GasCosts.WARM_ACCESS
152
    else:
153
        delegation_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
154
155
    return True, delegated_address, delegation_gas_cost

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:
159
    """
160
    Set the delegation code for the authorities in the message.
161
162
    Parameters
163
    ----------
164
    message :
165
        Transaction specific items.
166
167
    Returns
168
    -------
169
    refund_counter: `U256`
170
        Refund from authority which already exists in state.
171
172
    """
173
    tx_state = message.tx_env.state
174
    refund_counter = U256(0)
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(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(tx_state, authority):
200
            refund_counter += U256(
201
                GasCosts.AUTH_PER_EMPTY_ACCOUNT
202
                - REFUND_AUTH_PER_EXISTING_ACCOUNT
203
            )
204
205
        if auth.address == NULL_ADDRESS:
206
            code_to_set = b""
207
        else:
208
            code_to_set = EOA_DELEGATION_MARKER + auth.address
209
210
        set_code(tx_state, authority, code_to_set)
211
        increment_nonce(tx_state, authority)
212
213
    if message.code_address is None:
214
        raise InvalidBlock("Invalid type 4 transaction: no target")
215
216
    message.code = get_code(
217
        tx_state,
218
        get_account(tx_state, message.code_address).code_hash,
219
    )
220
221
    return refund_counter