ethereum.prague.vm.eoa_delegation

Set EOA account code.

SET_CODE_TX_MAGIC

22
SET_CODE_TX_MAGIC = b"\x05"

EOA_DELEGATION_MARKER

23
EOA_DELEGATION_MARKER = b"\xEF\x01\x00"

EOA_DELEGATION_MARKER_LENGTH

24
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)

EOA_DELEGATED_CODE_LENGTH

25
EOA_DELEGATED_CODE_LENGTH = 23

PER_EMPTY_ACCOUNT_COST

26
PER_EMPTY_ACCOUNT_COST = 25000

PER_AUTH_BASE_COST

27
PER_AUTH_BASE_COST = 12500

NULL_ADDRESS

28
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:
32
    """
33
    Whether the code is a valid delegation designation.
34
35
    Parameters
36
    ----------
37
    code: `bytes`
38
        The code to check.
39
40
    Returns
41
    -------
42
    valid : `bool`
43
        True if the code is a valid delegation designation,
44
        False otherwise.
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
    if is_valid_delegation(code):
69
        return Address(code[EOA_DELEGATION_MARKER_LENGTH:])
70
    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:
74
    """
75
    Recover the authority address from the authorization.
76
77
    Parameters
78
    ----------
79
    authorization
80
        The authorization to recover the authority from.
81
82
    Raises
83
    ------
84
    InvalidSignatureError
85
        If the signature is invalid.
86
87
    Returns
88
    -------
89
    authority : `Address`
90
        The recovered authority address.
91
    """
92
    y_parity, r, s = authorization.y_parity, authorization.r, authorization.s
93
    if y_parity not in (0, 1):
94
        raise InvalidSignatureError("Invalid y_parity in authorization")
95
    if U256(0) >= r or r >= SECP256K1N:
96
        raise InvalidSignatureError("Invalid r value in authorization")
97
    if U256(0) >= s or s > SECP256K1N // U256(2):
98
        raise InvalidSignatureError("Invalid s value in authorization")
99
100
    signing_hash = keccak256(
101
        SET_CODE_TX_MAGIC
102
        + rlp.encode(
103
            (
104
                authorization.chain_id,
105
                authorization.address,
106
                authorization.nonce,
107
            )
108
        )
109
    )
110
111
    public_key = secp256k1_recover(r, s, U256(y_parity), signing_hash)
112
    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]:
118
    """
119
    Get the delegation address, code, and the cost of access from the address.
120
121
    Parameters
122
    ----------
123
    evm : `Evm`
124
        The execution frame.
125
    address : `Address`
126
        The address to get the delegation from.
127
128
    Returns
129
    -------
130
    delegation : `Tuple[bool, Address, Bytes, Uint]`
131
        The delegation address, code, and access gas cost.
132
    """
133
    state = evm.message.block_env.state
134
    code = get_account(state, address).code
135
    if not is_valid_delegation(code):
136
        return False, address, code, Uint(0)
137
138
    address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
139
    if address in evm.accessed_addresses:
140
        access_gas_cost = GAS_WARM_ACCESS
141
    else:
142
        evm.accessed_addresses.add(address)
143
        access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
144
    code = get_account(state, address).code
145
146
    return True, address, code, access_gas_cost

set_delegation

Set the delegation code for the authorities in the message.

Parameters

message : Transaction specific items. env : External items required for EVM execution.

Returns

refund_counter: U256 Refund from authority which already exists in state.

def set_delegation(message: Message) -> U256:
150
    """
151
    Set the delegation code for the authorities in the message.
152
153
    Parameters
154
    ----------
155
    message :
156
        Transaction specific items.
157
    env :
158
        External items required for EVM execution.
159
160
    Returns
161
    -------
162
    refund_counter: `U256`
163
        Refund from authority which already exists in state.
164
    """
165
    state = message.block_env.state
166
    refund_counter = U256(0)
167
    for auth in message.tx_env.authorizations:
168
        if auth.chain_id not in (message.block_env.chain_id, U256(0)):
169
            continue
170
171
        if auth.nonce >= U64.MAX_VALUE:
172
            continue
173
174
        try:
175
            authority = recover_authority(auth)
176
        except InvalidSignatureError:
177
            continue
178
179
        message.accessed_addresses.add(authority)
180
181
        authority_account = get_account(state, authority)
182
        authority_code = authority_account.code
183
184
        if authority_code and not is_valid_delegation(authority_code):
185
            continue
186
187
        authority_nonce = authority_account.nonce
188
        if authority_nonce != auth.nonce:
189
            continue
190
191
        if account_exists(state, authority):
192
            refund_counter += U256(PER_EMPTY_ACCOUNT_COST - PER_AUTH_BASE_COST)
193
194
        if auth.address == NULL_ADDRESS:
195
            code_to_set = b""
196
        else:
197
            code_to_set = EOA_DELEGATION_MARKER + auth.address
198
        set_code(state, authority, code_to_set)
199
200
        increment_nonce(state, authority)
201
202
    if message.code_address is None:
203
        raise InvalidBlock("Invalid type 4 transaction: no target")
204
205
    message.code = get_account(state, message.code_address).code
206
207
    return refund_counter