ethereum.forks.osaka.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

21
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

23
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

24
EOA_DELEGATED_CODE_LENGTH = 23

PER_EMPTY_ACCOUNT_COST

25
PER_EMPTY_ACCOUNT_COST = 25000

PER_AUTH_BASE_COST

26
PER_AUTH_BASE_COST = 12500

NULL_ADDRESS

27
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:
31
    """
32
    Whether the code is a valid delegation designation.
33
34
    Parameters
35
    ----------
36
    code: `bytes`
37
        The code to check.
38
39
    Returns
40
    -------
41
    valid : `bool`
42
        True if the code is a valid delegation designation,
43
        False otherwise.
44
45
    """
46
    if (
47
        len(code) == EOA_DELEGATED_CODE_LENGTH
48
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
49
    ):
50
        return True
51
    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]:
55
    """
56
    Get the address to which the code delegates.
57
58
    Parameters
59
    ----------
60
    code: `bytes`
61
        The code to get the address from.
62
63
    Returns
64
    -------
65
    address : `Optional[Address]`
66
        The address of the delegated code.
67
68
    """
69
    if is_valid_delegation(code):
70
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
71
    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:
75
    """
76
    Recover the authority address from the authorization.
77
78
    Parameters
79
    ----------
80
    authorization
81
        The authorization to recover the authority from.
82
83
    Raises
84
    ------
85
    InvalidSignatureError
86
        If the signature is invalid.
87
88
    Returns
89
    -------
90
    authority : `Address`
91
        The recovered authority address.
92
93
    """
94
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
95
    if y_parity not in (0, 1):
96
        raise InvalidSignatureError("Invalid y_parity in authorization")
97
    if U256(0) >= r or r >= SECP256K1N:
98
        raise InvalidSignatureError("Invalid r value in authorization")
99
    if U256(0) >= s or s > SECP256K1N // U256(2):
100
        raise InvalidSignatureError("Invalid s value in authorization")
101
102
    signing_hash = keccak256(
103
        SET_CODE_TX_MAGIC
104
        + rlp.encode(
105
            (
106
                authorization.chain_id,
107
                authorization.address,
108
                authorization.nonce,
109
            )
110
        )
111
    )
112
113
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
114
    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]:
120
    """
121
    Get the delegation address, code, and the cost of access from the address.
122
123
    Parameters
124
    ----------
125
    evm : `Evm`
126
        The execution frame.
127
    address : `Address`
128
        The address to get the delegation from.
129
130
    Returns
131
    -------
132
    delegation : `Tuple[bool, Address, Bytes, Uint]`
133
        The delegation address, code, and access gas cost.
134
135
    """
136
    state = evm.message.block_env.state
137
138
    code = get_account(state, address).code
139
    if not is_valid_delegation(code):
140
        return False, address, code, Uint(0)
141
142
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
143
    if address in evm.accessed_addresses:
144
        access_gas_cost = GAS_WARM_ACCESS
145
    else:
146
        evm.accessed_addresses.add(address)
147
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
148
    code = get_account(state, address).code
149
150
    return True, address, code, access_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:
154
    """
155
    Set the delegation code for the authorities in the message.
156
157
    Parameters
158
    ----------
159
    message :
160
        Transaction specific items.
161
162
    Returns
163
    -------
164
    refund_counter: `U256`
165
        Refund from authority which already exists in state.
166
167
    """
168
    state = message.block_env.state
169
    refund_counter = U256(0)
170
    for auth in message.tx_env.authorizations:
171
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
172
            continue
173
174
        if auth.nonce >= U64.MAX_VALUE:
175
            continue
176
177
        try:
178
            authority = recover_authority(auth)
179
        except InvalidSignatureError:
180
            continue
181
182
        message.accessed_addresses.add(authority)
183
184
        authority_account = get_account(state, authority)
185
        authority_code = authority_account.code
186
187
        if authority_code and not is_valid_delegation(authority_code):
188
            continue
189
190
        authority_nonce = authority_account.nonce
191
        if authority_nonce != auth.nonce:
192
            continue
193
194
        if account_exists(state, authority):
195
            refund_counter += U256(PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST)
196
197
        if auth.address == NULL_ADDRESS:
198
            code_to_set = b""
199
        else:
200
            code_to_set = EOA_DELEGATION_MARKER + auth.address
201
        set_code(state, authority, code_to_set)
202
203
        increment_nonce(state, authority)
204
205
    if message.code_address is None:
206
        raise InvalidBlock("Invalid type 4 transaction: no target")
207
208
    message.code = get_account(state, message.code_address).code
209
210
    return refund_counter