ethereum.forks.bpo5.vm.gasethereum.forks.amsterdam.vm.gas

Ethereum Virtual Machine (EVM) Gas.

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

Introduction

EVM gas constants and calculators.

StateGasCosts

EIP-8037 state-gas constants.

Kept separate from GasCosts because these carry a different unit: state-byte counts that convert into gas via COST_PER_STATE_BYTE.

class StateGasCosts:

COST_PER_STATE_BYTE

56
    COST_PER_STATE_BYTE: Final[StateGasPerByte] = StateGasPerByte(Uint(1530))

STATE_BYTES_PER_NEW_ACCOUNT

57
    STATE_BYTES_PER_NEW_ACCOUNT: Final[Uint] = Uint(120)

STATE_BYTES_PER_STORAGE_SET

58
    STATE_BYTES_PER_STORAGE_SET: Final[Uint] = Uint(64)

STATE_BYTES_PER_AUTH_BASE

59
    STATE_BYTES_PER_AUTH_BASE: Final[Uint] = Uint(23)

STORAGE_SET

60
    STORAGE_SET: Final[StateGas] = (
61
        STATE_BYTES_PER_STORAGE_SET * COST_PER_STATE_BYTE
62
    )

NEW_ACCOUNT

63
    NEW_ACCOUNT: Final[StateGas] = (
64
        STATE_BYTES_PER_NEW_ACCOUNT * COST_PER_STATE_BYTE
65
    )

AUTH_BASE

66
    AUTH_BASE: Final[StateGas] = (
67
        STATE_BYTES_PER_AUTH_BASE * COST_PER_STATE_BYTE
68
    )

GasCosts

Constant gas values for the EVM.

class GasCosts:

BASE

36
    BASE: Final[Uint] = Uint(2)
78
    BASE: Final[ExecutionGas] = ExecutionGas(Uint(2))

VERY_LOW

37
    VERY_LOW: Final[Uint] = Uint(3)
79
    VERY_LOW: Final[ExecutionGas] = ExecutionGas(Uint(3))

LOW

38
    LOW: Final[Uint] = Uint(5)
80
    LOW: Final[ExecutionGas] = ExecutionGas(Uint(5))

MID

39
    MID: Final[Uint] = Uint(8)
81
    MID: Final[ExecutionGas] = ExecutionGas(Uint(8))

HIGH

40
    HIGH: Final[Uint] = Uint(10)
82
    HIGH: Final[ExecutionGas] = ExecutionGas(Uint(10))

WARM_ACCESS

43
    WARM_ACCESS: Final[Uint] = Uint(100)
85
    WARM_ACCESS: Final[ExecutionGas] = ExecutionGas(Uint(100))

COLD_ACCOUNT_ACCESS

44
    COLD_ACCOUNT_ACCESS: Final[Uint] = Uint(2600)
86
    COLD_ACCOUNT_ACCESS: Final[ExecutionGas] = ExecutionGas(Uint(3000))

COLD_STORAGE_ACCESS

45
    COLD_STORAGE_ACCESS: Final[Uint] = Uint(2100)
87
    COLD_STORAGE_ACCESS: Final[ExecutionGas] = ExecutionGas(Uint(2100))

STORAGE_SET

48
    STORAGE_SET: Final[Uint] = Uint(20000)

COLD_STORAGE_WRITE

49
    COLD_STORAGE_WRITE: Final[Uint] = Uint(5000)

STORAGE_WRITE

90
    STORAGE_WRITE: Final[ExecutionGas] = ExecutionGas(Uint(10000))

CALL_STIPEND

52
    CALL_STIPEND: Final[Uint] = Uint(2300)
93
    CALL_STIPEND: Final[ExecutionGas] = ExecutionGas(Uint(2300))

ACCOUNT_WRITE

94
    ACCOUNT_WRITE: Final[ExecutionGas] = ExecutionGas(Uint(9000))

CALL_VALUE

53
    CALL_VALUE: Final[Uint] = Uint(9000)
95
    CALL_VALUE: Final[ExecutionGas] = ACCOUNT_WRITE + CALL_STIPEND

NEW_ACCOUNT

54
    NEW_ACCOUNT: Final[Uint] = Uint(25000)

CODE_DEPOSIT_PER_BYTE

57
    CODE_DEPOSIT_PER_BYTE: Final[Uint] = Uint(200)

CODE_INIT_PER_WORD

58
    CODE_INIT_PER_WORD: Final[Uint] = Uint(2)
98
    CODE_INIT_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(2))

AUTH_PER_EMPTY_ACCOUNT

61
    AUTH_PER_EMPTY_ACCOUNT: Final[int] = 25000

CREATE_ACCESS

99
    CREATE_ACCESS: Final[ExecutionGas] = ACCOUNT_WRITE + COLD_ACCOUNT_ACCESS

ZERO

64
    ZERO: Final[Uint] = Uint(0)
102
    ZERO: Final[ExecutionGas] = ExecutionGas(Uint(0))

MEMORY_PER_WORD

65
    MEMORY_PER_WORD: Final[Uint] = Uint(3)
103
    MEMORY_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(3))

FAST_STEP

66
    FAST_STEP: Final[Uint] = Uint(5)
104
    FAST_STEP: Final[ExecutionGas] = ExecutionGas(Uint(5))

REFUND_STORAGE_CLEAR

69
    REFUND_STORAGE_CLEAR: Final[int] = 4800
107
    REFUND_STORAGE_CLEAR: Final[int] = int(
108
        (STORAGE_WRITE + COLD_STORAGE_ACCESS) * Uint(4800) // Uint(5000)
109
    )

REFUND_AUTH_PER_EXISTING_ACCOUNT

70
    REFUND_AUTH_PER_EXISTING_ACCOUNT: Final[int] = 12500

PRECOMPILE_ECRECOVER

73
    PRECOMPILE_ECRECOVER: Final[Uint] = Uint(3000)
112
    PRECOMPILE_ECRECOVER: Final[ExecutionGas] = ExecutionGas(Uint(3000))

PRECOMPILE_P256VERIFY

74
    PRECOMPILE_P256VERIFY: Final[Uint] = Uint(6900)
113
    PRECOMPILE_P256VERIFY: Final[ExecutionGas] = ExecutionGas(Uint(6900))

PRECOMPILE_SHA256_BASE

75
    PRECOMPILE_SHA256_BASE: Final[Uint] = Uint(60)
114
    PRECOMPILE_SHA256_BASE: Final[ExecutionGas] = ExecutionGas(Uint(60))

PRECOMPILE_SHA256_PER_WORD

76
    PRECOMPILE_SHA256_PER_WORD: Final[Uint] = Uint(12)
115
    PRECOMPILE_SHA256_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(12))

PRECOMPILE_RIPEMD160_BASE

77
    PRECOMPILE_RIPEMD160_BASE: Final[Uint] = Uint(600)
116
    PRECOMPILE_RIPEMD160_BASE: Final[ExecutionGas] = ExecutionGas(Uint(600))

PRECOMPILE_RIPEMD160_PER_WORD

78
    PRECOMPILE_RIPEMD160_PER_WORD: Final[Uint] = Uint(120)
