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

(wip) (fix) Fix Storage Usage #4247

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
134a899
Account V2
EgorKulikov Apr 21, 2021
816a00f
Update core/primitives/src/account.rs
EgorKulikov Apr 21, 2021
ab029b8
Update chain/rosetta-rpc/src/lib.rs
EgorKulikov Apr 21, 2021
2e028a0
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 21, 2021
d73e475
Fixing build
EgorKulikov Apr 21, 2021
ecaabfc
Fixing build
EgorKulikov Apr 21, 2021
b110634
Fixing build
EgorKulikov Apr 21, 2021
1fb8a6b
Fixing build
EgorKulikov Apr 21, 2021
dbef212
Fixing build
EgorKulikov Apr 21, 2021
9f388b0
Fixing test
EgorKulikov Apr 21, 2021
2cb26d4
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 21, 2021
933d861
Storage usage fix
EgorKulikov Apr 22, 2021
f76351b
Merge remote-tracking branch 'origin/fix-account-storage-usage' into …
EgorKulikov Apr 22, 2021
0bf112b
Compliation fix
EgorKulikov Apr 22, 2021
869aa91
Compliation fix
EgorKulikov Apr 22, 2021
77e4bc0
Compliation fix
EgorKulikov Apr 22, 2021
40d7981
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 22, 2021
10ea648
Compliation fix
EgorKulikov Apr 22, 2021
2f3b0e7
Merge remote-tracking branch 'origin/fix-account-storage-usage' into …
EgorKulikov Apr 22, 2021
a3d33ec
Compliation fix
EgorKulikov Apr 22, 2021
89c9780
Compliation fix
EgorKulikov Apr 22, 2021
fbad24f
Compliation fix
EgorKulikov Apr 22, 2021
f5712b1
Comment
EgorKulikov Apr 22, 2021
a823ecb
Compilation fix
EgorKulikov Apr 22, 2021
e5a2238
Compilation fix
EgorKulikov Apr 22, 2021
2e8eb29
Moving delta storage usage from runtime config to static
EgorKulikov Apr 26, 2021
854c759
Compilation fix
EgorKulikov Apr 26, 2021
c6c7675
Compilation fix
EgorKulikov Apr 26, 2021
54a4335
Compilation fix
EgorKulikov Apr 26, 2021
6d96ee0
Compilation fix
EgorKulikov Apr 26, 2021
87b76c9
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 26, 2021
1748c6c
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 27, 2021
7e68414
Removing unnecessary into
EgorKulikov Apr 27, 2021
f6c5bd0
Merge remote-tracking branch 'origin/fix-account-storage-usage' into …
EgorKulikov Apr 27, 2021
53f2729
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 27, 2021
b8ad914
Update runtime/runtime/src/actions.rs
EgorKulikov Apr 27, 2021
2a94d2b
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 28, 2021
7fc8509
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 29, 2021
6e2f721
Merge branch 'master' into fix-account-storage-usage
EgorKulikov Apr 30, 2021
8e61380
Compilation fix
EgorKulikov Apr 30, 2021
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: 3 additions & 1 deletion chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use tracing::debug;
use near_chain_primitives::{Error, ErrorKind};
use near_crypto::{KeyType, PublicKey, SecretKey, Signature};
use near_pool::types::PoolIterator;
use near_primitives::account::{AccessKey, Account};
use near_primitives::access_key::AccessKey;
use near_primitives::account::Account;
use near_primitives::challenge::ChallengesResult;
use near_primitives::epoch_manager::block_info::BlockInfo;
use near_primitives::epoch_manager::epoch_info::EpochInfo;
Expand Down Expand Up @@ -807,6 +808,7 @@ impl RuntimeAdapter for KeyValueRuntime {
0,
CryptoHash::default(),
0,
PROTOCOL_VERSION,
)
.into(),
),
Expand Down
11 changes: 10 additions & 1 deletion chain/rosetta-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use near_primitives::borsh::BorshDeserialize;
use near_primitives::serialize::BaseEncode;

pub use config::RosettaRpcConfig;
use near_primitives::version::PROTOCOL_VERSION;
use near_primitives::views::AccountView;
EgorKulikov marked this conversation as resolved.
Show resolved Hide resolved

