ethereum.gray_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
    if (evm.message.current_target, key) in evm.accessed_storage_keys:
47
        charge_gas(evm, GAS_WARM_ACCESS)
48
    else:
49
        evm.accessed_storage_keys.add((evm.message.current_target, key))
50
        charge_gas(evm, GAS_COLD_SLOAD)
51
52
    # OPERATION
53
    value = get_storage(evm.env.state, evm.message.current_target, key)
54
55
    push(evm.stack, value)
56
57
    # PROGRAM COUNTER
58
    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:
62
    """
63
    Stores a value at a certain key in the current context's storage.
64
65
    Parameters
66
    ----------
67
    evm :
68
        The current EVM frame.
69
70
    """
71
    # STACK
72
    key = pop(evm.stack).to_be_bytes32()
73
    new_value = pop(evm.stack)
74
    if evm.gas_left <= GAS_CALL_STIPEND:
75
        raise OutOfGasError
76
77
    original_value = get_storage_original(
78
        evm.env.state, evm.message.current_target, key
79
    )
80
    current_value = get_storage(evm.env.state, evm.message.current_target, key)
81
82
    gas_cost = Uint(0)
83
84
    if (evm.message.current_target, key) not in evm.accessed_storage_keys:
85
        evm.accessed_storage_keys.add((evm.message.current_target, key))
86
        gas_cost += GAS_COLD_SLOAD
87
88
    if original_value == current_value and current_value != new_value:
89
        if original_value == 0:
90
            gas_cost += GAS_STORAGE_SET
91
        else:
92
            gas_cost += GAS_STORAGE_UPDATE - GAS_COLD_SLOAD
93
    else:
94
        gas_cost += GAS_WARM_ACCESS
95
96
    # Refund Counter Calculation
97
    if current_value != new_value:
98
        if original_value != 0 and current_value != 0 and new_value == 0:
99
            # Storage is cleared for the first time in the transaction
100
            evm.refund_counter += int(GAS_STORAGE_CLEAR_REFUND)
101
102
        if original_value != 0 and current_value == 0:
103
            # Gas refund issued earlier to be reversed
104
            evm.refund_counter -= int(GAS_STORAGE_CLEAR_REFUND)
105
106
        if original_value == new_value:
107
            # Storage slot being restored to its original value
108
            if original_value == 0:
109
                # Slot was originally empty and was SET earlier
110
                evm.refund_counter += int(GAS_STORAGE_SET - GAS_WARM_ACCESS)
111
            else:
112
                # Slot was originally non-empty and was UPDATED earlier
113
                evm.refund_counter += int(
114
                    GAS_STORAGE_UPDATE - GAS_COLD_SLOAD - GAS_WARM_ACCESS
115
                )
116
117
    charge_gas(evm, gas_cost)
118
    if evm.message.is_static:
119
        raise WriteInStaticContext
120
    set_storage(evm.env.state, evm.message.current_target, key, new_value)
121
122
    # PROGRAM COUNTER
123
    evm.pc += Uint(1)