Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Feb 15, 2024
1 parent 38a7937 commit f49adef
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
44 changes: 29 additions & 15 deletions chain/rosetta-rpc/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,35 @@ mod tests {
use near_primitives::action::delegate::{DelegateAction, SignedDelegateAction};
use near_primitives::transaction::{Action, TransferAction};

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
#[test]
fn test_convert_nonrefundable_storage_transfer_action() {
let transfer_actions = vec![near_primitives::transaction::TransferAction {
deposit: near_primitives::types::Balance::MAX,
}
.into()];
let nonrefundable_transfer_actions =
vec![near_primitives::transaction::NonrefundableStorageTransferAction {
deposit: near_primitives::types::Balance::MAX,
}
.into()];
let near_transfer_actions = NearActions {
sender_account_id: "sender.near".parse().unwrap(),
receiver_account_id: "receiver.near".parse().unwrap(),
actions: transfer_actions,
};
let near_nonrefundable_transfer_actions = NearActions {
sender_account_id: "sender.near".parse().unwrap(),
receiver_account_id: "receiver.near".parse().unwrap(),
actions: nonrefundable_transfer_actions,
};
let transfer_operations_converted: Vec<crate::models::Operation> =
near_transfer_actions.into();
let nonrefundable_transfer_operations_converted: Vec<crate::models::Operation> =
near_nonrefundable_transfer_actions.into();
assert_eq!(transfer_operations_converted, nonrefundable_transfer_operations_converted);
}

#[test]
fn test_convert_block_changes_to_transactions() {
run_actix(async {
Expand Down Expand Up @@ -1034,12 +1063,6 @@ mod tests {
deposit: near_primitives::types::Balance::MAX,
}
.into()];
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let nonrefundable_transfer_actions =
vec![near_primitives::transaction::NonrefundableStorageTransferAction {
deposit: near_primitives::types::Balance::MAX,
}
.into()];
let stake_actions = vec![near_primitives::transaction::StakeAction {
stake: 456,
public_key: near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519)
Expand Down Expand Up @@ -1070,13 +1093,6 @@ mod tests {
let wallet_style_create_account_actions =
[create_account_actions.to_vec(), add_key_actions.to_vec(), transfer_actions.to_vec()]
.concat();
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let wallet_style_create_account_with_nonrefundable_actions = [
create_account_actions.to_vec(),
add_key_actions.to_vec(),
nonrefundable_transfer_actions.to_vec(),
]
.concat();
let create_account_and_stake_immediately_actions =
[create_account_actions.to_vec(), transfer_actions.to_vec(), stake_actions.to_vec()]
.concat();
Expand All @@ -1102,8 +1118,6 @@ mod tests {
function_call_without_balance_actions,
function_call_with_balance_actions,
wallet_style_create_account_actions,
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
wallet_style_create_account_with_nonrefundable_actions,
create_account_and_stake_immediately_actions,
deploy_contract_and_call_it_actions,
two_factor_auth_actions,
Expand Down
19 changes: 19 additions & 0 deletions core/chain-configs/src/genesis_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ mod test {
Account::new(100, 10, 0, Default::default(), 0, PROTOCOL_VERSION)
}

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
#[test]
fn test_total_supply_includes_nonrefundable_amount() {
let mut config = GenesisConfig::default();
config.epoch_length = 42;
config.total_supply = 111;
config.validators = vec![AccountInfo {
account_id: "test".parse().unwrap(),
public_key: VALID_ED25519_RISTRETTO_KEY.parse().unwrap(),
amount: 10,
}];
let records = GenesisRecords(vec![StateRecord::Account {
account_id: "test".parse().unwrap(),
account: Account::new(100, 10, 1, Default::default(), 0, PROTOCOL_VERSION),
}]);
let genesis = &Genesis::new(config, records).unwrap();
validate_genesis(genesis).unwrap();
}

#[test]
#[should_panic(expected = "wrong total supply")]
fn test_total_supply_not_match() {
Expand Down
29 changes: 29 additions & 0 deletions core/primitives-core/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,23 @@ pub struct FunctionCallPermission {
mod tests {

use crate::hash::hash;
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
use crate::version::ProtocolFeature;

use super::*;

#[test]
#[should_panic]
fn test_v1_account_cannot_have_nonrefundable_amount() {
#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
let protocol_version = crate::version::PROTOCOL_VERSION;

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let protocol_version = ProtocolFeature::NonRefundableBalance.protocol_version() - 1;

Account::new(0, 0, 1, CryptoHash::default(), 0, protocol_version);
}

#[test]
fn test_legacy_account_serde_serialization() {
let old_account = LegacyAccount {
Expand Down Expand Up @@ -574,6 +588,21 @@ mod tests {
assert_eq!(deserialized_account, account);
}

#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
#[test]
#[should_panic(expected = "account serialization sentinel not allowed for AccountV1")]
fn test_account_v1_borsh_serialization_sentinel() {
let account = Account {
amount: Account::SERIALIZATION_SENTINEL,
locked: 1_000_000,
code_hash: CryptoHash::default(),
storage_usage: 100,
version: AccountVersion::V1,
};
let serialized_account = borsh::to_vec(&account).unwrap();
<Account as BorshDeserialize>::deserialize(&mut &serialized_account[..]).unwrap();
}

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
/// It is impossible to construct V1 account with nonrefundable amount greater than 0.
/// So the situation in this test is theoretical.
Expand Down

0 comments on commit f49adef

Please sign in to comment.