mod adapters;
mod config;
Expand Down Expand Up @@ -376,7 +378,14 @@ async fn account_balance(
Err(crate::errors::ErrorKind::NotFound(_)) => (
block.header.hash,
block.header.height,
near_primitives::account::Account::new(0, 0, Default::default(), 0).into(),
AccountView {
amount: 0,
locked: 0,
code_hash: Default::default(),
storage_usage: 0,
storage_paid_at: 0,
}
.into(),
),
Err(err) => return Err(err.into()),
};
Expand Down
1 change: 0 additions & 1 deletion core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ lazy_static = "1.4"
[features]
default = []
costs_counting = []
protocol_feature_add_account_versions = []
protocol_feature_evm = []
protocol_feature_alt_bn128 = []
protocol_feature_tx_size_limit = []
70 changes: 70 additions & 0 deletions core/primitives-core/src/access_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use crate::serialize::option_u128_dec_format;
use crate::types::{AccountId, Balance, Nonce};

/// Access key provides limited access to an account. Each access key belongs to some account and
/// is identified by a unique (within the account) public key. One account may have large number of
/// access keys. Access keys allow to act on behalf of the account by restricting transactions
/// that can be issued.
/// `account_id,public_key` is a key in the state
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct AccessKey {
/// The nonce for this access key.
/// NOTE: In some cases the access key needs to be recreated. If the new access key reuses the
/// same public key, the nonce of the new access key should be equal to the nonce of the old
/// access key. It's required to avoid replaying old transactions again.
pub nonce: Nonce,

/// Defines permissions for this access key.
pub permission: AccessKeyPermission,
}

impl AccessKey {
pub const ACCESS_KEY_NONCE_RANGE_MULTIPLIER: u64 = 1_000_000;

pub fn full_access() -> Self {
Self { nonce: 0, permission: AccessKeyPermission::FullAccess }
}
}

/// Defines permissions for AccessKey
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub enum AccessKeyPermission {
FunctionCall(FunctionCallPermission),

/// Grants full access to the account.
/// NOTE: It's used to replace account-level public keys.
FullAccess,
}

/// Grants limited permission to make transactions with FunctionCallActions
/// The permission can limit the allowed balance to be spent on the prepaid gas.
/// It also restrict the account ID of the receiver for this function call.
/// It also can restrict the method name for the allowed function calls.
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct FunctionCallPermission {
/// Allowance is a balance limit to use by this access key to pay for function call gas and
/// transaction fees. When this access key is used, both account balance and the allowance is
/// decreased by the same value.
/// `None` means unlimited allowance.
/// NOTE: To change or increase the allowance, the old access key needs to be deleted and a new
/// access key should be created.
#[serde(with = "option_u128_dec_format")]
pub allowance: Option<Balance>,

/// The access key only allows transactions with the given receiver's account id.
pub receiver_id: AccountId,

/// A list of method names that can be used. The access key only allows transactions with the
/// function call of one of the given method names.
/// Empty list means any method name can be used.
pub method_names: Vec<String>,
}
2 changes: 1 addition & 1 deletion core/primitives-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use borsh;
pub use num_rational;

pub mod account;
pub mod access_key;
pub mod config;
pub mod contract;
pub mod hash;
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ default = ["jemallocator", "stable_protocol_features"]
dump_errors_schema = ["near-rpc-error-macro/dump_errors_schema"]
stable_protocol_features = ["protocol_feature_lower_storage_cost"]
protocol_feature_lower_storage_cost = []
protocol_feature_add_account_versions = ["near-primitives-core/protocol_feature_add_account_versions"]
protocol_feature_add_account_versions = []
protocol_feature_forward_chunk_parts = []
protocol_feature_rectify_inflation = []
protocol_feature_evm = ["near-primitives-core/protocol_feature_evm"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#[cfg(feature = "protocol_feature_add_account_versions")]
use crate::checked_feature;
use crate::serialize::u128_dec_format_compatible;
use borsh::{BorshDeserialize, BorshSerialize};
#[cfg(feature = "protocol_feature_add_account_versions")]
use core::default::Default;
#[cfg(feature = "protocol_feature_add_account_versions")]
use core::result::Result;
#[cfg(feature = "protocol_feature_add_account_versions")]
use core::result::Result::Ok;
EgorKulikov marked this conversation as resolved.
Show resolved Hide resolved
use near_primitives_core::hash::CryptoHash;
use near_primitives_core::types::{Balance, ProtocolVersion, StorageUsage};
use serde;
use serde::{Deserialize, Serialize};

use crate::hash::CryptoHash;
use crate::serialize::{option_u128_dec_format, u128_dec_format_compatible};
use crate::types::{AccountId, Balance, Nonce, StorageUsage};
#[cfg(feature = "protocol_feature_add_account_versions")]
use std::io;

Expand All @@ -12,7 +20,11 @@ use std::io;
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy,
)]
pub enum AccountVersion {
/// Versioning release
V1,
/// Recalculated storage usage due to previous bug, see
/// https://github.com/near/nearcore/issues/3824
V2,
}

