ethereum.forks.shanghai.vm.instructions.storageethereum.forks.cancun.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:
33
    """
34
    Loads to the stack, the value corresponding to a certain key from the
35
    storage of the current account.
36
37
    Parameters
38
    ----------
39
    evm :
40
        The current EVM frame.
41
42
    """
43
    # STACK
44
    key = pop(evm.stack).to_be_bytes32()
45
46
    # GAS
47
    if (evm.message.current_target, key) in evm.accessed_storage_keys:
48
        charge_gas(evm, GasCosts.WARM_ACCESS)
49
    else:
50
        evm.accessed_storage_keys.add((evm.message.current_target, key))
51
        charge_gas(evm, GasCosts.COLD_STORAGE_ACCESS)
52
53
    # OPERATION
54
    tx_state = evm.message.tx_env.state
55
    value = get_storage(tx_state, evm.message.current_target, key)
56
57
    push(evm.stack, value)
58
59
    # PROGRAM COUNTER
60
    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:
64
    """
65
    Stores a value at a certain key in the current context's storage.
66
67
    Parameters
68
    ----------
69
    evm :
70
        The current EVM frame.
71
72
    """
73
    # STACK
74
    key = pop(evm.stack).to_be_bytes32()
75
    new_value = pop(evm.stack)
76
    if evm.gas_left <= GasCosts.CALL_STIPEND:
77
        raise OutOfGasError
78
79
    tx_state = evm.message.tx_env.state
80
    original_value = get_storage_original(
81
        tx_state, evm.message.current_target, key
82
    )
83
    current_value = get_storage(tx_state, evm.message.current_target, key)
84
85
    gas_cost = Uint(0)
86
87
    if (evm.message.current_target, key) not in evm.accessed_storage_keys:
88
        evm.accessed_storage_keys.add((evm.message.current_target, key))
89
        gas_cost += GasCosts.COLD_STORAGE_ACCESS
90
91
    if original_value == current_value and current_value != new_value:
92
        if original_value == 0:
93
            gas_cost += GasCosts.STORAGE_SET
94
        else:
95
            gas_cost += (
96
                GasCosts.COLD_STORAGE_WRITE - GasCosts.COLD_STORAGE_ACCESS
97
            )
98
    else:
99
        gas_cost += GasCosts.WARM_ACCESS
100
101
    # Refund Counter Calculation
102
    if current_value != new_value:
103
        if original_value != 0 and current_value != 0 and new_value == 0:
104
            # Storage is cleared for the first time in the transaction
105
            evm.refund_counter += GasCosts.REFUND_STORAGE_CLEAR
106
107
        if original_value != 0 and current_value == 0:
108
            # Gas refund issued earlier to be reversed
109
            evm.refund_counter -= GasCosts.REFUND_STORAGE_CLEAR
110
111
        if original_value == new_value:
112
            # Storage slot being restored to its original value
113
            if original_value == 0:
114
                # Slot was originally empty and was SET earlier
115
                evm.refund_counter += int(
116
                    GasCosts.STORAGE_SET - GasCosts.WARM_ACCESS
117
                )
118
            else:
119
                # Slot was originally non-empty and was UPDATED earlier
120
                evm.refund_counter += int(
121
                    GasCosts.COLD_STORAGE_WRITE
122
                    - GasCosts.COLD_STORAGE_ACCESS
123
                    - GasCosts.WARM_ACCESS
124
                )
125
126
    charge_gas(evm, gas_cost)
127
    if evm.message.is_static:
128
        raise WriteInStaticContext
129
    set_storage(tx_state, evm.message.current_target, key, new_value)
130
131
    # PROGRAM COUNTER
132
    evm.pc += Uint(1)

tload

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

Parameters

evm : The current EVM frame.

def tload(evm: Evm) -> None:
136
    """
137
    Loads to the stack, the value corresponding to a certain key from the
138
    transient storage of the current account.
139
140
    Parameters
141
    ----------
142
    evm :
143
        The current EVM frame.
144
145
    """
146
    # STACK
147
    key = pop(evm.stack).to_be_bytes32()
148
149
    # GAS
150
    charge_gas(evm, GasCosts.WARM_ACCESS)
151
152
    # OPERATION
153
    value = get_transient_storage(
154
        evm.message.tx_env.state, evm.message.current_target, key
155
    )
156
    push(evm.stack, value)
157
158
    # PROGRAM COUNTER
159
    evm.pc += Uint(1)

tstore

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

Parameters

evm : The current EVM frame.

def tstore(evm: Evm) -> None:
163
    """
164
    Stores a value at a certain key in the current context's transient storage.
165
166
    Parameters
167
    ----------
168
    evm :
169
        The current EVM frame.
170
171
    """
172
    # STACK
173
    key = pop(evm.stack).to_be_bytes32()
174
    new_value = pop(evm.stack)
175
176
    # GAS
177
    charge_gas(evm, GasCosts.WARM_ACCESS)
178
    if evm.message.is_static:
179
        raise WriteInStaticContext
180
    set_transient_storage(
181
        evm.message.tx_env.state,
182
        evm.message.current_target,
183
        key,
184
        new_value,
185
    )
186
187
    # PROGRAM COUNTER
188
    evm.pc += Uint(1)