ethereum.forks.constantinople.vm.gasethereum.forks.istanbul.vm.gas

Ethereum Virtual Machine (EVM) Gas.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

EVM gas constants and calculators.

GAS_JUMPDEST

25
GAS_JUMPDEST = Uint(1)

GAS_BASE

26
GAS_BASE = Uint(2)

GAS_VERY_LOW

27
GAS_VERY_LOW = Uint(3)

GAS_SLOAD

28
GAS_SLOAD = Uint(200)
28
GAS_SLOAD = Uint(800)

GAS_STORAGE_SET

29
GAS_STORAGE_SET = Uint(20000)

GAS_STORAGE_UPDATE

30
GAS_STORAGE_UPDATE = Uint(5000)

GAS_STORAGE_CLEAR_REFUND

31
GAS_STORAGE_CLEAR_REFUND = Uint(15000)

GAS_LOW

32
GAS_LOW = Uint(5)

GAS_MID

33
GAS_MID = Uint(8)

GAS_HIGH

34
GAS_HIGH = Uint(10)

GAS_EXPONENTIATION

35
GAS_EXPONENTIATION = Uint(10)

GAS_EXPONENTIATION_PER_BYTE

36
GAS_EXPONENTIATION_PER_BYTE = Uint(50)

GAS_MEMORY

37
GAS_MEMORY = Uint(3)

GAS_KECCAK256

38
GAS_KECCAK256 = Uint(30)

GAS_KECCAK256_WORD

39
GAS_KECCAK256_WORD = Uint(6)

GAS_COPY

40
GAS_COPY = Uint(3)

GAS_BLOCK_HASH

41
GAS_BLOCK_HASH = Uint(20)

GAS_EXTERNAL

42
GAS_EXTERNAL = Uint(700)

GAS_BALANCE

43
GAS_BALANCE = Uint(400)
43
GAS_BALANCE = Uint(700)

GAS_LOG

44
GAS_LOG = Uint(375)

GAS_LOG_DATA

45
GAS_LOG_DATA = Uint(8)

GAS_LOG_TOPIC

46
GAS_LOG_TOPIC = Uint(375)

GAS_CREATE

47
GAS_CREATE = Uint(32000)

GAS_CODE_DEPOSIT

48
GAS_CODE_DEPOSIT = Uint(200)

GAS_ZERO

49
GAS_ZERO = Uint(0)

GAS_CALL

50
GAS_CALL = Uint(700)

GAS_NEW_ACCOUNT

51
GAS_NEW_ACCOUNT = Uint(25000)

GAS_CALL_VALUE

52
GAS_CALL_VALUE = Uint(9000)

GAS_CALL_STIPEND

53
GAS_CALL_STIPEND = Uint(2300)

GAS_SELF_DESTRUCT

54
GAS_SELF_DESTRUCT = Uint(5000)

GAS_SELF_DESTRUCT_NEW_ACCOUNT

55
GAS_SELF_DESTRUCT_NEW_ACCOUNT = Uint(25000)

REFUND_SELF_DESTRUCT

56
REFUND_SELF_DESTRUCT = 24000

GAS_ECRECOVER

57
GAS_ECRECOVER = Uint(3000)

GAS_SHA256

58
GAS_SHA256 = Uint(60)

GAS_SHA256_WORD

59
GAS_SHA256_WORD = Uint(12)

GAS_RIPEMD160

60
GAS_RIPEMD160 = Uint(600)

GAS_RIPEMD160_WORD

61
GAS_RIPEMD160_WORD = Uint(120)

GAS_IDENTITY

62
GAS_IDENTITY = Uint(15)

GAS_IDENTITY_WORD

63
GAS_IDENTITY_WORD = Uint(3)

GAS_RETURN_DATA_COPY

64
GAS_RETURN_DATA_COPY = Uint(3)

GAS_CODE_HASH

65
GAS_CODE_HASH = Uint(400)
65
GAS_CODE_HASH = Uint(700)

GAS_FAST_STEP

66
GAS_FAST_STEP = Uint(5)

GAS_BLAKE2_PER_ROUND

67
GAS_BLAKE2_PER_ROUND = Uint(1)

ExtendMemory

Define the parameters for memory extension in opcodes.

cost: ethereum.base_types.Uint The gas required to perform the extension expand_by: ethereum.base_types.Uint The size by which the memory will be extended

70
@dataclass
class ExtendMemory:

cost

81
    cost: Uint

expand_by

82
    expand_by: Uint

MessageCallGas

Define the gas cost and gas given to the sub-call for executing the call opcodes.

cost: ethereum.base_types.Uint The gas required to execute the call opcode, excludes memory expansion costs. sub_call: ethereum.base_types.Uint The portion of gas available to sub-calls that is refundable if not consumed.

85
@dataclass
class MessageCallGas:

cost

99
    cost: Uint

sub_call

100
    sub_call: Uint

charge_gas

Subtracts amount from evm.gas_left.

Parameters

evm : The current EVM. amount : The amount of gas the current operation requires.

def charge_gas(evm: Evm, ​​amount: Uint) -> None:
104
    """
105
    Subtracts `amount` from `evm.gas_left`.
106
107
    Parameters
108
    ----------
109
    evm :
110
        The current EVM.
111
    amount :
112
        The amount of gas the current operation requires.
113
114
    """
115
    evm_trace(evm, GasAndRefund(int(amount)))