#[cfg(feature = "protocol_feature_add_account_versions")]
Expand Down Expand Up @@ -55,14 +67,25 @@ impl Account {
locked: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
protocol_version: ProtocolVersion,
) -> Self {
Account {
#[cfg(not(feature = "protocol_feature_add_account_versions"))]
let _ = protocol_version;
matklad marked this conversation as resolved.
Show resolved Hide resolved
Self {
amount,
locked,
code_hash,
storage_usage,
#[cfg(feature = "protocol_feature_add_account_versions")]
version: AccountVersion::V1,
version: if checked_feature!(
"protocol_feature_add_account_versions",
AccountVersions,
protocol_version
) {
AccountVersion::V2
} else {
AccountVersion::V1
},
}
}

Expand Down Expand Up @@ -128,6 +151,16 @@ struct LegacyAccount {
storage_usage: StorageUsage,
}

#[cfg(feature = "protocol_feature_add_account_versions")]
#[derive(BorshSerialize, BorshDeserialize)]
struct SerializableAccount {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we implement From<SerializableAccount> for Account?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering it should ever be used only once I am not sure we need this

amount: Balance,
locked: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
version: AccountVersion,
}

#[cfg(feature = "protocol_feature_add_account_versions")]
impl BorshDeserialize for Account {
fn deserialize(buf: &mut &[u8]) -> Result<Self, io::Error> {
Expand All @@ -143,7 +176,14 @@ impl BorshDeserialize for Account {
version: AccountVersion::V1,
})
} else {
unreachable!();
let deserialized_account = SerializableAccount::deserialize(buf)?;
Ok(Account {
amount: deserialized_account.amount,
locked: deserialized_account.locked,
code_hash: deserialized_account.code_hash,
storage_usage: deserialized_account.storage_usage,
version: deserialized_account.version,
})
}
}
}
Expand All @@ -159,91 +199,73 @@ impl BorshSerialize for Account {
storage_usage: self.storage_usage,
}
.serialize(writer),
_ => SerializableAccount {
matklad marked this conversation as resolved.
Show resolved Hide resolved
amount: self.amount,
locked: self.locked,
code_hash: self.code_hash,
storage_usage: self.storage_usage,
version: self.version,
}
.serialize(writer),
}
}
}

/// Access key provides limited access to an account. Each access key belongs to some account and
/// is identified by a unique (within the account) public key. One account may have large number of
/// access keys. Access keys allow to act on behalf of the account by restricting transactions
/// that can be issued.
/// `account_id,public_key` is a key in the state
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct AccessKey {
/// The nonce for this access key.
/// NOTE: In some cases the access key needs to be recreated. If the new access key reuses the
/// same public key, the nonce of the new access key should be equal to the nonce of the old
/// access key. It's required to avoid replaying old transactions again.
pub nonce: Nonce,

/// Defines permissions for this access key.
pub permission: AccessKeyPermission,
}

impl AccessKey {
pub const ACCESS_KEY_NONCE_RANGE_MULTIPLIER: u64 = 1_000_000;

pub fn full_access() -> Self {
Self { nonce: 0, permission: AccessKeyPermission::FullAccess }
}
}

