Execution Gas
Gas is a sum of IntrinsicGas
and ExecutionGas
. In here, we would focus on how ExecutionGas
is calculated.
Execution gas related hardfork changes can be found at the bottom of this page. Go to Hardfork Changes.
Overview
Execution gas is charged during executing a contract under three distinct circumstances. Sometimes, some policies may be omitted.
- The first and most common is the
constantGas
. It's a fee intrinsic to the computation of the operation. - Second, gas may be deducted to form the payment for a subordinate message call or contract creation; this forms part of the payment for
CREATE
,CALL
andCALLCODE
. - Finally, gas may be charged due to an increase in memory usage.
Over an account's execution, the total fee payable for memory-usage payable is proportional to the smallest multiple of 32 bytes that are required to include all memory indices (whether for read or write) in the range. This fee is paid on a just-in-time basis; consequently, referencing an area of memory at least 32 bytes greater than any previously indexed memory will result in an additional memory usage fee. Due to this fee, it is highly unlikely that addresses will ever exceed the 32-bit bounds. That said, implementations must be able to manage this eventuality.
Storage fees have a slightly nuanced behavior. To incentivize minimization of the use of storage (which corresponds directly to a larger state database on all nodes), the execution fee for an operation that clears an entry from storage is not only waived but also elicits a qualified refund; in fact, this refund is effectively paid in advance because the initial usage of a storage location costs substantially more than normal usage.
Opcode Gas Schedule
The fee schedule G
is a tuple of 37 scalar values corresponding to the relative costs, in gas, of a number of abstract operations that a transaction may incur. Also, there's gas items to calculate the gas of the precompiled contracts called by CALL_*
opcodes.
Scalar values representing constantGas
of an opcode
Name | Value | Name in code | Opcodes |
---|---|---|---|
G_base | 2 | GasQuickStep | ADDRESS , ORIGIN , CALLER , CALLVALUE , CALLDATASIZE , CODESIZE , GASPRICE , COINBASE , TIMESTAMP , NUMBER , PREVRANDAO (originally it was DIFFICULTY ), GASLIMIT , RETURNDATASIZE , POP , PC , MSIZE , GAS , CHAINID , BASEFEE , PUSH0 , BLOBBASEFEE |
G_verylow | 3 | GasFastestStep | ADD , SUB , LT , GT , SLT , SGT , EQ , ISZERO , AND , OR , XOR , NOT , BYTE , CALLDATALOAD , MLOAD , MSTORE , MSTORE8 , PUSH , DUP , SWAP , BLOBHASH , MCOPY |
G_low | 5 | GasFastStep | MUL , DIV , SDIV , MOD , SMOD , SIGNEXTEND , SELFBALANCE |
G_mid | 8 | GasMidStep | ADDMOD , MULMOD , JUMP |
G_high | 10 | GasSlowStep | JUMPI |
G_selfdestruct | 5000 | SelfdestructGas | SELFDESTRUCT |
G_warmStorageReadCost | 100 | WarmStorageReadCostEIP2929 | EXTCODECOPY , EXTCODESIZE , EXTCODEHASH , BALANCE , CALL , CALLCODE , STATICCALL , DELEGATECALL , TSTORE , TLOAD |
G_blockhash | 20 | GasExtStep | BLOCKHASH |
G_jumpdest | 1 | JumpdestGas | JUMPDEST |
G_sha3 | 30 | Sha3Gas | SHA3 |
G_create | 32000 | CreateGas | CREATE , CREATE2 |