use crate::cost::{ExtCostsConfig, ParameterCost};
use borsh::BorshSerialize;
use near_primitives_core::config::AccountIdValidityRulesVersion;
use near_primitives_core::types::Gas;
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,
)]
#[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)]
pub enum StorageGetMode {
FlatStorage,
Trie,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct LimitConfig {
pub max_gas_burnt: Gas,
pub max_stack_height: u32,
#[serde(default = "ContractPrepareVersion::v0")]
pub contract_prepare_version: ContractPrepareVersion,
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_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(default = "wasmer2_stack_limit_default")]
pub wasmer2_stack_limit: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_locals_per_contract: Option<u64>,
#[serde(default = "AccountIdValidityRulesVersion::v0")]
pub account_id_validity_rules_version: AccountIdValidityRulesVersion,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Config {
pub ext_costs: ExtCostsConfig,
pub grow_mem_cost: u32,
pub regular_op_cost: u32,
pub vm_kind: VMKind,
pub disable_9393_fix: bool,
pub storage_get_mode: StorageGetMode,
pub fix_contract_loading_cost: bool,
pub implicit_account_creation: bool,
pub math_extension: bool,
pub ed25519_verify: bool,
pub alt_bn128: bool,
pub function_call_weight: bool,
pub eth_implicit_accounts: 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: 0, compute: 0 }
},
};
self.grow_mem_cost = 0;
self.regular_op_cost = 0;
self.limit_config.max_gas_burnt = u64::MAX;
}
}
fn wasmer2_stack_limit_default() -> i32 {
100 * 1024
}
#[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
}
}