ethereum.forks.byzantium.vm.interpreterethereum.forks.constantinople.vm.interpreter

Ethereum Virtual Machine (EVM) Interpreter.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

A straightforward interpreter that executes EVM code.

STACK_DEPTH_LIMIT

62
STACK_DEPTH_LIMIT = Uint(1024)

MAX_CODE_SIZE

63
MAX_CODE_SIZE = 0x6000

MessageCallOutput

Output of a particular message call.

Contains the following:

  1. `gas_left`: remaining gas after execution.
  2. `refund_counter`: gas to refund after execution.
  3. `logs`: list of `Log` generated during execution.
  4. `accounts_to_delete`: Contracts which have self-destructed.
  5. `touched_accounts`: Accounts that have been touched.
  6. `error`: The error from the execution if any.
66
@dataclass
class MessageCallOutput:

gas_left

81
    gas_left: Uint

refund_counter

82
    refund_counter: U256

logs

83
    logs: Tuple[Log, ...]

accounts_to_delete

84
    accounts_to_delete: Set[Address]

touched_accounts

85
    touched_accounts: Set[Address]

error

86
    error: Optional[EthereumException]

process_message_call

If message.target is empty then it creates a smart contract else it executes a call from the message.caller to the message.target.

Parameters

message : Transaction specific items.

Returns

output : MessageCallOutput Output of the message call

def process_message_call(message: Message) -> MessageCallOutput:
90
    """
91
    If `message.target` is empty then it creates a smart contract
92
    else it executes a call from the `message.caller` to the `message.target`.
93
94
    Parameters
95
    ----------
96
    message :
97
        Transaction specific items.
98
99
    Returns
100
    -------
101
    output : `MessageCallOutput`
102
        Output of the message call
103
104
    """
105
    block_env = message.block_env
106
    refund_counter = U256(0)
107
    if message.target == Bytes0(b""):
108
        is_collision = account_has_code_or_nonce(
109
            block_env.state, message.current_target
110
        ) or account_has_storage(block_env.state, message.current_target)
111
        if is_collision:
112
            return MessageCallOutput(
113
                Uint(0), U256(0), tuple(), set(), set(), AddressCollision()
114
            )
115
        else:
116
            evm = process_create_message(message)
117
    else:
118
        evm = process_message(message)
119
        if account_exists_and_is_empty(
120
            block_env.state, Address(message.target)
121
        ):
122
            evm.touched_accounts.add(Address(message.target))
123
124
    if evm.error:
125
        logs: Tuple[Log, ...] = ()
126
        accounts_to_delete = set()
127
        touched_accounts = set()
128
    else:
129
        logs = evm.logs
130
        accounts_to_delete = evm.accounts_to_delete
131
        touched_accounts = evm.touched_accounts
132
        refund_counter += U256(evm.refund_counter)
133
134
    tx_end = TransactionEnd(
135
        int(message.gas) - int(evm.gas_left), evm.output, evm.error
136
    )
137
    evm_trace(evm, tx_end)
138
139
    return MessageCallOutput(
140
        gas_left=evm.gas_left,
141
        refund_counter=refund_counter,
142
        logs=logs,
143
        accounts_to_delete=accounts_to_delete,
144
        touched_accounts=touched_accounts,
145
        error=evm.error,
146
    )

process_create_message

Executes a call to create a smart contract.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.byzantium.vm.Evm~ethereum.forks.constantinople.vm.Evm Items containing execution specific objects.

def process_create_message(message: Message) -> Evm:
150
    """
151
    Executes a call to create a smart contract.
152
153
    Parameters
154
    ----------
155
    message :
156
        Transaction specific items.
157
158
    Returns
159
    -------
160
    evm: :py:class:`~ethereum.forks.byzantium.vm.Evm`
160
    evm: :py:class:`~ethereum.forks.constantinople.vm.Evm`
161
        Items containing execution specific objects.
162
163
    """
164
    state = message.block_env.state
165
    # take snapshot of state before processing the message
166
    begin_transaction(state)
167
168
    # If the address where the account is being created has storage, it is
169
    # destroyed. This can only happen in the following highly unlikely
