Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update fees config #3653

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ pub const UPGRADABILITY_FIX_PROTOCOL_VERSION: ProtocolVersion = 37;
/// Updates the way receipt ID, data ID and random seeds are constructed.
pub const CREATE_HASH_PROTOCOL_VERSION: ProtocolVersion = 38;

pub const SHARD_CHUNK_HEADER_UPGRADE_VERSION: ProtocolVersion = 41;

/// Fix the storage usage of the delete key action.
pub const DELETE_KEY_STORAGE_USAGE_PROTOCOL_VERSION: ProtocolVersion = 40;

pub const SHARD_CHUNK_HEADER_UPGRADE_VERSION: ProtocolVersion = 41;

/// Upgrading RuntimeConfig Fees.
/// See [NEP 120](https://github.com/nearprotocol/NEPs/pull/120)
pub const RUNTIME_CONFIG_UPGRADE_FEES_PROTOCOL_VERSION: ProtocolVersion = 42;

pub struct ProtocolVersionRange {
lower: ProtocolVersion,
upper: Option<ProtocolVersion>,
Expand Down Expand Up @@ -84,11 +88,11 @@ pub enum ProtocolFeature {

/// Current latest stable version of the protocol.
#[cfg(not(feature = "nightly_protocol"))]
pub const PROTOCOL_VERSION: ProtocolVersion = 41;
pub const PROTOCOL_VERSION: ProtocolVersion = 42;

/// Current latest nightly version of the protocol.
#[cfg(feature = "nightly_protocol")]
pub const PROTOCOL_VERSION: ProtocolVersion = 43;
pub const PROTOCOL_VERSION: ProtocolVersion = 44;

lazy_static! {
static ref STABLE_PROTOCOL_FEATURES_TO_VERSION_MAPPING: HashMap<ProtocolFeature, ProtocolVersion> = vec![
Expand All @@ -113,9 +117,9 @@ lazy_static! {
ProtocolVersion,
> = vec![
#[cfg(feature = "protocol_feature_forward_chunk_parts")]
(ProtocolFeature::ForwardChunkParts, 42),
(ProtocolFeature::ForwardChunkParts, 43),
#[cfg(feature = "protocol_feature_rectify_inflation")]
(ProtocolFeature::RectifyInflation, 43),
(ProtocolFeature::RectifyInflation, 44),
]
.into_iter()
.collect();
Expand Down
1 change: 1 addition & 0 deletions core/runtime-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
serde = { version = "1", features = ["derive"] }
lazy_static = "1.4"

near-primitives = { path = "../primitives" }
near-runtime-fees = { path = "../../runtime/near-runtime-fees" }
Expand Down
186 changes: 180 additions & 6 deletions core/runtime-configs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use serde::{Deserialize, Serialize};
use near_primitives::account::Account;
use near_primitives::serialize::u128_dec_format;
use near_primitives::types::{AccountId, Balance};
use near_primitives::version::ProtocolVersion;
use near_runtime_fees::RuntimeFeesConfig;
use near_vm_logic::VMConfig;
use std::sync::Arc;
use near_primitives::version::{ProtocolVersion, RUNTIME_CONFIG_UPGRADE_FEES_PROTOCOL_VERSION};
use near_runtime_fees::{
AccessKeyCreationConfig, ActionCreationConfig, DataReceiptCreationConfig, Fee,
RuntimeFeesConfig,
};
use near_vm_logic::{ExtCostsConfig, VMConfig, VMLimitConfig};
use std::ops::DerefMut;
use std::sync::{Arc, Mutex};

/// The structure that holds the parameters of the runtime, mostly economics.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -38,6 +42,10 @@ impl Default for RuntimeConfig {
}
}

lazy_static::lazy_static! {
static ref FEE_UPGRADE_CONFIG: Mutex<Option<Arc<RuntimeConfig>>> = Mutex::new(None);
}

impl RuntimeConfig {
pub fn free() -> Self {
Self {
Expand All @@ -53,9 +61,175 @@ impl RuntimeConfig {
/// TODO: https://github.com/nearprotocol/NEPs/issues/120
pub fn from_protocol_version(
genesis_runtime_config: &Arc<RuntimeConfig>,
_protocol_version: ProtocolVersion,
genesis_protocol_version: ProtocolVersion,
protocol_version: ProtocolVersion,
) -> Arc<Self> {
genesis_runtime_config.clone()
if genesis_protocol_version < RUNTIME_CONFIG_UPGRADE_FEES_PROTOCOL_VERSION
&& protocol_version >= RUNTIME_CONFIG_UPGRADE_FEES_PROTOCOL_VERSION
{
let mut fee_config = FEE_UPGRADE_CONFIG.lock().unwrap();
if let Some(fee_config) = fee_config.deref_mut() {
fee_config.clone()
} else {
let upgraded_config = Arc::new(genesis_runtime_config.upgrade_fees());
*fee_config = Some(upgraded_config.clone());
upgraded_config
}
} else {
genesis_runtime_config.clone()
}
}

fn upgrade_fees(&self) -> Self {
let SAFETY_MULTIPLIER = 3;
Self {
storage_amount_per_byte: self.storage_amount_per_byte,
transaction_costs: RuntimeFeesConfig {
action_receipt_creation_config: Fee {
send_sir: 108059500000,
send_not_sir: 108059500000,
execution: 108059500000,
},
data_receipt_creation_config: DataReceiptCreationConfig {
base_cost: Fee {
send_sir: 4697339419375,
send_not_sir: 4697339419375,
execution: 4697339419375,
},
cost_per_byte: Fee {
send_sir: 59357464,
send_not_sir: 59357464,
execution: 59357464,
},
},
action_creation_config: ActionCreationConfig {
create_account_cost: Fee {
send_sir: 99607375000,
send_not_sir: 99607375000,
execution: 99607375000,
},
deploy_contract_cost: Fee {
send_sir: 184765750000,
send_not_sir: 184765750000,
execution: 184765750000,
},
deploy_contract_cost_per_byte: Fee {
send_sir: 6812999,
send_not_sir: 6812999,
execution: 6812999,
},
function_call_cost: Fee {
send_sir: 2319861500000,
send_not_sir: 2319861500000,
execution: 2319861500000,
},
function_call_cost_per_byte: Fee {
send_sir: 2235934,
send_not_sir: 2235934,
execution: 2235934,
},
transfer_cost: Fee {
send_sir: 115123062500,
send_not_sir: 115123062500,
execution: 115123062500,
},
stake_cost: Fee {
send_sir: 141715687500,
send_not_sir: 141715687500,
execution: 102217625000,
},
add_key_cost: AccessKeyCreationConfig {
full_access_cost: Fee {
send_sir: 101765125000,
send_not_sir: 101765125000,
execution: 101765125000,
},
function_call_cost: Fee {
send_sir: 102217625000,
send_not_sir: 102217625000,
execution: 102217625000,
},
function_call_cost_per_byte: Fee {
send_sir: 1925331,
send_not_sir: 1925331,
execution: 1925331,
},
},
delete_key_cost: Fee {
send_sir: 94946625000,
send_not_sir: 94946625000,
execution: 94946625000,
},
delete_account_cost: Fee {
send_sir: 147489000000,
send_not_sir: 147489000000,
execution: 147489000000,
},
},
storage_usage_config: self.transaction_costs.storage_usage_config.clone(),
burnt_gas_reward: self.transaction_costs.burnt_gas_reward,
pessimistic_gas_price_inflation_ratio: self
.transaction_costs
.pessimistic_gas_price_inflation_ratio,
},
wasm_config: VMConfig {
ext_costs: ExtCostsConfig {
base: SAFETY_MULTIPLIER * 88256037,
contract_compile_base: SAFETY_MULTIPLIER * 11815321,
contract_compile_bytes: SAFETY_MULTIPLIER * 72250,
read_memory_base: SAFETY_MULTIPLIER * 869954400,
read_memory_byte: SAFETY_MULTIPLIER * 1267111,
write_memory_base: SAFETY_MULTIPLIER * 934598287,
write_memory_byte: SAFETY_MULTIPLIER * 907924,
read_register_base: SAFETY_MULTIPLIER * 839055062,
read_register_byte: SAFETY_MULTIPLIER * 32854,
write_register_base: SAFETY_MULTIPLIER * 955174162,
write_register_byte: SAFETY_MULTIPLIER * 1267188,
utf8_decoding_base: SAFETY_MULTIPLIER * 1037259687,
utf8_decoding_byte: SAFETY_MULTIPLIER * 97193493,
utf16_decoding_base: SAFETY_MULTIPLIER * 1181104350,
utf16_decoding_byte: SAFETY_MULTIPLIER * 54525831,
sha256_base: SAFETY_MULTIPLIER * 1513656750,
sha256_byte: SAFETY_MULTIPLIER * 8039117,
keccak256_base: SAFETY_MULTIPLIER * 1959830425,
keccak256_byte: SAFETY_MULTIPLIER * 7157035,
keccak512_base: SAFETY_MULTIPLIER * 1937129412,
keccak512_byte: SAFETY_MULTIPLIER * 12216567,
log_base: SAFETY_MULTIPLIER * 1181104350,
log_byte: SAFETY_MULTIPLIER * 4399597,
storage_write_base: SAFETY_MULTIPLIER * 21398912000,
storage_write_key_byte: SAFETY_MULTIPLIER * 23494289,
storage_write_value_byte: SAFETY_MULTIPLIER * 10339513,
storage_write_evicted_byte: SAFETY_MULTIPLIER * 10705769,
storage_read_base: SAFETY_MULTIPLIER * 18785615250,
storage_read_key_byte: SAFETY_MULTIPLIER * 10317511,
storage_read_value_byte: SAFETY_MULTIPLIER * 1870335,
storage_remove_base: SAFETY_MULTIPLIER * 17824343500,
storage_remove_key_byte: SAFETY_MULTIPLIER * 12740128,
storage_remove_ret_value_byte: SAFETY_MULTIPLIER * 3843852,
storage_has_key_base: SAFETY_MULTIPLIER * 18013298875,
storage_has_key_byte: SAFETY_MULTIPLIER * 10263615,
storage_iter_create_prefix_base: SAFETY_MULTIPLIER * 0,
storage_iter_create_prefix_byte: SAFETY_MULTIPLIER * 0,
storage_iter_create_range_base: SAFETY_MULTIPLIER * 0,
storage_iter_create_from_byte: SAFETY_MULTIPLIER * 0,
storage_iter_create_to_byte: SAFETY_MULTIPLIER * 0,
storage_iter_next_base: SAFETY_MULTIPLIER * 0,
storage_iter_next_key_byte: SAFETY_MULTIPLIER * 0,
storage_iter_next_value_byte: SAFETY_MULTIPLIER * 0,
touching_trie_node: SAFETY_MULTIPLIER * 5367318642,
promise_and_base: SAFETY_MULTIPLIER * 488337800,
promise_and_per_promise: SAFETY_MULTIPLIER * 1817392,
promise_return: SAFETY_MULTIPLIER * 186717462,
validator_stake_base: SAFETY_MULTIPLIER * 303944908800,
validator_total_stake_base: SAFETY_MULTIPLIER * 303944908800,
},
grow_mem_cost: 1,
regular_op_cost: 3856371,
limit_config: self.wasm_config.limit_config.clone(),
},
account_creation_config: self.account_creation_config.clone(),
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions neard/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ impl NightshadeRuntime {
current_protocol_version,
config: RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
self.genesis_config.protocol_version,
current_protocol_version,
),
cache: Some(Arc::new(StoreCompiledContractCache { store: self.store.clone() })),
Expand Down Expand Up @@ -581,6 +582,7 @@ impl RuntimeAdapter for NightshadeRuntime {
) -> Result<Option<InvalidTxError>, Error> {
let runtime_config = RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
self.genesis_config.protocol_version,
current_protocol_version,
);

Expand Down Expand Up @@ -649,6 +651,7 @@ impl RuntimeAdapter for NightshadeRuntime {

let runtime_config = RuntimeConfig::from_protocol_version(
&self.genesis_runtime_config,
self.genesis_config.protocol_version,
current_protocol_version,
);

Expand Down