1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
pub const EIP2929_COLD_SLOAD_COST: usize = 2100;
pub const EIP2929_COLD_ACCOUNT_ACCESS_COST: usize = 2600;
pub const EIP2929_WARM_STORAGE_READ_COST: usize = 100;
pub const EIP2929_SSTORE_RESET_GAS: usize = 5000 - EIP2929_COLD_SLOAD_COST;
pub const EIP2930_ACCESS_LIST_STORAGE_KEY_COST: usize = 1900;
pub const EIP2930_ACCESS_LIST_ADDRESS_COST: usize = 2400;
pub const MAX_REFUND_QUOTIENT: usize = 2;
pub const EIP3529_MAX_REFUND_QUOTIENT: usize = 5;
pub const EIP3529_SSTORE_CLEARS_SCHEDULE: usize =
EIP2929_SSTORE_RESET_GAS + EIP2930_ACCESS_LIST_STORAGE_KEY_COST;
#[derive(Debug)]
pub struct Schedule {
pub exceptional_failed_code_deposit: bool,
pub have_delegate_call: bool,
pub have_create2: bool,
pub have_revert: bool,
pub have_extcodehash: bool,
pub stack_limit: usize,
pub max_depth: usize,
pub tier_step_gas: [usize; 8],
pub exp_gas: usize,
pub exp_byte_gas: usize,
pub sha3_gas: usize,
pub sha3_word_gas: usize,
pub sload_gas: usize,
pub sstore_set_gas: usize,
pub sstore_reset_gas: usize,
pub sstore_refund_gas: usize,
pub jumpdest_gas: usize,
pub log_gas: usize,
pub log_data_gas: usize,
pub log_topic_gas: usize,
pub create_gas: usize,
pub call_gas: usize,
pub cold_sload_cost: usize,
pub cold_account_access_cost: usize,
pub warm_storage_read_cost: usize,
pub call_stipend: usize,
pub call_value_transfer_gas: usize,
pub call_new_account_gas: usize,
pub suicide_refund_gas: usize,
pub memory_gas: usize,
pub quad_coeff_div: usize,
pub create_data_gas: usize,
pub create_data_limit: usize,
pub tx_gas: usize,
pub tx_create_gas: usize,
pub tx_data_zero_gas: usize,
pub tx_data_non_zero_gas: usize,
pub copy_gas: usize,
pub extcodesize_gas: usize,
pub extcodecopy_base_gas: usize,
pub balance_gas: usize,
pub extcodehash_gas: usize,
pub suicide_gas: usize,
pub suicide_to_new_account_cost: usize,
pub sub_gas_cap_divisor: Option<usize>,
pub no_empty: bool,
pub kill_empty: bool,
pub blockhash_gas: usize,
pub have_static_call: bool,
pub have_return_data: bool,
pub have_bitwise_shifting: bool,
pub have_chain_id: bool,
pub have_selfbalance: bool,
pub have_subs: bool,
pub kill_dust: CleanDustMode,
pub eip1283: bool,
pub eip1706: bool,
pub keep_unsigned_nonce: bool,
pub wasm: Option<WasmCosts>,
pub eip2929: bool,
pub eip2930: bool,
pub eip1559: bool,
pub eip1559_elasticity_multiplier: usize,
pub eip1559_gas_limit_bump: usize,
pub eip3198: bool,
pub max_refund_quotient: usize,
pub eip3541: bool,
}
#[derive(Debug)]
pub struct WasmCosts {
pub regular: u32,
pub div: u32,
pub mul: u32,
pub mem: u32,
pub static_u256: u32,
pub static_address: u32,
pub initial_mem: u32,
pub grow_mem: u32,
pub memcpy: u32,
pub max_stack_height: u32,
pub opcodes_mul: u32,
pub opcodes_div: u32,
pub have_create2: bool,
pub have_gasleft: bool,
}
impl Default for WasmCosts {
fn default() -> Self {
WasmCosts {
regular: 1,
div: 16,
mul: 4,
mem: 2,
static_u256: 64,
static_address: 40,
initial_mem: 4096,
grow_mem: 8192,
memcpy: 1,
max_stack_height: 64 * 1024,
opcodes_mul: 3,
opcodes_div: 8,
have_create2: false,
have_gasleft: false,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum CleanDustMode {
Off,
BasicOnly,
WithCodeAndStorage,
}
impl Schedule {
pub fn new_frontier() -> Schedule {
Self::new(false, false, 21000)
}
pub fn new_homestead() -> Schedule {
Self::new(true, true, 53000)
}
pub fn new_post_eip150(
max_code_size: usize,
fix_exp: bool,
no_empty: bool,
kill_empty: bool,
) -> Schedule {
Schedule {
exceptional_failed_code_deposit: true,
have_delegate_call: true,
have_create2: false,
have_revert: false,
have_return_data: false,
have_bitwise_shifting: false,
have_chain_id: false,
have_selfbalance: false,
have_subs: false,
have_extcodehash: false,
stack_limit: 1024,
max_depth: 1024,
tier_step_gas: [0, 2, 3, 5, 8, 10, 20, 0],
exp_gas: 10,
exp_byte_gas: if fix_exp { 50 } else { 10 },
sha3_gas: 30,
sha3_word_gas: 6,
sload_gas: 200,
sstore_set_gas: 20000,
sstore_reset_gas: 5000,
sstore_refund_gas: 15000,
jumpdest_gas: 1,
log_gas: 375,
log_data_gas: 8,
log_topic_gas: 375,
create_gas: 32000,
call_gas: 700,
cold_account_access_cost: 0,
cold_sload_cost: 0,
warm_storage_read_cost: 0,
call_stipend: 2300,
call_value_transfer_gas: 9000,
call_new_account_gas: 25000,
suicide_refund_gas: 24000,
memory_gas: 3,
quad_coeff_div: 512,
create_data_gas: 200,
create_data_limit: max_code_size,
tx_gas: 21000,
tx_create_gas: 53000,
tx_data_zero_gas: 4,
tx_data_non_zero_gas: 68,
copy_gas: 3,
extcodesize_gas: 700,
extcodecopy_base_gas: 700,
extcodehash_gas: 400,
balance_gas: 400,
suicide_gas: 5000,
suicide_to_new_account_cost: 25000,
sub_gas_cap_divisor: Some(64),
no_empty: no_empty,
kill_empty: kill_empty,
blockhash_gas: 20,
have_static_call: false,
kill_dust: CleanDustMode::Off,
eip1283: false,
eip1706: false,
keep_unsigned_nonce: false,
wasm: None,
eip2929: false,
eip2930: false,
eip1559: false,
eip1559_elasticity_multiplier: 1,
eip1559_gas_limit_bump: 1,
eip3198: false,
max_refund_quotient: MAX_REFUND_QUOTIENT,
eip3541: false,
}
}
pub fn new_byzantium() -> Schedule {
let mut schedule = Self::new_post_eip150(24576, true, true, true);
schedule.have_create2 = true;
schedule.have_revert = true;
schedule.have_static_call = true;
schedule.have_return_data = true;
schedule
}
pub fn new_constantinople() -> Schedule {
let mut schedule = Self::new_byzantium();
schedule.have_bitwise_shifting = true;
schedule.have_extcodehash = true;
schedule
}
pub fn new_istanbul() -> Schedule {
let mut schedule = Self::new_constantinople();
schedule.have_chain_id = true;
schedule.tx_data_non_zero_gas = 16;
schedule.sload_gas = 800;
schedule.balance_gas = 700;
schedule.extcodehash_gas = 700;
schedule.have_selfbalance = true;
schedule
}
pub fn new_berlin() -> Schedule {
let mut schedule = Self::new_istanbul();
schedule.have_subs = true;
schedule.eip1283 = true;
schedule.eip2929 = true;
schedule.eip2930 = true;
schedule.cold_sload_cost = EIP2929_COLD_SLOAD_COST;
schedule.cold_account_access_cost = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.warm_storage_read_cost = EIP2929_WARM_STORAGE_READ_COST;
schedule.sload_gas = EIP2929_WARM_STORAGE_READ_COST;
schedule.call_gas = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.balance_gas = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.extcodecopy_base_gas = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.extcodehash_gas = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.extcodesize_gas = EIP2929_COLD_ACCOUNT_ACCESS_COST;
schedule.sstore_reset_gas = EIP2929_SSTORE_RESET_GAS;
schedule
}
pub fn new_london() -> Schedule {
let mut schedule = Self::new_berlin();
schedule.eip1559 = true;
schedule.eip1559_elasticity_multiplier = 2;
schedule.eip3198 = true;
schedule.suicide_refund_gas = 0;
schedule.sstore_refund_gas = EIP3529_SSTORE_CLEARS_SCHEDULE;
schedule.max_refund_quotient = EIP3529_MAX_REFUND_QUOTIENT;
schedule.eip3541 = true;
schedule
}
fn new(efcd: bool, hdc: bool, tcg: usize) -> Schedule {
Schedule {
exceptional_failed_code_deposit: efcd,
have_delegate_call: hdc,
have_create2: false,
have_revert: false,
have_return_data: false,
have_bitwise_shifting: false,
have_chain_id: false,
have_selfbalance: false,
have_subs: false,
have_extcodehash: false,
stack_limit: 1024,
max_depth: 1024,
tier_step_gas: [0, 2, 3, 5, 8, 10, 20, 0],
exp_gas: 10,
exp_byte_gas: 10,
sha3_gas: 30,
sha3_word_gas: 6,
sload_gas: 50,
sstore_set_gas: 20000,
sstore_reset_gas: 5000,
sstore_refund_gas: 15000,
jumpdest_gas: 1,
log_gas: 375,
log_data_gas: 8,
log_topic_gas: 375,
create_gas: 32000,
call_gas: 40,
cold_account_access_cost: 0,
cold_sload_cost: 0,
warm_storage_read_cost: 0,
call_stipend: 2300,
call_value_transfer_gas: 9000,
call_new_account_gas: 25000,
suicide_refund_gas: 24000,
memory_gas: 3,
quad_coeff_div: 512,
create_data_gas: 200,
create_data_limit: usize::max_value(),
tx_gas: 21000,
tx_create_gas: tcg,
tx_data_zero_gas: 4,
tx_data_non_zero_gas: 68,
copy_gas: 3,
extcodesize_gas: 20,
extcodecopy_base_gas: 20,
extcodehash_gas: 400,
balance_gas: 20,
suicide_gas: 0,
suicide_to_new_account_cost: 0,
sub_gas_cap_divisor: None,
no_empty: false,
kill_empty: false,
blockhash_gas: 20,
have_static_call: false,
kill_dust: CleanDustMode::Off,
eip1283: false,
eip1706: false,
keep_unsigned_nonce: false,
wasm: None,
eip2929: false,
eip2930: false,
eip1559: false,
eip1559_elasticity_multiplier: 1,
eip1559_gas_limit_bump: 1,
eip3198: false,
max_refund_quotient: MAX_REFUND_QUOTIENT,
eip3541: false,
}
}
pub fn wasm(&self) -> &WasmCosts {
self.wasm.as_ref().expect(
"Wasm schedule expected to exist while checking wasm contract. Misconfigured client?",
)
}
}
impl Default for Schedule {
fn default() -> Self {
Schedule::new_frontier()
}
}
#[test]
#[cfg(test)]
fn schedule_evm_assumptions() {
let s1 = Schedule::new_frontier();
let s2 = Schedule::new_homestead();
assert_eq!(s1.quad_coeff_div, 512);
assert_eq!(s2.quad_coeff_div, 512);
}