117
    PRECOMPILE_RIPEMD160_PER_WORD: Final[ExecutionGas] = ExecutionGas(
118
        Uint(120)
119
    )

PRECOMPILE_IDENTITY_BASE

79
    PRECOMPILE_IDENTITY_BASE: Final[Uint] = Uint(15)
120
    PRECOMPILE_IDENTITY_BASE: Final[ExecutionGas] = ExecutionGas(Uint(15))

PRECOMPILE_IDENTITY_PER_WORD

80
    PRECOMPILE_IDENTITY_PER_WORD: Final[Uint] = Uint(3)
121
    PRECOMPILE_IDENTITY_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(3))

PRECOMPILE_BLAKE2F_PER_ROUND

81
    PRECOMPILE_BLAKE2F_PER_ROUND: Final[Uint] = Uint(1)
122
    PRECOMPILE_BLAKE2F_PER_ROUND: Final[ExecutionGas] = ExecutionGas(Uint(1))

PRECOMPILE_POINT_EVALUATION

82
    PRECOMPILE_POINT_EVALUATION: Final[Uint] = Uint(50000)
123
    PRECOMPILE_POINT_EVALUATION: Final[ExecutionGas] = ExecutionGas(
124
        Uint(50000)
125
    )

PRECOMPILE_BLS_G1ADD

83
    PRECOMPILE_BLS_G1ADD: Final[Uint] = Uint(375)
126
    PRECOMPILE_BLS_G1ADD: Final[ExecutionGas] = ExecutionGas(Uint(375))

PRECOMPILE_BLS_G1MUL

84
    PRECOMPILE_BLS_G1MUL: Final[Uint] = Uint(12000)
127
    PRECOMPILE_BLS_G1MUL: Final[ExecutionGas] = ExecutionGas(Uint(12000))

PRECOMPILE_BLS_G1MAP

85
    PRECOMPILE_BLS_G1MAP: Final[Uint] = Uint(5500)
128
    PRECOMPILE_BLS_G1MAP: Final[ExecutionGas] = ExecutionGas(Uint(5500))

PRECOMPILE_BLS_G2ADD

86
    PRECOMPILE_BLS_G2ADD: Final[Uint] = Uint(600)
129
    PRECOMPILE_BLS_G2ADD: Final[ExecutionGas] = ExecutionGas(Uint(600))

PRECOMPILE_BLS_G2MUL

87
    PRECOMPILE_BLS_G2MUL: Final[Uint] = Uint(22500)
130
    PRECOMPILE_BLS_G2MUL: Final[ExecutionGas] = ExecutionGas(Uint(22500))

PRECOMPILE_BLS_G2MAP

88
    PRECOMPILE_BLS_G2MAP: Final[Uint] = Uint(23800)
131
    PRECOMPILE_BLS_G2MAP: Final[ExecutionGas] = ExecutionGas(Uint(23800))

PRECOMPILE_ECADD

89
    PRECOMPILE_ECADD: Final[Uint] = Uint(150)
132
    PRECOMPILE_ECADD: Final[ExecutionGas] = ExecutionGas(Uint(150))

PRECOMPILE_ECMUL

90
    PRECOMPILE_ECMUL: Final[Uint] = Uint(6000)
133
    PRECOMPILE_ECMUL: Final[ExecutionGas] = ExecutionGas(Uint(6000))

PRECOMPILE_ECPAIRING_BASE

91
    PRECOMPILE_ECPAIRING_BASE: Final[Uint] = Uint(45000)
134
    PRECOMPILE_ECPAIRING_BASE: Final[ExecutionGas] = ExecutionGas(Uint(45000))

PRECOMPILE_ECPAIRING_PER_POINT

92
    PRECOMPILE_ECPAIRING_PER_POINT: Final[Uint] = Uint(34000)
135
    PRECOMPILE_ECPAIRING_PER_POINT: Final[ExecutionGas] = ExecutionGas(
136
        Uint(34000)
137
    )

PER_BLOB

140
    PER_BLOB: Final[U64] = U64(2**17)

BLOB_SCHEDULE_TARGET

141
    BLOB_SCHEDULE_TARGET: Final[U64] = U64(14)

BLOB_TARGET_GAS_PER_BLOCK

142
    BLOB_TARGET_GAS_PER_BLOCK: Final[U64] = PER_BLOB * BLOB_SCHEDULE_TARGET

BLOB_BASE_COST

143
    BLOB_BASE_COST: Final[Uint] = Uint(2**13)

BLOB_SCHEDULE_MAX

144
    BLOB_SCHEDULE_MAX: Final[U64] = U64(21)

BLOB_MIN_GASPRICE

145
    BLOB_MIN_GASPRICE: Final[Uint] = Uint(1)

BLOB_BASE_FEE_UPDATE_FRACTION

146
    BLOB_BASE_FEE_UPDATE_FRACTION: Final[Uint] = Uint(11684671)

BLOCK_ACCESS_LIST_ITEM

149
    BLOCK_ACCESS_LIST_ITEM: Final[ExecutionGas] = ExecutionGas(Uint(2000))

TX_BASE

104
    TX_BASE: Final[Uint] = Uint(21000)
152
    TX_BASE: Final[ExecutionGas] = ExecutionGas(Uint(12000))

TX_CREATE

105
    TX_CREATE: Final[Uint] = Uint(32000)

TX_VALUE_COST

153
    TX_VALUE_COST: Final[ExecutionGas] = ExecutionGas(Uint(6000))

TX_DATA_TOKEN_STANDARD

106
    TX_DATA_TOKEN_STANDARD: Final[Uint] = Uint(4)
154
    TX_DATA_TOKEN_STANDARD: Final[ExecutionGas] = ExecutionGas(Uint(4))

TX_DATA_TOKEN_FLOOR

107
    TX_DATA_TOKEN_FLOOR: Final[Uint] = Uint(10)
155
    TX_DATA_TOKEN_FLOOR: Final[ExecutionGas] = ExecutionGas(Uint(16))

TX_ACCESS_LIST_ADDRESS

108
    TX_ACCESS_LIST_ADDRESS: Final[Uint] = Uint(2400)
156
    TX_ACCESS_LIST_ADDRESS: Final[ExecutionGas] = (
157
        COLD_ACCOUNT_ACCESS - WARM_ACCESS
158
    )

TX_ACCESS_LIST_STORAGE_KEY

109
    TX_ACCESS_LIST_STORAGE_KEY: Final[Uint] = Uint(1900)
159
    TX_ACCESS_LIST_STORAGE_KEY: Final[ExecutionGas] = (
160
        COLD_STORAGE_ACCESS - WARM_ACCESS
161
    )

TX_MAX_GAS_LIMIT

162
    TX_MAX_GAS_LIMIT: Final[Uint] = Uint(16_777_216)

TX_MAX_TOTAL_GAS_LIMIT

163
    TX_MAX_TOTAL_GAS_LIMIT: Final[Uint] = Uint(4_294_967_295)

AUTH_TUPLE_BYTES

166
    AUTH_TUPLE_BYTES: Final[Uint] = Uint(101)

