ethereum.forks.london.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:
27
    """
28
    Loads to the stack, the value corresponding to a certain key from the
29
    storage of the current account.
30
31
    Parameters
32
    ----------
33
    evm :
34
        The current EVM frame.
35
36
    """
37
    # STACK
38
    key = pop(evm.stack).to_be_bytes32()
39
40
    # GAS
41
    if (evm.message.current_target, key) in evm.accessed_storage_keys:
42
        charge_gas(evm, GasCosts.WARM_ACCESS)
43
    else:
44
        evm.accessed_storage_keys.add((evm.message.current_target, key))
45
        charge_gas(evm, GasCosts.COLD_STORAGE_ACCESS)
46
47
    # OPERATION
48
    value = get_storage(
49
        evm.message.block_env.state, evm.message.current_target, key
50
    )
51
52
    push(evm.stack, value)
53
54
    # PROGRAM COUNTER
55
    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:
59
    """
60
    Stores a value at a certain key in the current context's storage.
61
62
    Parameters
63
    ----------
64
    evm :
65
        The current EVM frame.
66
67
    """
68
    # STACK
69
    key = pop(evm.stack).to_be_bytes32()
70
    new_value = pop(evm.stack)
71
    if evm.gas_left <= GasCosts.CALL_STIPEND:
72
        raise OutOfGasError
73
74
    state = evm.message.block_env.state
75
    original_value = get_storage_original(
76
        state, evm.message.current_target, key
77
    )
78
    current_value = get_storage(state, evm.message.current_target, key)
79
80
    gas_cost = Uint(0)
81
82
    if (evm.message.current_target, key) not in evm.accessed_storage_keys:
83
        evm.accessed_storage_keys.add((evm.message.current_target, key))
84
        gas_cost += GasCosts.COLD_STORAGE_ACCESS
85
86
    if original_value == current_value and current_value != new_value:
87
        if original_value == 0:
88
            gas_cost += GasCosts.STORAGE_SET
89
        else:
90
            gas_cost += (
91
                GasCosts.COLD_STORAGE_WRITE - GasCosts.COLD_STORAGE_ACCESS
92
            )
93
    else:
94
        gas_cost += GasCosts.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 += GasCosts.REFUND_STORAGE_CLEAR
101
102
        if original_value != 0 and current_value == 0:
103
            # Gas refund issued earlier to be reversed
104
            evm.refund_counter -= GasCosts.REFUND_STORAGE_CLEAR
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(
111
                    GasCosts.STORAGE_SET - GasCosts.WARM_ACCESS
112
                )
113
            else:
114
                # Slot was originally non-empty and was UPDATED earlier
115
                evm.refund_counter += int(
116
                    GasCosts.COLD_STORAGE_WRITE
117
                    - GasCosts.COLD_STORAGE_ACCESS
118
                    - GasCosts.WARM_ACCESS
119
                )
120
121
    charge_gas(evm, gas_cost)
122
    if evm.message.is_static:
123
        raise WriteInStaticContext
124
    set_storage(state, evm.message.current_target, key, new_value)
125
126
    # PROGRAM COUNTER
127
    evm.pc += Uint(1)