ethereum.forks.bogota.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¶
| 88 | STACK_DEPTH_LIMIT = Uint(1024) |
|---|
MAX_CODE_SIZE¶
| 89 | MAX_CODE_SIZE = 0x10000 |
|---|
MAX_INIT_CODE_SIZE¶
| 90 | MAX_INIT_CODE_SIZE = 2 * MAX_CODE_SIZE |
|---|
TransactionOutput ¶
Settled output of a transaction's execution.
Carry the figures fee settlement and the receipt need, so the EVM
frames themselves never leave the interpreter. Produced by
process_top_level for a transaction's single top-level
call, and by process_frames for a frame transaction's
frame sequence.
| 93 | @final |
|---|
| 94 | @dataclass |
|---|
class TransactionOutput:
gas_left¶
Execution gas remaining after execution.
| 109 | gas_left: ExecutionGas |
|---|
refund_counter¶
Gas eligible for refund at the end of the transaction.
| 112 | refund_counter: U256 |
|---|
logs¶
Logs emitted during execution; empty when it failed.
| 115 | logs: Tuple[Log, ...] |
|---|
accounts_to_delete¶
Accounts self-destructed during execution; empty when it failed.
| 118 | accounts_to_delete: Set[Address] |
|---|
error¶
The error the execution halted with, if any.
| 121 | error: Optional[EthereumException] |
|---|
return_data¶
The output of the execution.
| 124 | return_data: Bytes |
|---|
state_gas_left¶
State gas not charged at settlement: the reservoir remainder for a regular transaction, or the frames' unused state budgets — skipped frames and refill-reduced receipts included — for a frame transaction.
| 127 | state_gas_left: StateGas |
|---|
state_gas_used¶
Net state gas consumed; negative when refunds exceed charges.
| 135 | state_gas_used: int |
|---|
charge_value_transfer_to_non_alive_account ¶
Charge the state gas for creating recipient when a value
transfer revives an account that is not alive.
def charge_value_transfer_to_non_alive_account(state: TransactionState, gas_meter: GasMeter, recipient: Address, value: U256) -> None:
| 145 | <snip> |
|---|---|
| 149 | if value > U256(0) and not is_account_alive(state, recipient): |
| 150 | charge_state_gas_from_meter(gas_meter, StateGasCosts.NEW_ACCOUNT) |
create_evm ¶
Build the transaction's top-level frame.
Apply the EIP-7702 authorizations, charge the state-dependent
dispatch costs to gas_meter, and resolve the code the frame
runs. A preparation failure -- a creation-address collision or
insufficient gas -- raises instead of building a frame, leaving
the caller to roll back the state and gas the preparation charged
and settle the transaction without dispatching.
def create_evm(block_env: BlockEnvironment, tx_env: TransactionEnvironment, top_level_context: TopLevelContext, gas_meter: GasMeter) -> Evm:
| 159 | <snip> |
|---|---|
| 169 | current_target = top_level_context.recipient |
| 170 | if top_level_context.is_create: |
| 171 | call_data = Bytes(b"") |
| 172 | else: |
| 173 | call_data = top_level_context.data |
| 174 | |
| 175 | code_address: Optional[Address] = None |
| 176 | disable_precompiles = False |
| 177 | accessed_addresses: Set[Address] = set() |
| 178 | accessed_storage_keys = set(tx_env.access_list_storage_keys) |
| 179 | |
| 180 | ## Apply the 7702 delegations |
| 181 | if tx_env.authorizations != (): |
| 182 | accessed_authorities = set_delegation(block_env, tx_env, gas_meter) |
| 183 | accessed_addresses.update(accessed_authorities) |
| 184 | commit_state_gas(gas_meter) |
| 185 | |
| 186 | ## Warm up the access sets |
| 187 | accessed_addresses.add(block_env.coinbase) |
| 188 | accessed_addresses.update(PRE_COMPILED_CONTRACTS.keys()) |
| 189 | accessed_addresses.add(tx_env.origin) |
| 190 | accessed_addresses.update(tx_env.access_list_addresses) |
| 191 | accessed_addresses.add(current_target) |
| 192 | |
| 193 | ## Resolve dispatch and charge its state-dependent costs |
| 194 | if top_level_context.is_create: |
| 195 | if not account_deployable(tx_env.state, current_target): |
| 196 | raise AddressCollision() |
| 197 | |
| 198 | if ( |
| 199 | get_pre_state_account(tx_env.state, current_target) |
| 200 | == EMPTY_ACCOUNT |
| 201 | ): |
| 202 | charge_state_gas_from_meter(gas_meter, StateGasCosts.NEW_ACCOUNT) |
| 203 | |
| 204 | code = top_level_context.data |
| 205 | else: |
| 206 | charge_value_transfer_to_non_alive_account( |
| 207 | tx_env.state, gas_meter, current_target, top_level_context.value |
| 208 | ) |
| 209 | |
| 210 | code_address, disable_precompiles = resolve_delegated_code_address( |
| 211 | tx_env.state, |
| 212 | gas_meter, |
| 213 | accessed_addresses, |
| 214 | top_level_context.recipient, |
| 215 | ) |
| 216 | |
| 217 | code = get_code( |
| 218 | tx_env.state, |
| 219 | get_account(tx_env.state, code_address).code_hash, |
| 220 | ) |
| 221 | |
| 222 | ## Build the frame |
| 223 | return Evm( |
| 224 | # Context |
| 225 | block_env=block_env, |
| 226 | tx_env=tx_env, |
| 227 | parent_evm=None, |
| 228 | depth=Uint(0), |
| 229 | # Call Parameters |
| 230 | caller=tx_env.origin, |
| 231 | current_target=current_target, |
| 232 | value=top_level_context.value, |
| 233 | call_data=call_data, |
| 234 | should_transfer_value=True, |
| 235 | is_static=False, |
| 236 | disable_precompiles=disable_precompiles, |
| 237 | # Code |
| 238 | code_address=code_address, |
| 239 | code=code, |
| 240 | valid_jump_destinations=get_valid_jump_destinations(code), |
| 241 | # Machine State |
| 242 | gas_meter=gas_meter, |
| 243 | pc=Uint(0), |
| 244 | stack=[], |
| 245 | memory=bytearray(), |
| 246 | return_data=b"", |
| 247 | # Accrued Effects |
| 248 | logs=(), |
| 249 | accounts_to_delete=set(), |
| 250 | accessed_addresses=accessed_addresses, |
| 251 | accessed_storage_keys=accessed_storage_keys, |
| 252 | # Outcome |
| 253 | running=True, |
| 254 | output=b"", |
| 255 | error=None, |
| 256 | ) |
process_top_level ¶
Execute the top level of a transaction.
Prepare the transaction's top-level EVM frame and dispatch it: a contract creation or a call, per the transaction environment. A preparation failure rolls back everything the preparation changed and never dispatches; the transaction then settles as if execution halted at entry, forfeiting its entire gas grant.
Parameters
block_env : Environment for the Ethereum Virtual Machine. tx_env : Environment for the transaction.
Returns
tx_output : TransactionOutput
The settled output of the top-level execution.
def process_top_level(block_env: BlockEnvironment, tx_env: TransactionEnvironment) -> TransactionOutput:
| 263 | <snip> |
|---|---|
| 285 | top_level_context = tx_env.top_level_context |
| 286 | assert top_level_context is not None |
| 287 | assert tx_env.frame_context is None |
| 288 | |
| 289 | reservoir = StateGasReservoir( |
| 290 | state_gas_left=tx_env.state_gas_reservoir, |
| 291 | state_gas_baseline=tx_env.state_gas_reservoir, |
| 292 | ) |
| 293 | gas_meter = GasMeter( |
| 294 | gas_left=tx_env.execution_gas_grant, |
| 295 | reservoir=reservoir, |
| 296 | ) |
| 297 | |
| 298 | prep_snapshot = copy_tx_state(tx_env.state) |
| 299 | try: |
| 300 | evm = create_evm(block_env, tx_env, top_level_context, gas_meter) |
| 301 | except ExceptionalHalt as halt: |
| 302 | # The rollback also reverts any applied delegations, so their |
| 303 | # state gas commit is undone with it: roll state gas back to |
| 304 | # frame entry, refilling every state charge. |
| 305 | restore_tx_state(tx_env.state, prep_snapshot) |
| 306 | restore_state_gas_to_entry(gas_meter, tx_env.state_gas_reservoir) |
| 307 | forfeit_remaining_gas(gas_meter) |
| 308 | return TransactionOutput( |
| 309 | gas_left=gas_meter.gas_left, |
| 310 | refund_counter=U256(gas_meter.refund_counter), |
| 311 | logs=(), |
| 312 | accounts_to_delete=set(), |
| 313 | error=halt, |
| 314 | return_data=Bytes(b""), |
| 315 | state_gas_left=reservoir.state_gas_left, |
| 316 | state_gas_used=tx_state_gas_used( |
| 317 | gas_meter, tx_env.state_gas_reservoir |
| 318 | ), |
| 319 | ) |
| 320 | |
| 321 | if top_level_context.is_create: |
| 322 | process_create(evm) |
| 323 | else: |
| 324 | process_call(evm) |
| 325 | |
| 326 | # A failed execution contributes no logs or self-destructs. |
| 327 | if evm.error: |
| 328 | logs: Tuple[Log, ...] = () |
| 329 | accounts_to_delete: Set[Address] = set() |
| 330 | else: |
| 331 | logs = evm.logs |
| 332 | accounts_to_delete = evm.accounts_to_delete |
| 333 | |
| 334 | tx_end = TransactionEnd( |
| 335 | int(tx_env.execution_gas_grant) - int(gas_meter.gas_left), |
| 336 | evm.output, |
| 337 | evm.error, |
| 338 | ) |
| 339 | evm_trace(evm, tx_end) |
| 340 | |
| 341 | return TransactionOutput( |
| 342 | gas_left=gas_meter.gas_left, |
| 343 | refund_counter=U256(gas_meter.refund_counter), |
| 344 | logs=logs, |
| 345 | accounts_to_delete=accounts_to_delete, |
| 346 | error=evm.error, |
| 347 | return_data=evm.output, |
| 348 | state_gas_left=reservoir.state_gas_left, |
| 349 | state_gas_used=tx_state_gas_used( |
| 350 | gas_meter, tx_env.state_gas_reservoir |
| 351 | ), |
| 352 | ) |
process_create ¶
Executes a call to create a smart contract.
Parameters
evm : Currently running evm.
Returns
evm: :py:class:~ethereum.forks.bogota.vm.Evm
Items containing execution specific objects.
def process_create(evm: Evm) -> Evm:
| 356 | <snip> |
|---|---|
| 370 | tx_state = evm.tx_env.state |
| 371 | # take snapshot of state before processing the message |
| 372 | snapshot = copy_tx_state(tx_state) |
| 373 | frame_context_snapshot = copy_frame_context(evm.tx_env) |
| 374 | |
| 375 | # If the address where the account is being created has storage, it is |
| 376 | # destroyed. This can only happen in the following highly unlikely |
| 377 | # circumstances: |
| 378 | # * The address created by a `CREATE` call collides with a subsequent |
| 379 | # `CREATE` or `CREATE2` call. |
| 380 | # * The first `CREATE` happened before Spurious Dragon and left empty |
| 381 | # code. |
| 382 | destroy_storage(tx_state, evm.current_target) |
| 383 | |
| 384 | # In the previously mentioned edge case the preexisting storage is ignored |
| 385 | # for gas refund purposes. In order to do this we must track created |
| 386 | # accounts. This tracking is also needed to respect the constraints |
| 387 | # added to SELFDESTRUCT by EIP-6780. |
| 388 | mark_account_created(tx_state, evm.current_target) |
| 389 | |
| 390 | increment_nonce(tx_state, evm.current_target) |
| 391 | |
| 392 | evm = process_call(evm) |
| 393 | if not evm.error: |
| 394 | contract_code = evm.output |
| 395 | try: |
| 396 | if len(contract_code) > 0: |
| 397 | if contract_code[0] == 0xEF: |
| 398 | raise InvalidContractPrefix |
| 399 | if len(contract_code) > MAX_CODE_SIZE: |
| 400 | raise OutOfGasError |
| 401 | # Hash cost for computing keccak256 of deployed bytecode |
| 402 | code_hash_gas = ExecutionGas( |
| 403 | GasCosts.OPCODE_KECCAK256_PER_WORD |
| 404 | * ceil32(ulen(contract_code)) |
| 405 | // Uint(32) |
| 406 | ) |
| 407 | charge_gas(evm, code_hash_gas) |
| 408 | code_deposit_state_gas = ( |
| 409 | ulen(contract_code) * StateGasCosts.COST_PER_STATE_BYTE |
| 410 | ) |
| 411 | charge_state_gas(evm, code_deposit_state_gas) |
| 412 | except ExceptionalHalt as error: |
| 413 | restore_tx_state(tx_state, snapshot) |
| 414 | restore_frame_context(evm.tx_env, frame_context_snapshot) |
| 415 | # A create frame never applies authorizations, so its |
| 416 | # baseline is still the frame's entry reservoir. |
| 417 | restore_state_gas(evm.gas_meter) |
| 418 | forfeit_remaining_gas(evm.gas_meter) |
| 419 | evm.output = b"" |
| 420 | evm.error = error |
| 421 | else: |
| 422 | set_code(tx_state, evm.current_target, contract_code) |
| 423 | else: |
| 424 | restore_tx_state(tx_state, snapshot) |
| 425 | restore_frame_context(evm.tx_env, frame_context_snapshot) |
| 426 | return evm |
process_call ¶
Move ether and execute the relevant code.
Parameters
evm : The EVM frame to execute.
Returns
evm: :py:class:~ethereum.forks.bogota.vm.Evm
Items containing execution specific objects
def process_call(evm: Evm) -> Evm:
| 430 | <snip> |
|---|---|
| 444 | tx_state = evm.tx_env.state |
| 445 | if evm.depth > STACK_DEPTH_LIMIT: |
| 446 | raise StackDepthLimitError("Stack depth limit reached") |
| 447 | |
| 448 | snapshot = copy_tx_state(tx_state) |
| 449 | frame_context_snapshot = copy_frame_context(evm.tx_env) |
| 450 | |
| 451 | # Execute message code and handle errors |
| 452 | try: |
| 453 | if evm.should_transfer_value and evm.value != 0: |
| 454 | move_ether( |
| 455 | tx_state, |
| 456 | evm.caller, |
| 457 | evm.current_target, |
| 458 | evm.value, |
| 459 | ) |
| 460 | if evm.caller != evm.current_target: |
| 461 | emit_transfer_log( |
| 462 | evm, |
| 463 | evm.caller, |
| 464 | evm.current_target, |
| 465 | evm.value, |
| 466 | ) |
| 467 | if evm.code_address in PRE_COMPILED_CONTRACTS: |
| 468 | if not evm.disable_precompiles: |
| 469 | evm_trace(evm, PrecompileStart(evm.code_address)) |
| 470 | PRE_COMPILED_CONTRACTS[evm.code_address](evm) |
| 471 | evm_trace(evm, PrecompileEnd()) |
| 472 | else: |
| 473 | while evm.running and evm.pc < ulen(evm.code): |
| 474 | try: |
| 475 | op = Ops(evm.code[evm.pc]) |
| 476 | except ValueError as e: |
| 477 | raise InvalidOpcode(evm.code[evm.pc]) from e |
| 478 | |
| 479 | evm_trace(evm, OpStart(op)) |
| 480 | op_implementation[op](evm) |
| 481 | evm_trace(evm, OpEnd()) |
| 482 | |
| 483 | evm_trace(evm, EvmStop(Ops.STOP)) |
| 484 | |
| 485 | except ExceptionalHalt as error: |
| 486 | evm_trace(evm, OpException(error)) |
| 487 | # Frame settlement: refill state gas to the baseline, then |
| 488 | # forfeit -- a halted frame returns no execution gas to its |
| 489 | # parent. After these handlers the meter states exactly what |
| 490 | # the frame gives back, so parents absorb unconditionally. |
| 491 | restore_state_gas(evm.gas_meter) |
| 492 | forfeit_remaining_gas(evm.gas_meter) |
| 493 | evm.output = b"" |
| 494 | evm.error = error |
| 495 | except Revert as error: |
| 496 | evm_trace(evm, OpException(error)) |
| 497 | # Frame settlement: refill state gas to the baseline -- a |
| 498 | # reverted frame returns its unspent `gas_left` to its parent. |
| 499 | restore_state_gas(evm.gas_meter) |
| 500 | evm.error = error |
| 501 | |
| 502 | if evm.error: |
| 503 | restore_tx_state(tx_state, snapshot) |
| 504 | restore_frame_context(evm.tx_env, frame_context_snapshot) |
| 505 | return evm |