-
Notifications
You must be signed in to change notification settings - Fork 642
/
parameter_table.rs
564 lines (517 loc) · 19.9 KB
/
parameter_table.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use super::config::{AccountCreationConfig, RuntimeConfig};
use near_primitives_core::config::{ExtCostsConfig, VMConfig};
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_json::json;
use std::any::Any;
use std::collections::BTreeMap;
pub(crate) struct ParameterTable {
parameters: BTreeMap<Parameter, serde_json::Value>,
}
/// Changes made to parameters between versions.
pub(crate) struct ParameterTableDiff {
parameters: BTreeMap<Parameter, (serde_json::Value, serde_json::Value)>,
}
/// Error returned by ParameterTable::from_txt() that parses a runtime
/// configuration TXT file.
#[derive(thiserror::Error, Debug)]
pub(crate) enum InvalidConfigError {
#[error("could not parse `{1}` as a parameter")]
UnknownParameter(#[source] strum::ParseError, String),
#[error("could not parse `{1}` as a value")]
ValueParseError(#[source] serde_json::Error, String),
#[error("expected a `:` separator between name and value of a parameter `{1}` on line {0}")]
NoSeparator(usize, String),
#[error("intermediate JSON created by parser does not match `RuntimeConfig`")]
WrongStructure(#[source] serde_json::Error),
#[error("config diff expected to contain old value `{1}` for parameter `{0}`")]
OldValueExists(Parameter, String),
#[error(
"unexpected old value `{1}` for parameter `{0}` in config diff, previous version does not have such a value"
)]
NoOldValueExists(Parameter, String),
#[error("expected old value `{1}` but found `{2}` for parameter `{0}` in config diff")]
WrongOldValue(Parameter, String, String),
#[error("expected a value for `{0}` but found none")]
MissingParameter(Parameter),
#[error("expected a value of type `{2}` for `{1}` but could not parse it from `{3}`")]
WrongValueType(#[source] serde_json::Error, Parameter, &'static str, 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())?))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;
Ok(ParameterTable { parameters })
}
}
impl TryFrom<&ParameterTable> for RuntimeConfig {
type Error = InvalidConfigError;
fn try_from(params: &ParameterTable) -> Result<Self, Self::Error> {
Ok(RuntimeConfig {
fees: RuntimeFeesConfig {
action_fees: enum_map::enum_map! {
action_cost => params.fee(action_cost)
},
burnt_gas_reward: Rational::new(
params.get_parsed(Parameter::BurntGasRewardNumerator)?,
params.get_parsed(Parameter::BurntGasRewardDenominator)?,
),
pessimistic_gas_price_inflation_ratio: Rational::new(
params.get_parsed(Parameter::PessimisticGasPriceInflationNumerator)?,
params.get_parsed(Parameter::PessimisticGasPriceInflationDenominator)?,
),
storage_usage_config: StorageUsageConfig {
storage_amount_per_byte: params.get_u128(Parameter::StorageAmountPerByte)?,
num_bytes_account: params.get_parsed(Parameter::StorageNumBytesAccount)?,
num_extra_bytes_record: params
.get_parsed(Parameter::StorageNumExtraBytesRecord)?,
},
},
wasm_config: VMConfig {
ext_costs: ExtCostsConfig {
costs: enum_map::enum_map! {
cost => params.get_parsed(cost.param())?
},
},
grow_mem_cost: params.get_parsed(Parameter::WasmGrowMemCost)?,
regular_op_cost: params.get_parsed(Parameter::WasmRegularOpCost)?,
limit_config: serde_json::from_value(params.json_map(Parameter::vm_limits(), ""))
.map_err(InvalidConfigError::WrongStructure)?,
},
account_creation_config: AccountCreationConfig {
min_allowed_top_level_account_length: params
.get_parsed(Parameter::MinAllowedTopLevelAccountLength)?,
registrar_account_id: params.get_parsed(Parameter::RegistrarAccountId)?,
},
})
}
}
impl ParameterTable {
pub(crate) fn apply_diff(
&mut self,
diff: ParameterTableDiff,
) -> Result<(), InvalidConfigError> {
for (key, (before, after)) in diff.parameters {
if before.is_null() {
match self.parameters.get(&key) {
Some(serde_json::Value::Null) | None => {
self.parameters.insert(key, after);
}
Some(old_value) => {
return Err(InvalidConfigError::OldValueExists(key, old_value.to_string()))
}
}
} else {
match self.parameters.get(&key) {
Some(serde_json::Value::Null) | None => {
return Err(InvalidConfigError::NoOldValueExists(key, before.to_string()))
}
Some(old_value) => {
if *old_value != before {
return Err(InvalidConfigError::WrongOldValue(
key,
old_value.to_string(),
before.to_string(),
));
} else {
self.parameters.insert(key, after);
}
}
}
}
}
Ok(())
}
fn json_map(
&self,
params: impl Iterator<Item = &'static Parameter>,
remove_prefix: &'static str,
) -> serde_json::Value {
let mut json = serde_json::Map::new();
for param in params {
let mut key: &'static str = param.into();
key = key.strip_prefix(remove_prefix).unwrap_or(key);
if let Some(value) = self.get(*param) {
json.insert(key.to_owned(), value.clone());
}
}
json.into()
}
fn get(&self, key: Parameter) -> Option<&serde_json::Value> {
self.parameters.get(&key)
}
/// Access action fee by `ActionCosts`.
fn fee(
&self,
cost: near_primitives_core::config::ActionCosts,
) -> near_primitives_core::runtime::fees::Fee {
let json = self.fee_json(FeeParameter::from(cost));
serde_json::from_value::<near_primitives_core::runtime::fees::Fee>(json)
.expect("just constructed a Fee JSON")
}
/// Read and parse a parameter from the `ParameterTable`.
fn get_parsed<T: DeserializeOwned + Any>(
&self,
key: Parameter,
) -> Result<T, InvalidConfigError> {
let value = self.parameters.get(&key).ok_or(InvalidConfigError::MissingParameter(key))?;
serde_json::from_value(value.clone()).map_err(|parse_err| {
InvalidConfigError::WrongValueType(
parse_err,
key,
std::any::type_name::<T>(),
value.to_string(),
)
})
}
/// Read and parse a parameter from the `ParameterTable`.
fn get_u128(&self, key: Parameter) -> Result<u128, InvalidConfigError> {
let value = self.parameters.get(&key).ok_or(InvalidConfigError::MissingParameter(key))?;
near_primitives_core::serialize::dec_format::deserialize(value).map_err(|parse_err| {
InvalidConfigError::WrongValueType(
parse_err,
key,
std::any::type_name::<u128>(),
value.to_string(),
)
})
}
fn fee_json(&self, key: FeeParameter) -> serde_json::Value {
json!( {
"send_sir": self.get(format!("{key}_send_sir").parse().unwrap()),
"send_not_sir": self.get(format!("{key}_send_not_sir").parse().unwrap()),
"execution": self.get(format!("{key}_execution").parse().unwrap()),
})
}
}
impl std::str::FromStr for ParameterTableDiff {
type Err = InvalidConfigError;
fn from_str(arg: &str) -> Result<ParameterTableDiff, InvalidConfigError> {
let parameters = txt_to_key_values(arg)
.map(|result| {
let (typed_key, value) = result?;
if let Some((before, after)) = value.split_once("->") {
Ok((
typed_key,
(
parse_parameter_txt_value(before.trim())?,
parse_parameter_txt_value(after.trim())?,
),
))
} else {
Ok((
typed_key,
(serde_json::Value::Null, parse_parameter_txt_value(value.trim())?),
))
}
})
.collect::<Result<BTreeMap<_, _>, _>>()?;
Ok(ParameterTableDiff { parameters })
}
}
fn txt_to_key_values(
arg: &str,
) -> impl Iterator<Item = Result<(Parameter, &str), InvalidConfigError>> {
arg.lines()
.enumerate()
.filter_map(|(nr, line)| {
// ignore comments and empty lines
let trimmed = line.trim();
if trimmed.starts_with("#") || trimmed.is_empty() {
None
} else {
Some((nr, trimmed))
}
})
.map(|(nr, trimmed)| {
let (key, value) = trimmed
.split_once(":")
.ok_or(InvalidConfigError::NoSeparator(nr + 1, trimmed.to_owned()))?;
let typed_key: Parameter = key
.trim()
.parse()
.map_err(|err| InvalidConfigError::UnknownParameter(err, key.to_owned()))?;
Ok((typed_key, value))
})
}
/// Parses a value from the custom format for runtime parameter definitions.
///
/// A value can be a positive integer or a string, both written without quotes.
/// Integers can use underlines as separators (for readability).
fn parse_parameter_txt_value(value: &str) -> Result<serde_json::Value, InvalidConfigError> {
if value.is_empty() {
return Ok(serde_json::Value::Null);
}
if value.bytes().all(|c| c.is_ascii_digit() || c == '_' as u8) {
let mut raw_number = value.to_owned();
raw_number.retain(char::is_numeric);
// We do not have "arbitrary_precision" serde feature enabled, thus we
// can only store up to `u64::MAX`, which is `18446744073709551615` and
// has 20 characters.
if raw_number.len() < 20 {
Ok(serde_json::Value::Number(
raw_number
.parse()
.map_err(|err| InvalidConfigError::ValueParseError(err, value.to_owned()))?,
))
} else {
Ok(serde_json::Value::String(raw_number))
}
} else {
Ok(serde_json::Value::String(value.to_owned()))
}
}
#[cfg(test)]
mod tests {
use super::{InvalidConfigError, ParameterTable, ParameterTableDiff};
use assert_matches::assert_matches;
use near_primitives_core::parameter::Parameter;
use std::collections::BTreeMap;
#[track_caller]
fn check_parameter_table(
base_config: &str,
diffs: &[&str],
expected: impl IntoIterator<Item = (Parameter, &'static str)>,
) {
let mut params: ParameterTable = base_config.parse().unwrap();
for diff in diffs {
let diff: ParameterTableDiff = diff.parse().unwrap();
params.apply_diff(diff).unwrap();
}
let expected_map = BTreeMap::from_iter(expected.into_iter().map(|(param, value)| {
(
param,
if value.is_empty() {
serde_json::Value::Null
} else {
serde_json::from_str(value).expect("Test data has invalid JSON")
},
)
}));
assert_eq!(params.parameters, expected_map);
}
#[track_caller]
fn check_invalid_parameter_table(base_config: &str, diffs: &[&str]) -> InvalidConfigError {
let params = base_config.parse();
let result = params.and_then(|params: ParameterTable| {
diffs.iter().try_fold(params, |mut params, diff| {
params.apply_diff(diff.parse()?)?;
Ok(params)
})
});
match result {
Ok(_) => panic!("Input should have parser error"),
Err(err) => err,
}
}
static BASE_0: &str = r#"
# Comment line
registrar_account_id: registrar
min_allowed_top_level_account_length: 32
storage_amount_per_byte: 100_000_000_000_000_000_000
storage_num_bytes_account: 100
storage_num_extra_bytes_record: 40
"#;
static BASE_1: &str = r#"
registrar_account_id: registrar
# Comment line
min_allowed_top_level_account_length: 32
# Comment line with trailing whitespace #
storage_amount_per_byte: 100000000000000000000
storage_num_bytes_account: 100
storage_num_extra_bytes_record : 40
"#;
static DIFF_0: &str = r#"
# Comment line
registrar_account_id: registrar -> near
min_allowed_top_level_account_length: 32 -> 32_000
wasm_regular_op_cost: 3_856_371
"#;
static DIFF_1: &str = r#"
# Comment line
registrar_account_id: near -> registrar
storage_num_extra_bytes_record: 40 -> 77
wasm_regular_op_cost: 3_856_371 -> 0
max_memory_pages: 512
"#;
// Tests synthetic small example configurations. For tests with "real"
// input data, we already have
// `test_old_and_new_runtime_config_format_match` in `configs_store.rs`.
/// Check empty input
#[test]
fn test_empty_parameter_table() {
check_parameter_table("", &[], []);
}
/// Reading reading a normally formatted base parameter file with no diffs
#[test]
fn test_basic_parameter_table() {
check_parameter_table(
BASE_0,
&[],
[
(Parameter::RegistrarAccountId, "\"registrar\""),
(Parameter::MinAllowedTopLevelAccountLength, "32"),
(Parameter::StorageAmountPerByte, "\"100000000000000000000\""),
(Parameter::StorageNumBytesAccount, "100"),
(Parameter::StorageNumExtraBytesRecord, "40"),
],
);
}
/// Reading reading a slightly funky formatted base parameter file with no diffs
#[test]
fn test_basic_parameter_table_weird_syntax() {
check_parameter_table(
BASE_1,
&[],
[
(Parameter::RegistrarAccountId, "\"registrar\""),
(Parameter::MinAllowedTopLevelAccountLength, "32"),
(Parameter::StorageAmountPerByte, "\"100000000000000000000\""),
(Parameter::StorageNumBytesAccount, "100"),
(Parameter::StorageNumExtraBytesRecord, "40"),
],
);
}
/// Apply one diff
#[test]
fn test_parameter_table_with_diff() {
check_parameter_table(
BASE_0,
&[DIFF_0],
[
(Parameter::RegistrarAccountId, "\"near\""),
(Parameter::MinAllowedTopLevelAccountLength, "32000"),
(Parameter::StorageAmountPerByte, "\"100000000000000000000\""),
(Parameter::StorageNumBytesAccount, "100"),
(Parameter::StorageNumExtraBytesRecord, "40"),
(Parameter::WasmRegularOpCost, "3856371"),
],
);
}
/// Apply two diffs
#[test]
fn test_parameter_table_with_diffs() {
check_parameter_table(
BASE_0,
&[DIFF_0, DIFF_1],
[
(Parameter::RegistrarAccountId, "\"registrar\""),
(Parameter::MinAllowedTopLevelAccountLength, "32000"),
(Parameter::StorageAmountPerByte, "\"100000000000000000000\""),
(Parameter::StorageNumBytesAccount, "100"),
(Parameter::StorageNumExtraBytesRecord, "77"),
(Parameter::WasmRegularOpCost, "0"),
(Parameter::MaxMemoryPages, "512"),
],
);
}
#[test]
fn test_parameter_table_with_empty_value() {
let diff_with_empty_value = "min_allowed_top_level_account_length: 32 -> ";
check_parameter_table(
BASE_0,
&[diff_with_empty_value],
[
(Parameter::RegistrarAccountId, "\"registrar\""),
(Parameter::MinAllowedTopLevelAccountLength, ""),
(Parameter::StorageAmountPerByte, "\"100000000000000000000\""),
(Parameter::StorageNumBytesAccount, "100"),
(Parameter::StorageNumExtraBytesRecord, "40"),
],
);
}
#[test]
fn test_parameter_table_invalid_key() {
// Key that is not a `Parameter`
assert_matches!(
check_invalid_parameter_table("invalid_key: 100", &[]),
InvalidConfigError::UnknownParameter(_, _)
);
}
#[test]
fn test_parameter_table_invalid_key_in_diff() {
assert_matches!(
check_invalid_parameter_table("wasm_regular_op_cost: 100", &["invalid_key: 100"]),
InvalidConfigError::UnknownParameter(_, _)
);
}
#[test]
fn test_parameter_table_no_key() {
assert_matches!(
check_invalid_parameter_table(": 100", &[]),
InvalidConfigError::UnknownParameter(_, _)
);
}
#[test]
fn test_parameter_table_no_key_in_diff() {
assert_matches!(
check_invalid_parameter_table("wasm_regular_op_cost: 100", &[": 100"]),
InvalidConfigError::UnknownParameter(_, _)
);
}
#[test]
fn test_parameter_table_wrong_separator() {
assert_matches!(
check_invalid_parameter_table("wasm_regular_op_cost=100", &[]),
InvalidConfigError::NoSeparator(1, _)
);
}
#[test]
fn test_parameter_table_wrong_separator_in_diff() {
assert_matches!(
check_invalid_parameter_table(
"wasm_regular_op_cost: 100",
&["wasm_regular_op_cost=100"]
),
InvalidConfigError::NoSeparator(1, _)
);
}
#[test]
fn test_parameter_table_wrong_old_value() {
assert_matches!(
check_invalid_parameter_table(
"min_allowed_top_level_account_length: 3_200_000_000",
&["min_allowed_top_level_account_length: 3_200_000 -> 1_600_000"]
),
InvalidConfigError::WrongOldValue(
Parameter::MinAllowedTopLevelAccountLength,
expected,
found
) => {
assert_eq!(expected, "3200000000");
assert_eq!(found, "3200000");
}
);
}
#[test]
fn test_parameter_table_no_old_value() {
assert_matches!(
check_invalid_parameter_table(
"min_allowed_top_level_account_length: 3_200_000_000",
&["min_allowed_top_level_account_length: 1_600_000"]
),
InvalidConfigError::OldValueExists(Parameter::MinAllowedTopLevelAccountLength, expected) => {
assert_eq!(expected, "3200000000");
}
);
}
#[test]
fn test_parameter_table_old_parameter_undefined() {
assert_matches!(
check_invalid_parameter_table(
"min_allowed_top_level_account_length: 3_200_000_000",
&["wasm_regular_op_cost: 3_200_000 -> 1_600_000"]
),
InvalidConfigError::NoOldValueExists(Parameter::WasmRegularOpCost, found) => {
assert_eq!(found, "3200000");
}
);
}
}