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

PER_EMPTY_ACCOUNT_COST

31
PER_EMPTY_ACCOUNT_COST = 25000

PER_AUTH_BASE_COST

32
PER_AUTH_BASE_COST = 12500

NULL_ADDRESS

33
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:
37
    """
38
    Whether the code is a valid delegation designation.
39
40
    Parameters
41
    ----------
42
    code: `bytes`
43
        The code to check.
44
45
    Returns
46
    -------
47
    valid : `bool`
48
        True if the code is a valid delegation designation,
49
        False otherwise.
50
51
    """
52
    if (
53
        len(code) == EOA_DELEGATED_CODE_LENGTH
54
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
55
    ):
56
        return True
57
    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]:
61
    """
62
    Get the address to which the code delegates.
63
64
    Parameters
65
    ----------
66
    code: `bytes`
67
        The code to get the address from.
68
69
    Returns
70
    -------
71
    address : `Optional[Address]`
72
        The address of the delegated code.
73
74
    """
75
    if is_valid_delegation(code):
76
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
77
    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:
81
    """
82
    Recover the authority address from the authorization.
83
84
    Parameters
85
    ----------
86
    authorization
87
        The authorization to recover the authority from.
88
89
    Raises
90
    ------
91
    InvalidSignatureError
92
        If the signature is invalid.
93
94
    Returns
95
    -------
96
    authority : `Address`
97
        The recovered authority address.
98
99
    """
100
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
101
    if y_parity not in (0, 1):
102
        raise InvalidSignatureError("Invalid y_parity in authorization")
103
    if U256(0) >= r or r >= SECP256K1N:
104
        raise InvalidSignatureError("Invalid r value in authorization")
105
    if U256(0) >= s or s > SECP256K1N // U256(2):
106
        raise InvalidSignatureError("Invalid s value in authorization")
107
108
    signing_hash = keccak256(
109
        SET_CODE_TX_MAGIC
110
        + rlp.encode(
111
            (
112
                authorization.chain_id,
113
                authorization.address,
114
                authorization.nonce,
115
            )
116
        )
117
    )
118
119
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
120
    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]:
126
    """
127
    Get the delegation address 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 check for delegation.
135
136
    Returns
137
    -------
138
    delegation : `Tuple[bool, Address, Uint]`
139
        The delegation address and access gas cost.
140
141
    """
142
    tx_state = evm.message.tx_env.state
143
144
    code = get_account(tx_state, address).code
145
    track_address(tx_state, address)
146
147
    if not is_valid_delegation(code):
148
        return False, address, Uint(0)
149
150
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
151
152
    if delegated_address in evm.accessed_addresses:
153
        delegation_gas_cost = GAS_WARM_ACCESS
154
    else:
155
        delegation_gas_cost = GAS_COLD_ACCOUNT_ACCESS
156
157
    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:
161
    """
162
    Set the delegation code for the authorities in the message.
163
164
    Parameters
165
    ----------
166
    message :
167
        Transaction specific items.
168
169
    Returns
170
    -------
171
    refund_counter: `U256`
172
        Refund from authority which already exists in state.
173
174
    """
175
    tx_state = message.tx_env.state
176
    refund_counter = U256(0)
177
    for auth in message.tx_env.authorizations:
178
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
179
            continue
180
181
        if auth.nonce >= U64.MAX_VALUE:
182
            continue
183
184
        try:
185
            authority = recover_authority(auth)
186
        except InvalidSignatureError:
187
            continue
188
189
        message.accessed_addresses.add(authority)
190
191
        authority_account = get_account(tx_state, authority)
192
        authority_code = authority_account.code
193
        track_address(tx_state, authority)
194
195
        if authority_code and not is_valid_delegation(authority_code):
196
            continue
197
198
        authority_nonce = authority_account.nonce
199
        if authority_nonce != auth.nonce:
200
            continue
201
202
        if account_exists(tx_state, authority):
203
            refund_counter += U256(PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST)
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_account(tx_state, message.code_address).code
217
218
    return refund_counter