ethereum.forks.istanbul.vm.instructions.storageethereum.forks.muir_glacier.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:
24
    <snip>
34
    # STACK
35
    key = pop(evm.stack).to_be_bytes32()
36
37
    # GAS
38
    charge_gas(evm, GasCosts.SLOAD)
39
40
    # OPERATION
41
    tx_state = evm.message.tx_env.state
42
    value = get_storage(tx_state, evm.message.current_target, key)
43
44
    push(evm.stack, value)
45
46
    # PROGRAM COUNTER
47
    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:
51
    <snip>
60
    # STACK
61
    key = pop(evm.stack).to_be_bytes32()
62
    new_value = pop(evm.stack)
63
    if evm.gas_left <= GasCosts.CALL_STIPEND:
64
        raise OutOfGasError
65
66
    tx_state = evm.message.tx_env.state
67
    original_value = get_storage_original(
68
        tx_state, evm.message.current_target, key
69
    )
70
    current_value = get_storage(tx_state, evm.message.current_target, key)
71
72
    if original_value == current_value and current_value != new_value:
73
        if original_value == 0:
74
            gas_cost = GasCosts.STORAGE_SET
75
        else:
76
            gas_cost = GasCosts.COLD_STORAGE_WRITE
77
    else:
78
        gas_cost = GasCosts.SLOAD
79
80
    # Refund Counter Calculation
81
    if current_value != new_value:
82
        if original_value != 0 and current_value != 0 and new_value == 0:
83
            # Storage is cleared for the first time in the transaction
84
            evm.refund_counter += GasCosts.REFUND_STORAGE_CLEAR
85
86
        if original_value != 0 and current_value == 0:
87
            # Gas refund issued earlier to be reversed
88
            evm.refund_counter -= GasCosts.REFUND_STORAGE_CLEAR
89
90
        if original_value == new_value:
91
            # Storage slot being restored to its original value
92
            if original_value == 0:
93
                # Slot was originally empty and was SET earlier
94
                evm.refund_counter += int(
95
                    GasCosts.STORAGE_SET - GasCosts.SLOAD
96
                )
97
            else:
98
                # Slot was originally non-empty and was UPDATED earlier
99
                evm.refund_counter += int(
100
                    GasCosts.COLD_STORAGE_WRITE - GasCosts.SLOAD
101
                )
102
103
    charge_gas(evm, gas_cost)
104
    if evm.message.is_static:
105
        raise WriteInStaticContext
106
    set_storage(tx_state, evm.message.current_target, key, new_value)
107
108
    # PROGRAM COUNTER
109
    evm.pc += Uint(1)