EXECUTION_PER_AUTH_BASE_COST

167
    EXECUTION_PER_AUTH_BASE_COST: Final[ExecutionGas] = ExecutionGas(
168
        AUTH_TUPLE_BYTES * TX_DATA_TOKEN_FLOOR
169
        + PRECOMPILE_ECRECOVER
170
        + COLD_ACCOUNT_ACCESS
171
        + Uint(2) * WARM_ACCESS
172
    )

LIMIT_ADJUSTMENT_FACTOR

175
    LIMIT_ADJUSTMENT_FACTOR: Final[Uint] = Uint(1024)

LIMIT_MINIMUM

176
    LIMIT_MINIMUM: Final[Uint] = Uint(5000)

OPCODE_ADD

117
    OPCODE_ADD: Final[Uint] = VERY_LOW
179
    OPCODE_ADD: Final[ExecutionGas] = VERY_LOW

OPCODE_SUB

118
    OPCODE_SUB: Final[Uint] = VERY_LOW
180
    OPCODE_SUB: Final[ExecutionGas] = VERY_LOW

OPCODE_MUL

119
    OPCODE_MUL: Final[Uint] = LOW
181
    OPCODE_MUL: Final[ExecutionGas] = LOW

OPCODE_DIV

120
    OPCODE_DIV: Final[Uint] = LOW
182
    OPCODE_DIV: Final[ExecutionGas] = LOW

OPCODE_SDIV

121
    OPCODE_SDIV: Final[Uint] = LOW
183
    OPCODE_SDIV: Final[ExecutionGas] = LOW

OPCODE_MOD

122
    OPCODE_MOD: Final[Uint] = LOW
184
    OPCODE_MOD: Final[ExecutionGas] = LOW

OPCODE_SMOD

123
    OPCODE_SMOD: Final[Uint] = LOW
185
    OPCODE_SMOD: Final[ExecutionGas] = LOW

OPCODE_ADDMOD

124
    OPCODE_ADDMOD: Final[Uint] = MID
186
    OPCODE_ADDMOD: Final[ExecutionGas] = MID

OPCODE_MULMOD

125
    OPCODE_MULMOD: Final[Uint] = MID
187
    OPCODE_MULMOD: Final[ExecutionGas] = MID

OPCODE_SIGNEXTEND

126
    OPCODE_SIGNEXTEND: Final[Uint] = LOW
188
    OPCODE_SIGNEXTEND: Final[ExecutionGas] = LOW

OPCODE_LT

127
    OPCODE_LT: Final[Uint] = VERY_LOW
189
    OPCODE_LT: Final[ExecutionGas] = VERY_LOW

OPCODE_GT

128
    OPCODE_GT: Final[Uint] = VERY_LOW
190
    OPCODE_GT: Final[ExecutionGas] = VERY_LOW

OPCODE_SLT

129
    OPCODE_SLT: Final[Uint] = VERY_LOW
191
    OPCODE_SLT: Final[ExecutionGas] = VERY_LOW

OPCODE_SGT

130
    OPCODE_SGT: Final[Uint] = VERY_LOW
192
    OPCODE_SGT: Final[ExecutionGas] = VERY_LOW

OPCODE_EQ

131
    OPCODE_EQ: Final[Uint] = VERY_LOW
193
    OPCODE_EQ: Final[ExecutionGas] = VERY_LOW

OPCODE_ISZERO

132
    OPCODE_ISZERO: Final[Uint] = VERY_LOW
194
    OPCODE_ISZERO: Final[ExecutionGas] = VERY_LOW

OPCODE_AND

133
    OPCODE_AND: Final[Uint] = VERY_LOW
195
    OPCODE_AND: Final[ExecutionGas] = VERY_LOW

OPCODE_OR

134
    OPCODE_OR: Final[Uint] = VERY_LOW
196
    OPCODE_OR: Final[ExecutionGas] = VERY_LOW

OPCODE_XOR

135
    OPCODE_XOR: Final[Uint] = VERY_LOW
197
    OPCODE_XOR: Final[ExecutionGas] = VERY_LOW

OPCODE_NOT

136
    OPCODE_NOT: Final[Uint] = VERY_LOW
198
    OPCODE_NOT: Final[ExecutionGas] = VERY_LOW

OPCODE_BYTE

137
    OPCODE_BYTE: Final[Uint] = VERY_LOW
199
    OPCODE_BYTE: Final[ExecutionGas] = VERY_LOW

OPCODE_SHL

138
    OPCODE_SHL: Final[Uint] = VERY_LOW
200
    OPCODE_SHL: Final[ExecutionGas] = VERY_LOW

OPCODE_SHR

139
    OPCODE_SHR: Final[Uint] = VERY_LOW
201
    OPCODE_SHR: Final[ExecutionGas] = VERY_LOW

OPCODE_SAR

140
    OPCODE_SAR: Final[Uint] = VERY_LOW
202
    OPCODE_SAR: Final[ExecutionGas] = VERY_LOW

OPCODE_CLZ

141
    OPCODE_CLZ: Final[Uint] = LOW
203
    OPCODE_CLZ: Final[ExecutionGas] = LOW

OPCODE_JUMP

142
    OPCODE_JUMP: Final[Uint] = MID
204
    OPCODE_JUMP: Final[ExecutionGas] = MID

OPCODE_JUMPI

143
    OPCODE_JUMPI: Final[Uint] = HIGH
205
    OPCODE_JUMPI: Final[ExecutionGas] = HIGH

OPCODE_JUMPDEST

144
    OPCODE_JUMPDEST: Final[Uint] = Uint(1)
206
    OPCODE_JUMPDEST: Final[ExecutionGas] = ExecutionGas(Uint(1))

OPCODE_CALLDATALOAD

145
    OPCODE_CALLDATALOAD: Final[Uint] = VERY_LOW
207
    OPCODE_CALLDATALOAD: Final[ExecutionGas] = VERY_LOW

OPCODE_BLOCKHASH

146
    OPCODE_BLOCKHASH: Final[Uint] = Uint(20)
208
    OPCODE_BLOCKHASH: Final[ExecutionGas] = ExecutionGas(Uint(20))

OPCODE_COINBASE

147
    OPCODE_COINBASE: Final[Uint] = BASE
209
    OPCODE_COINBASE: Final[ExecutionGas] = BASE

OPCODE_POP

148
    OPCODE_POP: Final[Uint] = BASE
210
    OPCODE_POP: Final[ExecutionGas] = BASE

OPCODE_MSIZE

149
    OPCODE_MSIZE: Final[Uint] = BASE
211
    OPCODE_MSIZE: Final[ExecutionGas] = BASE

OPCODE_PC

150
    OPCODE_PC: Final[Uint] = BASE
212
    OPCODE_PC: Final[ExecutionGas] = BASE

OPCODE_GAS

151
    OPCODE_GAS: Final[Uint] = BASE
213
    OPCODE_GAS: Final[ExecutionGas] = BASE

OPCODE_ADDRESS

152
    OPCODE_ADDRESS: Final[Uint] = BASE
214
    OPCODE_ADDRESS: Final[ExecutionGas] = BASE

