ethereum.forks.byzantium.vm.gasethereum.forks.constantinople.vm.gas
Ethereum Virtual Machine (EVM) Gas.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
EVM gas constants and calculators.
GasCosts ¶
Constant gas values for the EVM.
class GasCosts:
BASE¶
| 33 | BASE: Final[Uint] = Uint(2) |
|---|
VERY_LOW¶
| 34 | VERY_LOW: Final[Uint] = Uint(3) |
|---|
LOW¶
| 35 | LOW: Final[Uint] = Uint(5) |
|---|
MID¶
| 36 | MID: Final[Uint] = Uint(8) |
|---|
HIGH¶
| 37 | HIGH: Final[Uint] = Uint(10) |
|---|
SLOAD¶
| 40 | SLOAD: Final[Uint] = Uint(200) |
|---|
STORAGE_SET¶
| 43 | STORAGE_SET: Final[Uint] = Uint(20000) |
|---|
COLD_STORAGE_WRITE¶
| 44 | COLD_STORAGE_WRITE: Final[Uint] = Uint(5000) |
|---|
CALL_VALUE¶
| 47 | CALL_VALUE: Final[Uint] = Uint(9000) |
|---|
CALL_STIPEND¶
| 48 | CALL_STIPEND: Final[Uint] = Uint(2300) |
|---|
NEW_ACCOUNT¶
| 49 | NEW_ACCOUNT: Final[Uint] = Uint(25000) |
|---|
CODE_DEPOSIT_PER_BYTE¶
| 52 | CODE_DEPOSIT_PER_BYTE: Final[Uint] = Uint(200) |
|---|
ZERO¶
| 55 | ZERO: Final[Uint] = Uint(0) |
|---|
MEMORY_PER_WORD¶
| 56 | MEMORY_PER_WORD: Final[Uint] = Uint(3) |
|---|
REFUND_STORAGE_CLEAR¶
| 59 | REFUND_STORAGE_CLEAR: Final[int] = 15000 |
|---|
REFUND_SELF_DESTRUCT¶
| 60 | REFUND_SELF_DESTRUCT: Final[int] = 24000 |
|---|
PRECOMPILE_ECRECOVER¶
| 63 | PRECOMPILE_ECRECOVER: Final[Uint] = Uint(3000) |
|---|
PRECOMPILE_SHA256_BASE¶
| 64 | PRECOMPILE_SHA256_BASE: Final[Uint] = Uint(60) |
|---|
PRECOMPILE_SHA256_PER_WORD¶
| 65 | PRECOMPILE_SHA256_PER_WORD: Final[Uint] = Uint(12) |
|---|
PRECOMPILE_RIPEMD160_BASE¶
| 66 | PRECOMPILE_RIPEMD160_BASE: Final[Uint] = Uint(600) |
|---|
PRECOMPILE_RIPEMD160_PER_WORD¶
| 67 | PRECOMPILE_RIPEMD160_PER_WORD: Final[Uint] = Uint(120) |
|---|
PRECOMPILE_IDENTITY_BASE¶
| 68 | PRECOMPILE_IDENTITY_BASE: Final[Uint] = Uint(15) |
|---|
PRECOMPILE_IDENTITY_PER_WORD¶
| 69 | PRECOMPILE_IDENTITY_PER_WORD: Final[Uint] = Uint(3) |
|---|
PRECOMPILE_ECADD¶
| 70 | PRECOMPILE_ECADD: Final[Uint] = Uint(500) |
|---|
PRECOMPILE_ECMUL¶
| 71 | PRECOMPILE_ECMUL: Final[Uint] = Uint(40000) |
|---|
PRECOMPILE_ECPAIRING_BASE¶
| 72 | PRECOMPILE_ECPAIRING_BASE: Final[Uint] = Uint(100000) |
|---|
PRECOMPILE_ECPAIRING_PER_POINT¶
| 73 | PRECOMPILE_ECPAIRING_PER_POINT: Final[Uint] = Uint(80000) |
|---|
TX_BASE¶
| 76 | TX_BASE: Final[Uint] = Uint(21000) |
|---|
TX_CREATE¶
| 77 | TX_CREATE: Final[Uint] = Uint(32000) |
|---|
TX_DATA_PER_ZERO¶
| 78 | TX_DATA_PER_ZERO: Final[Uint] = Uint(4) |
|---|
TX_DATA_PER_NON_ZERO¶
| 79 | TX_DATA_PER_NON_ZERO: Final[Uint] = Uint(68) |
|---|
LIMIT_ADJUSTMENT_FACTOR¶
| 82 | LIMIT_ADJUSTMENT_FACTOR: Final[Uint] = Uint(1024) |
|---|
LIMIT_MINIMUM¶
| 83 | LIMIT_MINIMUM: Final[Uint] = Uint(5000) |
|---|
OPCODE_ADD¶
| 86 | OPCODE_ADD: Final[Uint] = VERY_LOW |
|---|
OPCODE_SUB¶
| 87 | OPCODE_SUB: Final[Uint] = VERY_LOW |
|---|
OPCODE_MUL¶
| 88 | OPCODE_MUL: Final[Uint] = LOW |
|---|
OPCODE_DIV¶
| 89 | OPCODE_DIV: Final[Uint] = LOW |
|---|
OPCODE_SDIV¶
| 90 | OPCODE_SDIV: Final[Uint] = LOW |
|---|
OPCODE_MOD¶
| 91 | OPCODE_MOD: Final[Uint] = LOW |
|---|
OPCODE_SMOD¶
| 92 | OPCODE_SMOD: Final[Uint] = LOW |
|---|
OPCODE_ADDMOD¶
| 93 | OPCODE_ADDMOD: Final[Uint] = MID |
|---|
OPCODE_MULMOD¶
| 94 | OPCODE_MULMOD: Final[Uint] = MID |
|---|
OPCODE_SIGNEXTEND¶
| 95 | OPCODE_SIGNEXTEND: Final[Uint] = LOW |
|---|
OPCODE_LT¶
| 96 | OPCODE_LT: Final[Uint] = VERY_LOW |
|---|
OPCODE_GT¶
| 97 | OPCODE_GT: Final[Uint] = VERY_LOW |
|---|
OPCODE_SLT¶
| 98 | OPCODE_SLT: Final[Uint] = VERY_LOW |
|---|
OPCODE_SGT¶
| 99 | OPCODE_SGT: Final[Uint] = VERY_LOW |
|---|
OPCODE_EQ¶
| 100 | OPCODE_EQ: Final[Uint] = VERY_LOW |
|---|
OPCODE_ISZERO¶
| 101 | OPCODE_ISZERO: Final[Uint] = VERY_LOW |
|---|
OPCODE_AND¶
| 102 | OPCODE_AND: Final[Uint] = VERY_LOW |
|---|
OPCODE_OR¶
| 103 | OPCODE_OR: Final[Uint] = VERY_LOW |
|---|
OPCODE_XOR¶
| 104 | OPCODE_XOR: Final[Uint] = VERY_LOW |
|---|
OPCODE_NOT¶
| 105 | OPCODE_NOT: Final[Uint] = VERY_LOW |
|---|
OPCODE_BYTE¶
| 106 | OPCODE_BYTE: Final[Uint] = VERY_LOW |
|---|
OPCODE_SHL¶
| 107 | OPCODE_SHL: Final[Uint] = VERY_LOW |
|---|
OPCODE_SHR¶
| 108 | OPCODE_SHR: Final[Uint] = VERY_LOW |
|---|
OPCODE_SAR¶
| 109 | OPCODE_SAR: Final[Uint] = VERY_LOW |
|---|
OPCODE_JUMP¶
| 110 | OPCODE_JUMP: Final[Uint] = MID |
|---|
OPCODE_JUMPI¶
| 111 | OPCODE_JUMPI: Final[Uint] = HIGH |
|---|
OPCODE_JUMPDEST¶
| 112 | OPCODE_JUMPDEST: Final[Uint] = Uint(1) |
|---|
OPCODE_CALLDATALOAD¶
| 113 | OPCODE_CALLDATALOAD: Final[Uint] = VERY_LOW |
|---|
OPCODE_EXTCODEHASH¶
| 114 | OPCODE_EXTCODEHASH: Final[Uint] = Uint(400) |
|---|
OPCODE_BLOCKHASH¶
| 115 | OPCODE_BLOCKHASH: Final[Uint] = Uint(20) |
|---|
OPCODE_COINBASE¶
| 116 | OPCODE_COINBASE: Final[Uint] = BASE |
|---|
OPCODE_POP¶
| 117 | OPCODE_POP: Final[Uint] = BASE |
|---|
OPCODE_MSIZE¶
| 118 | OPCODE_MSIZE: Final[Uint] = BASE |
|---|
OPCODE_PC¶
| 119 | OPCODE_PC: Final[Uint] = BASE |
|---|
OPCODE_GAS¶
| 120 | OPCODE_GAS: Final[Uint] = BASE |
|---|
OPCODE_ADDRESS¶
| 121 | OPCODE_ADDRESS: Final[Uint] = BASE |
|---|
OPCODE_ORIGIN¶
| 122 | OPCODE_ORIGIN: Final[Uint] = BASE |
|---|
OPCODE_CALLER¶
| 123 | OPCODE_CALLER: Final[Uint] = BASE |
|---|
OPCODE_CALLVALUE¶
| 124 | OPCODE_CALLVALUE: Final[Uint] = BASE |
|---|
OPCODE_CALLDATASIZE¶
| 125 | OPCODE_CALLDATASIZE: Final[Uint] = BASE |
|---|
OPCODE_CODESIZE¶
| 126 | OPCODE_CODESIZE: Final[Uint] = BASE |
|---|
OPCODE_GASPRICE¶
| 127 | OPCODE_GASPRICE: Final[Uint] = BASE |
|---|
OPCODE_TIMESTAMP¶
| 128 | OPCODE_TIMESTAMP: Final[Uint] = BASE |
|---|
OPCODE_NUMBER¶
| 129 | OPCODE_NUMBER: Final[Uint] = BASE |
|---|
OPCODE_GASLIMIT¶
| 130 | OPCODE_GASLIMIT: Final[Uint] = BASE |
|---|
OPCODE_DIFFICULTY¶
| 131 | OPCODE_DIFFICULTY: Final[Uint] = BASE |
|---|
OPCODE_RETURNDATASIZE¶
| 132 | OPCODE_RETURNDATASIZE: Final[Uint] = BASE |
|---|
OPCODE_PUSH¶
| 133 | OPCODE_PUSH: Final[Uint] = VERY_LOW |
|---|
OPCODE_DUP¶
| 134 | OPCODE_DUP: Final[Uint] = VERY_LOW |
|---|
OPCODE_SWAP¶
| 135 | OPCODE_SWAP: Final[Uint] = VERY_LOW |
|---|
OPCODE_RETURNDATACOPY_BASE¶
| 138 | OPCODE_RETURNDATACOPY_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_RETURNDATACOPY_PER_WORD¶
| 139 | OPCODE_RETURNDATACOPY_PER_WORD: Final[Uint] = Uint(3) |
|---|
OPCODE_CALLDATACOPY_BASE¶
| 140 | OPCODE_CALLDATACOPY_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_CODECOPY_BASE¶
| 141 | OPCODE_CODECOPY_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_MLOAD_BASE¶
| 142 | OPCODE_MLOAD_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_MSTORE_BASE¶
| 143 | OPCODE_MSTORE_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_MSTORE8_BASE¶
| 144 | OPCODE_MSTORE8_BASE: Final[Uint] = VERY_LOW |
|---|
OPCODE_COPY_PER_WORD¶
| 145 | OPCODE_COPY_PER_WORD: Final[Uint] = Uint(3) |
|---|
OPCODE_CREATE_BASE¶
| 146 | OPCODE_CREATE_BASE: Final[Uint] = Uint(32000) |
|---|
OPCODE_EXP_BASE¶
| 147 | OPCODE_EXP_BASE: Final[Uint] = Uint(10) |
|---|
OPCODE_EXP_PER_BYTE¶
| 148 | OPCODE_EXP_PER_BYTE: Final[Uint] = Uint(50) |
|---|
OPCODE_KECCAK256_BASE¶
| 149 | OPCODE_KECCAK256_BASE: Final[Uint] = Uint(30) |
|---|
OPCODE_KECCACK256_PER_WORD¶
| 150 | OPCODE_KECCACK256_PER_WORD: Final[Uint] = Uint(6) |
|---|
OPCODE_LOG_BASE¶
| 151 | OPCODE_LOG_BASE: Final[Uint] = Uint(375) |
|---|
OPCODE_LOG_DATA_PER_BYTE¶
| 152 | OPCODE_LOG_DATA_PER_BYTE: Final[Uint] = Uint(8) |
|---|
OPCODE_LOG_TOPIC¶
| 153 | OPCODE_LOG_TOPIC: Final[Uint] = Uint(375) |
|---|
OPCODE_SELFDESTRUCT_BASE¶
| 154 | OPCODE_SELFDESTRUCT_BASE: Final[Uint] = Uint(5000) |
|---|
OPCODE_SELFDESTRUCT_NEW_ACCOUNT¶
| 155 | OPCODE_SELFDESTRUCT_NEW_ACCOUNT: Final[Uint] = Uint(25000) |
|---|
OPCODE_EXTERNAL_BASE¶
| 156 | OPCODE_EXTERNAL_BASE: Final[Uint] = Uint(700) |
|---|
OPCODE_BALANCE¶
| 157 | OPCODE_BALANCE: Final[Uint] = Uint(400) |
|---|
OPCODE_CALL_BASE¶
| 158 | OPCODE_CALL_BASE: Final[Uint] = Uint(700) |
|---|
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
| 161 | @dataclass |
|---|
class ExtendMemory:
cost¶
| 172 | cost: Uint |
|---|
expand_by¶
| 173 | 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.
| 176 | @dataclass |
|---|
class MessageCallGas:
cost¶
| 190 | cost: Uint |
|---|
sub_call¶
| 191 | 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.
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:
| 215 | <snip> |
|---|---|
| 231 | size_in_words = ceil32(size_in_bytes) // Uint(32) |
| 232 | linear_cost = size_in_words * GasCosts.MEMORY_PER_WORD |
| 233 | quadratic_cost = size_in_words ** Uint(2) // Uint(512) |
| 234 | total_gas_cost = linear_cost + quadratic_cost |
| 235 | try: |
| 236 | return total_gas_cost |
| 237 | except ValueError as e: |
| 238 | 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:
| 244 | <snip> |
|---|---|
| 260 | size_to_extend = Uint(0) |
| 261 | to_be_paid = Uint(0) |
| 262 | current_size = ulen(memory) |
| 263 | for start_position, size in extensions: |
| 264 | if size == 0: |
| 265 | continue |
| 266 | before_size = ceil32(current_size) |
| 267 | after_size = ceil32(Uint(start_position) + Uint(size)) |
| 268 | if after_size <= before_size: |
| 269 | continue |
| 270 | |
| 271 | size_to_extend += after_size - before_size |
| 272 | already_paid = calculate_memory_gas_cost(before_size) |
| 273 | total_cost = calculate_memory_gas_cost(after_size) |
| 274 | to_be_paid += total_cost - already_paid |
| 275 | |
| 276 | current_size = after_size |
| 277 | |
| 278 | 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:
| 289 | <snip> |
|---|---|
| 315 | call_stipend = Uint(0) if value == 0 else call_stipend |
| 316 | if gas_left < extra_gas + memory_cost: |
| 317 | return MessageCallGas(gas + extra_gas, gas + call_stipend) |
| 318 | |
| 319 | gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas)) |
| 320 | |
| 321 | 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.