ethereum.forks.bpo5.vm.eoa_delegationethereum.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

32
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500

NULL_ADDRESS

31
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:
35
    """
36
    Whether the code is a valid delegation designation.
37
38
    Parameters
39
    ----------
40
    code: `bytes`
41
        The code to check.
42
43
    Returns
44
    -------
45
    valid : `bool`
46
        True if the code is a valid delegation designation,
47
        False otherwise.
48
49
    """
50
    if (
51
        len(code) == EOA_DELEGATED_CODE_LENGTH
52
        and code[:EOA_DELEGATION_MARKER_LENGTH] == EOA_DELEGATION_MARKER
53
    ):
54
        return True
55
    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]:
59
    """
60
    Get the address to which the code delegates.
61
62
    Parameters
63
    ----------
64
    code: `bytes`
65
        The code to get the address from.
66
67
    Returns
68
    -------
69
    address : `Optional[Address]`
70
        The address of the delegated code.
71
72
    """
73
    if is_valid_delegation(code):
74
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
75
    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:
79
    """
80
    Recover the authority address from the authorization.
81
82
    Parameters
83
    ----------
84
    authorization
85
        The authorization to recover the authority from.
86
87
    Raises
88
    ------
89
    InvalidSignatureError
90
        If the signature is invalid.
91
92
    Returns
93
    -------
94
    authority : `Address`
95
        The recovered authority address.
96
97
    """
98
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
99
    if y_parity not in (0, 1):
100
        raise InvalidSignatureError("Invalid y_parity in authorization")
101
    if U256(0) >= r or r >= SECP256K1N:
102
        raise InvalidSignatureError("Invalid r value in authorization")
103
    if U256(0) >= s or s > SECP256K1N // U256(2):
104
        raise InvalidSignatureError("Invalid s value in authorization")
105
106
    signing_hash = keccak256(
107
        SET_CODE_TX_MAGIC
108
        + rlp.encode(
109
            (
110
                authorization.chain_id,
111
                authorization.address,
112
                authorization.nonce,
113
            )
114
        )
115
    )
116
117
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
118
    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]:
126
    """
127
    Get the delegation address, code, 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 get the delegation from.
135
136
    Returns
137
    -------
138
    delegation : `Tuple[bool, Address, Bytes, Uint]`
139
        The delegation address, code, and access gas cost.
140
141
    """
142
    state = evm.message.block_env.state
143
    code = get_code(state, get_account(state, address).code_hash)
144
    if not is_valid_delegation(code):
145
        return False, address, code, Uint(0)
146
147
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
148
    if address in evm.accessed_addresses:
149
        access_gas_cost = GasCosts.WARM_ACCESS
150
    else:
151
        evm.accessed_addresses.add(address)
152
        access_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
153
    code = get_code(state, get_account(state, address).code_hash)
154
155
    return True, address, code, access_gas_cost

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]:
124
    """
125
    Get the delegation address and the cost of access from the address.
126
127
    Parameters
128
    ----------
129
    evm : `Evm`
130
        The execution frame.
131
    address : `Address`
132
        The address to check for delegation.
133
134
    Returns
135
    -------
136
    delegation : `Tuple[bool, Address, Uint]`
137
        The delegation address and access gas cost.
138
139
    """
140
    tx_state = evm.message.tx_env.state
141
142
    code = get_code(tx_state, get_account(tx_state, address).code_hash)
143
144
    if not is_valid_delegation(code):
145
        return False, address, Uint(0)
146
147
    delegated_address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
148
149
    if delegated_address in evm.accessed_addresses:
150
        delegation_gas_cost = GasCosts.WARM_ACCESS
151
    else:
152
        delegation_gas_cost = GasCosts.COLD_ACCOUNT_ACCESS
153
154
    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) -> U256None:
158
    """
159
    Set the delegation code for the authorities in the message.
160
161
    Parameters
162
    ----------
163
    message :
164
        Transaction specific items.
165
167
    Returns
168
    -------
169
    refund_counter: `U256`
170
        Refund from authority which already exists in state.
171
166
    """
173
    state = message.block_env.state
174
    refund_counter = U256(0)
167
    tx_state = message.tx_env.state
168
    for auth in message.tx_env.authorizations:
169
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
170
            continue
171
172
        if auth.nonce >= U64.MAX_VALUE:
173
            continue
174
175
        try:
176
            authority = recover_authority(auth)
177
        except InvalidSignatureError:
178
            continue
179
180
        message.accessed_addresses.add(authority)
181
189
        authority_account = get_account(state, authority)
190
        authority_code = get_code(state, authority_account.code_hash)
182
        authority_account = get_account(tx_state, authority)
183
        authority_code = get_code(tx_state, authority_account.code_hash)
184
185
        if authority_code and not is_valid_delegation(authority_code):
186
            continue
187
188
        authority_nonce = authority_account.nonce
189
        if authority_nonce != auth.nonce:
190
            continue
191
199
        if account_exists(state, authority):
200
            refund_counter += U256(
201
                GasCosts.AUTH_PER_EMPTY_ACCOUNT
202
                - REFUND_AUTH_PER_EXISTING_ACCOUNT
203
            )
192
        if is_account_alive(tx_state, authority):
193
            refund = StateCosts.NEW_ACCOUNT * StateCosts.PER_BYTE
194
            message.state_gas_reservoir += refund
195
196
        if auth.address == NULL_ADDRESS:
197
            code_to_set = b""
198
        else:
199
            code_to_set = EOA_DELEGATION_MARKER + auth.address
209
        set_code(state, authority, code_to_set)
200
211
        increment_nonce(state, authority)
201
        set_code(tx_state, authority, code_to_set)
202
        increment_nonce(tx_state, authority)
203
204
    if message.code_address is None:
205
        raise InvalidBlock("Invalid type 4 transaction: no target")
206
216
    message.code = get_code(
217
        state, get_account(state, message.code_address).code_hash
218
    )
219
220
    return refund_counter
207
    message.code = get_code(
208
        tx_state,
209
        get_account(tx_state, message.code_address).code_hash,
210
    )