170
    # circumstances:
171
    # * The address created by two `CREATE` calls collide.
171
    # * The address created by a `CREATE` call collides with a subsequent
172
    #   `CREATE` or `CREATE2` call.
173
    # * The first `CREATE` happened before Spurious Dragon and left empty
174
    #   code.
175
    destroy_storage(state, message.current_target)
176
177
    increment_nonce(state, message.current_target)
178
    evm = process_message(message)
179
    if not evm.error:
180
        contract_code = evm.output
181
        contract_code_gas = Uint(len(contract_code)) * GAS_CODE_DEPOSIT
182
        try:
183
            charge_gas(evm, contract_code_gas)
184
            if len(contract_code) > MAX_CODE_SIZE:
185
                raise OutOfGasError
186
        except ExceptionalHalt as error:
187
            rollback_transaction(state)
188
            evm.gas_left = Uint(0)
189
            evm.output = b""
190
            evm.error = error
191
        else:
192
            set_code(state, message.current_target, contract_code)
193
            commit_transaction(state)
194
    else:
195
        rollback_transaction(state)
196
    return evm

process_message

Move ether and execute the relevant code.

Parameters

message : Transaction specific items.

Returns

evm: :py:class:~ethereum.forks.byzantium.vm.Evm~ethereum.forks.constantinople.vm.Evm Items containing execution specific objects

def process_message(message: Message) -> Evm:
200
    """
201
    Move ether and execute the relevant code.
202
203
    Parameters
204
    ----------
205
    message :
206
        Transaction specific items.
207
208
    Returns
209
    -------
209
    evm: :py:class:`~ethereum.forks.byzantium.vm.Evm`
210
    evm: :py:class:`~ethereum.forks.constantinople.vm.Evm`
211
        Items containing execution specific objects
212
213
    """
214
    state = message.block_env.state
215
    if message.depth > STACK_DEPTH_LIMIT:
216
        raise StackDepthLimitError("Stack depth limit reached")
217
218
    code = message.code
219
    valid_jump_destinations = get_valid_jump_destinations(code)
220
    evm = Evm(
221
        pc=Uint(0),
222
        stack=[],
223
        memory=bytearray(),
224
        code=code,
225
        gas_left=message.gas,
226
        valid_jump_destinations=valid_jump_destinations,
227
        logs=(),
228
        refund_counter=0,
229
        running=True,
230
        message=message,
231
        output=b"",
232
        accounts_to_delete=set(),
233
        touched_accounts=set(),
234
        return_data=b"",
235
        error=None,
236
    )
237
238
    # take snapshot of state before processing the message
239
    begin_transaction(state)
240
241
    touch_account(state, message.current_target)
242
243
    if message.should_transfer_value and message.value != 0:
244
        move_ether(
245
            state, message.caller, message.current_target, message.value
246
        )
247
248
    try:
249
        if evm.message.code_address in PRE_COMPILED_CONTRACTS:
250
            evm_trace(evm, PrecompileStart(evm.message.code_address))
251
            PRE_COMPILED_CONTRACTS[evm.message.code_address](evm)
252
            evm_trace(evm, PrecompileEnd())
253
        else:
254
            while evm.running and evm.pc < ulen(evm.code):
255
                try:
256
                    op = Ops(evm.code[evm.pc])
257
                except ValueError as e:
258
                    raise InvalidOpcode(evm.code[evm.pc]) from e
259
260
                evm_trace(evm, OpStart(op))
261
                op_implementation[op](evm)
262
                evm_trace(evm, OpEnd())
263
264
            evm_trace(evm, EvmStop(Ops.STOP))
265
266
    except ExceptionalHalt as error:
267
        evm_trace(evm, OpException(error))
268
        evm.gas_left = Uint(0)
269
        evm.output = b""
270
        evm.error = error
271
    except Revert as error:
272
        evm_trace(evm, OpException(error))
273
        evm.error = error
274
275
    if evm.error:
276
        # revert state to the last saved checkpoint
277
        # since the message call resulted in an error
278
        rollback_transaction(state)
279
    else:
280
        commit_transaction(state)
281
    return evm