ethereum.shanghai.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_STORAGE_SET
28 | GAS_STORAGE_SET = Uint(20000) |
---|
GAS_STORAGE_UPDATE
29 | GAS_STORAGE_UPDATE = Uint(5000) |
---|
GAS_STORAGE_CLEAR_REFUND
30 | GAS_STORAGE_CLEAR_REFUND = Uint(4800) |
---|
GAS_LOW
31 | GAS_LOW = Uint(5) |
---|
GAS_MID
32 | GAS_MID = Uint(8) |
---|
GAS_HIGH
33 | GAS_HIGH = Uint(10) |
---|
GAS_EXPONENTIATION
34 | GAS_EXPONENTIATION = Uint(10) |
---|
GAS_EXPONENTIATION_PER_BYTE
35 | GAS_EXPONENTIATION_PER_BYTE = Uint(50) |
---|
GAS_MEMORY
36 | GAS_MEMORY = Uint(3) |
---|
GAS_KECCAK256
37 | GAS_KECCAK256 = Uint(30) |
---|
GAS_KECCAK256_WORD
38 | GAS_KECCAK256_WORD = Uint(6) |
---|
GAS_COPY
39 | GAS_COPY = Uint(3) |
---|
GAS_BLOCK_HASH
40 | GAS_BLOCK_HASH = Uint(20) |
---|
GAS_LOG
41 | GAS_LOG = Uint(375) |
---|
GAS_LOG_DATA
42 | GAS_LOG_DATA = Uint(8) |
---|
GAS_LOG_TOPIC
43 | GAS_LOG_TOPIC = Uint(375) |
---|
GAS_CREATE
44 | GAS_CREATE = Uint(32000) |
---|
GAS_CODE_DEPOSIT
45 | GAS_CODE_DEPOSIT = Uint(200) |
---|
GAS_ZERO
46 | GAS_ZERO = Uint(0) |
---|
GAS_NEW_ACCOUNT
47 | GAS_NEW_ACCOUNT = Uint(25000) |
---|
GAS_CALL_VALUE
48 | GAS_CALL_VALUE = Uint(9000) |
---|
GAS_CALL_STIPEND
49 | GAS_CALL_STIPEND = Uint(2300) |
---|
GAS_SELF_DESTRUCT
50 | GAS_SELF_DESTRUCT = Uint(5000) |
---|
GAS_SELF_DESTRUCT_NEW_ACCOUNT
51 | GAS_SELF_DESTRUCT_NEW_ACCOUNT = Uint(25000) |
---|
GAS_ECRECOVER
52 | GAS_ECRECOVER = Uint(3000) |
---|
GAS_SHA256
53 | GAS_SHA256 = Uint(60) |
---|
GAS_SHA256_WORD
54 | GAS_SHA256_WORD = Uint(12) |
---|
GAS_RIPEMD160
55 | GAS_RIPEMD160 = Uint(600) |
---|
GAS_RIPEMD160_WORD
56 | GAS_RIPEMD160_WORD = Uint(120) |
---|
GAS_IDENTITY
57 | GAS_IDENTITY = Uint(15) |
---|
GAS_IDENTITY_WORD
58 | GAS_IDENTITY_WORD = Uint(3) |
---|
GAS_RETURN_DATA_COPY
59 | GAS_RETURN_DATA_COPY = Uint(3) |
---|
GAS_FAST_STEP
60 | GAS_FAST_STEP = Uint(5) |
---|
GAS_BLAKE2_PER_ROUND
61 | GAS_BLAKE2_PER_ROUND = Uint(1) |
---|
GAS_COLD_SLOAD
62 | GAS_COLD_SLOAD = Uint(2100) |
---|
GAS_COLD_ACCOUNT_ACCESS
63 | GAS_COLD_ACCOUNT_ACCESS = Uint(2600) |
---|
GAS_WARM_ACCESS
64 | GAS_WARM_ACCESS = Uint(100) |
---|
GAS_INIT_CODE_WORD_COST
65 | GAS_INIT_CODE_WORD_COST = Uint(2) |
---|
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
68 | @dataclass |
---|
class ExtendMemory:
cost
79 | cost: Uint |
---|
expand_by
80 | expand_by: Uint |
---|
MessageCallGas
Define the gas cost and stipend for executing the call opcodes.
cost
: ethereum.base_types.Uint
The non-refundable portion of gas reserved for executing the
call opcode.
stipend
: ethereum.base_types.Uint
The portion of gas available to sub-calls that is refundable
if not consumed
83 | @dataclass |
---|
class MessageCallGas:
cost
96 | cost: Uint |
---|
stipend
97 | stipend: 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:
101 | """ |
---|---|
102 | Subtracts `amount` from `evm.gas_left`. |
103 |
|
104 | Parameters |
105 | ---------- |
106 | evm : |
107 | The current EVM. |
108 | amount : |
109 | The amount of gas the current operation requires. |
110 |
|
111 | """ |
112 | evm_trace(evm, GasAndRefund(int(amount))) |
113 | |
114 | if evm.gas_left < amount: |
115 | raise OutOfGasError |
116 | else: |
117 | 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:
121 | """ |
---|---|
122 | Calculates the gas cost for allocating memory |
123 | to the smallest multiple of 32 bytes, |
124 | such that the allocated size is at least as big as the given size. |
125 |
|
126 | Parameters |
127 | ---------- |
128 | size_in_bytes : |
129 | The size of the data in bytes. |
130 |
|
131 | Returns |
132 | ------- |
133 | total_gas_cost : `ethereum.base_types.Uint` |
134 | The gas cost for storing data in memory. |
135 | """ |
136 | size_in_words = ceil32(size_in_bytes) // Uint(32) |
137 | linear_cost = size_in_words * GAS_MEMORY |
138 | quadratic_cost = size_in_words ** Uint(2) // Uint(512) |
139 | total_gas_cost = linear_cost + quadratic_cost |
140 | try: |
141 | return total_gas_cost |
142 | except ValueError: |
143 | raise OutOfGasError |
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:
149 | """ |
---|---|
150 | Calculates the gas amount to extend memory |
151 |
|
152 | Parameters |
153 | ---------- |
154 | memory : |
155 | Memory contents of the EVM. |
156 | extensions: |
157 | List of extensions to be made to the memory. |
158 | Consists of a tuple of start position and size. |
159 |
|
160 | Returns |
161 | ------- |
162 | extend_memory: `ExtendMemory` |
163 | """ |
164 | size_to_extend = Uint(0) |
165 | to_be_paid = Uint(0) |
166 | current_size = Uint(len(memory)) |
167 | for start_position, size in extensions: |
168 | if size == 0: |
169 | continue |
170 | before_size = ceil32(current_size) |
171 | after_size = ceil32(Uint(start_position) + Uint(size)) |
172 | if after_size <= before_size: |
173 | continue |
174 |
|
175 | size_to_extend += after_size - before_size |
176 | already_paid = calculate_memory_gas_cost(before_size) |
177 | total_cost = calculate_memory_gas_cost(after_size) |
178 | to_be_paid += total_cost - already_paid |
179 |
|
180 | current_size = after_size |
181 | |
182 | return ExtendMemory(to_be_paid, size_to_extend) |
calculate_message_call_gas
Calculates the MessageCallGas (cost and stipend) 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:
193 | """ |
---|---|
194 | Calculates the MessageCallGas (cost and stipend) for |
195 | executing call Opcodes. |
196 |
|
197 | Parameters |
198 | ---------- |
199 | value: |
200 | The amount of `ETH` that needs to be transferred. |
201 | gas : |
202 | The amount of gas provided to the message-call. |
203 | gas_left : |
204 | The amount of gas left in the current frame. |
205 | memory_cost : |
206 | The amount needed to extend the memory in the current frame. |
207 | extra_gas : |
208 | The amount of gas needed for transferring value + creating a new |
209 | account inside a message call. |
210 | call_stipend : |
211 | The amount of stipend provided to a message call to execute code while |
212 | transferring value(ETH). |
213 |
|
214 | Returns |
215 | ------- |
216 | message_call_gas: `MessageCallGas` |
217 | """ |
218 | call_stipend = Uint(0) if value == 0 else call_stipend |
219 | if gas_left < extra_gas + memory_cost: |
220 | return MessageCallGas(gas + extra_gas, gas + call_stipend) |
221 | |
222 | gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas)) |
223 | |
224 | 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:
228 | """ |
---|---|
229 | Calculates the maximum gas that is allowed for making a message call |
230 |
|
231 | Parameters |
232 | ---------- |
233 | gas : |
234 | The amount of gas provided to the message-call. |
235 |
|
236 | Returns |
237 | ------- |
238 | max_allowed_message_call_gas: `ethereum.base_types.Uint` |
239 | The maximum gas allowed for making the message-call. |
240 | """ |
241 | return gas - (gas // Uint(64)) |
init_code_cost
Calculates the gas to be charged for the init code in CREAT* opcodes as well as create transactions.
Parameters
init_code_length : The length of the init code provided to the opcode or a create transaction
Returns
init_code_gas: ethereum.base_types.Uint
The gas to be charged for the init code.
def init_code_cost(init_code_length: Uint) -> Uint:
245 | """ |
---|---|
246 | Calculates the gas to be charged for the init code in CREAT* |
247 | opcodes as well as create transactions. |
248 |
|
249 | Parameters |
250 | ---------- |
251 | init_code_length : |
252 | The length of the init code provided to the opcode |
253 | or a create transaction |
254 |
|
255 | Returns |
256 | ------- |
257 | init_code_gas: `ethereum.base_types.Uint` |
258 | The gas to be charged for the init code. |
259 | """ |
260 | return GAS_INIT_CODE_WORD_COST * ceil32(init_code_length) // Uint(32) |