OPCODE_ORIGIN

153
    OPCODE_ORIGIN: Final[Uint] = BASE
215
    OPCODE_ORIGIN: Final[ExecutionGas] = BASE

OPCODE_CALLER

154
    OPCODE_CALLER: Final[Uint] = BASE
216
    OPCODE_CALLER: Final[ExecutionGas] = BASE

OPCODE_CALLVALUE

155
    OPCODE_CALLVALUE: Final[Uint] = BASE
217
    OPCODE_CALLVALUE: Final[ExecutionGas] = BASE

OPCODE_CALLDATASIZE

156
    OPCODE_CALLDATASIZE: Final[Uint] = BASE
218
    OPCODE_CALLDATASIZE: Final[ExecutionGas] = BASE

OPCODE_CODESIZE

157
    OPCODE_CODESIZE: Final[Uint] = BASE
219
    OPCODE_CODESIZE: Final[ExecutionGas] = BASE

OPCODE_GASPRICE

158
    OPCODE_GASPRICE: Final[Uint] = BASE
220
    OPCODE_GASPRICE: Final[ExecutionGas] = BASE

OPCODE_TIMESTAMP

159
    OPCODE_TIMESTAMP: Final[Uint] = BASE
221
    OPCODE_TIMESTAMP: Final[ExecutionGas] = BASE

OPCODE_NUMBER

160
    OPCODE_NUMBER: Final[Uint] = BASE
222
    OPCODE_NUMBER: Final[ExecutionGas] = BASE

OPCODE_GASLIMIT

161
    OPCODE_GASLIMIT: Final[Uint] = BASE
223
    OPCODE_GASLIMIT: Final[ExecutionGas] = BASE

OPCODE_PREVRANDAO

162
    OPCODE_PREVRANDAO: Final[Uint] = BASE
224
    OPCODE_PREVRANDAO: Final[ExecutionGas] = BASE

OPCODE_RETURNDATASIZE

163
    OPCODE_RETURNDATASIZE: Final[Uint] = BASE
225
    OPCODE_RETURNDATASIZE: Final[ExecutionGas] = BASE

OPCODE_CHAINID

164
    OPCODE_CHAINID: Final[Uint] = BASE
226
    OPCODE_CHAINID: Final[ExecutionGas] = BASE

OPCODE_SELFBALANCE

165
    OPCODE_SELFBALANCE: Final[Uint] = FAST_STEP
227
    OPCODE_SELFBALANCE: Final[ExecutionGas] = FAST_STEP

OPCODE_BASEFEE

166
    OPCODE_BASEFEE: Final[Uint] = BASE
228
    OPCODE_BASEFEE: Final[ExecutionGas] = BASE

OPCODE_BLOBBASEFEE

167
    OPCODE_BLOBBASEFEE: Final[Uint] = BASE
229
    OPCODE_BLOBBASEFEE: Final[ExecutionGas] = BASE

OPCODE_SLOTNUM

230
    OPCODE_SLOTNUM: Final[ExecutionGas] = BASE

OPCODE_BLOBHASH

168
    OPCODE_BLOBHASH: Final[Uint] = Uint(3)
231
    OPCODE_BLOBHASH: Final[ExecutionGas] = ExecutionGas(Uint(3))

OPCODE_PUSH

169
    OPCODE_PUSH: Final[Uint] = VERY_LOW
232
    OPCODE_PUSH: Final[ExecutionGas] = VERY_LOW

OPCODE_PUSH0

170
    OPCODE_PUSH0: Final[Uint] = BASE
233
    OPCODE_PUSH0: Final[ExecutionGas] = BASE

OPCODE_DUP

171
    OPCODE_DUP: Final[Uint] = VERY_LOW
234
    OPCODE_DUP: Final[ExecutionGas] = VERY_LOW

OPCODE_SWAP

172
    OPCODE_SWAP: Final[Uint] = VERY_LOW
235
    OPCODE_SWAP: Final[ExecutionGas] = VERY_LOW

OPCODE_DUPN

236
    OPCODE_DUPN: Final[ExecutionGas] = VERY_LOW

OPCODE_SWAPN

237
    OPCODE_SWAPN: Final[ExecutionGas] = VERY_LOW

OPCODE_EXCHANGE

238
    OPCODE_EXCHANGE: Final[ExecutionGas] = VERY_LOW

OPCODE_TLOAD

173
    OPCODE_TLOAD: Final[Uint] = WARM_ACCESS
239
    OPCODE_TLOAD: Final[ExecutionGas] = ExecutionGas(Uint(100))

OPCODE_TSTORE

174
    OPCODE_TSTORE: Final[Uint] = WARM_ACCESS
240
    OPCODE_TSTORE: Final[ExecutionGas] = ExecutionGas(Uint(100))

OPCODE_RETURNDATACOPY_BASE

177
    OPCODE_RETURNDATACOPY_BASE: Final[Uint] = VERY_LOW
243
    OPCODE_RETURNDATACOPY_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_RETURNDATACOPY_PER_WORD

178
    OPCODE_RETURNDATACOPY_PER_WORD: Final[Uint] = Uint(3)
244
    OPCODE_RETURNDATACOPY_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(3))

OPCODE_CALLDATACOPY_BASE

179
    OPCODE_CALLDATACOPY_BASE: Final[Uint] = VERY_LOW
245
    OPCODE_CALLDATACOPY_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_CODECOPY_BASE

180
    OPCODE_CODECOPY_BASE: Final[Uint] = VERY_LOW
246
    OPCODE_CODECOPY_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_MCOPY_BASE

181
    OPCODE_MCOPY_BASE: Final[Uint] = VERY_LOW
247
    OPCODE_MCOPY_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_MLOAD_BASE

182
    OPCODE_MLOAD_BASE: Final[Uint] = VERY_LOW
248
    OPCODE_MLOAD_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_MSTORE_BASE

183
    OPCODE_MSTORE_BASE: Final[Uint] = VERY_LOW
249
    OPCODE_MSTORE_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_MSTORE8_BASE

184
    OPCODE_MSTORE8_BASE: Final[Uint] = VERY_LOW
250
    OPCODE_MSTORE8_BASE: Final[ExecutionGas] = VERY_LOW

OPCODE_COPY_PER_WORD

185
    OPCODE_COPY_PER_WORD: Final[Uint] = Uint(3)
251
    OPCODE_COPY_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(3))

OPCODE_CREATE_BASE

186
    OPCODE_CREATE_BASE: Final[Uint] = Uint(32000)

OPCODE_EXP_BASE

187
    OPCODE_EXP_BASE: Final[Uint] = Uint(10)
252
    OPCODE_EXP_BASE: Final[ExecutionGas] = ExecutionGas(Uint(10))

OPCODE_EXP_PER_BYTE

188
    OPCODE_EXP_PER_BYTE: Final[Uint] = Uint(50)
253
    OPCODE_EXP_PER_BYTE: Final[ExecutionGas] = ExecutionGas(Uint(50))

OPCODE_KECCAK256_BASE

189
    OPCODE_KECCAK256_BASE: Final[Uint] = Uint(30)
