Skip to content

Commit

Permalink
Specify compute costs directly instead of weights
Browse files Browse the repository at this point in the history
  • Loading branch information
aborg-dev committed Feb 14, 2023
1 parent 98dd860 commit 2b67fe6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
18 changes: 9 additions & 9 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExtCosts, WeightedGas>,
pub costs: EnumMap<ExtCosts, ParameterCost>,
}

// We multiply the actual computed costs by the fixed factor to ensure we
Expand All @@ -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
Expand Down Expand Up @@ -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 }
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/res/runtime_configs/parameters.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion core/primitives/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Self, Self::Error> {
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::<WeightedGas>(),
std::any::type_name::<ParameterCost>(),
value.clone(),
)),
}
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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#"
Expand Down Expand Up @@ -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 },
}
"#;

Expand Down Expand Up @@ -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 }",
),
],
);
Expand Down Expand Up @@ -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 }",
),
],
);
Expand All @@ -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 }",
),
],
);
Expand All @@ -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 }",
),
],
);
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2822,7 +2822,7 @@ impl From<ExtCostsConfigView> 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 }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,7 +96,7 @@ fn ext_costs_config(cost_table: &CostTable) -> anyhow::Result<ExtCostsConfig> {
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 }),
})
}

Expand Down

0 comments on commit 2b67fe6

Please sign in to comment.