Skip to content

Commit

Permalink
fixed redundant wrong check !
Browse files Browse the repository at this point in the history
  • Loading branch information
Supremesource committed Mar 4, 2024
1 parent b811f58 commit 6c5c99e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
20 changes: 6 additions & 14 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
// --- 1. Check that the caller has signed the transaction.
let key = ensure_signed(origin.clone())?;

// --- 2. Ensure, that we are not exceeding the max allowed
// registrations per block.
ensure!(
Expand All @@ -45,12 +44,12 @@ impl<T: Config> Pallet<T> {

// --- 5. Ensure the caller has enough stake to register.
let min_stake: u64 = MinStake::<T>::get(netuid);
let min_burn: u64 = Self::get_burn();
let current_burn: u64 = Self::get_burn();

// also ensures that in the case min_burn is present, the stake is enough
// also ensures that in the case current_burn is present, the stake is enough
// as burn, will be decreased from the stake on the module
ensure!(
Self::enough_stake_to_register(netuid, min_stake, min_burn, stake_amount),
Self::enough_stake_to_register(netuid, min_stake, current_burn, stake_amount),
Error::<T>::NotEnoughStakeToRegister
);

Expand All @@ -76,17 +75,10 @@ impl<T: Config> Pallet<T> {
// --- 9. Add the stake to the module, now that it is registered on the network.
Self::do_add_stake(origin, netuid, module_key.clone(), stake_amount)?;

// constant -> min_burn logic
if min_burn > 0 {
// constant -> current_burn logic
if current_burn > 0 {
// if min burn is present, decrease the stake by the min burn
Self::decrease_stake(netuid, &key, &module_key, min_burn);

// Ensure that the stake decreased after the burn.
let current_stake: u64 = Self::get_total_stake_to(netuid, &key);
ensure!(
current_stake == stake_amount.saturating_sub(min_burn),
Error::<T>::NotEnoughStakeToRegister
);
Self::decrease_stake(netuid, &key, &module_key, current_burn);
}

// Make sure that the registration went through.
Expand Down
4 changes: 2 additions & 2 deletions pallets/subspace/tests/profit_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn test_add_profit_share() {
let netuid = 0;
let miner_key = U256::from(0);
let voter_key = U256::from(1);
register_module(netuid, miner_key, 1_000_000_000);
register_module(netuid, voter_key, 1_000_000_000);
assert_ok!(register_module(netuid, miner_key, 1_000_000_000));
assert_ok!(register_module(netuid, voter_key, 1_000_000_000));
let miner_uid = SubspaceModule::get_uid_for_key(netuid, &miner_key);
let _voter_uid = SubspaceModule::get_uid_for_key(netuid, &voter_key);

Expand Down
34 changes: 18 additions & 16 deletions pallets/subspace/tests/registration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod mock;

use frame_support::assert_ok;
use mock::*;
use sp_core::U256;

Expand All @@ -18,7 +19,7 @@ fn test_min_stake() {
let max_registrations_per_block = 10;
let reg_this_block: u16 = 100;

register_module(netuid, U256::from(0), 0);
assert_ok!(register_module(netuid, U256::from(0), 0));
SubspaceModule::set_min_stake(netuid, min_stake);
SubspaceModule::set_max_registrations_per_block(max_registrations_per_block);
step_block(1);
Expand All @@ -32,7 +33,8 @@ 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);
// some of these are supposed to fail
let _ = register_module(netuid, key, min_stake_to_register);
println!(
"Registered module with key: {:?} and min_stake_to_register: {:?}",
key, min_stake_to_register
Expand Down Expand Up @@ -72,7 +74,7 @@ fn test_max_registration() {
"min_stake_to_register: {:?} min_stake: {:?} factor {:?}",
min_stake_to_register, min_stake, factor
);
register_module(netuid, key, factor * min_stake);
assert_ok!(register_module(netuid, key, factor * min_stake));
let registrations_this_block = SubspaceModule::get_registrations_this_block();
assert_eq!(registrations_this_block, i);
assert!(SubspaceModule::is_registered(netuid, &key));
Expand Down Expand Up @@ -114,7 +116,7 @@ fn test_registration_ok() {
let _tempo: u16 = 13;
let key: U256 = U256::from(1);

register_module(netuid, key, 0);
assert_ok!(register_module(netuid, key, 0));
// Check if neuron has added to the specified network(netuid)
assert_eq!(SubspaceModule::get_subnet_n(netuid), 1);

Expand All @@ -138,7 +140,7 @@ fn test_many_registrations() {
let n = 100;
SubspaceModule::set_max_registrations_per_block(n);
for i in 0..n {
register_module(netuid, U256::from(i), stake);
assert_ok!(register_module(netuid, U256::from(i), stake));
assert_eq!(
SubspaceModule::get_subnet_n(netuid),
i + 1,
Expand All @@ -165,20 +167,20 @@ fn test_registration_with_stake() {
println!("stake: {:?}", stake_value);
let stake_before: u64 = SubspaceModule::get_stake(netuid, &key);
println!("stake_before: {:?}", stake_before);
register_module(netuid, key, stake_value);
assert_ok!(register_module(netuid, key, stake_value));
println!("balance: {:?}", SubspaceModule::get_balance_u64(&key));
assert_eq!(SubspaceModule::get_stake_for_uid(netuid, uid), stake_value);
}
});
}

#[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);
register_module(netuid, key, stake);
});
}
// #[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));
// register_module(netuid, key, stake);
// });
// }
3 changes: 2 additions & 1 deletion pallets/subspace/tests/staking.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod mock;

use frame_support::assert_ok;
use mock::*;
use sp_core::U256;
use substrate_fixed::types::I64F64;
Expand Down Expand Up @@ -55,7 +56,7 @@ fn test_stake() {
amount_staked
);

register_module(netuid, *key, amount_staked);
assert_ok!(register_module(netuid, *key, amount_staked));
// add_stake_and_balance(netuid, *key, amount_staked);
println!(
" KEY STAKE {} STAKING AMOUNT {} ",
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 @@ -18,12 +18,12 @@ fn test_add_subnets() {
let num_subnets: u16 = n;

for i in 0..num_subnets {
register_module(i, U256::from(i), stake_per_module);
assert_ok!(register_module(i, U256::from(i), stake_per_module));
for j in 0..n {
if j != i {
let n = SubspaceModule::get_subnet_n(i);
println!("registering module i:{} j:{} n:{}", i, j, n);
register_module(i, U256::from(j), stake_per_module);
assert_ok!(register_module(i, U256::from(j), stake_per_module));
}
}
expected_subnets += 1;
Expand Down Expand Up @@ -78,7 +78,7 @@ fn test_set_single_temple(tempo: u16) {
let stake: u64 = 0;
let key = U256::from(0);
let _tempos: Vec<u16> = vec![2, 4];
register_module(netuid, key, stake);
assert_ok!(register_module(netuid, key, stake));
let mut params = SubspaceModule::subnet_params(netuid).clone();
params.tempo = tempo;

Expand Down
32 changes: 16 additions & 16 deletions pallets/subspace/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_weights_err_weights_vec_not_equal_size() {
new_test_ext().execute_with(|| {
let netuid: u16 = 0;
let key_account_id = U256::from(55);
register_module(netuid, key_account_id, 1_000_000_000);
assert_ok!(register_module(netuid, key_account_id, 1_000_000_000));
let _neuron_uid: u16 = SubspaceModule::get_uid_for_key(netuid, &key_account_id);
let weights_keys: Vec<u16> = vec![1, 2, 3, 4, 5, 6];
let weight_values: Vec<u16> = vec![1, 2, 3, 4, 5]; // Uneven sizes
Expand All @@ -39,19 +39,19 @@ fn test_weights_err_has_duplicate_ids() {
let netuid: u16 = 0;
SubspaceModule::set_max_registrations_per_block(100);

register_module(netuid, key_account_id, 10);
assert_ok!(register_module(netuid, key_account_id, 10));
SubspaceModule::set_max_allowed_uids(netuid, 100); // Allow many registrations per block.

// uid 1
register_module(netuid, U256::from(1), 100);
assert_ok!(register_module(netuid, U256::from(1), 100));
SubspaceModule::get_uid_for_key(netuid, &U256::from(1));

// uid 2
register_module(netuid, U256::from(2), 10000);
assert_ok!(register_module(netuid, U256::from(2), 10000));
SubspaceModule::get_uid_for_key(netuid, &U256::from(2));

// uid 3
register_module(netuid, U256::from(3), 10000000);
assert_ok!(register_module(netuid, U256::from(3), 10000000));
SubspaceModule::get_uid_for_key(netuid, &U256::from(3));

assert_eq!(SubspaceModule::get_subnet_n(netuid), 4);
Expand Down Expand Up @@ -85,7 +85,7 @@ fn test_set_weights_err_invalid_uid() {
new_test_ext().execute_with(|| {
let key_account_id = U256::from(55);
let netuid: u16 = 0;
register_module(netuid, key_account_id, 1_000_000_000);
assert_ok!(register_module(netuid, key_account_id, 1_000_000_000));
let _neuron_uid: u16 = SubspaceModule::get_uid_for_key(netuid, &key_account_id);
let weight_keys: Vec<u16> = vec![9999]; // Does not exist
let weight_values: Vec<u16> = vec![88]; // random value
Expand All @@ -107,10 +107,10 @@ fn test_set_weight_not_enough_values() {
let n = 100;
SubspaceModule::set_max_registrations_per_block(n);
let account_id = U256::from(0);
register_module(netuid, account_id, 1_000_000_000);
assert_ok!(register_module(netuid, account_id, 1_000_000_000));
let _neuron_uid: u16 = SubspaceModule::get_uid_for_key(netuid, &account_id);
for i in 1..n {
register_module(netuid, U256::from(i), 1_000_000_000);
assert_ok!(register_module(netuid, U256::from(i), 1_000_000_000));
}

SubspaceModule::set_min_allowed_weights(netuid, 2);
Expand Down Expand Up @@ -159,10 +159,10 @@ fn test_set_max_allowed_uids() {
let n = 100;
SubspaceModule::set_max_registrations_per_block(n);
let account_id = U256::from(0);
register_module(netuid, account_id, 1_000_000_000);
assert_ok!(register_module(netuid, account_id, 1_000_000_000));
let _neuron_uid: u16 = SubspaceModule::get_uid_for_key(netuid, &account_id);
for i in 1..n {
register_module(netuid, U256::from(i), 1_000_000_000);
assert_ok!(register_module(netuid, U256::from(i), 1_000_000_000));
}

let max_allowed_uids: u16 = 10;
Expand Down Expand Up @@ -281,9 +281,9 @@ fn test_check_len_uids_within_allowed_within_network_pool() {
SubspaceModule::set_max_registrations_per_block(100);

/* @TODO: use a loop maybe */
register_module(netuid, U256::from(1), 1_000_000_000);
register_module(netuid, U256::from(3), 1_000_000_000);
register_module(netuid, U256::from(5), 1_000_000_000);
assert_ok!(register_module(netuid, U256::from(1), 1_000_000_000));
assert_ok!(register_module(netuid, U256::from(3), 1_000_000_000));
assert_ok!(register_module(netuid, U256::from(5), 1_000_000_000));
let max_allowed: u16 = SubspaceModule::get_subnet_n(netuid);

let uids: Vec<u16> = Vec::from_iter(0..max_allowed);
Expand All @@ -305,9 +305,9 @@ fn test_check_len_uids_within_allowed_not_within_network_pool() {
SubspaceModule::set_max_registrations_per_block(100);

/* @TODO: use a loop maybe */
register_module(netuid, U256::from(1), 1_000_000_000);
register_module(netuid, U256::from(3), 1_000_000_000);
register_module(netuid, U256::from(5), 1_000_000_000);
assert_ok!( register_module(netuid, U256::from(1), 1_000_000_000));
assert_ok!(register_module(netuid, U256::from(3), 1_000_000_000));
assert_ok!(register_module(netuid, U256::from(5), 1_000_000_000));
let max_allowed: u16 = SubspaceModule::get_subnet_n(netuid);

SubspaceModule::set_max_allowed_uids(netuid, max_allowed);
Expand Down

0 comments on commit 6c5c99e

Please sign in to comment.