254
    OPCODE_KECCAK256_BASE: Final[ExecutionGas] = ExecutionGas(Uint(30))

OPCODE_KECCAK256_PER_WORD

190
    OPCODE_KECCAK256_PER_WORD: Final[Uint] = Uint(6)
255
    OPCODE_KECCAK256_PER_WORD: Final[ExecutionGas] = ExecutionGas(Uint(6))

OPCODE_LOG_BASE

191
    OPCODE_LOG_BASE: Final[Uint] = Uint(375)
256
    OPCODE_LOG_BASE: Final[ExecutionGas] = ExecutionGas(Uint(375))

OPCODE_LOG_DATA_PER_BYTE

192
    OPCODE_LOG_DATA_PER_BYTE: Final[Uint] = Uint(8)
257
    OPCODE_LOG_DATA_PER_BYTE: Final[ExecutionGas] = ExecutionGas(Uint(8))

OPCODE_LOG_TOPIC

193
    OPCODE_LOG_TOPIC: Final[Uint] = Uint(375)
258
    OPCODE_LOG_TOPIC: Final[ExecutionGas] = ExecutionGas(Uint(375))

OPCODE_SELFDESTRUCT_BASE

194
    OPCODE_SELFDESTRUCT_BASE: Final[Uint] = Uint(5000)
259
    OPCODE_SELFDESTRUCT_BASE: Final[ExecutionGas] = ExecutionGas(Uint(5000))

OPCODE_SELFDESTRUCT_NEW_ACCOUNT

195
    OPCODE_SELFDESTRUCT_NEW_ACCOUNT: Final[Uint] = Uint(25000)

MAX_BLOB_GAS_PER_BLOCK

262
MAX_BLOB_GAS_PER_BLOCK: Final[U64] = (
263
    GasCosts.BLOB_SCHEDULE_MAX * GasCosts.PER_BLOB
264
)

GasMeter

Track a frame's gas consumption across both gas dimensions.

Bundle every mutable gas quantity a frame maintains, so the frame and its settlement work against one object instead of a scatter of fields on the Evm.

267
@final
268
@dataclass
class GasMeter:

gas_left

Gas still available from the frame's execution-gas grant. Pays execution-gas charges, and state charges as spill once the reservoir empties.

280
    gas_left: ExecutionGas

state_gas_left

State gas still available in the frame's reservoir. Charges draw from here first and spill into gas_left once it is empty.

289
    state_gas_left: StateGas

state_gas_baseline

Reservoir level a rollback refills to: the frame's grant at entry, moved down by commit_state_gas when charges become non-refillable.

295
    state_gas_baseline: StateGas

refund_counter

Gas eligible for refund at the end of the transaction.

304
    refund_counter: int = 0

state_gas_spilled

Execution gas spent covering state charges after the reservoir emptied, not yet credited back. Credited back to gas_left first, in LIFO order, on a refund or failure, and repaid from the reservoir when a successful child merges (repay_state_gas_spill). EIP-8037 names this quantity state_gas_from_gas_left.

307
    state_gas_spilled: StateGas = StateGas(Uint(0))

state_gas_committed_spill

Spill that commit_state_gas marked non-refillable. It outlives the rollbacks restore_state_gas performs; only restore_state_gas_to_entry credits it back to gas_left. Committed reservoir draw needs no counter of its own: each commit lowers the baseline, so it is the frame's grant minus state_gas_baseline.

320
    state_gas_committed_spill: StateGas = StateGas(Uint(0))

ExtendMemory

Define the parameters for memory extension in opcodes.

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

336
@final
337
@dataclass
class ExtendMemory:

cost

210
    cost: Uint
348
    cost: ExecutionGas

expand_by

349
    expand_by: Uint

MessageCallGas

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

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

352
@final
353
@dataclass
class MessageCallGas:

cost

229
    cost: Uint
367
    cost: ExecutionGas

sub_call

230
    sub_call: Uint
368
    sub_call: ExecutionGas

check_gas

Checks if amount gas is available without charging it. Raises OutOfGasError if insufficient gas.

Parameters

evm : The current EVM. amount : The amount of execution gas to check.

def check_gas(​evm: "Evm", ​​amount: ExecutionGas​) -> None:
372
    <snip>
384
    if evm.gas_meter.gas_left < amount:
385
        raise OutOfGasError

charge_gas_from_meter

Subtracts amount from gas_left (execution gas).

Parameters

gas_meter : The gas meter. amount : The amount of execution gas the current operation requires.

def charge_gas_from_meter(​gas_meter: GasMeter, ​​amount: ExecutionGas​) -> None:
389
    <snip>
400
    if gas_meter.gas_left < amount:
401
        raise OutOfGasError
402
    gas_meter.gas_left -= amount

charge_gas

Subtracts amount from evm.gas_leftgas_left. (execution gas).

Parameters

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

def charge_gas(​evm: Evm"Evm", ​​amount: UintExecutionGas​) -> None:
406
    <snip>
417
    evm_trace(evm, GasAndRefund(int(amount)))
418
247
    if evm.gas_left < amount:
248
        raise OutOfGasError
249
    else:
250
        evm.gas_left -= amount
419
    charge_gas_from_meter(evm.gas_meter, amount)

charge_state_gas_from_meter

Subtracts amount from the state gas reservoir, then from gas_left when the reservoir is empty, tracking any spill.

Parameters

gas_meter : The gas meter. amount : The amount of state gas the current operation requires.

def charge_state_gas_from_meter(​gas_meter: GasMeter, ​​amount: StateGas​) -> None:
423
    <snip>
437
    if gas_meter.state_gas_left >= amount:
438
        gas_meter.state_gas_left -= amount
439
    elif Uint(gas_meter.state_gas_left) + Uint(gas_meter.gas_left) >= amount:
440
        remainder = amount - gas_meter.state_gas_left
441
        gas_meter.state_gas_left = StateGas(Uint(0))
442
        gas_meter.gas_left = ExecutionGas(gas_meter.gas_left - Uint(remainder))
443
        gas_meter.state_gas_spilled += remainder
444
    else:
445
        raise OutOfGasError

charge_state_gas

Subtracts amount from the state gas reservoir, then from gas_left when the reservoir is empty, tracking any spill.

Parameters

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

def charge_state_gas(​evm: "Evm", ​​amount: StateGas​) -> None:
449
    <snip>
463
    evm_trace(evm, StateGasAndRefund(int(amount)))
464
465
    charge_state_gas_from_meter(evm.gas_meter, amount)

commit_state_gas

Mark the state gas spent so far as non-refillable.

A later rollback via restore_state_gas leaves the state bought so far in place, so it must not credit this gas back. In the top frame that protects the delegations applied by set_delegation, which survive a failure of the dispatched code. A failure that reverts the committed state as well -- one raised before dispatch -- must instead undo the commit with restore_state_gas_to_entry.

Move the baseline down to the current reservoir level and fold the spill into state_gas_committed_spill, so later refunds route to the reservoir instead of back into gas_left.

Parameters

gas_meter : The frame's gas meter.

def commit_state_gas(​gas_meter: GasMeter​) -> None:
469
    <snip>
