Skip to content

Commit

Permalink
refactor: ExtCostsConfig as simple map of costs (#8115)
Browse files Browse the repository at this point in the history
This is the next step in the refactoring steps for changing gas profiles
to track gas by parameter (#8033).

Here we make `ExtCostsConfig` opaque and look up parameters by
```rust
pub fn cost(&self, param: ExtCosts) -> Gas
```
instead of using a specific field inside.

There are side-effects for this in
1. parameter definition
2. JSON RPC 
3. parameter estimator

1) We no longer load the parameters through
"parameter table -> JSON -> serde_deser" steps because `ExtCostsConfig`
no longer has serde derives. Instead each `ExtCosts` maps to a
`Parameter` that allows looking up the value directly from
`ParameterTable`.
This explicit mapping also replaces the `Parameter::ext_costs()`
iterator previously used to find all parameters that are ext costs.

We used to define `wasm_read_cached_trie_node` in `53.txt` and fill
old values with serde default. Serde was removed here, so I changed it
to define the parameter in the base files. This is equivalent to the
old behavior, only it is less clear when we added the parameter.

2) JSON RPC must keep the old format. Thus, I added `ExtCostsConfigView`
and `VMConfigView` there. It is a direct copy-paste of the old structs
in the old format but without serde magic to fill in missing values.

3) The estimator generates a `ExtCostsConfig` from estimations. This is
now done through a mapping from estimated costs to `ExtCosts`.

# Testing

The exact JSON output is checked in existing tests
`test_json_unchanged`.
  • Loading branch information
jakmeier authored Dec 7, 2022
1 parent 80e9960 commit 8c83455
Show file tree
Hide file tree
Showing 13 changed files with 612 additions and 544 deletions.
526 changes: 153 additions & 373 deletions core/primitives-core/src/config.rs

Large diffs are not rendered by default.

71 changes: 0 additions & 71 deletions core/primitives-core/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,77 +210,6 @@ pub enum FeeParameter {
}

impl Parameter {
/// Iterate through all parameters that define external gas costs that may
/// be charged during WASM execution. These are essentially all costs from
/// host function calls. Note that the gas cost for regular WASM operation
/// is treated separately and therefore not included in this list.
pub fn ext_costs() -> slice::Iter<'static, Parameter> {
[
Parameter::WasmBase,
Parameter::WasmContractLoadingBase,
Parameter::WasmContractLoadingBytes,
Parameter::WasmReadMemoryBase,
Parameter::WasmReadMemoryByte,
Parameter::WasmWriteMemoryBase,
Parameter::WasmWriteMemoryByte,
Parameter::WasmReadRegisterBase,
Parameter::WasmReadRegisterByte,
Parameter::WasmWriteRegisterBase,
Parameter::WasmWriteRegisterByte,
Parameter::WasmUtf8DecodingBase,
Parameter::WasmUtf8DecodingByte,
Parameter::WasmUtf16DecodingBase,
Parameter::WasmUtf16DecodingByte,
Parameter::WasmSha256Base,
Parameter::WasmSha256Byte,
Parameter::WasmKeccak256Base,
Parameter::WasmKeccak256Byte,
Parameter::WasmKeccak512Base,
Parameter::WasmKeccak512Byte,
Parameter::WasmRipemd160Base,
Parameter::WasmRipemd160Block,
Parameter::WasmEcrecoverBase,
Parameter::WasmEd25519VerifyBase,
Parameter::WasmEd25519VerifyByte,
Parameter::WasmLogBase,
Parameter::WasmLogByte,
Parameter::WasmStorageWriteBase,
Parameter::WasmStorageWriteKeyByte,
Parameter::WasmStorageWriteValueByte,
Parameter::WasmStorageWriteEvictedByte,
Parameter::WasmStorageReadBase,
Parameter::WasmStorageReadKeyByte,
Parameter::WasmStorageReadValueByte,
Parameter::WasmStorageRemoveBase,
Parameter::WasmStorageRemoveKeyByte,
Parameter::WasmStorageRemoveRetValueByte,
Parameter::WasmStorageHasKeyBase,
Parameter::WasmStorageHasKeyByte,
Parameter::WasmStorageIterCreatePrefixBase,
Parameter::WasmStorageIterCreatePrefixByte,
Parameter::WasmStorageIterCreateRangeBase,
Parameter::WasmStorageIterCreateFromByte,
Parameter::WasmStorageIterCreateToByte,
Parameter::WasmStorageIterNextBase,
Parameter::WasmStorageIterNextKeyByte,
Parameter::WasmStorageIterNextValueByte,
Parameter::WasmTouchingTrieNode,
Parameter::WasmReadCachedTrieNode,
Parameter::WasmPromiseAndBase,
Parameter::WasmPromiseAndPerPromise,
Parameter::WasmPromiseReturn,
Parameter::WasmValidatorStakeBase,
Parameter::WasmValidatorTotalStakeBase,
Parameter::WasmAltBn128G1MultiexpBase,
Parameter::WasmAltBn128G1MultiexpElement,
Parameter::WasmAltBn128PairingCheckBase,
Parameter::WasmAltBn128PairingCheckElement,
Parameter::WasmAltBn128G1SumBase,
Parameter::WasmAltBn128G1SumElement,
]
.iter()
}

/// Iterate through all parameters that define numerical limits for
/// contracts that are executed in the WASM VM.
pub fn vm_limits() -> slice::Iter<'static, Parameter> {
Expand Down
1 change: 0 additions & 1 deletion core/primitives/res/runtime_configs/53.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
action_deploy_contract_per_byte_execution: 6_812_999 -> 64_572_944
wasm_read_cached_trie_node: 2_280_000_000
wasmer2_stack_limit: 204_800
max_length_storage_key: 4_194_304 -> 2_048
max_locals_per_contract: 1_000_000
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/parameters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ 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
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
wasm_promise_return: 560_152_386
Expand Down
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/parameters_testnet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ 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
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
wasm_promise_return: 560_152_386
Expand Down
6 changes: 3 additions & 3 deletions core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod tests {
use crate::version::ProtocolFeature::{
LowerDataReceiptAndEcrecoverBaseCost, LowerStorageCost, LowerStorageKeyLimit,
};
use near_primitives_core::config::ActionCosts;
use near_primitives_core::config::{ActionCosts, ExtCosts};

const GENESIS_PROTOCOL_VERSION: ProtocolVersion = 29;
const RECEIPTS_DEPTH: u64 = 63;
Expand Down Expand Up @@ -212,8 +212,8 @@ mod tests {
let base_cfg = store.get_config(LowerStorageCost.protocol_version());
let new_cfg = store.get_config(LowerDataReceiptAndEcrecoverBaseCost.protocol_version());
assert!(
base_cfg.wasm_config.ext_costs.ecrecover_base
> new_cfg.wasm_config.ext_costs.ecrecover_base
base_cfg.wasm_config.ext_costs.cost(ExtCosts::ecrecover_base)
> new_cfg.wasm_config.ext_costs.cost(ExtCosts::ecrecover_base)
);
}

Expand Down
9 changes: 6 additions & 3 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::config::{AccountCreationConfig, RuntimeConfig};
use near_primitives_core::config::VMConfig;
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;
Expand Down Expand Up @@ -81,8 +81,11 @@ impl TryFrom<&ParameterTable> for RuntimeConfig {
},
},
wasm_config: VMConfig {
ext_costs: serde_json::from_value(params.json_map(Parameter::ext_costs(), "wasm_"))
.map_err(InvalidConfigError::WrongStructure)?,
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(), ""))
Expand Down
Loading

0 comments on commit 8c83455

Please sign in to comment.