ethereum.forks.bpo1.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¶
| 65 | STACK_DEPTH_LIMIT = Uint(1024) |
|---|
MAX_CODE_SIZE¶
| 66 | MAX_CODE_SIZE = 0x6000 |
|---|
MAX_INIT_CODE_SIZE¶
| 67 | MAX_INIT_CODE_SIZE = 2 * MAX_CODE_SIZE |
|---|
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. `error`: The error from the execution if any.
6. `return_data`: The output of the execution.
| 70 | @dataclass |
|---|
class MessageCallOutput:
gas_left¶
| 85 | gas_left: Uint |
|---|
refund_counter¶
| 86 | refund_counter: U256 |
|---|
logs¶
| 87 | logs: Tuple[Log, ...] |
|---|
accounts_to_delete¶
| 88 | accounts_to_delete: Set[Address] |
|---|
error¶
| 89 | error: Optional[EthereumException] |
|---|
return_data¶
| 90 | return_data: Bytes |
|---|
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:
| 94 | """ |
|---|---|
| 95 | If `message.target` is empty then it creates a smart contract |
| 96 | else it executes a call from the `message.caller` to the `message.target`. |
| 97 | |
| 98 | Parameters |
| 99 | ---------- |
| 100 | message : |
| 101 | Transaction specific items. |
| 102 | |
| 103 | Returns |
| 104 | ------- |
| 105 | output : `MessageCallOutput` |
| 106 | Output of the message call |
| 107 | |
| 108 | """ |
| 109 | block_env = message.block_env |
| 110 | refund_counter = U256(0) |
| 111 | if message.target == Bytes0(b""): |
| 112 | is_collision = account_has_code_or_nonce( |
| 113 | block_env.state, message.current_target |
| 114 | ) or account_has_storage(block_env.state, message.current_target) |
| 115 | if is_collision: |
| 116 | return MessageCallOutput( |
| 117 | Uint(0), |
| 118 | U256(0), |
| 119 | tuple(), |
| 120 | set(), |
| 121 | AddressCollision(), |
| 122 | Bytes(b""), |
| 123 | ) |
| 124 | else: |
| 125 | evm = process_create_message(message) |
| 126 | else: |
| 127 | if message.tx_env.authorizations != (): |
| 128 | refund_counter += set_delegation(message) |
| 129 | |
| 130 | delegated_address = get_delegated_code_address(message.code) |
| 131 | if delegated_address is not None: |
| 132 | message.disable_precompiles = True |
| 133 | message.accessed_addresses.add(delegated_address) |
| 134 | message.code = get_code( |
| 135 | block_env.state, |
| 136 | get_account(block_env.state, delegated_address).code_hash, |
| 137 | ) |
| 138 | message.code_address = delegated_address |
| 139 | |
| 140 | evm = process_message(message) |
| 141 | |
| 142 | if evm.error: |
| 143 | logs: Tuple[Log, ...] = () |
| 144 | accounts_to_delete = set() |
| 145 | else: |
| 146 | logs = evm.logs |
| 147 | accounts_to_delete = evm.accounts_to_delete |
| 148 | refund_counter += U256(evm.refund_counter) |
| 149 | |
| 150 | tx_end = TransactionEnd( |
| 151 | int(message.gas) - int(evm.gas_left), evm.output, evm.error |
| 152 | ) |
| 153 | evm_trace(evm, tx_end) |
| 154 | |
| 155 | return MessageCallOutput( |
| 156 | gas_left=evm.gas_left, |
| 157 | refund_counter=refund_counter, |
| 158 | logs=logs, |
| 159 | accounts_to_delete=accounts_to_delete, |
| 160 | error=evm.error, |
| 161 | return_data=evm.output, |
| 162 | ) |
process_create_message ¶
Executes a call to create a smart contract.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:~ethereum.forks.bpo1.vm.Evm
Items containing execution specific objects.
def process_create_message(message: Message) -> Evm:
| 166 | """ |
|---|---|
| 167 | Executes a call to create a smart contract. |
| 168 | |
| 169 | Parameters |
| 170 | ---------- |
| 171 | message : |
| 172 | Transaction specific items. |
| 173 | |
| 174 | Returns |
| 175 | ------- |
| 176 | evm: :py:class:`~ethereum.forks.bpo1.vm.Evm` |
| 177 | Items containing execution specific objects. |
| 178 | |
| 179 | """ |
| 180 | state = message.block_env.state |
| 181 | transient_storage = message.tx_env.transient_storage |
| 182 | # take snapshot of state before processing the message |
| 183 | begin_transaction(state, transient_storage) |
| 184 | |
| 185 | # If the address where the account is being created has storage, it is |
| 186 | # destroyed. This can only happen in the following highly unlikely |
| 187 | # circumstances: |
| 188 | # * The address created by a `CREATE` call collides with a subsequent |
| 189 | # `CREATE` or `CREATE2` call. |
| 190 | # * The first `CREATE` happened before Spurious Dragon and left empty |
| 191 | # code. |
| 192 | destroy_storage(state, message.current_target) |
| 193 | |
| 194 | # In the previously mentioned edge case the preexisting storage is ignored |
| 195 | # for gas refund purposes. In order to do this we must track created |
| 196 | # accounts. This tracking is also needed to respect the constraints |
| 197 | # added to SELFDESTRUCT by EIP-6780. |
| 198 | mark_account_created(state, message.current_target) |
| 199 | |
| 200 | increment_nonce(state, message.current_target) |
| 201 | evm = process_message(message) |
| 202 | if not evm.error: |
| 203 | contract_code = evm.output |
| 204 | contract_code_gas = ( |
| 205 | Uint(len(contract_code)) * GAS_CODE_DEPOSIT_PER_BYTE |
| 206 | ) |
| 207 | try: |
| 208 | if len(contract_code) > 0: |
| 209 | if contract_code[0] == 0xEF: |
| 210 | raise InvalidContractPrefix |
| 211 | charge_gas(evm, contract_code_gas) |
| 212 | if len(contract_code) > MAX_CODE_SIZE: |
| 213 | raise OutOfGasError |
| 214 | except ExceptionalHalt as error: |
| 215 | rollback_transaction(state, transient_storage) |
| 216 | evm.gas_left = Uint(0) |
| 217 | evm.output = b"" |
| 218 | evm.error = error |
| 219 | else: |
| 220 | set_code(state, message.current_target, contract_code) |
| 221 | commit_transaction(state, transient_storage) |
| 222 | else: |
| 223 | rollback_transaction(state, transient_storage) |
| 224 | return evm |
process_message ¶
Move ether and execute the relevant code.
Parameters
message : Transaction specific items.
Returns
evm: :py:class:~ethereum.forks.bpo1.vm.Evm
Items containing execution specific objects
def process_message(message: Message) -> Evm:
| 228 | """ |
|---|---|
| 229 | Move ether and execute the relevant code. |
| 230 | |
| 231 | Parameters |
| 232 | ---------- |
| 233 | message : |
| 234 | Transaction specific items. |
| 235 | |
| 236 | Returns |
| 237 | ------- |
| 238 | evm: :py:class:`~ethereum.forks.bpo1.vm.Evm` |
| 239 | Items containing execution specific objects |
| 240 | |
| 241 | """ |
| 242 | state = message.block_env.state |
| 243 | if message.depth > STACK_DEPTH_LIMIT: |
| 244 | raise StackDepthLimitError("Stack depth limit reached") |
| 245 | |
| 246 | transient_storage = message.tx_env.transient_storage |
| 247 | code = message.code |
| 248 | valid_jump_destinations = get_valid_jump_destinations(code) |
| 249 | evm = Evm( |
| 250 | pc=Uint(0), |
| 251 | stack=[], |
| 252 | memory=bytearray(), |
| 253 | code=code, |
| 254 | gas_left=message.gas, |
| 255 | valid_jump_destinations=valid_jump_destinations, |
| 256 | logs=(), |
| 257 | refund_counter=0, |
| 258 | running=True, |
| 259 | message=message, |
| 260 | output=b"", |
| 261 | accounts_to_delete=set(), |
| 262 | return_data=b"", |
| 263 | error=None, |
| 264 | accessed_addresses=message.accessed_addresses, |
| 265 | accessed_storage_keys=message.accessed_storage_keys, |
| 266 | ) |
| 267 | |
| 268 | # take snapshot of state before processing the message |
| 269 | begin_transaction(state, transient_storage) |
| 270 | |
| 271 | if message.should_transfer_value and message.value != 0: |
| 272 | move_ether( |
| 273 | state, message.caller, message.current_target, message.value |
| 274 | ) |
| 275 | |
| 276 | try: |
| 277 | if evm.message.code_address in PRE_COMPILED_CONTRACTS: |
| 278 | if not message.disable_precompiles: |
| 279 | evm_trace(evm, PrecompileStart(evm.message.code_address)) |
| 280 | PRE_COMPILED_CONTRACTS[evm.message.code_address](evm) |
| 281 | evm_trace(evm, PrecompileEnd()) |
| 282 | else: |
| 283 | while evm.running and evm.pc < ulen(evm.code): |
| 284 | try: |
| 285 | op = Ops(evm.code[evm.pc]) |
| 286 | except ValueError as e: |
| 287 | raise InvalidOpcode(evm.code[evm.pc]) from e |
| 288 | |
| 289 | evm_trace(evm, OpStart(op)) |
| 290 | op_implementation[op](evm) |
| 291 | evm_trace(evm, OpEnd()) |
| 292 | |
| 293 | evm_trace(evm, EvmStop(Ops.STOP)) |
| 294 | |
| 295 | except ExceptionalHalt as error: |
| 296 | evm_trace(evm, OpException(error)) |
| 297 | evm.gas_left = Uint(0) |
| 298 | evm.output = b"" |
| 299 | evm.error = error |
| 300 | except Revert as error: |
| 301 | evm_trace(evm, OpException(error)) |
| 302 | evm.error = error |
| 303 | |
| 304 | if evm.error: |
| 305 | # revert state to the last saved checkpoint |
| 306 | # since the message call resulted in an error |
| 307 | rollback_transaction(state, transient_storage) |
| 308 | else: |
| 309 | commit_transaction(state, transient_storage) |
| 310 | return evm |