494
    # Only charges precede a commit, so no refund has pushed the
495
    # reservoir above the baseline: a commit only ever lowers it.
496
    assert gas_meter.state_gas_left <= gas_meter.state_gas_baseline
497
    gas_meter.state_gas_committed_spill += gas_meter.state_gas_spilled
498
    gas_meter.state_gas_baseline = gas_meter.state_gas_left
499
    gas_meter.state_gas_spilled = StateGas(Uint(0))

restore_state_gas

Roll the frame's state gas back to the baseline on revert or halt.

The frame's state changes are undone, so the state gas consumed since the baseline is credited back in LIFO order: the spill returns to gas_left first, then the reservoir resets to the baseline. The refunds accrued on the undone changes are discarded with them. State gas committed as non-refillable stays charged.

Parameters

gas_meter : The frame's gas meter.

def restore_state_gas(​gas_meter: GasMeter​) -> None:
503
    <snip>  # noqa: E501
521
    gas_meter.gas_left = ExecutionGas(
522
        gas_meter.gas_left + Uint(gas_meter.state_gas_spilled)
523
    )
524
    gas_meter.state_gas_spilled = StateGas(Uint(0))
525
    gas_meter.state_gas_left = gas_meter.state_gas_baseline
526
    gas_meter.refund_counter = 0

restore_state_gas_to_entry

Roll the frame's state gas back to frame entry, undoing any commit.

Used when the transaction-state rollback also reverts the applied delegations a commit_state_gas protected: every state charge refills -- all spill, committed or not, returns to gas_left -- and the baseline resets to the frame's grant.

Parameters

gas_meter : The frame's gas meter. state_gas_reservoir : The frame's immutable state gas grant.

def restore_state_gas_to_entry(​gas_meter: GasMeter, ​​state_gas_reservoir: StateGas​) -> None:
532
    <snip>  # noqa: E501
551
    # The baseline starts at the grant and only ever moves down.
552
    assert gas_meter.state_gas_baseline <= state_gas_reservoir
553
    # Only pre-dispatch failures roll back to entry, and no refund
554
    # accrues before dispatch.
555
    assert gas_meter.refund_counter == 0
556
    gas_meter.gas_left = ExecutionGas(
557
        gas_meter.gas_left
558
        + Uint(gas_meter.state_gas_spilled)
559
        + Uint(gas_meter.state_gas_committed_spill)
560
    )
561
    gas_meter.state_gas_spilled = StateGas(Uint(0))
562
    gas_meter.state_gas_committed_spill = StateGas(Uint(0))
563
    gas_meter.state_gas_left = state_gas_reservoir
564
    gas_meter.state_gas_baseline = state_gas_reservoir

tx_state_gas_used

Return the net state gas a transaction's execution consumed.

Measured off the top frame's finished gas meter: the reservoir drawn down since the transaction's grant plus the spill, outstanding or committed. May be negative when refunds exceed charges.

Parameters

gas_meter : The top frame's finished gas meter. state_gas_reservoir : The transaction's immutable state gas grant.

Returns

state_gas_used : int The net state gas consumed.

def tx_state_gas_used(​gas_meter: GasMeter, ​​state_gas_reservoir: StateGas​) -> int:
570
    <snip>
593
    # The baseline starts at the grant and only ever moves down.
594
    assert gas_meter.state_gas_baseline <= state_gas_reservoir
595
    return (
596
        int(state_gas_reservoir)
597
        - int(gas_meter.state_gas_left)
598
        + int(gas_meter.state_gas_spilled)
599
        + int(gas_meter.state_gas_committed_spill)
600
    )

credit_state_gas_refund

Credit a state gas refund to the local frame, in LIFO order.

State-gas charges draw from the reservoir first and from gas_left last, so refunds credit the pool charged last first: gas_left up to the spill, then the reservoir. This restores the exact pools the charge drew from, so the two never drift.

Parameters

gas_meter : The gas meter crediting the refund. amount : The refund amount to credit.

def credit_state_gas_refund(​gas_meter: GasMeter, ​​amount: StateGas​) -> None:
604
    <snip>
622
    from_gas_left = min(amount, gas_meter.state_gas_spilled)
623
    gas_meter.gas_left = ExecutionGas(gas_meter.gas_left + Uint(from_gas_left))
624
    gas_meter.state_gas_spilled -= from_gas_left
625
    gas_meter.state_gas_left += amount - from_gas_left

repay_state_gas_spill

Repay outstanding spill from the reservoir after a child merges.

A refund may land in a different frame than the charge it undoes: the refunding frame's spill can be smaller than the refund, so the excess credits the reservoir even though the charge drew from gas_left. Once a successful child's meter is absorbed, the claim and the credit sit in one meter, and the reservoir repays gas_left up to the spill still outstanding. No state creation is undone, so the used counters do not move; gas only crosses back between the two pools it drifted across.

Parameters

gas_meter : The merged gas meter.

def repay_state_gas_spill(​gas_meter: GasMeter​) -> None:
629
    <snip>  # noqa: E501
649
    repayment = min(gas_meter.state_gas_left, gas_meter.state_gas_spilled)
650
    gas_meter.gas_left = ExecutionGas(gas_meter.gas_left + Uint(repayment))
651
    gas_meter.state_gas_left -= repayment
652
    gas_meter.state_gas_spilled -= repayment
653
    # The claim and the credit cannot both survive a repayment.
654
    assert gas_meter.state_gas_left == Uint(0) or (
655
        gas_meter.state_gas_spilled == Uint(0)
656
    )

forfeit_remaining_gas

Consume all remaining execution gas on an exceptional halt.

Parameters

gas_meter : The halted frame's gas meter.

def forfeit_remaining_gas(​gas_meter: GasMeter​) -> None:
660
    <snip>
669
    # A rollback owes any outstanding spill back to `gas_left`; it
670
    # must be restored before the remainder burns.
671
    assert gas_meter.state_gas_spilled == Uint(0)
672
    gas_meter.gas_left = ExecutionGas(Uint(0))

withhold_create_gas

Withhold and return the gas made available to a CREATE* child.

Deduct the all-but-one-64th share from the frame's gas_left and return it as the child frame's execution-gas grant.

Parameters

gas_meter : The creating frame's gas meter.

Returns

child_gas : ExecutionGas The execution gas granted to the child frame.

def withhold_create_gas(​gas_meter: GasMeter​) -> ExecutionGas:
676
    <snip>
693
    child_gas = max_message_call_gas(gas_meter.gas_left)
694
    gas_meter.gas_left -= child_gas
695
    return child_gas

drain_state_gas_reservoir

Empty the frame's state gas reservoir for a child frame.

A child frame receives the parent's entire reservoir; there is no all-but-one-64th rule for state gas. The parent's reservoir is restored when the child returns.

Parameters

gas_meter : The parent frame's gas meter.

Returns

reservoir : StateGas The state gas granted to the child frame.

def drain_state_gas_reservoir(​gas_meter: GasMeter​) -> StateGas:
699
    <snip>
717
    reservoir = gas_meter.state_gas_left
718
    gas_meter.state_gas_left = StateGas(Uint(0))
