ethereum.forks.bpo5.vm.instructions.storageethereum.forks.amsterdam.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
    """
40
    Loads to the stack, the value corresponding to a certain key from the
41
    storage of the current account.
42
43
    Parameters
44
    ----------
45
    evm :
46
        The current EVM frame.
47
48
    """
49
    # STACK
50
    key = pop(evm.stack).to_be_bytes32()
51
52
    # GAS
53
    if (evm.message.current_target, key) in evm.accessed_storage_keys:
54
        charge_gas(evm, GAS_WARM_ACCESS)
55
    else:
56
        evm.accessed_storage_keys.add((evm.message.current_target, key))
57
        charge_gas(evm, GAS_COLD_SLOAD)
58
59
    # OPERATION
59
    value = get_storage(
60
        evm.message.block_env.state, evm.message.current_target, key
61
    )
60
    tx_state = evm.message.tx_env.state
61
    value = get_storage(tx_state, evm.message.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
    """
71
    Stores a value at a certain key in the current context's storage.
72
73
    Parameters
74
    ----------
75
    evm :
76
        The current EVM frame.
77
78
    """
79
    if evm.message.is_static:
80
        raise WriteInStaticContext
81
82
    # STACK
83
    key = pop(evm.stack).to_be_bytes32()
84
    new_value = pop(evm.stack)
82
    if evm.gas_left <= GAS_CALL_STIPEND:
83
        raise OutOfGasError
85
85
    state = evm.message.block_env.state
86
    original_value = get_storage_original(
87
        state, evm.message.current_target, key
86
    # check we have at least the stipend gas
87
    check_gas(evm, GAS_CALL_STIPEND + Uint(1))
88
89
    tx_state = evm.message.tx_env.state
90
    original_value = get_storage_original(
91
        tx_state, evm.message.current_target, key
92
    )
89
    current_value = get_storage(state, evm.message.current_target, key)
93
    current_value = get_storage(tx_state, evm.message.current_target, key)
94
95
    gas_cost = Uint(0)
96
97
    if (evm.message.current_target, key) not in evm.accessed_storage_keys:
98
        evm.accessed_storage_keys.add((evm.message.current_target, key))
99
        gas_cost += GAS_COLD_SLOAD
100
101
    if original_value == current_value and current_value != new_value:
102
        if original_value == 0:
103
            gas_cost += GAS_STORAGE_SET
104
        else:
105
            gas_cost += GAS_STORAGE_UPDATE - GAS_COLD_SLOAD
106
    else:
107
        gas_cost += GAS_WARM_ACCESS
108
109
    # Refund Counter Calculation
110
    if current_value != new_value:
111
        if original_value != 0 and current_value != 0 and new_value == 0:
112
            # Storage is cleared for the first time in the transaction
113
            evm.refund_counter += REFUND_STORAGE_CLEAR
114
115
        if original_value != 0 and current_value == 0:
116
            # Gas refund issued earlier to be reversed
117
            evm.refund_counter -= REFUND_STORAGE_CLEAR
118
119
        if original_value == new_value:
120
            # Storage slot being restored to its original value
121
            if original_value == 0:
122
                # Slot was originally empty and was SET earlier
123
                evm.refund_counter += int(GAS_STORAGE_SET - GAS_WARM_ACCESS)
124
            else:
125
                # Slot was originally non-empty and was UPDATED earlier
126
                evm.refund_counter += int(
127
                    GAS_STORAGE_UPDATE - GAS_COLD_SLOAD - GAS_WARM_ACCESS
128
                )
129
130
    charge_gas(evm, gas_cost)
127
    if evm.message.is_static:
128
        raise WriteInStaticContext
129
    set_storage(state, evm.message.current_target, key, new_value)
131
    set_storage(tx_state, evm.message.current_target, key, new_value)
132
133
    # PROGRAM COUNTER
134
    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:
138
    """
139
    Loads to the stack, the value corresponding to a certain key from the
140
    transient storage of the current account.
141
142
    Parameters
143
    ----------
144
    evm :
145
        The current EVM frame.
146
147
    """
148
    # STACK
149
    key = pop(evm.stack).to_be_bytes32()
150
151
    # GAS
152
    charge_gas(evm, GAS_WARM_ACCESS)
153
154
    # OPERATION
153
    value = get_transient_storage(
154
        evm.message.tx_env.transient_storage, evm.message.current_target, key
155
    value = get_transient_storage(
156
        evm.message.tx_env.state, evm.message.current_target, key
157
    )
158
    push(evm.stack, value)
159
160
    # PROGRAM COUNTER
161
    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:
165
    """
166
    Stores a value at a certain key in the current context's transient storage.
167
168
    Parameters
169
    ----------
170
    evm :
171
        The current EVM frame.
172
173
    """
174
    if evm.message.is_static:
175
        raise WriteInStaticContext
176
177
    # STACK
178
    key = pop(evm.stack).to_be_bytes32()
179
    new_value = pop(evm.stack)
180
181
    # GAS
182
    charge_gas(evm, GAS_WARM_ACCESS)
178
    if evm.message.is_static:
179
        raise WriteInStaticContext
180
    set_transient_storage(
181
        evm.message.tx_env.transient_storage,
183
    set_transient_storage(
184
        evm.message.tx_env.state,
185
        evm.message.current_target,
186
        key,
187
        new_value,
188
    )
189
190
    # PROGRAM COUNTER
191
    evm.pc += Uint(1)