From 2b67fe6d34a89271fdc4ec46ab6c3a8ea1912e98 Mon Sep 17 00:00:00 2001 From: Andrei Kashin Date: Tue, 14 Feb 2023 09:21:33 +0000 Subject: [PATCH] Specify compute costs directly instead of weights --- core/primitives-core/src/config.rs | 18 +++++------ .../res/runtime_configs/parameters.snap | 2 +- .../res/runtime_configs/parameters.yaml | 3 +- .../primitives/src/runtime/parameter_table.rs | 32 +++++++++---------- core/primitives/src/views.rs | 4 +-- .../src/costs_to_runtime_config.rs | 4 +-- 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/core/primitives-core/src/config.rs b/core/primitives-core/src/config.rs index 9dd0d9ee948..77c1cf714cc 100644 --- a/core/primitives-core/src/config.rs +++ b/core/primitives-core/src/config.rs @@ -261,14 +261,14 @@ pub struct ViewConfig { } #[derive(Debug, Clone, Hash, PartialEq, Eq)] -pub struct WeightedGas { - pub base: Gas, - pub weight: Rational32, +pub struct ParameterCost { + pub gas: Gas, + pub compute: u64, } #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct ExtCostsConfig { - pub costs: EnumMap, + pub costs: EnumMap, } // We multiply the actual computed costs by the fixed factor to ensure we @@ -277,11 +277,11 @@ const SAFETY_MULTIPLIER: u64 = 3; impl ExtCostsConfig { pub fn cost(&self, param: ExtCosts) -> Gas { - self.costs[param].base + self.costs[param].gas } - pub fn weight(&self, param: ExtCosts) -> Rational32 { - self.costs[param].weight + pub fn compute_cost(&self, param: ExtCosts) -> u64 { + self.costs[param].compute } /// Convenience constructor to use in tests where the exact gas cost does @@ -352,14 +352,14 @@ impl ExtCostsConfig { ExtCosts::alt_bn128_g1_sum_base => 3_000_000_000, ExtCosts::alt_bn128_g1_sum_element => 5_000_000_000, } - .map(|_, value| WeightedGas { base: value, weight: 1.into() }); + .map(|_, value| ParameterCost { gas: value, compute: value }); ExtCostsConfig { costs } } fn free() -> ExtCostsConfig { ExtCostsConfig { costs: enum_map! { - _ => WeightedGas { base: 0, weight: 1.into() } + _ => ParameterCost { gas: 0, compute: 0 } }, } } diff --git a/core/primitives/res/runtime_configs/parameters.snap b/core/primitives/res/runtime_configs/parameters.snap index 95a51d295e4..382a6ab23d6 100644 --- a/core/primitives/res/runtime_configs/parameters.snap +++ b/core/primitives/res/runtime_configs/parameters.snap @@ -123,7 +123,7 @@ wasm_storage_iter_create_to_byte 0 wasm_storage_iter_next_base 0 wasm_storage_iter_next_key_byte 0 wasm_storage_iter_next_value_byte 0 -wasm_touching_trie_node 16_101_955_926, weight: 2 / 3 +wasm_touching_trie_node 16_101_955_926, compute: 30_000_000_000 wasm_read_cached_trie_node 2_280_000_000 wasm_promise_and_base 1_465_013_400 wasm_promise_and_per_promise 5_452_176 diff --git a/core/primitives/res/runtime_configs/parameters.yaml b/core/primitives/res/runtime_configs/parameters.yaml index 1060cbe4bc1..f7ac645d326 100644 --- a/core/primitives/res/runtime_configs/parameters.yaml +++ b/core/primitives/res/runtime_configs/parameters.yaml @@ -158,7 +158,8 @@ wasm_storage_iter_create_to_byte: 0 wasm_storage_iter_next_base: 0 wasm_storage_iter_next_key_byte: 0 wasm_storage_iter_next_value_byte: 0 -wasm_touching_trie_node: { base: 16_101_955_926, weight: { numerator: 2, denominator: 3 } } +# TODO(akashin): Remove this before committing. +wasm_touching_trie_node: { gas: 16_101_955_926, compute: 30_000_000_000 } wasm_read_cached_trie_node: 2_280_000_000 wasm_promise_and_base: 1_465_013_400 wasm_promise_and_per_promise: 5_452_176 diff --git a/core/primitives/src/runtime/parameter_table.rs b/core/primitives/src/runtime/parameter_table.rs index bbcdec22024..9cc628754aa 100644 --- a/core/primitives/src/runtime/parameter_table.rs +++ b/core/primitives/src/runtime/parameter_table.rs @@ -1,6 +1,6 @@ use super::config::{AccountCreationConfig, RuntimeConfig}; use near_primitives_core::account::id::ParseAccountError; -use near_primitives_core::config::{ExtCostsConfig, VMConfig, WeightedGas}; +use near_primitives_core::config::{ExtCostsConfig, ParameterCost, VMConfig}; use near_primitives_core::parameter::{FeeParameter, Parameter}; use near_primitives_core::runtime::fees::{Fee, RuntimeFeesConfig, StorageUsageConfig}; use near_primitives_core::types::AccountId; @@ -31,7 +31,7 @@ impl core::fmt::Display for Rational { pub(crate) enum ParameterValue { U64(u64), Rational(Rational), - WeightedGas { base: u64, weight: Rational }, + ParameterCost { gas: u64, compute: u64 }, Fee { send_sir: u64, send_not_sir: u64, execution: u64 }, // Can be used to store either a string or u128. Ideally, we would use a dedicated enum member // for u128, but this is currently impossible to express in YAML (see @@ -106,17 +106,17 @@ impl TryFrom<&ParameterValue> for Rational32 { } } -impl TryFrom<&ParameterValue> for WeightedGas { +impl TryFrom<&ParameterValue> for ParameterCost { type Error = ValueConversionError; fn try_from(value: &ParameterValue) -> Result { match value { - ParameterValue::WeightedGas { base, weight } => { - Ok(WeightedGas { base: *base, weight: weight.into() }) + ParameterValue::ParameterCost { gas, compute } => { + Ok(ParameterCost { gas: *gas, compute: *compute }) } - &ParameterValue::U64(v) => Ok(WeightedGas { base: v, weight: 1.into() }), + &ParameterValue::U64(v) => Ok(ParameterCost { gas: v, compute: v }), _ => Err(ValueConversionError::ParseType( - std::any::type_name::(), + std::any::type_name::(), value.clone(), )), } @@ -174,8 +174,8 @@ impl core::fmt::Display for ParameterValue { match self { ParameterValue::U64(v) => write!(f, "{:>20}", format_number(*v)), ParameterValue::Rational(r) => write!(f, "{r}"), - ParameterValue::WeightedGas { base, weight } => { - write!(f, "{:>20}, weight: {weight}", format_number(*base)) + ParameterValue::ParameterCost { gas, compute } => { + write!(f, "{:>20}, compute: {:>20}", format_number(*gas), format_number(*compute)) } ParameterValue::Fee { send_sir, send_not_sir, execution } => { write!( @@ -505,7 +505,7 @@ burnt_gas_reward: { numerator: 1_000_000, denominator: 300, } -wasm_storage_read_base: { base: 50_000_000_000, weight: { numerator: 15, denominator: 10 } } +wasm_storage_read_base: { gas: 50_000_000_000, compute: 100_000_000_000 } "#; static BASE_1: &str = r#" @@ -533,8 +533,8 @@ burnt_gas_reward: { new: { numerator: 2_000_000, denominator: 500 }, } wasm_storage_read_base: { - old: { base: 50_000_000_000, weight: { numerator: 15, denominator: 10 } }, - new: { base: 50_000_000_000, weight: { numerator: 45, denominator: 17 } }, + old: { gas: 50_000_000_000, compute: 100_000_000_000 }, + new: { gas: 50_000_000_000, compute: 200_000_000_000 }, } "#; @@ -575,7 +575,7 @@ burnt_gas_reward: { (Parameter::BurntGasReward, "{ numerator: 1_000_000, denominator: 300 }"), ( Parameter::WasmStorageReadBase, - "{ base: 50_000_000_000, weight: { numerator: 15, denominator: 10 } }", + "{ gas: 50_000_000_000, compute: 100_000_000_000 }", ), ], ); @@ -613,7 +613,7 @@ burnt_gas_reward: { (Parameter::BurntGasReward, "{ numerator: 2_000_000, denominator: 500 }"), ( Parameter::WasmStorageReadBase, - "{ base: 50_000_000_000, weight: { numerator: 45, denominator: 17 } }", + "{ gas: 50_000_000_000, compute: 200_000_000_000 }", ), ], ); @@ -636,7 +636,7 @@ burnt_gas_reward: { (Parameter::BurntGasReward, "{ numerator: 3_000_000, denominator: 800 }"), ( Parameter::WasmStorageReadBase, - "{ base: 50_000_000_000, weight: { numerator: 45, denominator: 17 } }", + "{ gas: 50_000_000_000, compute: 200_000_000_000 }", ), ], ); @@ -656,7 +656,7 @@ burnt_gas_reward: { (Parameter::BurntGasReward, "{ numerator: 1_000_000, denominator: 300 }"), ( Parameter::WasmStorageReadBase, - "{ base: 50_000_000_000, weight: { numerator: 15, denominator: 10 } }", + "{ gas: 50_000_000_000, compute: 100_000_000_000 }", ), ], ); diff --git a/core/primitives/src/views.rs b/core/primitives/src/views.rs index fad4345f2c0..665a1a705b0 100644 --- a/core/primitives/src/views.rs +++ b/core/primitives/src/views.rs @@ -39,7 +39,7 @@ use borsh::{BorshDeserialize, BorshSerialize}; use chrono::DateTime; use near_crypto::{PublicKey, Signature}; use near_o11y::pretty; -use near_primitives_core::config::{ActionCosts, ExtCosts, VMConfig}; +use near_primitives_core::config::{ActionCosts, ExtCosts, ParameterCost, VMConfig}; use near_primitives_core::runtime::fees::Fee; use num_rational::Rational32; use std::collections::HashMap; @@ -2822,7 +2822,7 @@ impl From for near_primitives_core::config::ExtCostsConfig { ExtCosts::alt_bn128_pairing_check_base => view.alt_bn128_pairing_check_base, ExtCosts::alt_bn128_pairing_check_element => view.alt_bn128_pairing_check_element, } - .map(|_, value| WeightedGas { base: value, weight: 1.into() }); + .map(|_, value| ParameterCost { gas: value, compute: value }); Self { costs } } } diff --git a/runtime/runtime-params-estimator/src/costs_to_runtime_config.rs b/runtime/runtime-params-estimator/src/costs_to_runtime_config.rs index 9507bfbad78..d14ee36e160 100644 --- a/runtime/runtime-params-estimator/src/costs_to_runtime_config.rs +++ b/runtime/runtime-params-estimator/src/costs_to_runtime_config.rs @@ -2,7 +2,7 @@ use near_primitives::runtime::config::AccountCreationConfig; use near_primitives::runtime::config_store::RuntimeConfigStore; use near_primitives::runtime::fees::{Fee, RuntimeFeesConfig}; use near_primitives::version::PROTOCOL_VERSION; -use near_vm_logic::{ActionCosts, ExtCosts, ExtCostsConfig, VMConfig, WeightedGas}; +use near_vm_logic::{ActionCosts, ExtCosts, ExtCostsConfig, ParameterCost, VMConfig}; use node_runtime::config::RuntimeConfig; use anyhow::Context; @@ -96,7 +96,7 @@ fn ext_costs_config(cost_table: &CostTable) -> anyhow::Result { let estimation = estimation(cost).with_context(|| format!("external WASM cost has no estimation defined: {}", cost))?; cost_table.get(estimation).with_context(|| format!("undefined external WASM cost: {}", cost))? }, - }.map(|_, value| WeightedGas { base: value, weight: 1.into() }), + }.map(|_, value| ParameterCost { gas: value, compute: value }), }) }