Computation

BaseComputation

class eth.vm.computation.BaseComputation(state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI)

The base class for all execution computations.

Note

Each BaseComputation class must be configured with:

opcodes: A mapping from the opcode integer value to the logic

function for the opcode.

_precompiles: A mapping of contract address to the precompile function

for execution of precompiled contracts.

add_child_computation(child_computation: ComputationAPI) None

Add the given child_computation.

add_log_entry(account: Address, topics: Tuple[int, ...], data: bytes) None

Add a log entry.

apply_child_computation(child_msg: MessageAPI) ComputationAPI

Apply the vm message child_msg as a child computation.

classmethod apply_computation(state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI, parent_computation: ComputationAPI | None = None) ComputationAPI

Execute the logic within the message: Either run the precompile, or step through each opcode. Generally, the only VM-specific logic is for each opcode as it executes.

This should rarely be called directly, because it will skip over other important VM-specific logic that happens before or after the execution.

Instead, prefer apply_message() or apply_create_message().

classmethod apply_create_message(state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI, parent_computation: ComputationAPI | None = None) ComputationAPI

Execute a VM message to create a new contract. This is where the VM-specific create logic exists.

classmethod apply_message(state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI, parent_computation: ComputationAPI | None = None) ComputationAPI

Execute a VM message. This is where the VM-specific call logic exists.

consume_gas(amount: int, reason: str) None

Consume amount of gas from the remaining gas. Raise eth.exceptions.OutOfGas if there is not enough gas remaining.

classmethod consume_initcode_gas_cost(computation: ComputationAPI) None

Before starting the computation, consume initcode gas cost.

extend_memory(start_position: int, size: int) None

Extend the size of the memory to be at minimum start_position + size bytes in length. Raise eth.exceptions.OutOfGas if there is not enough gas to pay for extending the memory.

generate_child_computation(child_msg: MessageAPI) ComputationAPI

Generate a child computation from the given child_msg.

get_accounts_for_deletion() List[Address]

Return a tuple of addresses that are registered for deletion.

get_gas_meter() GasMeterAPI

Return the gas meter for the computation.

get_gas_refund() int

Return the number of refunded gas.

get_gas_remaining() int

Return the number of remaining gas.

get_gas_used() int

Return the number of used gas.

get_log_entries() Tuple[Tuple[bytes, Tuple[int, ...], bytes], ...]

Return the log entries for this computation and its children.

They are sorted in the same order they were emitted during the transaction processing, and include the sequential counter as the first element of the tuple representing every entry.

get_opcode_fn(opcode: int) OpcodeAPI

Return the function for the given opcode.

classmethod get_precompiles() Dict[Address, Callable[[ComputationAPI], Any]]

Return a dictionary where the keys are the addresses of precompiles and the values are the precompile functions.

get_raw_log_entries() Tuple[Tuple[int, bytes, Tuple[int, ...], bytes], ...]

Return a tuple of raw log entries.

get_self_destruct_beneficiaries() List[Address]

Return a list of addresses that were beneficiaries of the self-destruct opcode - whether or not the contract was self-destructed, post-Cancun.

memory_copy(destination: int, source: int, length: int) None

Copy bytes of memory with size length from source to destination

memory_read_bytes(start_position: int, size: int) bytes

Read and return size bytes from memory starting at start_position.

memory_write(start_position: int, size: int, value: bytes) None

Write value to memory at start_position. Require that len(value) == size.

prepare_child_message(gas: int, to: Address, value: int, data: bytes | memoryview, code: bytes, **kwargs: Any) MessageAPI

Helper method for creating a child computation.

raise_if_error() None

If there was an error during computation, raise it as an exception immediately.

Raises:

VMError

refund_gas(amount: int) None

Add amount of gas to the pool of gas marked to be refunded.

register_account_for_deletion(beneficiary: Address) None

Register the address of beneficiary for deletion.

return_gas(amount: int) None

Return amount of gas to the available gas pool.

stack_dup(position: int) None

Duplicate the stack item at position and pushes it onto the stack.

stack_swap(position: int) None

Swap the item on the top of the stack with the item at position.

accounts_to_delete: List[Address] = None
beneficiaries: List[Address] = None
children: List[ComputationAPI] = None
code: CodeStreamAPI = None
property error: VMError

Return the VMError of the computation. Raise AttributeError if no error exists.

property is_error: bool

Return True if the computation resulted in an error.

property is_origin_computation: bool

Return True if this computation is the outermost computation at depth == 0.

property is_success: bool

Return True if the computation did not result in an error.

logger: ExtendedDebugLogger = <ExtendedDebugLogger eth.vm.computation.BaseComputation (WARNING)>
msg: MessageAPI = None
opcodes: Dict[int, OpcodeAPI] = None
property output: bytes

Get the return value of the computation.

property precompiles: Dict[Address, Callable[[ComputationAPI], Any]]

Return a dictionary where the keys are the addresses of precompiles and the values are the precompile functions.

return_data: bytes = b''
property should_burn_gas: bool

Return True if the remaining gas should be burned.

property should_erase_return_data: bool

Return True if the return data should be zerod out due to an error.

property should_return_gas: bool

Return True if the remaining gas should be returned.

stack_pop1_any
stack_pop1_bytes
stack_pop1_int
stack_pop_any
stack_pop_bytes
stack_pop_ints
stack_push_bytes
stack_push_int
state: StateAPI = None
transaction_context: TransactionContextAPI = None