ethereum.vm.instructions

Ethereum Virtual Machine (EVM) Instructions

Introduction

Implementations of the instructions understood by the EVM.

Module Contents

Functions

add

Adds the top two elements of the stack together, and pushes the result back

sstore

Stores a value at a certain key in the current context’s storage.

push1

Pushes a one-byte immediate onto the stack.

Module Details

add

ethereum.vm.instructions.add(evm: ethereum.vm.Evm)None

Adds the top two elements of the stack together, and pushes the result back on the stack.

Parameters

evm – The current EVM frame.

Raises
def add(evm: Evm) -> None:
    evm.gas_left = subtract_gas(evm.gas_left, GAS_VERY_LOW)

    x = pop(evm.stack)
    y = pop(evm.stack)

    val = x.wrapping_add(y)

    push(evm.stack, val)

sstore

ethereum.vm.instructions.sstore(evm: ethereum.vm.Evm)None

Stores a value at a certain key in the current context’s storage.

Parameters

evm – The current EVM frame.

Raises
def sstore(evm: Evm) -> None:
    key = pop(evm.stack).to_be_bytes32()
    new_value = pop(evm.stack)
    current_value = evm.env.state[evm.current].storage.get(key, U256(0))

    # TODO: SSTORE gas usage hasn't been tested yet. Testing this needs
    # other opcodes to be implemented.
    # Calculating the gas needed for the storage
    if new_value != 0 and current_value == 0:
        gas_cost = GAS_STORAGE_SET
    else:
        gas_cost = GAS_STORAGE_UPDATE

    evm.gas_left = subtract_gas(evm.gas_left, gas_cost)

    # TODO: Refund counter hasn't been tested yet. Testing this needs other
    # Opcodes to be implemented
    if new_value == 0 and current_value != 0:
        evm.refund_counter += GAS_STORAGE_CLEAR_REFUND

    if new_value == 0:
        del evm.env.state[evm.current].storage[key]
    else:
        evm.env.state[evm.current].storage[key] = new_value

push1

ethereum.vm.instructions.push1(evm: ethereum.vm.Evm)None

Pushes a one-byte immediate onto the stack.

Parameters

evm – The current EVM frame.

Raises
def push1(evm: Evm) -> None:
    evm.gas_left = subtract_gas(evm.gas_left, GAS_VERY_LOW)
    push(evm.stack, U256(evm.code[evm.pc + 1]))
    evm.pc += 1