-
Notifications
You must be signed in to change notification settings - Fork 643
/
lib.rs
111 lines (102 loc) · 3.98 KB
/
lib.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
//! Settings of the parameters of the runtime.
use serde::{Deserialize, Serialize};
use near_primitives::account::Account;
use near_primitives::serialize::u128_dec_format;
use near_primitives::types::{AccountId, Balance};
use near_runtime_fees::RuntimeFeesConfig;
use near_vm_logic::VMConfig;
/// The structure that holds the parameters of the runtime, mostly economics.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(default)]
pub struct RuntimeConfig {
/// Amount of yN per byte required to have on the account.
/// See https://nomicon.io/Economics/README.html#state-stake for details.
#[serde(with = "u128_dec_format")]
pub storage_amount_per_byte: Balance,
/// Costs of different actions that need to be performed when sending and processing transaction
/// and receipts.
pub transaction_costs: RuntimeFeesConfig,
/// Config of wasm operations.
pub wasm_config: VMConfig,
/// Config that defines rules for account creation.
pub account_creation_config: AccountCreationConfig,
}
impl Default for RuntimeConfig {
fn default() -> Self {
RuntimeConfig {
// See https://nomicon.io/Economics/README.html#general-variables for how it was calculated.
storage_amount_per_byte: 909 * 100_000_000_000_000_000,
transaction_costs: RuntimeFeesConfig::default(),
wasm_config: VMConfig::default(),
account_creation_config: AccountCreationConfig::default(),
}
}
}
impl RuntimeConfig {
pub fn free() -> Self {
Self {
storage_amount_per_byte: 0,
transaction_costs: RuntimeFeesConfig::free(),
wasm_config: VMConfig::free(),
account_creation_config: AccountCreationConfig::default(),
}
}
}
/// The structure describes configuration for creation of new accounts.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct AccountCreationConfig {
/// The minimum length of the top-level account ID that is allowed to be created by any account.
pub min_allowed_top_level_account_length: u8,
/// The account ID of the account registrar. This account ID allowed to create top-level
/// accounts of any valid length.
pub registrar_account_id: AccountId,
}
impl Default for AccountCreationConfig {
fn default() -> Self {
Self {
min_allowed_top_level_account_length: 0,
registrar_account_id: AccountId::from("registrar"),
}
}
}
/// Checks if given account has enough balance for storage stake, and returns:
/// - None if account has enough balance,
/// - Some(insufficient_balance) if account doesn't have enough and how much need to be added,
/// - Err(message) if account has invalid storage usage or amount/locked.
///
/// Read details of state staking https://nomicon.io/Economics/README.html#state-stake
pub fn get_insufficient_storage_stake(
account: &Account,
runtime_config: &RuntimeConfig,
) -> Result<Option<Balance>, String> {
let required_amount = Balance::from(account.storage_usage)
.checked_mul(runtime_config.storage_amount_per_byte)
.ok_or_else(|| {
format!("Account's storage_usage {} overflows multiplication", account.storage_usage)
})?;
let available_amount = account.amount.checked_add(account.locked).ok_or_else(|| {
format!(
"Account's amount {} and locked {} overflow addition",
account.amount, account.locked
)
})?;
if available_amount >= required_amount {
Ok(None)
} else {
Ok(Some(required_amount - available_amount))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_max_prepaid_gas() {
let config = RuntimeConfig::default();
assert!(
config.wasm_config.limit_config.max_total_prepaid_gas
/ config.transaction_costs.min_receipt_with_function_call_gas()
<= 63,
"The maximum desired depth of receipts should be at most 63"
);
}
}