use crate::cost::{ExtCostsConfig, ParameterCost};
use borsh::BorshSerialize;
use near_primitives_core::config::AccountIdValidityRulesVersion;
use near_primitives_core::types::Gas;
use near_schema_checker_lib::ProtocolSchema;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(
Clone,
Copy,
Debug,
Hash,
BorshSerialize,
PartialEq,
Eq,
strum::EnumString,
serde::Serialize,
serde::Deserialize,
ProtocolSchema,
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum VMKind {
Wasmer0,
Wasmtime,
Wasmer2,
NearVm,
}
impl VMKind {
pub fn replace_with_wasmtime_if_unsupported(self) -> Self {
if cfg!(not(target_arch = "x86_64")) { Self::Wasmtime } else { self }
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum StorageGetMode {
FlatStorage,
Trie,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct LimitConfig {
pub max_gas_burnt: Gas,
pub max_stack_height: u32,
pub initial_memory_pages: u32,
pub max_memory_pages: u32,
pub registers_memory_limit: u64,
pub max_register_size: u64,
pub max_number_registers: u64,
pub max_number_logs: u64,
pub max_total_log_length: u64,
pub max_total_prepaid_gas: Gas,
pub max_actions_per_receipt: u64,
pub max_number_bytes_method_names: u64,
pub max_length_method_name: u64,
pub max_arguments_length: u64,
pub max_length_returned_data: u64,
pub max_contract_size: u64,
pub max_transaction_size: u64,
pub max_receipt_size: u64,
pub max_length_storage_key: u64,
pub max_length_storage_value: u64,
pub max_promises_per_function_call_action: u64,
pub max_number_input_data_dependencies: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_functions_number_per_contract: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_locals_per_contract: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tables_per_contract: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_elements_per_contract_table: Option<usize>,
#[serde(default = "AccountIdValidityRulesVersion::v0")]
pub account_id_validity_rules_version: AccountIdValidityRulesVersion,
pub yield_timeout_length_in_blocks: u64,
pub max_yield_payload_size: u64,
pub per_receipt_storage_proof_size_limit: usize,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Config {
pub ext_costs: ExtCostsConfig,
pub grow_mem_cost: u32,
pub regular_op_cost: u32,
pub linear_op_base_cost: u64,
pub linear_op_unit_cost: u64,
pub vm_kind: VMKind,
pub storage_get_mode: StorageGetMode,
pub fix_contract_loading_cost: bool,
pub eth_implicit_accounts: bool,
pub eth_implicit_global_contract: bool,
pub discard_custom_sections: bool,
pub global_contract_host_fns: bool,
pub reftypes_bulk_memory: bool,
pub deterministic_account_ids: bool,
pub gas_key_host_fns: bool,
pub limit_config: LimitConfig,
}
impl Config {
pub fn non_crypto_hash(&self) -> u64 {
let mut s = DefaultHasher::new();
self.hash(&mut s);
s.finish()
}
pub fn make_free(&mut self) {
self.ext_costs = ExtCostsConfig {
costs: near_primitives_core::enum_map::enum_map! {
_ => ParameterCost { gas: Gas::ZERO, compute: 0 }
},
};
self.grow_mem_cost = 0;
self.regular_op_cost = 0;
self.linear_op_base_cost = 0;
self.linear_op_unit_cost = 0;
self.limit_config.max_gas_burnt = Gas::MAX;
}
pub fn enable_all_features(&mut self) {
self.eth_implicit_accounts = true;
self.eth_implicit_global_contract = true;
self.global_contract_host_fns = true;
self.gas_key_host_fns = true;
}
}
#[derive(
Debug,
Clone,
Copy,
Hash,
PartialEq,
Eq,
serde_repr::Serialize_repr,
serde_repr::Deserialize_repr,
)]
#[repr(u8)]
pub enum ContractPrepareVersion {
V0,
V1,
V2,
}
impl ContractPrepareVersion {
pub fn v0() -> ContractPrepareVersion {
ContractPrepareVersion::V0
}
}