719
    return reservoir

restore_child_gas

Return a child frame's unused gas grant to the parent.

Used when the child frame is never entered (for example, a stack depth or balance check fails): the withheld execution gas and drained reservoir are returned untouched.

Parameters

gas_meter : The parent frame's gas meter. gas : The execution gas grant to return. state_gas_reservoir : The state gas reservoir to return.

def restore_child_gas(​gas_meter: GasMeter, ​​gas: ExecutionGas, ​​state_gas_reservoir: StateGas​) -> None:
725
    <snip>
742
    gas_meter.gas_left += gas
743
    gas_meter.state_gas_left += state_gas_reservoir

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.UintExecutionGas The gas cost for storing data in memory.

def calculate_memory_gas_cost(​size_in_bytes: Uint​) -> UintExecutionGas:
747
    <snip>
763
    size_in_words = ceil32(size_in_bytes) // Uint(32)
764
    linear_cost = size_in_words * GasCosts.MEMORY_PER_WORD
765
    quadratic_cost = size_in_words ** Uint(2) // Uint(512)
766
    total_gas_cost = linear_cost + quadratic_cost
767
    try:
275
        return total_gas_cost
768
        return ExecutionGas(total_gas_cost)
769
    except ValueError as e:
770
        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:
776
    <snip>
792
    size_to_extend = Uint(0)
300
    to_be_paid = Uint(0)
793
    to_be_paid = GasCosts.ZERO
794
    current_size = ulen(memory)
795
    for start_position, size in extensions:
796
        if size == 0:
797
            continue
798
        before_size = ceil32(current_size)
799
        after_size = ceil32(Uint(start_position) + Uint(size))
800
        if after_size <= before_size:
801
            continue
802
803
        size_to_extend += after_size - before_size
804
        already_paid = calculate_memory_gas_cost(before_size)
805
        total_cost = calculate_memory_gas_cost(after_size)
806
        to_be_paid += total_cost - already_paid
807
808
        current_size = after_size
809
810
    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 newThe call's own execution charge (access, value transfer and account inside a message call.delegation resolution) that the forwarding budget must cover. Account creation is charged in state gas separately; CALL charges this itself and passes zero here. 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: UintExecutionGas, ​​gas_left: UintExecutionGas, ​​memory_cost: UintExecutionGas, ​​extra_gas: UintExecutionGas, ​​call_stipend: UintExecutionGas​) -> MessageCallGas:
821
    <snip>
354
    call_stipend = Uint(0) if value == 0 else call_stipend
849
    call_stipend = GasCosts.ZERO if value == 0 else call_stipend
850
    if gas_left < extra_gas + memory_cost:
851
        return MessageCallGas(gas + extra_gas, gas + call_stipend)
852
853
    gas = min(gas, max_message_call_gas(gas_left - memory_cost - extra_gas))
854
855
    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.UintExecutionGas The maximum gas allowed for making the message-call.

def max_message_call_gas(​gas: UintExecutionGas​) -> UintExecutionGas:
859
    <snip>
