ethereum.forks.osaka.vm.eoa_delegationethereum.forks.bpo1.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
137
    code = get_account(state, address).code
138
    if not is_valid_delegation(code):
139
        return False, address, code, Uint(0)
140
141
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
142
    if address in evm.accessed_addresses:
143
        access_gas_cost = GAS_WARM_ACCESS
144
    else:
145
        evm.accessed_addresses.add(address)
146
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
147
    code = get_account(state, address).code
148
149
    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:
153
    """
154
    Set the delegation code for the authorities in the message.
155
156
    Parameters
157
    ----------
158
    message :
159
        Transaction specific items.
160
161
    Returns
162
    -------
163
    refund_counter: `U256`
164
        Refund from authority which already exists in state.
165
166
    """
167
    state = message.block_env.state
168
    refund_counter = U256(0)
169
    for auth in message.tx_env.authorizations:
170
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
171
            continue
172
173
        if auth.nonce >= U64.MAX_VALUE:
174
            continue
175
176
        try:
177
            authority = recover_authority(auth)
178
        except InvalidSignatureError:
179
            continue
180
181
        message.accessed_addresses.add(authority)
182
183
        authority_account = get_account(state, authority)
184
        authority_code = authority_account.code
185
186
        if authority_code and not is_valid_delegation(authority_code):
187
            continue
188
189
        authority_nonce = authority_account.nonce
190
        if authority_nonce != auth.nonce:
191
            continue
192
193
        if account_exists(state, authority):
194
            refund_counter += U256(PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST)
195
196
        if auth.address == NULL_ADDRESS:
197
            code_to_set = b""
198
        else:
199
            code_to_set = EOA_DELEGATION_MARKER + auth.address
200
        set_code(state, authority, code_to_set)
201
202
        increment_nonce(state, authority)
203
204
    if message.code_address is None:
205
        raise InvalidBlock("Invalid type 4 transaction: no target")
206
207
    message.code = get_account(state, message.code_address).code
208
209
    return refund_counter