ethereum.forks.bpo4.vm.eoa_delegationethereum.forks.bpo5.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

28
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

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

EOA_DELEGATION_MARKER_LENGTH

30
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

31
EOA_DELEGATED_CODE_LENGTH = 23

GAS_AUTH_PER_EMPTY_ACCOUNT

32
GAS_AUTH_PER_EMPTY_ACCOUNT = 25000

REFUND_AUTH_PER_EXISTING_ACCOUNT

33
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500

NULL_ADDRESS

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