Skip to content

Commit

Permalink
Use protocol version for Account::new()
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Jan 30, 2024
1 parent c43bee3 commit e29955d
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 21 deletions.
1 change: 1 addition & 0 deletions chain/chain/src/test_utils/kv_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ impl RuntimeAdapter for KeyValueRuntime {
0,
CryptoHash::default(),
0,
PROTOCOL_VERSION,
)
.into(),
),
Expand Down
12 changes: 10 additions & 2 deletions chain/rosetta-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use config::RosettaRpcConfig;
use near_chain_configs::Genesis;
use near_client::{ClientActor, ViewClientActor};
use near_o11y::WithSpanContextExt;
use near_primitives::borsh::BorshDeserialize;
use near_primitives::{borsh::BorshDeserialize, version::PROTOCOL_VERSION};

mod adapters;
mod config;
Expand Down Expand Up @@ -368,7 +368,15 @@ async fn account_balance(
Err(crate::errors::ErrorKind::NotFound(_)) => (
block.header.hash,
block.header.height,
near_primitives::account::Account::new(0, 0, 0, Default::default(), 0).into(),
near_primitives::account::Account::new(
0,
0,
0,
Default::default(),
0,
PROTOCOL_VERSION,
)
.into(),
),
Err(err) => return Err(err.into()),
};
Expand Down
3 changes: 2 additions & 1 deletion core/chain-configs/src/genesis_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ mod test {
use near_crypto::{KeyType, PublicKey};
use near_primitives::account::{AccessKey, Account};
use near_primitives::types::AccountInfo;
use near_primitives::version::PROTOCOL_VERSION;

const VALID_ED25519_RISTRETTO_KEY: &str = "ed25519:KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7";

fn create_account() -> Account {
Account::new(100, 10, 0, Default::default(), 0)
Account::new(100, 10, 0, Default::default(), 0, PROTOCOL_VERSION)
}

#[test]
Expand Down
21 changes: 17 additions & 4 deletions core/primitives-core/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
use crate::checked_feature;
use crate::hash::CryptoHash;
use crate::serialize::dec_format;
use crate::types::{Balance, Nonce, StorageUsage};
use crate::types::{Balance, Nonce, ProtocolVersion, StorageUsage};
use borsh::{BorshDeserialize, BorshSerialize};
pub use near_account_id as id;
use std::io;
Expand Down Expand Up @@ -80,9 +82,20 @@ impl Account {
nonrefundable: Balance,
code_hash: CryptoHash,
storage_usage: StorageUsage,
#[cfg_attr(not(feature = "protocol_feature_nonrefundable_transfer_nep491"), allow(unused))]
protocol_version: ProtocolVersion,
) -> Self {
let version = AccountVersion::default();
if version == AccountVersion::V1 {
#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
let account_version = AccountVersion::V1;

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let account_version = if checked_feature!("stable", NonRefundableBalance, protocol_version)
{
AccountVersion::V2
} else {
AccountVersion::V1
};
if account_version == AccountVersion::V1 {
assert_eq!(nonrefundable, 0);
}
Account {
Expand All @@ -92,7 +105,7 @@ impl Account {
nonrefundable,
code_hash,
storage_usage,
version,
version: account_version,
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::version::PROTOCOL_VERSION;
use crate::views::{ExecutionStatusView, FinalExecutionOutcomeView, FinalExecutionStatus};

pub fn account_new(amount: Balance, code_hash: CryptoHash) -> Account {
Account::new(amount, 0, 0, code_hash, std::mem::size_of::<Account>() as u64)
Account::new(amount, 0, 0, code_hash, std::mem::size_of::<Account>() as u64, PROTOCOL_VERSION)
}

impl Transaction {
Expand Down
10 changes: 9 additions & 1 deletion core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use chrono::DateTime;
use near_crypto::{PublicKey, Signature};
use near_fmt::{AbbrBytes, Slice};
use near_parameters::{ActionCosts, ExtCosts};
use near_primitives_core::version::PROTOCOL_VERSION;
use serde_with::base64::Base64;
use serde_with::serde_as;
use std::collections::HashMap;
Expand Down Expand Up @@ -126,7 +127,14 @@ impl From<&AccountView> for Account {
let nonrefundable = view.nonrefundable;
#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
let nonrefundable = 0;
Account::new(view.amount, view.locked, nonrefundable, view.code_hash, view.storage_usage)
Account::new(
view.amount,
view.locked,
nonrefundable,
view.code_hash,
view.storage_usage,
PROTOCOL_VERSION,
)
}
}

Expand Down
10 changes: 9 additions & 1 deletion genesis-tools/genesis-csv-to-json/src/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use near_primitives::receipt::{ActionReceipt, Receipt, ReceiptEnum};
use near_primitives::state_record::StateRecord;
use near_primitives::transaction::{Action, FunctionCallAction};
use near_primitives::types::{AccountId, AccountInfo, Balance, Gas};
use near_primitives::version::PROTOCOL_VERSION;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
Expand Down Expand Up @@ -187,7 +188,14 @@ fn account_records(row: &Row, gas_price: Balance) -> Vec<StateRecord> {

let mut res = vec![StateRecord::Account {
account_id: row.account_id.clone(),
account: Account::new(row.amount, row.validator_stake, 0, smart_contract_hash, 0),
account: Account::new(
row.amount,
row.validator_stake,
0,
smart_contract_hash,
0,
PROTOCOL_VERSION,
),
}];

// Add restricted access keys.
Expand Down
1 change: 1 addition & 0 deletions genesis-tools/genesis-populate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ impl GenesisBuilder {
0,
self.additional_accounts_code_hash,
0,
self.genesis.config.protocol_version,
);
set_account(&mut state_update, account_id.clone(), &account);
let account_record = StateRecord::Account { account_id: account_id.clone(), account };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ fn run_chunk_validation_test(seed: u64, prob_missing_chunk: f64) {
let staked = if i < num_validators { validator_stake } else { 0 };
records.push(StateRecord::Account {
account_id: account.clone(),
account: Account::new(initial_balance, staked, 0, CryptoHash::default(), 0),
account: Account::new(
initial_balance,
staked,
0,
CryptoHash::default(),
0,
PROTOCOL_VERSION,
),
});
records.push(StateRecord::AccessKey {
account_id: account.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ fn test_in_memory_trie_node_consistency() {
let staked = if i < 2 { validator_stake } else { 0 };
records.push(StateRecord::Account {
account_id: account.clone(),
account: Account::new(initial_balance, staked, 0, CryptoHash::default(), 0),
account: Account::new(
initial_balance,
staked,
0,
CryptoHash::default(),
0,
PROTOCOL_VERSION,
),
});
records.push(StateRecord::AccessKey {
account_id: account.clone(),
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/runtime/state_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ fn test_view_state_too_large() {
set_account(
&mut state_update,
alice_account(),
&Account::new(0, 0, 0, CryptoHash::default(), 50_001),
&Account::new(0, 0, 0, CryptoHash::default(), 50_001, PROTOCOL_VERSION),
);
let trie_viewer = TrieViewer::new(Some(50_000), None);
let result = trie_viewer.view_state(&state_update, &alice_account(), b"", false);
Expand All @@ -375,7 +375,7 @@ fn test_view_state_with_large_contract() {
set_account(
&mut state_update,
alice_account(),
&Account::new(0, 0, 0, sha256(&contract_code), 50_001),
&Account::new(0, 0, 0, sha256(&contract_code), 50_001, PROTOCOL_VERSION),
);
state_update.set(TrieKey::ContractCode { account_id: alice_account() }, contract_code);
let trie_viewer = TrieViewer::new(Some(50_000), None);
Expand Down
2 changes: 1 addition & 1 deletion nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ fn add_account_with_key(
) {
records.push(StateRecord::Account {
account_id: account_id.clone(),
account: Account::new(amount, staked, 0, code_hash, 0),
account: Account::new(amount, staked, 0, code_hash, 0, PROTOCOL_VERSION),
});
records.push(StateRecord::AccessKey {
account_id,
Expand Down
11 changes: 9 additions & 2 deletions runtime/runtime/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ pub(crate) fn action_create_account(
account_id: &AccountId,
predecessor_id: &AccountId,
result: &mut ActionResult,
protocol_version: ProtocolVersion,
) {
if account_id.is_top_level() {
if account_id.len() < account_creation_config.min_allowed_top_level_account_length as usize
Expand Down Expand Up @@ -461,6 +462,7 @@ pub(crate) fn action_create_account(
0,
CryptoHash::default(),
fee_config.storage_usage_config.num_bytes_account,
protocol_version,
));
}

Expand Down Expand Up @@ -508,6 +510,7 @@ pub(crate) fn action_implicit_account_creation_transfer(
+ public_key.len() as u64
+ borsh::object_length(&access_key).unwrap() as u64
+ fee_config.storage_usage_config.num_extra_bytes_record,
current_protocol_version,
));

set_access_key(state_update, account_id.clone(), public_key, &access_key);
Expand All @@ -531,6 +534,7 @@ pub(crate) fn action_implicit_account_creation_transfer(
nonrefundable_balance,
*magic_bytes.hash(),
storage_usage,
current_protocol_version,
));
set_code(state_update, account_id.clone(), &magic_bytes);

Expand Down Expand Up @@ -1084,6 +1088,7 @@ mod tests {
use near_primitives::transaction::CreateAccountAction;
use near_primitives::trie_key::TrieKey;
use near_primitives::types::{EpochId, StateChangeCause};
use near_primitives_core::version::PROTOCOL_VERSION;
use near_store::set_account;
use near_store::test_utils::TestTriesBuilder;
use std::sync::Arc;
Expand All @@ -1107,6 +1112,7 @@ mod tests {
&account_id,
&predecessor_id,
&mut action_result,
PROTOCOL_VERSION,
);
if action_result.result.is_ok() {
assert!(account.is_some());
Expand Down Expand Up @@ -1192,7 +1198,8 @@ mod tests {
storage_usage: u64,
state_update: &mut TrieUpdate,
) -> ActionResult {
let mut account = Some(Account::new(100, 0, 0, *code_hash, storage_usage));
let mut account =
Some(Account::new(100, 0, 0, *code_hash, storage_usage, PROTOCOL_VERSION));
let mut actor_id = account_id.clone();
let mut action_result = ActionResult::default();
let receipt = Receipt::new_balance_refund(&"alice.near".parse().unwrap(), 0);
Expand Down Expand Up @@ -1330,7 +1337,7 @@ mod tests {
let tries = TestTriesBuilder::new().build();
let mut state_update =
tries.new_trie_update(ShardUId::single_shard(), CryptoHash::default());
let account = Account::new(100, 0, 0, CryptoHash::default(), 100);
let account = Account::new(100, 0, 0, CryptoHash::default(), 100, PROTOCOL_VERSION);
set_account(&mut state_update, account_id.clone(), &account);
set_access_key(&mut state_update, account_id.clone(), public_key.clone(), access_key);

Expand Down
1 change: 1 addition & 0 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ impl Runtime {
&receipt.receiver_id,
&receipt.predecessor_id,
&mut result,
apply_state.current_protocol_version,
);
}
Action::DeployContract(deploy_contract) => {
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime/tests/runtime_group_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl RuntimeGroup {
0,
code_hash,
0,
PROTOCOL_VERSION,
),
});
state_records.push(StateRecord::AccessKey {
Expand Down
5 changes: 3 additions & 2 deletions test-utils/testlib/src/runtime_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use near_primitives::account::{AccessKey, Account};
use near_primitives::hash::hash;
use near_primitives::state_record::StateRecord;
use near_primitives::types::{AccountId, Balance};
use near_primitives::version::PROTOCOL_VERSION;

pub fn alice_account() -> AccountId {
"alice.near".parse().unwrap()
Expand Down Expand Up @@ -46,7 +47,7 @@ pub fn add_contract(genesis: &mut Genesis, account_id: &AccountId, code: Vec<u8>
if !is_account_record_found {
records.push(StateRecord::Account {
account_id: account_id.clone(),
account: Account::new(0, 0, 0, hash, 0),
account: Account::new(0, 0, 0, hash, 0, PROTOCOL_VERSION),
});
}
records.push(StateRecord::Contract { account_id: account_id.clone(), code });
Expand All @@ -63,7 +64,7 @@ pub fn add_account_with_access_key(
let records = genesis.force_read_records().as_mut();
records.push(StateRecord::Account {
account_id: account_id.clone(),
account: Account::new(balance, 0, 0, Default::default(), 0),
account: Account::new(balance, 0, 0, Default::default(), 0, PROTOCOL_VERSION),
});
records.push(StateRecord::AccessKey { account_id, public_key, access_key });
}
12 changes: 10 additions & 2 deletions tools/amend-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use near_primitives::utils;
use near_primitives::version::ProtocolVersion;
use near_primitives_core::account::{AccessKey, Account};
use near_primitives_core::types::{Balance, BlockHeightDelta, NumBlocks, NumSeats, NumShards};
use near_primitives_core::version::PROTOCOL_VERSION;
use num_rational::Rational32;
use serde::ser::{SerializeSeq, Serializer};
use std::collections::{hash_map, HashMap};
Expand Down Expand Up @@ -73,8 +74,14 @@ impl AccountRecords {
num_bytes_account: u64,
) {
assert!(self.account.is_none());
let account =
Account::new(amount, locked, nonrefundable, CryptoHash::default(), num_bytes_account);
let account = Account::new(
amount,
locked,
nonrefundable,
CryptoHash::default(),
num_bytes_account,
PROTOCOL_VERSION,
);
self.account = Some(account);
}

Expand Down Expand Up @@ -473,6 +480,7 @@ mod test {
nonrefundable_balance,
CryptoHash::default(),
*storage_usage,
PROTOCOL_VERSION,
);
StateRecord::Account { account_id: account_id.parse().unwrap(), account }
}
Expand Down
1 change: 1 addition & 0 deletions tools/fork-network/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ impl ForkNetworkCommand {
0,
CryptoHash::default(),
storage_bytes,
PROTOCOL_VERSION,
),
)?;
storage_mutator.set_access_key(
Expand Down

0 comments on commit e29955d

Please sign in to comment.