cfx_vm_types/spec.rs
1// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2// This file is part of Parity.
3
4// Parity is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16
17// Copyright 2019 Conflux Foundation. All rights reserved.
18// Conflux is free software and distributed under GNU General Public License.
19// See http://www.gnu.org/licenses/
20
21//! Cost spec and other parameterisations for the EVM.
22use cfx_types::{address_util::AddressUtil, Address};
23use primitives::{block::BlockHeight, BlockNumber};
24
25/// Definition of the cost spec and other parameterisations for the VM.
26#[derive(Debug, Clone)]
27pub struct Spec {
28 /// VM stack limit
29 pub stack_limit: usize,
30 /// Max number of nested calls/creates
31 pub max_depth: usize,
32 /// Gas prices for instructions in all tiers
33 pub tier_step_gas: [usize; 8],
34 /// Gas price for `EXP` opcode
35 pub exp_gas: usize,
36 /// Additional gas for `EXP` opcode for each byte of exponent
37 pub exp_byte_gas: usize,
38 /// Gas price for `SHA3` opcode
39 pub sha3_gas: usize,
40 /// Additional gas for `SHA3` opcode for each word of hashed memory
41 pub sha3_word_gas: usize,
42 /// Gas price for loading from storage. Code sload gas after CIP-645f:
43 /// EIP-2929
44 pub cold_sload_gas: usize,
45 /// Gas price for setting new value to storage (`storage==0`, `new!=0`)
46 pub sstore_set_gas: usize,
47 /// Gas price for altering value in storage
48 pub sstore_reset_gas: usize,
49 /// Gas refund for `SSTORE` clearing (when `storage!=0`, `new==0`)
50 pub sstore_refund_gas: usize,
51 /// Gas price for `JUMPDEST` opcode
52 pub jumpdest_gas: usize,
53 /// Gas price for `LOG*`
54 pub log_gas: usize,
55 /// Additional gas for data in `LOG*`
56 pub log_data_gas: usize,
57 /// Additional gas for each topic in `LOG*`
58 pub log_topic_gas: usize,
59 /// Gas price for `CREATE` opcode
60 pub create_gas: usize,
61 /// Gas price for `*CALL*` opcodes
62 pub call_gas: usize,
63 /// Stipend for transfer for `CALL|CALLCODE` opcode when `value>0`
64 pub call_stipend: usize,
65 /// Additional gas required for value transfer (`CALL|CALLCODE`)
66 pub call_value_transfer_gas: usize,
67 /// Additional gas for creating new account (`CALL|CALLCODE`)
68 pub call_new_account_gas: usize,
69 /// Refund for SUICIDE
70 pub suicide_refund_gas: usize,
71 /// Gas for used memory
72 pub memory_gas: usize,
73 /// Coefficient used to convert memory size to gas price for memory
74 pub quad_coeff_div: usize,
75 /// Cost for contract length when executing `CREATE`
76 pub create_data_gas: usize,
77 /// Maximum code size when creating a contract.
78 pub create_data_limit: usize,
79 /// Maximum init code size (CIP-645i: EIP-3860)
80 pub init_code_data_limit: usize,
81 /// Init code word size (CIP-645i: EIP-3860)
82 pub init_code_word_gas: usize,
83 /// Transaction cost
84 pub tx_gas: usize,
85 /// `CREATE` transaction cost
86 pub tx_create_gas: usize,
87 /// Additional cost for empty data transaction
88 pub tx_data_zero_gas: usize,
89 /// Aditional cost for non-empty data transaction
90 pub tx_data_non_zero_gas: usize,
91 /// Floor gas cost from empty data transaction (EIP-7623)
92 pub tx_data_floor_zero_gas: usize,
93 /// Floor gas cost from non-empty data transaction (EIP-7623)
94 pub tx_data_floor_non_zero_gas: usize,
95 /// Gas price for copying memory
96 pub copy_gas: usize,
97 /// Price of EXTCODESIZE
98 pub extcodesize_gas: usize,
99 /// Base price of EXTCODECOPY
100 pub extcodecopy_base_gas: usize,
101 /// Price of BALANCE
102 pub balance_gas: usize,
103 /// Price of EXTCODEHASH
104 pub extcodehash_gas: usize,
105 /// Price of SUICIDE
106 pub suicide_gas: usize,
107 /// Price for retiring PoS node.
108 pub retire_gas: usize,
109 /// Price for deploying Eip-1820 contract.
110 pub eip1820_gas: usize,
111 pub access_list_storage_key_gas: usize,
112 pub access_list_address_gas: usize,
113 pub cold_account_access_cost: usize,
114 pub warm_access_gas: usize,
115 /// Amount of additional gas to pay when SUICIDE credits a non-existant
116 /// account
117 pub suicide_to_new_account_cost: usize,
118 /// If Some(x):
119 /// let limit = GAS * (x - 1) / x;
120 /// let CALL's gas = min(requested, limit);
121 /// let CREATE's gas = limit;
122 /// If None:
123 /// let CALL's gas = (requested > GAS ? \[OOG\] : GAS);
124 /// let CREATE's gas = GAS;
125 pub sub_gas_cap_divisor: Option<usize>,
126 /// Blockhash instruction gas cost.
127 pub blockhash_gas: usize,
128 /// The magnification of gas storage occupying related operaions.
129 pub evm_gas_ratio: usize,
130 /// `PER_AUTH_BASE_COST` in CIP-7702
131 pub per_auth_base_cost: usize,
132 /// `PER_EMPTY_ACCOUNT_COST` in CIP-7702
133 pub per_empty_account_cost: usize,
134 /// CIP-43: Introduce Finality via Voting Among Staked
135 pub cip43_init: bool,
136 pub cip43_contract: bool,
137 /// CIP-62: Enable EC-related builtin contract
138 pub cip62: bool,
139 /// CIP-64: Get current epoch number through internal contract
140 pub cip64: bool,
141 /// CIP-71: Disable anti-reentrancy
142 pub cip71: bool,
143 /// CIP-78: Correct `is_sponsored` fields in receipt
144 pub cip78a: bool,
145 /// CIP-78: Correct `is_sponsored` fields in receipt
146 pub cip78b: bool,
147 /// CIP-90: A Space that Fully EVM Compatible
148 pub cip90: bool,
149 /// CIP-94: On-chain Parameter DAO Vote
150 pub cip94: bool,
151 pub cip94_activation_block_number: u64,
152 pub params_dao_vote_period: u64,
153 /// CIP-97: Remove staking list
154 pub cip97: bool,
155 /// CIP-98: Fix espace bug
156 pub cip98: bool,
157 /// CIP-105: Minimal DAO votes requirement based on PoS votes.
158 pub cip105: bool,
159 pub cip_sigma_fix: bool,
160 /// CIP-107: Reduce storage collateral refund.
161 pub cip107: bool,
162 /// CIP-118: Query Unused Storage Points in Internal Contract
163 pub cip118: bool,
164 /// CIP-119: PUSH0 instruction
165 pub cip119: bool,
166 /// CIP-131: Retain Whitelist on Contract Deletion
167 pub cip131: bool,
168 /// CIP-132: Fix Static Context Check for Internal Contracts
169 pub cip132: bool,
170 /// CIP-133: Enhanced Block Hash Query
171 pub cip133_b: BlockNumber,
172 pub cip133_e: BlockHeight,
173 pub cip133_core: bool,
174 /// CIP-137: Base Fee Sharing in CIP-1559
175 pub cip137: bool,
176 /// CIP-1559: Fee Market Change for Conflux
177 pub cip1559: bool,
178 /// CIP-141: Disable Subroutine Opcodes
179 /// CIP-142: Transient Storage Opcodes
180 /// CIP-143: MCOPY (0x5e) Opcode for Efficient Memory Copy
181 pub cancun_opcodes: bool,
182 /// CIP-144: Point Evaluation Precompile from EIP-4844
183 pub cip144: bool,
184 /// CIP-145: Fix Receipts upon `NotEnoughBalance` Error
185 pub cip145: bool,
186 pub cip145_fix: bool,
187 /// CIP-150: Reject New Contract Code Starting with the 0xEF byte
188 pub cip150: bool,
189 /// CIP-151: SELFDESTRUCT only in Same Transaction
190 pub cip151: bool,
191 /// CIP-152: Reject Transactions from Senders with Deployed Code
192 pub cip152: bool,
193 /// CIP-154: Fix Inconsistent Implementation of TLOAD
194 pub cip154: bool,
195 /// CIP-7702: Set Code for EOA
196 pub cip7702: bool,
197 /// CIP-645: Align Conflux Gas Pricing with EVM
198 pub cip645: CIP645Spec,
199 /// EIP-2935: Serve historical block hashes from state
200 pub eip2935: bool,
201 /// EIP-7623: Increase calldata cost
202 pub eip7623: bool,
203 pub align_evm: bool,
204 pub cip_c2_fix: bool,
205 /// EIP-7939: Count Leading Zeros Instruction
206 pub eip7939: bool,
207}
208
209/// Represents the feature flags for CIP-645 implementation.
210///
211/// While the protocol treats these features as a single atomic upgrade,
212/// separating them into named fields is merely to make the code more
213/// maintainable and self-documenting.
214///
215/// IMPORTANT NOTE:
216/// All fields must be consistently set to either `true` (enabled) or `false`
217/// (disabled). Mixed states will lead to undefined behavior as these features
218/// were designed to be activated as a coordinated bundle in CIP-645.
219#[derive(Debug, Clone, Copy)]
220pub struct CIP645Spec {
221 /// EIP-1108: Reduces gas costs for alt_bn128 precompile
222 pub eip1108: bool,
223
224 /// EIP-1884: Reprices trie-size-dependent opcodes
225 pub eip1884: bool,
226
227 /// EIP-2028: Reduces Calldata gas cost
228 pub eip2028: bool,
229
230 /// EIP-2200: Rebalances net-metered SSTORE gas cost
231 /// EIP-3529: Removes gas refunds for SELFDESTRUCT and reduces SSTORE
232 /// refunds
233 pub eip_sstore_and_refund_gas: bool,
234
235 /// EIP-2565: Reduces gas cost for modular exponentiation transactions
236 pub eip2565: bool,
237
238 /// EIP-2929: Increases gas costs for opcode transactions to mitigate DDoS
239 /// EIP-3651: Reduces gas fees for accessing COINBASE address
240 pub eip_cold_warm_access: bool,
241
242 /// EIP-3860: Limits initcode size to 49152
243 pub eip3860: bool,
244
245 /// EIP-684: Revert creation in case of collision
246 pub fix_eip684: bool,
247
248 /// EIP-1559: EIP-1559: Fee market change for ETH 1.0 chain
249 pub fix_eip1559: bool,
250
251 /// EIP-5656: MCOPY - Memory copying instruction
252 pub fix_eip5656: bool,
253
254 /// EIP-1153: Transient storage opcodes
255 pub fix_eip1153: bool,
256
257 pub blockhash_gas: bool,
258
259 pub opcode_update: bool,
260
261 pub fix_extcodehash: bool,
262}
263
264impl CIP645Spec {
265 pub const fn new(enabled: bool) -> Self {
266 Self {
267 eip1108: enabled,
268 eip1884: enabled,
269 eip2028: enabled,
270 eip_sstore_and_refund_gas: enabled,
271 eip2565: enabled,
272 eip_cold_warm_access: enabled,
273 eip3860: enabled,
274 fix_eip684: enabled,
275 fix_eip1153: enabled,
276 fix_eip1559: enabled,
277 fix_eip5656: enabled,
278 blockhash_gas: enabled,
279 opcode_update: enabled,
280 fix_extcodehash: enabled,
281 }
282 }
283}
284
285/// Spec parameters are determined solely by block height and thus accessible to
286/// the consensus protocol.
287#[derive(Debug, Clone)]
288pub struct ConsensusGasSpec {
289 /// EIP-7623: Increase calldata cost
290 pub eip7623: bool,
291 /// CIP-1559: Fee Market Change for Conflux
292 pub cip1559: bool,
293 /// CIP-645(GAS)
294 pub cip645: CIP645Spec,
295 /// Transaction cost
296 pub tx_gas: usize,
297 /// `CREATE` transaction cost
298 pub tx_create_gas: usize,
299 /// Additional cost for empty data transaction
300 pub tx_data_zero_gas: usize,
301 /// Aditional cost for non-empty data transaction
302 pub tx_data_non_zero_gas: usize,
303 /// Floor gas cost from empty data transaction (EIP-7623)
304 pub tx_data_floor_zero_gas: usize,
305 /// Floor gas cost from non-empty data transaction (EIP-7623)
306 pub tx_data_floor_non_zero_gas: usize,
307 /// Maximum init code size (CIP-645i: EIP-3860)
308 pub init_code_data_limit: usize,
309 /// Init code word size (CIP-645i: EIP-3860)
310 pub init_code_word_gas: usize,
311 pub access_list_storage_key_gas: usize,
312 pub access_list_address_gas: usize,
313 /// `PER_AUTH_BASE_COST` in CIP-7702
314 pub per_auth_base_cost: usize,
315 /// `PER_EMPTY_ACCOUNT_COST` in CIP-7702
316 pub per_empty_account_cost: usize,
317 /// The magnification of gas storage occupying related operaions.
318 pub evm_gas_ratio: usize,
319 pub align_evm: bool,
320}
321
322impl Spec {
323 /// The spec when Conflux launches the mainnet. It should never changed
324 /// since the mainnet has launched.
325 pub const fn genesis_spec() -> Spec {
326 Spec {
327 stack_limit: 1024,
328 max_depth: 1024,
329 tier_step_gas: [0, 2, 3, 5, 8, 10, 20, 0],
330 exp_gas: 10,
331 exp_byte_gas: 50,
332 sha3_gas: 30,
333 sha3_word_gas: 6,
334 // Become 800 after CIP-142
335 cold_sload_gas: 200,
336 sstore_set_gas: 20000,
337 sstore_reset_gas: 5000,
338 sstore_refund_gas: 15000,
339 jumpdest_gas: 1,
340 log_gas: 375,
341 log_data_gas: 8,
342 log_topic_gas: 375,
343 create_gas: 32000,
344 call_gas: 700,
345 call_stipend: 2300,
346 call_value_transfer_gas: 9000,
347 call_new_account_gas: 25000,
348 suicide_refund_gas: 24000,
349 memory_gas: 3,
350 quad_coeff_div: 512,
351 create_data_gas: 200,
352 create_data_limit: 49152,
353 init_code_data_limit: 49152,
354 init_code_word_gas: 2,
355 tx_gas: 21000,
356 tx_create_gas: 53000,
357 tx_data_zero_gas: 4,
358 tx_data_non_zero_gas: 68,
359 tx_data_floor_zero_gas: 10,
360 tx_data_floor_non_zero_gas: 40,
361 copy_gas: 3,
362 extcodesize_gas: 700,
363 extcodecopy_base_gas: 700,
364 extcodehash_gas: 400,
365 balance_gas: 400,
366 suicide_gas: 5000,
367 retire_gas: 5_000_000,
368 eip1820_gas: 1_500_000,
369 access_list_storage_key_gas: 1900,
370 access_list_address_gas: 2400,
371 cold_account_access_cost: 2600,
372 warm_access_gas: 100,
373 suicide_to_new_account_cost: 25000,
374 per_auth_base_cost: 17000,
375 per_empty_account_cost: 25000,
376 sub_gas_cap_divisor: Some(64),
377 blockhash_gas: 20,
378 cip43_init: false,
379 cip43_contract: false,
380 cip62: false,
381 cip64: false,
382 cip71: false,
383 cip90: false,
384 cip78a: false,
385 cip78b: false,
386 cip94: false,
387 evm_gas_ratio: 2,
388 cip94_activation_block_number: u64::MAX,
389 params_dao_vote_period: 0,
390 cip97: false,
391 cip98: false,
392 cip105: false,
393 cip_sigma_fix: false,
394 cip107: false,
395 cip118: false,
396 cip119: false,
397 cip131: false,
398 cip132: false,
399 cip133_b: u64::MAX,
400 cip133_e: u64::MAX,
401 cip133_core: false,
402 cip137: false,
403 cip145: false,
404 cip145_fix: false,
405 cip1559: false,
406 cancun_opcodes: false,
407 cip144: false,
408 cip150: false,
409 cip151: false,
410 cip152: false,
411 cip154: false,
412 cip645: CIP645Spec::new(false),
413 cip7702: false,
414 eip2935: false,
415 eip7623: false,
416 cip_c2_fix: false,
417 align_evm: false,
418 eip7939: false,
419 }
420 }
421
422 // `cold_sload_gas` replaces `sload_gas` in certain contexts, primarily for
423 // core space internal contracts. However, some `sload_gas` usages retain
424 // their original semantics. This function is introduced to distinguish
425 // these cases.
426 pub fn sload_gas(&self) -> usize {
427 assert!(!self.cip645.eip_cold_warm_access);
428 self.cold_sload_gas
429 }
430
431 pub fn overwrite_gas_plan_by_cip(&mut self) {
432 if self.cancun_opcodes {
433 self.cold_sload_gas = 800;
434 }
435 if self.cip645.eip1884 {
436 self.balance_gas = 700;
437 self.extcodehash_gas = 700;
438 }
439
440 if self.cip645.eip2028 {
441 self.tx_data_non_zero_gas = 16;
442 }
443
444 if self.cip645.eip_cold_warm_access {
445 self.cold_sload_gas = 2100;
446 self.sstore_reset_gas = 2900;
447 }
448
449 if self.align_evm {
450 self.per_auth_base_cost = 12500;
451 self.create_data_limit = 24576;
452 self.evm_gas_ratio = 1;
453 }
454
455 // Don't forget also update GenesisGasSpec::overwrite_gas_plan_by_cip
456 }
457
458 #[cfg(any(test, feature = "testonly_code"))]
459 pub fn new_spec_for_test() -> Spec { Self::genesis_spec() }
460
461 pub fn is_valid_address(&self, address: &Address) -> bool {
462 address.is_genesis_valid_address()
463 }
464
465 #[inline]
466 pub const fn to_consensus_spec(&self) -> ConsensusGasSpec {
467 ConsensusGasSpec {
468 cip1559: self.cip1559,
469 cip645: self.cip645,
470 eip7623: self.eip7623,
471 tx_gas: self.tx_gas,
472 tx_create_gas: self.tx_create_gas,
473 tx_data_zero_gas: self.tx_data_zero_gas,
474 tx_data_non_zero_gas: self.tx_data_non_zero_gas,
475 init_code_data_limit: self.init_code_data_limit,
476 init_code_word_gas: self.init_code_word_gas,
477 access_list_storage_key_gas: self.access_list_storage_key_gas,
478 access_list_address_gas: self.access_list_address_gas,
479 per_auth_base_cost: self.per_auth_base_cost,
480 per_empty_account_cost: self.per_empty_account_cost,
481 align_evm: self.align_evm,
482 evm_gas_ratio: self.evm_gas_ratio,
483 tx_data_floor_zero_gas: self.tx_data_floor_zero_gas,
484 tx_data_floor_non_zero_gas: self.tx_data_floor_non_zero_gas,
485 }
486 }
487}
488
489impl ConsensusGasSpec {
490 pub const fn genesis_spec() -> Self {
491 Spec::genesis_spec().to_consensus_spec()
492 }
493
494 pub fn overwrite_gas_plan_by_cip(&mut self) {
495 if self.cip645.eip2028 {
496 self.tx_data_non_zero_gas = 16;
497 }
498
499 if self.align_evm {
500 self.per_auth_base_cost = 12500;
501 self.evm_gas_ratio = 1;
502 }
503 }
504}
505
506#[cfg(any(test, feature = "testonly_code"))]
507impl Default for Spec {
508 fn default() -> Self { Spec::new_spec_for_test() }
509}