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

Fixing test case warnings + making tests safer #35

Merged
merged 1 commit into from
Mar 12, 2024
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
28 changes: 17 additions & 11 deletions pallets/subspace/tests/registration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod mock;

use frame_support::{assert_err, assert_ok};
use mock::*;
use sp_core::U256;

use pallet_subspace::Error;

/********************************************
subscribing::subscribe() tests
*********************************************/
Expand All @@ -29,7 +32,7 @@ fn test_min_stake() {
let min_stake_to_register = SubspaceModule::get_min_stake(netuid);

for key in keys_list {
register_module(netuid, key, min_stake_to_register);
let _ = register_module(netuid, key, min_stake_to_register);
println!("registered module with key: {key:?} and min_stake_to_register: {min_stake_to_register:?}");
}
let registrations_this_block = SubspaceModule::get_registrations_this_block();
Expand Down Expand Up @@ -160,13 +163,16 @@ fn test_registration_with_stake() {
});
}

// #[test]
// fn register_same_key_twice() {
// new_test_ext().execute_with(|| {
// let netuid = 0;
// let stake = 10;
// let key = U256::from(1);
// register_module(netuid, key, stake).expect("register module failed");
// register_module(netuid, key, stake).expect("register module failed");
// });
// }
#[test]
fn register_same_key_twice() {
new_test_ext().execute_with(|| {
let netuid = 0;
let stake = 10;
let key = U256::from(1);
assert_ok!(register_module(netuid, key, stake));
assert_err!(
register_module(netuid, key, stake),
Error::<Test>::KeyAlreadyRegistered
);
});
}
77 changes: 62 additions & 15 deletions pallets/subspace/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ fn test_stake() {
assert_eq!(SubspaceModule::get_balance(key), 1);

// REMOVE STAKE
SubspaceModule::remove_stake(get_origin(*key), netuid, *key, amount_staked);
assert_ok!(SubspaceModule::remove_stake(
get_origin(*key),
netuid,
*key,
amount_staked
));
assert_eq!(SubspaceModule::get_balance(key), amount_staked + 1);
assert_eq!(SubspaceModule::get_stake(netuid, key), 0);

// ADD STAKE AGAIN LOL
SubspaceModule::add_stake(get_origin(*key), netuid, *key, amount_staked);
assert_ok!(SubspaceModule::add_stake(
get_origin(*key),
netuid,
*key,
amount_staked
));
assert_eq!(SubspaceModule::get_stake(netuid, key), amount_staked);
assert_eq!(SubspaceModule::get_balance(key), 1);

Expand Down Expand Up @@ -124,12 +134,12 @@ fn test_multiple_stake() {
println!("TOTAL ACTUAL STAKE: {}", total_actual_stake);
println!("TOTAL STAKE: {}", total_stake);
println!("STAKER BALANCE: {}", staker_balance);
SubspaceModule::add_stake_multiple(
assert_ok!(SubspaceModule::add_stake_multiple(
get_origin(controler_key),
netuid,
keys.clone(),
stake_amounts.clone(),
);
));

let total_actual_stake: u64 =
keys.clone().into_iter().map(|k| SubspaceModule::get_stake(netuid, &k)).sum();
Expand All @@ -146,12 +156,12 @@ fn test_multiple_stake() {
);

// unstake from all modules
SubspaceModule::remove_stake_multiple(
assert_ok!(SubspaceModule::remove_stake_multiple(
get_origin(controler_key),
netuid,
keys.clone(),
stake_amounts.clone(),
);
));

let total_actual_stake: u64 =
keys.clone().into_iter().map(|k| SubspaceModule::get_stake(netuid, &k)).sum();
Expand Down Expand Up @@ -183,14 +193,26 @@ fn test_transfer_stake() {

let keys: Vec<U256> = SubspaceModule::get_keys(netuid);

SubspaceModule::transfer_stake(get_origin(keys[0]), netuid, keys[0], keys[1], stake_amount);
assert_ok!(SubspaceModule::transfer_stake(
get_origin(keys[0]),
netuid,
keys[0],
keys[1],
stake_amount
));

let key0_stake = SubspaceModule::get_stake(netuid, &keys[0]);
let key1_stake = SubspaceModule::get_stake(netuid, &keys[1]);
assert_eq!(key0_stake, 0);
assert_eq!(key1_stake, stake_amount * 2);

SubspaceModule::transfer_stake(get_origin(keys[0]), netuid, keys[1], keys[0], stake_amount);
assert_ok!(SubspaceModule::transfer_stake(
get_origin(keys[0]),
netuid,
keys[1],
keys[0],
stake_amount
));

let key0_stake = SubspaceModule::get_stake(netuid, &keys[0]);
let key1_stake = SubspaceModule::get_stake(netuid, &keys[1]);
Expand Down Expand Up @@ -228,15 +250,20 @@ fn test_delegate_stake() {
let delegate_key: U256 = delegate_key_vector[i];
add_balance(delegate_key, amount_staked + 1);

register_module(netuid, *key, 0);
assert_ok!(register_module(netuid, *key, 0));
// add_stake_and_balance(netuid, *key, amount_staked);
println!(
" DELEGATE KEY STAKE {} STAKING AMOUNT {} ",
SubspaceModule::get_stake(netuid, &delegate_key),
amount_staked
);

SubspaceModule::add_stake(get_origin(delegate_key), netuid, *key, amount_staked);
assert_ok!(SubspaceModule::add_stake(
get_origin(delegate_key),
netuid,
*key,
amount_staked
));
let uid = SubspaceModule::get_uid_for_key(netuid, key);
// SubspaceModule::add_stake(get_origin(*key), netuid, amount_staked);
assert_eq!(
Expand All @@ -249,7 +276,12 @@ fn test_delegate_stake() {
1
);
// REMOVE STAKE
SubspaceModule::remove_stake(get_origin(delegate_key), netuid, *key, amount_staked);
assert_ok!(SubspaceModule::remove_stake(
get_origin(delegate_key),
netuid,
*key,
amount_staked
));
assert_eq!(
SubspaceModule::get_balance(&delegate_key),
amount_staked + 1
Expand All @@ -260,8 +292,13 @@ fn test_delegate_stake() {
0
);

// ADD STAKE AGAIN LOL
SubspaceModule::add_stake(get_origin(delegate_key), netuid, *key, amount_staked);
// ADD STAKE AGAIN
assert_ok!(SubspaceModule::add_stake(
get_origin(delegate_key),
netuid,
*key,
amount_staked
));
assert_eq!(
SubspaceModule::get_stake_for_uid(netuid, uid),
amount_staked
Expand Down Expand Up @@ -311,7 +348,12 @@ fn test_ownership_ratio() {
println!("KEY: {}", k);
for (i, d) in delegate_keys.iter().enumerate() {
println!("DELEGATE KEY: {}", d);
SubspaceModule::add_stake(get_origin(*d), netuid, *k, stake_per_module);
assert_ok!(SubspaceModule::add_stake(
get_origin(*d),
netuid,
*k,
stake_per_module
));
let stake_from_vector = SubspaceModule::get_stake_from_vector(netuid, k);
assert_eq!(
stake_from_vector.len(),
Expand Down Expand Up @@ -358,6 +400,11 @@ fn test_min_stake() {

SubspaceModule::set_min_stake(netuid, min_stake - 100);

SubspaceModule::remove_stake(get_origin(keys[0]), netuid, keys[0], 10_000_000_000);
assert_ok!(SubspaceModule::remove_stake(
get_origin(keys[0]),
netuid,
keys[0],
10_000_000_000
));
});
}
64 changes: 32 additions & 32 deletions pallets/subspace/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ fn test_dividends_diff_stake() {
});
}

#[test]
fn test_pruning() {
new_test_ext().execute_with(|| {
// CONSSTANTS
// CONSTANTS
let netuid: u16 = 0;
let n: u16 = 100;
let _blocks_per_epoch_list: u64 = 1;
Expand All @@ -251,58 +252,57 @@ fn test_pruning() {

// SETUP NETWORK
register_n_modules(netuid, n, stake_per_module);

SubspaceModule::set_max_allowed_modules(n);
SubspaceModule::set_tempo(netuid, 1);
SubspaceModule::set_max_allowed_weights(netuid, n);
SubspaceModule::set_min_allowed_weights(netuid, 0);

// for i in 0..n {

// let key: U256 = U256::from(i);
// register_module( netuid, key, stake_per_module );

// }
let voter_idx = 0;
let keys = SubspaceModule::get_keys(netuid);
let _uids = SubspaceModule::get_uids(netuid);

// do a list of ones for weights
let weight_uids: Vec<u16> = (0..n).collect();
// do a list of ones for weights
// Create a list of UIDs excluding the voter_idx
let weight_uids: Vec<u16> = (0..n).filter(|&x| x != voter_idx as u16).collect();

// Create a list of ones for weights, excluding the voter_idx
let mut weight_values: Vec<u16> = weight_uids.iter().map(|_x| 1_u16).collect();

let prune_uid: u16 = n - 1;
weight_values[prune_uid as usize] = 0;
set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone());
let prune_uid: u16 = weight_uids.last().cloned().unwrap_or(0);

if let Some(prune_idx) = weight_uids.iter().position(|&uid| uid == prune_uid) {
weight_values[prune_idx] = 0;
}

set_weights(
netuid,
keys[voter_idx as usize],
weight_uids.clone(),
weight_values.clone(),
);

step_block(tempo);

let lowest_priority_uid: u16 = SubspaceModule::get_lowest_uid(netuid);
assert!(lowest_priority_uid == prune_uid);

let new_key: U256 = U256::from(n + 1);
let lowest_priority_staker_vector: Vec<(U256, u64)> =
SubspaceModule::get_stake_from_vector(netuid, &keys[lowest_priority_uid as usize]);
let lowest_priority_stakers_balance_before: Vec<u64> = lowest_priority_staker_vector
.iter()
.map(|x| SubspaceModule::get_balance_u64(&x.0))
.collect();
assert_ok!(register_module(netuid, new_key, stake_per_module));

for (i, (staker_key, staker_stake)) in lowest_priority_staker_vector.iter().enumerate() {
let expected_balance: u64 = lowest_priority_stakers_balance_before[i] - staker_stake;
let actual_balance: u64 = SubspaceModule::get_balance_u64(staker_key);
assert!(
expected_balance == actual_balance,
"expected_balance: {} != actual_balance: {}",
expected_balance,
actual_balance
);
}
assert_ok!(register_module(netuid, new_key, stake_per_module));

let is_registered: bool = SubspaceModule::key_registered(netuid, &new_key);
assert!(is_registered);
assert!(SubspaceModule::get_subnet_n(netuid) == n);

assert!(
SubspaceModule::get_subnet_n(netuid) == n,
"SubspaceModule::get_subnet_n(netuid): {} != n: {}",
SubspaceModule::get_subnet_n(netuid),
n
);

let is_prune_registered: bool =
SubspaceModule::key_registered(netuid, &keys[prune_uid as usize]);
assert!(!is_prune_registered);

check_network_stats(netuid);
});
}
Expand Down
6 changes: 3 additions & 3 deletions pallets/subspace/tests/subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn test_set_max_allowed_uids_growing() {
let mut max_uids: u16 = 100;
let extra_uids: u16 = 10;
let rounds = 10;
register_module(netuid, U256::from(0), stake);
assert_ok!(register_module(netuid, U256::from(0), stake));
SubspaceModule::set_max_registrations_per_block(max_uids + extra_uids * rounds);
for i in 1..max_uids {
assert_ok!(register_module(netuid, U256::from(i), stake));
Expand All @@ -215,7 +215,7 @@ fn test_set_max_allowed_uids_growing() {
let new_n = old_n + extra_uids * (r - 1);
// print the pruned uids
for uid in old_n + extra_uids * (r - 1)..old_n + extra_uids * r {
register_module(netuid, U256::from(uid), stake);
assert_ok!(register_module(netuid, U256::from(uid), stake));
}

// set max allowed uids to max_uids
Expand Down Expand Up @@ -248,7 +248,7 @@ fn test_set_max_allowed_uids_shrinking() {

let mut n = SubspaceModule::get_subnet_n(netuid);
println!("registering module {}", n);
register_module(netuid, U256::from(0), stake);
assert_ok!(register_module(netuid, U256::from(0), stake));
SubspaceModule::set_max_allowed_uids(netuid, max_uids + extra_uids);
SubspaceModule::set_max_registrations_per_block(max_uids + extra_uids);

Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod mock;
use frame_support::assert_ok;

use mock::*;
use sp_arithmetic::per_things::Percent;

use sp_core::U256;
use sp_std::vec;

Expand Down
Loading