Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add zero balance accounts #8378

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chain/chain/src/tests/simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn build_chain() {
// cargo insta test --accept -p near-chain --features nightly -- tests::simple_chain::build_chain
let hash = chain.head().unwrap().last_block_hash;
if cfg!(feature = "nightly") {
insta::assert_display_snapshot!(hash, @"96KiRJdbMN8A9cFPXarZdaRQ8U2HvYcrGTGC8a4EgFzM");
insta::assert_display_snapshot!(hash, @"7f7HTgxYS9NbF61kqqQSD4ivtPj7hCReXqyMMqiUfMZq");
} else {
insta::assert_display_snapshot!(hash, @"7r5VSLXhkxHHEeiAAPQbKPGv3rr877obehGYwPbKZMA7");
}
Expand Down Expand Up @@ -73,7 +73,7 @@ fn build_chain() {

let hash = chain.head().unwrap().last_block_hash;
if cfg!(feature = "nightly") {
insta::assert_display_snapshot!(hash, @"4eW4jvyu1Ek6WmY3EuUoFFkrascC7svRww5UcZbNMkUf");
insta::assert_display_snapshot!(hash, @"5GXKgTvpLBcSDncu3P8tdmFZ48m89rzUivJEtDTjp6oq");
} else {
insta::assert_display_snapshot!(hash, @"9772sSKzm1eGPV3pRi17YaZkotrcN6dAkJUn226CopTm");
}
Expand Down
2 changes: 2 additions & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protocol_feature_reject_blocks_with_outdated_protocol_version = []
protocol_feature_ed25519_verify = [
"near-primitives-core/protocol_feature_ed25519_verify"
]
protocol_feature_zero_balance_account = []
protocol_feature_nep366_delegate_action = [
"near-primitives-core/protocol_feature_nep366_delegate_action"
]
Expand All @@ -61,6 +62,7 @@ nightly = [
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_ed25519_verify",
"protocol_feature_nep366_delegate_action",
"protocol_feature_zero_balance_account"
]

nightly_protocol = []
Expand Down
34 changes: 0 additions & 34 deletions core/primitives/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
pub use near_primitives_core::runtime::fees;
pub use near_primitives_core::runtime::*;

use crate::account::Account;
use crate::runtime::config::RuntimeConfig;
use crate::types::Balance;

pub mod apply_state;
pub mod config;
pub mod config_store;
pub mod migration_data;
pub mod parameter_table;

/// 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))
}
}
7 changes: 6 additions & 1 deletion core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub enum ProtocolFeature {
RejectBlocksWithOutdatedProtocolVersions,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
DelegateAction,
#[cfg(feature = "protocol_feature_zero_balance_account")]
bowenwang1996 marked this conversation as resolved.
Show resolved Hide resolved
/// NEP 448: https://github.com/near/NEPs/pull/448
ZeroBalanceAccount,
}

/// Both, outgoing and incoming tcp connections to peers, will be rejected if `peer's`
Expand All @@ -163,7 +166,7 @@ const STABLE_PROTOCOL_VERSION: ProtocolVersion = 58;
/// Largest protocol version supported by the current binary.
pub const PROTOCOL_VERSION: ProtocolVersion = if cfg!(feature = "nightly_protocol") {
// On nightly, pick big enough version to support all features.
133
134
} else {
// Enable all stable features.
STABLE_PROTOCOL_VERSION
Expand Down Expand Up @@ -238,6 +241,8 @@ impl ProtocolFeature {
ProtocolFeature::RejectBlocksWithOutdatedProtocolVersions => 132,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ProtocolFeature::DelegateAction => 133,
#[cfg(feature = "protocol_feature_zero_balance_account")]
ProtocolFeature::ZeroBalanceAccount => 134,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ protocol_feature_reject_blocks_with_outdated_protocol_version = [
"near-chain/protocol_feature_reject_blocks_with_outdated_protocol_version"
]
protocol_feature_flat_state = ["nearcore/protocol_feature_flat_state"]
protocol_feature_zero_balance_account = ["node-runtime/protocol_feature_zero_balance_account"]

nightly = [
"nightly_protocol",
"nearcore/nightly",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_flat_state",
"protocol_feature_zero_balance_account"
]
nightly_protocol = ["nearcore/nightly_protocol"]
sandbox = [
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/src/tests/client/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ mod limit_contract_functions_number;
mod lower_storage_key_limit;
mod restore_receipts_after_fix_apply_chunks;
mod wasmer2;
#[cfg(feature = "protocol_feature_zero_balance_account")]
mod zero_balance_account;
Loading