Skip to content

Commit

Permalink
Move flat-storage-reads/fix-contract-loading-cost/implicit-accounts f…
Browse files Browse the repository at this point in the history
…eatures to runtime configuration (#9364)

This PR is built on top of a previous PR (see the base branch). Here the noted protocol features have been replaced with runtime configuration via parameters instead.

This would be one solution/option to getting rid of compile-time features in contract runtime which is interfering with limited replayability (compile-time feature control means that all of the crates still need to be built as a single compilation unit with consistent options.)

cc @jakmeier 

cc  #8197
  • Loading branch information
nagisa authored Aug 18, 2023
1 parent ac607c9 commit e4bd885
Show file tree
Hide file tree
Showing 61 changed files with 1,147 additions and 441 deletions.
7 changes: 1 addition & 6 deletions chain/indexer/src/streamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::time::Duration;

use actix::Addr;
use async_recursion::async_recursion;
use near_indexer_primitives::types::ProtocolVersion;
use node_runtime::config::RuntimeConfig;
use rocksdb::DB;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -128,7 +127,6 @@ async fn build_streamer_message(
let chunk_local_receipts = convert_transactions_sir_into_local_receipts(
&client,
&runtime_config,
protocol_config_view.protocol_version,
indexer_transactions
.iter()
.filter(|tx| tx.transaction.signer_id == tx.transaction.receiver_id)
Expand Down Expand Up @@ -175,7 +173,6 @@ async fn build_streamer_message(
if let Some(receipt) = find_local_receipt_by_id_in_block(
&client,
&runtime_config,
protocol_config_view.protocol_version,
prev_block,
execution_outcome.id,
)
Expand All @@ -188,7 +185,7 @@ async fn build_streamer_message(
}
};
receipt_execution_outcomes
.push(IndexerExecutionOutcomeWithReceipt { execution_outcome, receipt: receipt });
.push(IndexerExecutionOutcomeWithReceipt { execution_outcome, receipt });
}

// Blocks #47317863 and #47317864
Expand Down Expand Up @@ -246,7 +243,6 @@ async fn build_streamer_message(
async fn find_local_receipt_by_id_in_block(
client: &Addr<near_client::ViewClientActor>,
runtime_config: &RuntimeConfig,
protocol_version: ProtocolVersion,
block: views::BlockView,
receipt_id: near_primitives::hash::CryptoHash,
) -> Result<Option<views::ReceiptView>, FailedToFetchData> {
Expand Down Expand Up @@ -276,7 +272,6 @@ async fn find_local_receipt_by_id_in_block(
let local_receipts = convert_transactions_sir_into_local_receipts(
&client,
&runtime_config,
protocol_version,
vec![&indexer_transaction],
&block,
)
Expand Down
3 changes: 0 additions & 3 deletions chain/indexer/src/streamer/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use actix::Addr;

use near_indexer_primitives::types::ProtocolVersion;
use near_indexer_primitives::IndexerTransactionWithOutcome;
use near_primitives::views;
use node_runtime::config::{tx_cost, RuntimeConfig};
Expand All @@ -11,7 +10,6 @@ use super::fetchers::fetch_block;
pub(crate) async fn convert_transactions_sir_into_local_receipts(
client: &Addr<near_client::ViewClientActor>,
runtime_config: &RuntimeConfig,
protocol_version: ProtocolVersion,
txs: Vec<&IndexerTransactionWithOutcome>,
block: &views::BlockView,
) -> Result<Vec<views::ReceiptView>, FailedToFetchData> {
Expand Down Expand Up @@ -44,7 +42,6 @@ pub(crate) async fn convert_transactions_sir_into_local_receipts(
},
prev_block_gas_price,
true,
protocol_version,
);
views::ReceiptView {
predecessor_id: tx.transaction.signer_id.clone(),
Expand Down
17 changes: 17 additions & 0 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ pub struct VMConfig {
/// Disable the fix for the #9393 issue in near-vm-runner.
pub disable_9393_fix: bool,

/// Enable the `FlatStorageReads` protocol feature.
///
// TODO(nagisa): replace with StorageGetMode when VMConfig is moved to near-vm-runner.
pub flat_storage_reads: bool,

/// Enable the `FixContractLoadingCost` protocol feature.
pub fix_contract_loading_cost: bool,

/// Enable the `ImplicitAccountCreation` protocol feature.
pub implicit_account_creation: bool,

/// Describes limits for VM and Runtime.
pub limit_config: VMLimitConfig,
}
Expand Down Expand Up @@ -184,6 +195,9 @@ impl VMConfig {
regular_op_cost: (SAFETY_MULTIPLIER as u32) * 1285457,
disable_9393_fix: false,
limit_config: VMLimitConfig::test(),
fix_contract_loading_cost: cfg!(feature = "protocol_feature_fix_contract_loading_cost"),
flat_storage_reads: true,
implicit_account_creation: true,
}
}

Expand All @@ -203,6 +217,9 @@ impl VMConfig {
disable_9393_fix: false,
// We shouldn't have any costs in the limit config.
limit_config: VMLimitConfig { max_gas_burnt: u64::MAX, ..VMLimitConfig::test() },
fix_contract_loading_cost: cfg!(feature = "protocol_feature_fix_contract_loading_cost"),
flat_storage_reads: true,
implicit_account_creation: true,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions core/primitives-core/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ pub enum Parameter {
MaxLocalsPerContract,
AccountIdValidityRulesVersion,

// Contract runtime features
#[strum(serialize = "disable_9393_fix")]
Disable9393Fix,
FlatStorageReads,
ImplicitAccountCreation,
FixContractLoadingCost,
}

#[derive(
Expand Down
4 changes: 0 additions & 4 deletions core/primitives-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,13 @@ pub enum ProtocolFeature {
///
/// Flat Storage NEP-399: https://github.com/near/NEPs/blob/master/neps/nep-0399.md
FlatStorageReads,

/// Enables preparation V2. Note that this setting is not supported in production settings
/// without NearVmRuntime enabled alongside it, as the VM runner would be too slow.
PreparationV2,

/// Enables Near-Vm. Note that this setting is not at all supported without PreparationV2,
/// as it hardcodes preparation v2 code into the generated assembly.
NearVmRuntime,

BlockHeaderV4,

/// In case not all validator seats are occupied our algorithm provide incorrect minimal seat
/// price - it reports as alpha * sum_stake instead of alpha * sum_stake / (1 - alpha), where
/// alpha is min stake ratio
Expand Down
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/129.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix_contract_loading_cost: { old: false, new: true }
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/35.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implicit_account_creation: { old: false, new: true }
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/61.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ wasm_storage_write_base: { old: 64_196_736_000, new: { gas: 64_196_736_000, c
wasm_storage_remove_base: { old: 53_473_030_500, new: { gas: 53_473_030_500, compute: 200_000_000_000 } }
wasm_storage_read_base: { old: 56_356_845_750, new: { gas: 56_356_845_750, compute: 200_000_000_000 } }
wasm_storage_has_key_base: { old: 54_039_896_625, new: { gas: 54_039_896_625, compute: 200_000_000_000 } }
flat_storage_reads: { old: false, new: true }
3 changes: 3 additions & 0 deletions core/primitives/res/runtime_configs/parameters.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,7 @@ wasmer2_stack_limit 204_800
max_locals_per_contract 1_000_000
account_id_validity_rules_version 1
disable_9393_fix false
flat_storage_reads true
implicit_account_creation true
fix_contract_loading_cost true

4 changes: 4 additions & 0 deletions core/primitives/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,8 @@ max_promises_per_function_call_action: 1_024
max_number_input_data_dependencies: 128
account_id_validity_rules_version: 0

# Contract runtime configuration
disable_9393_fix: false
flat_storage_reads: false
implicit_account_creation: false
fix_contract_loading_cost: false
3 changes: 3 additions & 0 deletions core/primitives/res/runtime_configs/parameters_testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,6 @@ max_promises_per_function_call_action: 1_024
max_number_input_data_dependencies: 128

disable_9393_fix: false
flat_storage_reads: false
implicit_account_creation: false
fix_contract_loading_cost: false
9 changes: 5 additions & 4 deletions core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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.
static CONFIG_DIFFS: &[(ProtocolVersion, &str)] = &[
(35, include_config!("35.yaml")),
(42, include_config!("42.yaml")),
(48, include_config!("48.yaml")),
(49, include_config!("49.yaml")),
Expand All @@ -33,6 +34,7 @@ static CONFIG_DIFFS: &[(ProtocolVersion, &str)] = &[
(61, include_config!("61.yaml")),
(62, include_config!("62.yaml")),
(63, include_config!("63.yaml")),
(129, include_config!("129.yaml")),
];

/// Testnet parameters for versions <= 29, which (incorrectly) differed from mainnet parameters
Expand Down Expand Up @@ -261,17 +263,16 @@ mod tests {
);

let expected_config = {
let first_diff = CONFIG_DIFFS[0].1.parse().unwrap();
base_params.apply_diff(first_diff).unwrap();
base_params.apply_diff(CONFIG_DIFFS[0].1.parse().unwrap()).unwrap();
base_params.apply_diff(CONFIG_DIFFS[1].1.parse().unwrap()).unwrap();
RuntimeConfig::new(&base_params).unwrap()
};
assert_eq!(**config, expected_config);

let config = store.get_config(LowerDataReceiptAndEcrecoverBaseCost.protocol_version());
assert_eq!(config.fees.fee(ActionCosts::new_data_receipt_base).send_sir, 36_486_732_312);
let expected_config = {
let second_diff = CONFIG_DIFFS[1].1.parse().unwrap();
base_params.apply_diff(second_diff).unwrap();
base_params.apply_diff(CONFIG_DIFFS[2].1.parse().unwrap()).unwrap();
RuntimeConfig::new(&base_params).unwrap()
};
assert_eq!(config.as_ref(), &expected_config);
Expand Down
3 changes: 3 additions & 0 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ impl TryFrom<&ParameterTable> for RuntimeConfig {
disable_9393_fix: params.get(Parameter::Disable9393Fix)?,
limit_config: serde_yaml::from_value(params.yaml_map(Parameter::vm_limits()))
.map_err(InvalidConfigError::InvalidYaml)?,
fix_contract_loading_cost: params.get(Parameter::FixContractLoadingCost)?,
flat_storage_reads: params.get(Parameter::FlatStorageReads)?,
implicit_account_creation: params.get(Parameter::ImplicitAccountCreation)?,
},
account_creation_config: AccountCreationConfig {
min_allowed_top_level_account_length: params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ expression: config_view
"grow_mem_cost": 1,
"regular_op_cost": 3856371,
"disable_9393_fix": false,
"flat_storage_reads": false,
"fix_contract_loading_cost": false,
"implicit_account_creation": false,
"limit_config": {
"max_gas_burnt": 200000000000000,
"max_stack_height": 16384,
Expand Down
Loading

0 comments on commit e4bd885

Please sign in to comment.