ethereum.forks.osaka.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
    value = get_storage(
55
        evm.message.block_env.state, evm.message.current_target, key
56
    )
57
58
    push(evm.stack, value)
59
60
    # PROGRAM COUNTER
61
    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:
65
    """
66
    Stores a value at a certain key in the current context's storage.
67
68
    Parameters
69
    ----------
70
    evm :
71
        The current EVM frame.
72
73
    """
74
    # STACK
75
    key = pop(evm.stack).to_be_bytes32()
76
    new_value = pop(evm.stack)
77
    if evm.gas_left <= GasCosts.CALL_STIPEND:
78
        raise OutOfGasError
79
80
    state = evm.message.block_env.state
81
    original_value = get_storage_original(
82
        state, evm.message.current_target, key
83
    )
84
    current_value = get_storage(state, evm.message.current_target, key)
85
86
    gas_cost = Uint(0)
87
88
    if (evm.message.current_target, key) not in evm.accessed_storage_keys:
89
        evm.accessed_storage_keys.add((evm.message.current_target, key))
90
        gas_cost += GasCosts.COLD_STORAGE_ACCESS
91
92
    if original_value == current_value and current_value != new_value:
93
        if original_value == 0:
94
            gas_cost += GasCosts.STORAGE_SET
95
        else:
96
            gas_cost += (
97
                GasCosts.COLD_STORAGE_WRITE - GasCosts.COLD_STORAGE_ACCESS
98
            )
99
    else:
100
        gas_cost += GasCosts.WARM_ACCESS
101
102
    # Refund Counter Calculation
103
    if current_value != new_value:
104
        if original_value != 0 and current_value != 0 and new_value == 0:
105
            evm.refund_counter += GasCosts.REFUND_STORAGE_CLEAR
106
107
        if original_value != 0 and current_value == 0:
108
            evm.refund_counter -= GasCosts.REFUND_STORAGE_CLEAR
109
110
        if original_value == new_value:
111
            if original_value == 0:
112
                evm.refund_counter += int(
113
                    GasCosts.STORAGE_SET - GasCosts.WARM_ACCESS
114
                )
115
            else:
116
                evm.refund_counter += int(
117
                    GasCosts.COLD_STORAGE_WRITE
118
                    - GasCosts.COLD_STORAGE_ACCESS
119
                    - GasCosts.WARM_ACCESS
120
                )
121
122
    charge_gas(evm, gas_cost)
123
    if evm.message.is_static:
124
        raise WriteInStaticContext
125
    set_storage(state, evm.message.current_target, key, new_value)
126
127
    # PROGRAM COUNTER
128
    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:
132
    """
133
    Loads to the stack, the value corresponding to a certain key from the
134
    transient storage of the current account.
135
136
    Parameters
137
    ----------
138
    evm :
139
        The current EVM frame.
140
141
    """
142
    # STACK
143
    key = pop(evm.stack).to_be_bytes32()
144
145
    # GAS
146
    charge_gas(evm, GasCosts.WARM_ACCESS)
147
148
    # OPERATION
149
    value = get_transient_storage(
150
        evm.message.tx_env.transient_storage, evm.message.current_target, key
151
    )
152
    push(evm.stack, value)
153
154
    # PROGRAM COUNTER
155
    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:
159
    """
160
    Stores a value at a certain key in the current context's transient storage.
161
162
    Parameters
163
    ----------
164
    evm :
165
        The current EVM frame.
166
167
    """
168
    # STACK
169
    key = pop(evm.stack).to_be_bytes32()
170
    new_value = pop(evm.stack)
171
172
    # GAS
173
    charge_gas(evm, GasCosts.WARM_ACCESS)
174
    if evm.message.is_static:
175
        raise WriteInStaticContext
176
    set_transient_storage(
177
        evm.message.tx_env.transient_storage,
178
        evm.message.current_target,
179
        key,
180
        new_value,
181
    )
182
183
    # PROGRAM COUNTER
184
    evm.pc += Uint(1)