Skip to content

Commit

Permalink
refactor: Store parameters as YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Kashin committed Jan 6, 2023
1 parent a56420c commit 68ce635
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
32 changes: 26 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ near-o11y = { path = "../o11y" }
near-primitives-core = { path = "../primitives-core"}
near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" }
near-vm-errors = { path = "../../runtime/near-vm-errors" }
serde_yaml = "0.9.16"

[features]
sandbox = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pessimistic_gas_price_inflation_denominator: 100

# Account creation config
min_allowed_top_level_account_length: 32
registrar_account_id: registrar
registrar_account_id: "registrar"

# Storage usage config
storage_amount_per_byte: 100_000_000_000_000_000_000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pessimistic_gas_price_inflation_denominator: 100

# Account creation config
min_allowed_top_level_account_length: 0
registrar_account_id: registrar
registrar_account_id: "registrar"

# Storage usage config
storage_amount_per_byte: 100_000_000_000_000_000_000
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! include_config {

/// The base config file with all initial parameter values defined.
/// Later version are calculated by applying diffs to this base.
static BASE_CONFIG: &str = include_config!("parameters.txt");
static BASE_CONFIG: &str = include_config!("parameters.yaml");

/// Stores pairs of protocol versions for which runtime config was updated and
/// the file containing the diffs in bytes.
Expand All @@ -31,7 +31,7 @@ static CONFIG_DIFFS: &[(ProtocolVersion, &str)] = &[
];

/// Testnet parameters for versions <= 29, which (incorrectly) differed from mainnet parameters
pub static INITIAL_TESTNET_CONFIG: &str = include_config!("parameters_testnet.txt");
pub static INITIAL_TESTNET_CONFIG: &str = include_config!("parameters_testnet.yaml");

/// Stores runtime config for each protocol version where it was updated.
#[derive(Debug)]
Expand Down
37 changes: 30 additions & 7 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use near_primitives_core::parameter::{FeeParameter, Parameter};
use near_primitives_core::runtime::fees::{RuntimeFeesConfig, StorageUsageConfig};
use num_rational::Rational;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_json::json;
use std::any::Any;
use std::collections::BTreeMap;
Expand All @@ -29,6 +30,8 @@ pub(crate) enum InvalidConfigError {
NoSeparator(usize, String),
#[error("intermediate JSON created by parser does not match `RuntimeConfig`")]
WrongStructure(#[source] serde_json::Error),
#[error("could not parse YAML that defines the structure of the config")]
InvalidYaml(#[source] serde_yaml::Error),
#[error("config diff expected to contain old value `{1}` for parameter `{0}`")]
OldValueExists(Parameter, String),
#[error(
Expand All @@ -43,15 +46,35 @@ pub(crate) enum InvalidConfigError {
WrongValueType(#[source] serde_json::Error, Parameter, &'static str, String),
}

/// Represents YAML values supported by parameter config.
#[derive(Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum ParameterValue {
Number(u64),
String(String),
}

impl std::str::FromStr for ParameterTable {
type Err = InvalidConfigError;
fn from_str(arg: &str) -> Result<ParameterTable, InvalidConfigError> {
let parameters = txt_to_key_values(arg)
.map(|result| {
let (typed_key, value) = result?;
Ok((typed_key, parse_parameter_txt_value(value.trim())?))
let yaml_map: BTreeMap<String, ParameterValue> =
serde_yaml::from_str(arg).map_err(|err| InvalidConfigError::InvalidYaml(err))?;

let parameters = yaml_map
.iter()
.map(|(key, value)| {
let typed_key: Parameter = key
.trim()
.parse()
.map_err(|err| InvalidConfigError::UnknownParameter(err, key.to_owned()))?;
let json_value = match value {
ParameterValue::Number(n) => json!(n),
ParameterValue::String(s) => parse_parameter_txt_value(s)?,
};
Ok((typed_key, json_value))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;

Ok(ParameterTable { parameters })
}
}
Expand Down Expand Up @@ -352,7 +375,7 @@ min_allowed_top_level_account_length: 32
# Comment line with trailing whitespace #
storage_amount_per_byte: 100000000000000000000
storage_amount_per_byte: "100000000000000000000"
storage_num_bytes_account: 100
storage_num_extra_bytes_record : 40
Expand Down Expand Up @@ -487,7 +510,7 @@ max_memory_pages: 512
fn test_parameter_table_no_key() {
assert_matches!(
check_invalid_parameter_table(": 100", &[]),
InvalidConfigError::UnknownParameter(_, _)
InvalidConfigError::InvalidYaml(_)
);
}

Expand All @@ -503,7 +526,7 @@ max_memory_pages: 512
fn test_parameter_table_wrong_separator() {
assert_matches!(
check_invalid_parameter_table("wasm_regular_op_cost=100", &[]),
InvalidConfigError::NoSeparator(1, _)
InvalidConfigError::InvalidYaml(_)
);
}

Expand Down

0 comments on commit 68ce635

Please sign in to comment.