ethereum.istanbul.vm.instructions.storageethereum.muir_glacier.vm.instructions.storage

Ethereum Virtual Machine (EVM) Storage Instructions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM storage related instructions.

sload

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:
32
    """
33
    Loads to the stack, the value corresponding to a certain key from the
34
    storage of the current account.
35
36
    Parameters
37
    ----------
38
    evm :
39
        The current EVM frame.
40
41
    """
42
    # STACK
43
    key = pop(evm.stack).to_be_bytes32()
44
45
    # GAS
46
    charge_gas(evm, GAS_SLOAD)
47
48
    # OPERATION
49
    value = get_storage(evm.env.state, evm.message.current_target, key)
50
51
    push(evm.stack, value)
52
53
    # PROGRAM COUNTER
54
    evm.pc += Uint(1)

sstore

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

Parameters

evm : The current EVM frame.

def sstore(evm: Evm) -> None:
58
    """
59
    Stores a value at a certain key in the current context's storage.
60
61
    Parameters
62
    ----------
63
    evm :
64
        The current EVM frame.
65
66
    """
67
    # STACK
68
    key = pop(evm.stack).to_be_bytes32()
69
    new_value = pop(evm.stack)
70
    if evm.gas_left <= GAS_CALL_STIPEND:
71
        raise OutOfGasError
72
73
    original_value = get_storage_original(
74
        evm.env.state, evm.message.current_target, key
75
    )
76
    current_value = get_storage(evm.env.state, evm.message.current_target, key)
77
78
    if original_value == current_value and current_value != new_value:
79
        if original_value == 0:
80
            gas_cost = GAS_STORAGE_SET
81
        else:
82
            gas_cost = GAS_STORAGE_UPDATE
83
    else:
84
        gas_cost = GAS_SLOAD
85
86
    # Refund Counter Calculation
87
    if current_value != new_value:
88
        if original_value != 0 and current_value != 0 and new_value == 0:
89
            # Storage is cleared for the first time in the transaction
90
            evm.refund_counter += int(GAS_STORAGE_CLEAR_REFUND)
91
92
        if original_value != 0 and current_value == 0:
93
            # Gas refund issued earlier to be reversed
94
            evm.refund_counter -= int(GAS_STORAGE_CLEAR_REFUND)
95
96
        if original_value == new_value:
97
            # Storage slot being restored to its original value
98
            if original_value == 0:
99
                # Slot was originally empty and was SET earlier
100
                evm.refund_counter += int(GAS_STORAGE_SET - GAS_SLOAD)
101
            else:
102
                # Slot was originally non-empty and was UPDATED earlier
103
                evm.refund_counter += int(GAS_STORAGE_UPDATE - GAS_SLOAD)
104
105
    charge_gas(evm, gas_cost)
106
    if evm.message.is_static:
107
        raise WriteInStaticContext
108
    set_storage(evm.env.state, evm.message.current_target, key, new_value)
109
110
    # PROGRAM COUNTER
111
    evm.pc += Uint(1)