116
117
    if evm.gas_left < amount:
118
        raise OutOfGasError
119
    else:
120
        evm.gas_left -= amount

calculate_memory_gas_cost

Calculates the gas cost for allocating memory to the smallest multiple of 32 bytes, such that the allocated size is at least as big as the given size.

Parameters

size_in_bytes : The size of the data in bytes.

Returns

total_gas_cost : ethereum.base_types.Uint The gas cost for storing data in memory.

def calculate_memory_gas_cost(size_in_bytes: Uint) -> Uint:
124
    """
125
    Calculates the gas cost for allocating memory
126
    to the smallest multiple of 32 bytes,
127
    such that the allocated size is at least as big as the given size.
128
129
    Parameters
130
    ----------
131
    size_in_bytes :
132
        The size of the data in bytes.
133
134
    Returns
135
    -------
136
    total_gas_cost : `ethereum.base_types.Uint`
137
        The gas cost for storing data in memory.
138
139
    """
140
    size_in_words = ceil32(size_in_bytes) // Uint(32)
141
    linear_cost = size_in_words * GAS_MEMORY
142
    quadratic_cost = size_in_words ** Uint(2) // Uint(512)
143
    total_gas_cost = linear_cost + quadratic_cost
144
    try:
145
        return total_gas_cost
146
    except ValueError as e:
147
        raise OutOfGasError from e

calculate_gas_extend_memory

Calculates the gas amount to extend memory.

Parameters

memory : Memory contents of the EVM. extensions: List of extensions to be made to the memory. Consists of a tuple of start position and size.

Returns

extend_memory: ExtendMemory

def calculate_gas_extend_memory(memory: bytearray, ​​extensions: List[Tuple[U256, U256]]) -> ExtendMemory:
153
    """
154
    Calculates the gas amount to extend memory.
155
156
    Parameters
157
    ----------
158
    memory :
159
        Memory contents of the EVM.
160
    extensions:
161
        List of extensions to be made to the memory.
162
        Consists of a tuple of start position and size.
163
164
    Returns
165
    -------
166
    extend_memory: `ExtendMemory`
167
168
    """
169
    size_to_extend = Uint(0)
170
    to_be_paid = Uint(0)
171
    current_size = Uint(len(memory))
172
    for start_position, size in extensions:
173
        if size == 0:
174
            continue
175
        before_size = ceil32(current_size)
176
        after_size = ceil32(Uint(start_position) + Uint(size))
177
        if after_size <= before_size:
178
            continue
179
180
        size_to_extend += after_size - before_size
181
        already_paid = calculate_memory_gas_cost(before_size)
182
        total_cost = calculate_memory_gas_cost(after_size)
183
        to_be_paid += total_cost - already_paid
184
185
        current_size = after_size
186
187
    return ExtendMemory(to_be_paid, size_to_extend)

calculate_message_call_gas

Calculates the MessageCallGas (cost and gas made available to the sub-call) for executing call Opcodes.

Parameters

value: The amount of ETH that needs to be transferred. gas : The amount of gas provided to the message-call. gas_left : The amount of gas left in the current frame. memory_cost : The amount needed to extend the memory in the current frame. extra_gas : The amount of gas needed for transferring value + creating a new account inside a message call. call_stipend : The amount of stipend provided to a message call to execute code while transferring value(ETH).

Returns

message_call_gas: MessageCallGas

def calculate_message_call_gas(value: U256, ​​gas: Uint, ​​gas_left: Uint, ​​memory_cost: Uint, ​​extra_gas: Uint, ​​call_stipend: Uint) -> MessageCallGas:
198
    """
199
    Calculates the MessageCallGas (cost and gas made available to the sub-call)
200
    for executing call Opcodes.
201
202
    Parameters
203
    ----------
204
    value:
205
        The amount of `ETH` that needs to be transferred.
206
    gas :
207
        The amount of gas provided to the message-call.
208
    gas_left :
209
        The amount of gas left in the current frame.
210
    memory_cost :
211
        The amount needed to extend the memory in the current frame.
212
    extra_gas :
213
        The amount of gas needed for transferring value + creating a new
214
        account inside a message call.
215
    call_stipend :
216
        The amount of stipend provided to a message call to execute code while
217
        transferring value(ETH).
218
219
    Returns
220
    -------
221
    message_call_gas: `MessageCallGas`
222
223
    """
224
    call_stipend = Uint(0) if value == 0 else call_stipend
225
    if gas_left < extra_gas + memory_cost:
226
        return MessageCallGas(gas + extra_gas, gas + call_stipend)
227
228
    gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
229
230
    return MessageCallGas(gas + extra_gas, gas + call_stipend)

max_message_call_gas

Calculates the maximum gas that is allowed for making a message call.

Parameters

gas : The amount of gas provided to the message-call.

Returns

max_allowed_message_call_gas: ethereum.base_types.Uint The maximum gas allowed for making the message-call.

def max_message_call_gas(gas: Uint) -> Uint:
234
    """
235
    Calculates the maximum gas that is allowed for making a message call.
236
237
    Parameters
238
    ----------
239
    gas :
240
        The amount of gas provided to the message-call.
241
242
    Returns
243
    -------
244
    max_allowed_message_call_gas: `ethereum.base_types.Uint`
245
        The maximum gas allowed for making the message-call.
246
247
    """
248
    return gas - (gas // Uint(64))