Ethereum Virtual Machine (EVM) Storage Instructions

Introduction

Implementations of the EVM storage related instructions.

Module Contents

Functions

sload

Loads to the stack, the value corresponding to a certain key from the

sstore

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

Module Details

sload

sload(evm: ethereum.homestead.vm.Evm)None

Loads to the stack, the value corresponding to a certain key from the storage of the current account.

Parameters

evm – The current EVM frame.

def sload(evm: Evm) -> None:
    # STACK
    key = pop(evm.stack).to_be_bytes32()

    # GAS
    charge_gas(evm, GAS_SLOAD)

    # OPERATION
    value = get_storage(evm.env.state, evm.message.current_target, key)

    push(evm.stack, value)

    # PROGRAM COUNTER
    evm.pc += 1

sstore

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

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

Parameters

evm – The current EVM frame.

def sstore(evm: Evm) -> None:
    # STACK
    key = pop(evm.stack).to_be_bytes32()
    new_value = pop(evm.stack)

    # GAS
    current_value = get_storage(evm.env.state, evm.message.current_target, key)
    if new_value != 0 and current_value == 0:
        gas_cost = GAS_STORAGE_SET
    else:
        gas_cost = GAS_STORAGE_UPDATE

    if new_value == 0 and current_value != 0:
        evm.refund_counter += GAS_STORAGE_CLEAR_REFUND

    charge_gas(evm, gas_cost)

    # OPERATION
    set_storage(evm.env.state, evm.message.current_target, key, new_value)

    # PROGRAM COUNTER
    evm.pc += 1