378
    return gas - (gas // Uint(64))
873
    return ExecutionGas(gas - (gas // Uint(64)))

init_code_cost

Calculates the gas to be charged for the init code in CREATE* 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.UintExecutionGas The gas to be charged for the init code.

def init_code_cost(​init_code_length: Uint​) -> UintExecutionGas:
877
    <snip>
398
    return GasCosts.CODE_INIT_PER_WORD * ceil32(init_code_length) // Uint(32)
893
    return ExecutionGas(
894
        GasCosts.CODE_INIT_PER_WORD * ceil32(init_code_length) // Uint(32)
895
    )

calculate_excess_blob_gas

Calculates the excess blob gas for the current block based on the gas used in the parent block.

Parameters

parent_header : The parent block of the current block.

Returns

excess_blob_gas: ethereum.base_types.U64 The excess blob gas for the current block.

def calculate_excess_blob_gas(​parent_header: Header | PreviousHeaderPreviousHeader​) -> U64:
901
    <snip>
916
    # Defaults for a parent without blob gas fields.
917
    excess_blob_gas = U64(0)
918
    blob_gas_used = U64(0)
919
    base_fee_per_gas = Uint(0)
920
424
    if isinstance(parent_header, (Header, PreviousHeader)):
921
    if isinstance(parent_header, (Header, PreviousHeader)):
922
        # Read them from any parent that carries the fields, so
923
        # accumulated excess blob gas survives a fork transition.
924
        excess_blob_gas = parent_header.excess_blob_gas
925
        blob_gas_used = parent_header.blob_gas_used
926
        base_fee_per_gas = parent_header.base_fee_per_gas
927
928
    parent_blob_gas = excess_blob_gas + blob_gas_used
929
    if parent_blob_gas < GasCosts.BLOB_TARGET_GAS_PER_BLOCK:
930
        return U64(0)
931
932
    target_blob_gas_price = Uint(GasCosts.PER_BLOB)
933
    target_blob_gas_price *= calculate_blob_gas_price(excess_blob_gas)
934
935
    base_blob_tx_price = GasCosts.BLOB_BASE_COST * base_fee_per_gas
936
    if base_blob_tx_price > target_blob_gas_price:
937
        blob_schedule_delta = (
938
            GasCosts.BLOB_SCHEDULE_MAX - GasCosts.BLOB_SCHEDULE_TARGET
939
        )
940
        return (
941
            excess_blob_gas
942
            + blob_gas_used * blob_schedule_delta // GasCosts.BLOB_SCHEDULE_MAX
943
        )
944
945
    return parent_blob_gas - GasCosts.BLOB_TARGET_GAS_PER_BLOCK

calculate_total_blob_gas

Calculate the total blob gas for a transaction.

Parameters

tx : The transaction for which the blob gas is to be calculated.

Returns

total_blob_gas: ethereum.base_types.Uint The total blob gas for the transaction.

def calculate_total_blob_gas(​tx: Transaction​) -> U64:
949
    <snip>
963
    if isinstance(tx, BlobTransaction):
964
        return GasCosts.PER_BLOB * U64(len(tx.blob_versioned_hashes))
965
    else:
966
        return U64(0)

calculate_blob_gas_price

Calculate the blob gasprice for a block.

Parameters

excess_blob_gas : The excess blob gas for the block.

Returns

blob_gasprice: Uint The blob gasprice.

def calculate_blob_gas_price(​excess_blob_gas: U64​) -> Uint:
970
    <snip>
984
    return taylor_exponential(
985
        GasCosts.BLOB_MIN_GASPRICE,
986
        Uint(excess_blob_gas),
987
        GasCosts.BLOB_BASE_FEE_UPDATE_FRACTION,
988
    )

calculate_data_fee

Calculate the blob data fee for a transaction.

Parameters

excess_blob_gas : The excess_blob_gas for the execution. tx : The transaction for which the blob data fee is to be calculated.

Returns

data_fee: Uint The blob data fee.

def calculate_data_fee(​excess_blob_gas: U64, ​​tx: Transaction​) -> Uint:
992
    <snip>
1008
    return Uint(calculate_total_blob_gas(tx)) * calculate_blob_gas_price(
1009
        excess_blob_gas
1010
    )

check_max_fee_per_blob_gas

Check that a transaction carrying blobs pays at least the blob gas price.

A transaction without blobs pays no blob fee, so its fee cap is not checked.

Parameters

blob_versioned_hashes : The transaction's blob versioned hashes. max_fee_per_blob_gas : The transaction's fee cap per unit of blob gas. excess_blob_gas : The block's excess blob gas.

Raises

InsufficientMaxFeePerBlobGasError : If the fee cap does not cover the blob gas price.

def check_max_fee_per_blob_gas(​blob_versioned_hashes: Tuple[VersionedHash, ...], ​​max_fee_per_blob_gas: U256, ​​excess_blob_gas: U64​) -> None:
1018
    <snip>
1040
    if not blob_versioned_hashes:
1041
        return
1042
1043
    blob_gas_price = calculate_blob_gas_price(excess_blob_gas)
1044
    if Uint(max_fee_per_blob_gas) < blob_gas_price:
1045
        raise InsufficientMaxFeePerBlobGasError(
1046
            "insufficient max fee per blob gas"
1047
        )

check_block_gas_capacity

Check that the transaction fits the block's remaining gas capacity.

Each dimension is checked against its own remaining budget: execution gas, where a single transaction can consume at most TX_MAX_GAS_LIMIT; state gas; and blob gas.

Parameters

block_env : The block scoped environment. block_output : The block output for the current block. tx_gas : The transaction's gas limit. tx_blob_gas : The blob gas used by the transaction.

Raises

GasUsedExceedsLimitError : If the transaction exceeds the block's remaining execution or state gas. BlobGasLimitExceededError : If the transaction exceeds the block's remaining blob gas.

def check_block_gas_capacity(​block_env: "BlockEnvironment", ​​block_output: "BlockOutput", ​​tx_gas: Uint, ​​tx_blob_gas: U64​) -> None:
1056
    <snip>  # noqa: E501
1085
    execution_gas_available = (
1086
        block_env.block_gas_limit - block_output.block_gas_used
1087
    )
1088
    state_gas_available = (
1089
        block_env.block_gas_limit - block_output.block_state_gas_used
1090
    )
1091
    blob_gas_available = MAX_BLOB_GAS_PER_BLOCK - block_output.blob_gas_used
1092
1093
    if min(GasCosts.TX_MAX_GAS_LIMIT, tx_gas) > execution_gas_available:
1094
        raise GasUsedExceedsLimitError("execution gas used exceeds limit")
1095
1096
    if tx_gas > state_gas_available:
1097
        raise GasUsedExceedsLimitError("state gas used exceeds limit")
1098
1099
    if tx_blob_gas > blob_gas_available:
1100
        raise BlobGasLimitExceededError("blob gas limit exceeded")

EvmGasAllocation

Split of a transaction's EVM gas across the two dimensions.

1103
@final
1104
@dataclass
class EvmGasAllocation:

execution_gas

Execution gas granted to the top frame, capped by the budget.

1110
    execution_gas: ExecutionGas

state_gas_reservoir

State gas set aside for the top frame's reservoir.

1113
    state_gas_reservoir: StateGas

allocate_evm_gas

Split EVM gas into an execution-gas grant and a state reservoir.

After the intrinsic cost is removed, the remaining EVM gas is divided into execution gas -- capped by the execution-gas budget that remains below TX_MAX_GAS_LIMIT -- and a state gas reservoir that holds whatever exceeds that cap.

Only valid once validate_transaction has confirmed the transaction can afford its intrinsic cost, which guarantees the subtractions below do not underflow.

Parameters

tx_gas : The transaction's gas limit. intrinsic : The transaction's intrinsic gas cost.

Returns

allocation : EvmGasAllocation The execution gas grant and state gas reservoir.

def allocate_evm_gas(​tx_gas: Uint, ​​intrinsic: IntrinsicGasCost​) -> EvmGasAllocation:
1120
    <snip>
1145
    evm_gas = tx_gas - Uint(intrinsic.execution)
1146
    execution_gas_budget = GasCosts.TX_MAX_GAS_LIMIT - intrinsic.execution
1147
    execution_gas = ExecutionGas(min(execution_gas_budget, evm_gas))
1148
    state_gas_reservoir = StateGas(evm_gas - execution_gas)
1149
    return EvmGasAllocation(execution_gas, state_gas_reservoir)

TransactionGasSettlement

Settled gas amounts for a finished transaction.

Hold only gas figures; the caller turns them into fee payments and block-accounting updates.

1152
@final
1153
@dataclass
class TransactionGasSettlement:

gas_used

Total gas charged to the sender, after refund and floor.

1162
    gas_used: Uint

gas_left

Gas returned to the sender, priced at the effective gas price.

1165
    gas_left: Uint

execution_gas_used

Execution gas the transaction contributes to the block total.

1168
    execution_gas_used: ExecutionGas

state_gas_used

State gas the transaction contributes to the block total.

1171
    state_gas_used: StateGas

settle_transaction_gas

Settle a transaction's gas after execution.

Compute, in order:

  • the gas used before refunds, from the gas limit less the execution gas and reservoir the top frame returned;

  • the refund, capped at one fifth of that pre-refund usage;

  • the gas used, taken as the larger of the post-refund usage and the calldata floor, so a transaction never pays below the floor; and

  • the per-dimension block amounts: the state gas used (clamped to zero, since refunds can drive it negative) and the execution gas used, which carries the floor because the floor binds the execution dimension. Unlike the sender-facing gas_used, it ignores refunds: block accounting counts pre-refund gas (EIP-7778).

Parameters

tx_gas : The transaction's gas limit. calldata_floor : The transaction's calldata floor gas. gas_left : Execution gas the top frame returned. state_gas_left : State gas reservoir the top frame returned. refund_counter : The refund the top frame accrued. state_gas_used : Net state gas the top frame consumed, possibly negative.

Returns

settlement : TransactionGasSettlement The settled gas amounts.

def settle_transaction_gas(​tx_gas: Uint, ​​calldata_floor: Uint, ​​gas_left: ExecutionGas, ​​state_gas_left: StateGas, ​​refund_counter: U256, ​​state_gas_used: int​) -> TransactionGasSettlement:
1183
    <snip>
1223
    gas_used_before_refund = tx_gas - gas_left - state_gas_left
1224
    gas_refund = min(gas_used_before_refund // Uint(5), Uint(refund_counter))
1225
    gas_used_after_refund = gas_used_before_refund - gas_refund
1226
    gas_used = max(gas_used_after_refund, calldata_floor)
1227
1228
    settled_state_gas_used = StateGas(Uint(max(0, state_gas_used)))
1229
    execution_gas_used = ExecutionGas(
1230
        max(
1231
            gas_used_before_refund - settled_state_gas_used,
1232
            calldata_floor,
1233
        )
1234
    )
1235
    return TransactionGasSettlement(
1236
        gas_used=gas_used,
1237
        gas_left=tx_gas - gas_used,
1238
        execution_gas_used=execution_gas_used,
1239
        state_gas_used=settled_state_gas_used,
1240
    )