/// Defines permissions for AccessKey
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub enum AccessKeyPermission {
FunctionCall(FunctionCallPermission),

/// Grants full access to the account.
/// NOTE: It's used to replace account-level public keys.
FullAccess,
}

/// Grants limited permission to make transactions with FunctionCallActions
/// The permission can limit the allowed balance to be spent on the prepaid gas.
/// It also restrict the account ID of the receiver for this function call.
/// It also can restrict the method name for the allowed function calls.
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug,
)]
pub struct FunctionCallPermission {
/// Allowance is a balance limit to use by this access key to pay for function call gas and
/// transaction fees. When this access key is used, both account balance and the allowance is
/// decreased by the same value.
/// `None` means unlimited allowance.
/// NOTE: To change or increase the allowance, the old access key needs to be deleted and a new
/// access key should be created.
#[serde(with = "option_u128_dec_format")]
pub allowance: Option<Balance>,

/// The access key only allows transactions with the given receiver's account id.
pub receiver_id: AccountId,

/// A list of method names that can be used. The access key only allows transactions with the
/// function call of one of the given method names.
/// Empty list means any method name can be used.
pub method_names: Vec<String>,
}

#[cfg(test)]
mod tests {
use crate::account::Account;
#[cfg(feature = "protocol_feature_add_account_versions")]
use crate::account::{AccountVersion, LegacyAccount};
#[cfg(feature = "protocol_feature_add_account_versions")]
use crate::borsh::BorshDeserialize;
use crate::hash::CryptoHash;
#[cfg(feature = "protocol_feature_add_account_versions")]
use crate::version::ProtocolFeature;
#[cfg(not(feature = "protocol_feature_add_account_versions"))]
use crate::version::PROTOCOL_VERSION;
use borsh::BorshSerialize;

use crate::hash::hash;
use crate::serialize::to_base;

use super::*;
use near_primitives_core::hash::hash;
use near_primitives_core::serialize::to_base;

#[test]
fn test_account_serialization() {
let acc = Account::new(1_000_000, 1_000_000, CryptoHash::default(), 100);
#[cfg(feature = "protocol_feature_add_account_versions")]
let acc = Account::new(
1_000_000,
1_000_000,
CryptoHash::default(),
100,
ProtocolFeature::AccountVersions.protocol_version() - 1,
);
#[cfg(not(feature = "protocol_feature_add_account_versions"))]
let acc = Account::new(1_000_000, 1_000_000, CryptoHash::default(), 100, PROTOCOL_VERSION);
let bytes = acc.try_to_vec().unwrap();
assert_eq!(to_base(&hash(&bytes)), "EVk5UaxBe8LQ8r8iD5EAxVBs6TJcMDKqyH7PBuho6bBJ");
}

#[test]
#[cfg(feature = "protocol_feature_add_account_versions")]
fn test_account_size() {
let new_account = Account::new(
0,
0,
CryptoHash::default(),
0,
ProtocolFeature::AccountVersions.protocol_version(),
);
let old_account = Account::new(
0,
0,
CryptoHash::default(),
0,
ProtocolFeature::AccountVersions.protocol_version() - 1,
);
let new_bytes = new_account.try_to_vec().unwrap();
let old_bytes = old_account.try_to_vec().unwrap();
assert!(new_bytes.len() > old_bytes.len());
assert_eq!(old_bytes.len(), std::mem::size_of::<LegacyAccount>());
}

#[test]
#[cfg(feature = "protocol_feature_add_account_versions")]
fn test_account_deserialization() {
Expand Down
3 changes: 2 additions & 1 deletion core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub use near_primitives_core::borsh;
pub use near_primitives_core::num_rational;

pub use near_primitives_core::account;
pub use near_primitives_core::access_key;
pub mod account;
pub mod block;
pub mod block_header;
pub mod challenge;
Expand Down
3 changes: 2 additions & 1 deletion core/primitives/src/state_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::fmt::{Display, Formatter};

use near_crypto::PublicKey;

use crate::account::{AccessKey, Account};
use crate::access_key::AccessKey;
use crate::account::Account;
use crate::hash::{hash, CryptoHash};
use crate::receipt::{Receipt, ReceivedData};
use crate::serialize::{base64_format, option_base64_format};
Expand Down
Loading