2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-11 19:25:37 +01:00
|
|
|
//! Cost schedule and other parameterisations for the EVM.
|
|
|
|
|
2015-12-20 21:45:43 +01:00
|
|
|
/// Definition of the cost schedule and other parameterisations for the EVM.
|
2016-01-11 16:28:30 +01:00
|
|
|
pub struct Schedule {
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Does it support exceptional failed code deposit
|
2015-12-20 21:45:43 +01:00
|
|
|
pub exceptional_failed_code_deposit: bool,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Does it have a delegate cal
|
2015-12-20 21:45:43 +01:00
|
|
|
pub have_delegate_call: bool,
|
2017-04-19 14:30:00 +02:00
|
|
|
/// Does it have a CREATE_P2SH instruction
|
2017-05-05 16:00:40 +02:00
|
|
|
pub have_create2: bool,
|
2017-05-23 15:49:17 +02:00
|
|
|
/// Does it have a REVERT instruction
|
|
|
|
pub have_revert: bool,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// VM stack limit
|
2015-12-20 21:45:43 +01:00
|
|
|
pub stack_limit: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Max number of nested calls/creates
|
2016-01-14 01:40:55 +01:00
|
|
|
pub max_depth: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas prices for instructions in all tiers
|
2015-12-20 21:45:43 +01:00
|
|
|
pub tier_step_gas: [usize; 8],
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `EXP` opcode
|
2015-12-20 21:45:43 +01:00
|
|
|
pub exp_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas for `EXP` opcode for each byte of exponent
|
2015-12-20 21:45:43 +01:00
|
|
|
pub exp_byte_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `SHA3` opcode
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sha3_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas for `SHA3` opcode for each word of hashed memory
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sha3_word_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for loading from storage
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sload_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for setting new value to storage (`storage==0`, `new!=0`)
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sstore_set_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for altering value in storage
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sstore_reset_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas refund for `SSTORE` clearing (when `storage!=0`, `new==0`)
|
2015-12-20 21:45:43 +01:00
|
|
|
pub sstore_refund_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `JUMPDEST` opcode
|
2015-12-20 21:45:43 +01:00
|
|
|
pub jumpdest_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `LOG*`
|
2015-12-20 21:45:43 +01:00
|
|
|
pub log_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas for data in `LOG*`
|
2015-12-20 21:45:43 +01:00
|
|
|
pub log_data_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas for each topic in `LOG*`
|
2015-12-20 21:45:43 +01:00
|
|
|
pub log_topic_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `CREATE` opcode
|
2015-12-20 21:45:43 +01:00
|
|
|
pub create_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for `*CALL*` opcodes
|
2015-12-20 21:45:43 +01:00
|
|
|
pub call_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Stipend for transfer for `CALL|CALLCODE` opcode when `value>0`
|
2015-12-20 21:45:43 +01:00
|
|
|
pub call_stipend: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas required for value transfer (`CALL|CALLCODE`)
|
2015-12-20 21:45:43 +01:00
|
|
|
pub call_value_transfer_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional gas for creating new account (`CALL|CALLCODE`)
|
2015-12-20 21:45:43 +01:00
|
|
|
pub call_new_account_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Refund for SUICIDE
|
2015-12-20 21:45:43 +01:00
|
|
|
pub suicide_refund_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas for used memory
|
2015-12-20 21:45:43 +01:00
|
|
|
pub memory_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Coefficient used to convert memory size to gas price for memory
|
2015-12-20 21:45:43 +01:00
|
|
|
pub quad_coeff_div: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Cost for contract length when executing `CREATE`
|
2015-12-20 21:45:43 +01:00
|
|
|
pub create_data_gas: usize,
|
2016-11-11 11:56:58 +01:00
|
|
|
/// Maximum code size when creating a contract.
|
|
|
|
pub create_data_limit: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Transaction cost
|
2015-12-20 21:45:43 +01:00
|
|
|
pub tx_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// `CREATE` transaction cost
|
2015-12-20 21:45:43 +01:00
|
|
|
pub tx_create_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Additional cost for empty data transaction
|
2015-12-20 21:45:43 +01:00
|
|
|
pub tx_data_zero_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Aditional cost for non-empty data transaction
|
2015-12-20 21:45:43 +01:00
|
|
|
pub tx_data_non_zero_gas: usize,
|
2016-01-26 10:48:33 +01:00
|
|
|
/// Gas price for copying memory
|
2015-12-20 21:45:43 +01:00
|
|
|
pub copy_gas: usize,
|
2016-10-15 14:39:15 +02:00
|
|
|
/// Price of EXTCODESIZE
|
|
|
|
pub extcodesize_gas: usize,
|
|
|
|
/// Base price of EXTCODECOPY
|
|
|
|
pub extcodecopy_base_gas: usize,
|
|
|
|
/// Price of BALANCE
|
|
|
|
pub balance_gas: usize,
|
|
|
|
/// Price of SUICIDE
|
|
|
|
pub suicide_gas: usize,
|
|
|
|
/// Amount of additional gas to pay when SUICIDE credits a non-existant account
|
|
|
|
pub suicide_to_new_account_cost: usize,
|
|
|
|
/// If Some(x): let limit = GAS * (x - 1) / x; let CALL's gas = min(requested, limit). let CREATE's gas = limit.
|
|
|
|
/// If None: let CALL's gas = (requested > GAS ? [OOG] : GAS). let CREATE's gas = GAS
|
|
|
|
pub sub_gas_cap_divisor: Option<usize>,
|
2016-11-03 22:22:25 +01:00
|
|
|
/// Don't ever make empty accounts; contracts start with nonce=1. Also, don't charge 25k when sending/suicide zero-value.
|
|
|
|
pub no_empty: bool,
|
|
|
|
/// Kill empty accounts if touched.
|
|
|
|
pub kill_empty: bool,
|
2017-05-30 11:52:33 +02:00
|
|
|
/// Blockhash instruction gas cost.
|
|
|
|
pub blockhash_gas: usize,
|
2017-06-19 11:41:46 +02:00
|
|
|
/// Static Call opcode enabled.
|
|
|
|
pub have_static_call: bool,
|
2017-07-19 14:34:59 +02:00
|
|
|
/// RETURNDATA and RETURNDATASIZE opcodes enabled.
|
|
|
|
pub have_return_data: bool,
|
2017-06-28 09:10:57 +02:00
|
|
|
/// Kill basic accounts below this balance if touched.
|
|
|
|
pub kill_dust: CleanDustMode,
|
2017-09-15 21:07:54 +02:00
|
|
|
/// Enable EIP-86 rules
|
|
|
|
pub eip86: bool,
|
2017-10-09 13:12:58 +02:00
|
|
|
/// Wasm extra schedule settings
|
|
|
|
pub wasm: WasmCosts,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wasm cost table
|
|
|
|
pub struct WasmCosts {
|
|
|
|
/// Arena allocator cost, per byte
|
|
|
|
pub alloc: u32,
|
|
|
|
/// Div operations multiplier.
|
|
|
|
pub div: u32,
|
|
|
|
/// Div operations multiplier.
|
|
|
|
pub mul: u32,
|
|
|
|
/// Memory (load/store) operations multiplier.
|
|
|
|
pub mem: u32,
|
|
|
|
/// Memory copy operation.
|
|
|
|
pub mem_copy: u32,
|
|
|
|
/// Static region charge, per byte.
|
|
|
|
pub static_region: u32,
|
|
|
|
/// General static query of u64 value from env-info
|
|
|
|
pub static_u64: u32,
|
|
|
|
/// General static query of U256 value from env-info
|
|
|
|
pub static_u256: u32,
|
|
|
|
/// General static query of Address value from env-info
|
|
|
|
pub static_address: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WasmCosts {
|
|
|
|
fn default() -> Self {
|
|
|
|
WasmCosts {
|
|
|
|
alloc: 2,
|
|
|
|
div: 16,
|
|
|
|
mul: 4,
|
|
|
|
mem: 2,
|
|
|
|
mem_copy: 1,
|
|
|
|
static_region: 1,
|
|
|
|
|
|
|
|
// due to runtime issues, this can be slow
|
|
|
|
static_u64: 32,
|
|
|
|
|
|
|
|
static_u256: 64,
|
|
|
|
static_address: 40,
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 09:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dust accounts cleanup mode.
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub enum CleanDustMode {
|
|
|
|
/// Dust cleanup is disabled.
|
|
|
|
Off,
|
|
|
|
/// Basic dust accounts will be removed.
|
|
|
|
BasicOnly,
|
|
|
|
/// Basic and contract dust accounts will be removed.
|
|
|
|
WithCodeAndStorage,
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 16:28:30 +01:00
|
|
|
impl Schedule {
|
2015-12-20 21:45:43 +01:00
|
|
|
/// Schedule for the Frontier-era of the Ethereum main net.
|
2016-01-11 16:28:30 +01:00
|
|
|
pub fn new_frontier() -> Schedule {
|
2015-12-20 21:45:43 +01:00
|
|
|
Self::new(false, false, 21000)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Schedule for the Homestead-era of the Ethereum main net.
|
2016-01-11 16:28:30 +01:00
|
|
|
pub fn new_homestead() -> Schedule {
|
2015-12-20 21:45:43 +01:00
|
|
|
Self::new(true, true, 53000)
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:22:25 +01:00
|
|
|
/// Schedule for the post-EIP-150-era of the Ethereum main net.
|
2017-06-19 11:41:46 +02:00
|
|
|
pub fn new_post_eip150(max_code_size: usize, fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule {
|
2016-11-03 22:22:25 +01:00
|
|
|
Schedule {
|
2016-10-15 14:39:15 +02:00
|
|
|
exceptional_failed_code_deposit: true,
|
|
|
|
have_delegate_call: true,
|
2017-06-19 11:41:46 +02:00
|
|
|
have_create2: false,
|
|
|
|
have_revert: false,
|
2017-07-19 14:34:59 +02:00
|
|
|
have_return_data: false,
|
2016-10-15 14:39:15 +02:00
|
|
|
stack_limit: 1024,
|
|
|
|
max_depth: 1024,
|
|
|
|
tier_step_gas: [0, 2, 3, 5, 8, 10, 20, 0],
|
|
|
|
exp_gas: 10,
|
2016-11-03 22:22:25 +01:00
|
|
|
exp_byte_gas: if fix_exp {50} else {10},
|
2016-10-15 14:39:15 +02:00
|
|
|
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,
|
|
|
|
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,
|
2016-11-11 11:56:58 +01:00
|
|
|
create_data_limit: max_code_size,
|
2016-10-15 14:39:15 +02:00
|
|
|
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,
|
|
|
|
balance_gas: 400,
|
|
|
|
suicide_gas: 5000,
|
|
|
|
suicide_to_new_account_cost: 25000,
|
|
|
|
sub_gas_cap_divisor: Some(64),
|
2016-11-03 22:22:25 +01:00
|
|
|
no_empty: no_empty,
|
|
|
|
kill_empty: kill_empty,
|
2017-06-19 11:41:46 +02:00
|
|
|
blockhash_gas: 20,
|
|
|
|
have_static_call: false,
|
2017-06-28 09:10:57 +02:00
|
|
|
kill_dust: CleanDustMode::Off,
|
2017-09-15 21:07:54 +02:00
|
|
|
eip86: false,
|
2017-10-09 13:12:58 +02:00
|
|
|
wasm: Default::default(),
|
2017-06-19 11:41:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
/// Schedule for the Byzantium fork of the Ethereum main net.
|
|
|
|
pub fn new_byzantium() -> Schedule {
|
2017-06-19 11:41:46 +02:00
|
|
|
let mut schedule = Self::new_post_eip150(24576, true, true, true);
|
|
|
|
schedule.have_create2 = true;
|
|
|
|
schedule.have_revert = true;
|
|
|
|
schedule.have_static_call = true;
|
2017-07-19 14:34:59 +02:00
|
|
|
schedule.have_return_data = true;
|
2017-06-19 11:41:46 +02:00
|
|
|
schedule
|
2017-04-19 14:30:00 +02:00
|
|
|
}
|
|
|
|
|
2016-01-11 16:28:30 +01:00
|
|
|
fn new(efcd: bool, hdc: bool, tcg: usize) -> Schedule {
|
2016-11-03 22:22:25 +01:00
|
|
|
Schedule {
|
2015-12-20 21:45:43 +01:00
|
|
|
exceptional_failed_code_deposit: efcd,
|
|
|
|
have_delegate_call: hdc,
|
2017-05-05 16:00:40 +02:00
|
|
|
have_create2: false,
|
2017-05-23 15:49:17 +02:00
|
|
|
have_revert: false,
|
2017-07-19 14:34:59 +02:00
|
|
|
have_return_data: false,
|
2015-12-20 21:45:43 +01:00
|
|
|
stack_limit: 1024,
|
2016-01-14 01:40:55 +01:00
|
|
|
max_depth: 1024,
|
2016-01-13 23:53:00 +01:00
|
|
|
tier_step_gas: [0, 2, 3, 5, 8, 10, 20, 0],
|
2015-12-20 21:45:43 +01:00
|
|
|
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,
|
|
|
|
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,
|
2016-11-11 11:56:58 +01:00
|
|
|
create_data_limit: usize::max_value(),
|
2015-12-20 21:45:43 +01:00
|
|
|
tx_gas: 21000,
|
|
|
|
tx_create_gas: tcg,
|
|
|
|
tx_data_zero_gas: 4,
|
|
|
|
tx_data_non_zero_gas: 68,
|
2016-07-30 15:38:44 +02:00
|
|
|
copy_gas: 3,
|
2016-10-15 14:39:15 +02:00
|
|
|
extcodesize_gas: 20,
|
|
|
|
extcodecopy_base_gas: 20,
|
|
|
|
balance_gas: 20,
|
|
|
|
suicide_gas: 0,
|
|
|
|
suicide_to_new_account_cost: 0,
|
|
|
|
sub_gas_cap_divisor: None,
|
2016-11-03 22:22:25 +01:00
|
|
|
no_empty: false,
|
|
|
|
kill_empty: false,
|
2017-05-30 11:52:33 +02:00
|
|
|
blockhash_gas: 20,
|
2017-06-19 11:41:46 +02:00
|
|
|
have_static_call: false,
|
2017-06-28 09:10:57 +02:00
|
|
|
kill_dust: CleanDustMode::Off,
|
2017-09-15 21:07:54 +02:00
|
|
|
eip86: false,
|
2017-10-09 13:12:58 +02:00
|
|
|
wasm: Default::default(),
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 19:25:37 +01:00
|
|
|
}
|
2016-07-30 15:38:44 +02:00
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
impl Default for Schedule {
|
|
|
|
fn default() -> Self {
|
|
|
|
Schedule::new_frontier()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 15:38:44 +02:00
|
|
|
#[test]
|
|
|
|
#[cfg(test)]
|
|
|
|
fn schedule_evm_assumptions() {
|
|
|
|
let s1 = Schedule::new_frontier();
|
|
|
|
let s2 = Schedule::new_homestead();
|
|
|
|
|
|
|
|
// To optimize division we assume 2**9 for quad_coeff_div
|
|
|
|
assert_eq!(s1.quad_coeff_div, 512);
|
|
|
|
assert_eq!(s2.quad_coeff_div, 512);
|
|
|
|
}
|
2017-08-01 12:37:57 +02:00
|
|
|
|