ethereum.forks.bogota.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:
39
    <snip>
49
    # STACK
50
    key = pop(evm.stack).to_be_bytes32()
51
52
    # GAS
53
    if (evm.current_target, key) in evm.accessed_storage_keys:
54
        charge_gas(evm, GasCosts.WARM_ACCESS)
55
    else:
56
        evm.accessed_storage_keys.add((evm.current_target, key))
57
        charge_gas(evm, GasCosts.COLD_STORAGE_ACCESS)
58
59
    # OPERATION
60
    tx_state = evm.tx_env.state
61
    value = get_storage(tx_state, evm.current_target, key)
62
63
    push(evm.stack, value)
64
65
    # PROGRAM COUNTER
66
    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:
70
    <snip>
79
    if evm.is_static:
80
        raise WriteInStaticContext
81
82
    # STACK
83
    key = pop(evm.stack).to_be_bytes32()
84
    new_value = pop(evm.stack)
85
86
    # GAS (STATE-INDEPENDENT)
87
    # Price what is computable without touching state, and check it is
88
    # affordable before any state access is performed.
89
    gas_cost = GasCosts.ZERO
90
91
    # Access cost: cold or warm, always charged.
92
    is_cold_access = (
93
        evm.current_target,
94
        key,
95
    ) not in evm.accessed_storage_keys
96
    if is_cold_access:
97
        gas_cost += GasCosts.COLD_STORAGE_ACCESS
98
    else:
99
        gas_cost += GasCosts.WARM_ACCESS
100
101
    # EIP-2200 stipend sentry, checked before the state access below
102
    # records the slot read in the Block Access List. The `max` guards an
103
    # access cost repriced above the stipend; neither warmth is today.
104
    check_gas(
105
        evm, max(gas_cost, ExecutionGas(GasCosts.CALL_STIPEND + Uint(1)))
106
    )
107
108
    # STATE ACCESS (STATE-DEPENDENT GAS)
109
    # Perform the access and complete the state-dependent pricing from
110
    # the slot's original and current values, adjusting the
111
    # transaction's refunds.
112
    if is_cold_access:
113
        evm.accessed_storage_keys.add((evm.current_target, key))
114
115
    tx_state = evm.tx_env.state
116
    original_value = get_storage_original(tx_state, evm.current_target, key)
117
    current_value = get_storage(tx_state, evm.current_target, key)
118
119
    state_gas = StateGas(Uint(0))
120
121
    # Write cost: charged each time the slot moves away from its
122
    # transaction-start value; restoring it refunds the charge below.
123
    if original_value == current_value and current_value != new_value:
124
        gas_cost += GasCosts.STORAGE_WRITE
125
126
    # Refund Counter Calculation
127
    if current_value != new_value:
128
        if original_value != 0 and current_value != 0 and new_value == 0:
129
            # Slot non-zero at transaction start is cleared; granted on
130
            # each such clear.
131
            evm.gas_meter.refund_counter += GasCosts.REFUND_STORAGE_CLEAR
132
133
        if original_value != 0 and current_value == 0:
134
            # Gas refund issued earlier to be reversed
135
            evm.gas_meter.refund_counter -= GasCosts.REFUND_STORAGE_CLEAR
136
137
        if original_value == new_value:
138
            # Slot restored to its original value: refund the
139
            # STORAGE_WRITE charged when it was moved away.
140
            evm.gas_meter.refund_counter += int(GasCosts.STORAGE_WRITE)
141
142
    # STATE GAS
143
    # A first-time set of a zero slot pays for the state it creates; a
144
    # slot set then cleared refills the earlier charge — to the meter's
145
    # reservoir, or to the frame that owns the outstanding charge.
146
    frame_context = evm.tx_env.frame_context
147
    if original_value == current_value and current_value != new_value:
148
        if original_value == 0:
149
            state_gas = StateGasCosts.STORAGE_SET
150
151
    if current_value != new_value and original_value == new_value:
152
        if original_value == 0:
153
            if frame_context is None:
154
                credit_state_gas_refund(
155
                    evm.gas_meter, StateGasCosts.STORAGE_SET
156
                )
157
            else:
158
                owner = frame_context.outstanding_charge_owners.pop(
159
                    (evm.current_target, key)
160
                )
161
                credit_frame_state_gas_refund(
162
                    frame_context, owner, StateGasCosts.STORAGE_SET
163
                )
164
165
    # Charge execution gas before state gas so that an execution-gas
166
    # OOG does not consume state gas that would inflate the parent's
167
    # reservoir on frame failure.
168
    charge_gas(evm, gas_cost)
169
    charge_state_gas(evm, state_gas)
170
    # Record the executing frame as the outstanding charge's owner: a
171
    # later refill of this slot is attributed back to it.
172
    if frame_context is not None and state_gas != Uint(0):
173
        frame_context.outstanding_charge_owners[(evm.current_target, key)] = (
174
            frame_context.current_frame_index
175
        )
176
    set_storage(tx_state, evm.current_target, key, new_value)
177
178
    # PROGRAM COUNTER
179
    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:
183
    <snip>
193
    # STACK
194
    key = pop(evm.stack).to_be_bytes32()
195
196
    # GAS
197
    charge_gas(evm, GasCosts.OPCODE_TLOAD)
198
199
    # OPERATION
200
    value = get_transient_storage(evm.tx_env.state, evm.current_target, key)
201
    push(evm.stack, value)
202
203
    # PROGRAM COUNTER
204
    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:
208
    <snip>
217
    if evm.is_static:
218
        raise WriteInStaticContext
219
220
    # STACK
221
    key = pop(evm.stack).to_be_bytes32()
222
    new_value = pop(evm.stack)
223
224
    # GAS
225
    charge_gas(evm, GasCosts.OPCODE_TSTORE)
226
    set_transient_storage(
227
        evm.tx_env.state,
228
        evm.current_target,
229
        key,
230
        new_value,
231
    )
232
233
    # PROGRAM COUNTER
234
    evm.pc += Uint(1)