Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
YourUsername committed Mar 12, 2024
1 parent 2414c81 commit 5a48cdd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 65 deletions.
1 change: 0 additions & 1 deletion pallets/subspace/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::voting::AUTHORITY_MODE;
// TODO: deposit events on sets

use super::*;
use frame_support::pallet_prelude::DispatchResult;
Expand Down
9 changes: 1 addition & 8 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ pub mod pallet {
pub max_weight_age: u64, // max age of a weight
pub min_stake: u64, // min stake required
pub name: Vec<u8>,
pub self_vote: bool, //
pub tempo: u16, // how many blocks to wait before rewarding models
pub tempo: u16, // how many blocks to wait before rewarding models
pub trust_ratio: u16,
pub vote_threshold: u16, // out of 100
pub vote_mode: Vec<u8>,
Expand All @@ -374,7 +373,6 @@ pub mod pallet {
vote_threshold: DefaultVoteThreshold::<T>::get(),
vote_mode: DefaultVoteMode::<T>::get(),
trust_ratio: DefaultTrustRatio::<T>::get(),
self_vote: DefaultSelfVote::<T>::get(),
founder_share: DefaultFounderShare::<T>::get(),
incentive_ratio: DefaultIncentiveRatio::<T>::get(),
min_stake: DefaultMinStake::<T>::get(),
Expand Down Expand Up @@ -998,7 +996,6 @@ pub mod pallet {
vote_threshold: default_params.vote_threshold,
vote_mode: default_params.vote_mode.clone(),
trust_ratio: default_params.trust_ratio,
self_vote: default_params.self_vote,
founder_share: default_params.founder_share,
incentive_ratio: default_params.incentive_ratio,
max_weight_age: default_params.max_weight_age,
Expand Down Expand Up @@ -1320,7 +1317,6 @@ pub mod pallet {
max_weight_age: u64,
min_stake: u64,
name: Vec<u8>,
self_vote: bool,
tempo: u16,
trust_ratio: u16,
vote_mode: Vec<u8>,
Expand All @@ -1338,7 +1334,6 @@ pub mod pallet {
params.max_weight_age = max_weight_age;
params.min_stake = min_stake;
params.name = name;
params.self_vote = self_vote;
params.tempo = tempo;
params.trust_ratio = trust_ratio;
params.vote_mode = vote_mode;
Expand Down Expand Up @@ -1366,7 +1361,6 @@ pub mod pallet {
min_allowed_weights: u16,
min_stake: u64,
name: Vec<u8>,
self_vote: bool,
tempo: u16,
trust_ratio: u16,
vote_mode: Vec<u8>,
Expand All @@ -1384,7 +1378,6 @@ pub mod pallet {
params.min_allowed_weights = min_allowed_weights;
params.min_stake = min_stake;
params.name = name;
params.self_vote = self_vote;
params.tempo = tempo;
params.trust_ratio = trust_ratio;
params.vote_mode = vote_mode;
Expand Down
15 changes: 2 additions & 13 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use frame_system::ensure_signed;

use sp_std::vec::Vec;

// IterableStorageMap
use frame_support::storage::IterableStorageMap;

impl<T: Config> Pallet<T> {
pub fn do_register(
origin: T::RuntimeOrigin,
Expand Down Expand Up @@ -185,15 +182,7 @@ impl<T: Config> Pallet<T> {
let max_subnets: u16 = Self::get_global_max_allowed_subnets();
// if we have not reached the max number of subnets, then we can start a new one
if num_subnets >= max_subnets {
let mut min_stake: u64 = u64::MAX; // initially represents the maximum amount that is possible to have
let mut min_stake_netuid: u16 = max_subnets.saturating_sub(1); // initially represents the last netuid

for (netuid, net_stake) in <TotalStake<T> as IterableStorageMap<u16, u64>>::iter() {
if net_stake <= min_stake {
min_stake = net_stake;
min_stake_netuid = netuid;
}
}
let (min_stake_netuid, min_stake) = Self::least_staked_netuid();
// if the stake is greater than the least staked network, then we can start a new one
ensure!(stake > min_stake, Error::<T>::NotEnoughStakeToStartNetwork);
Self::remove_subnet(min_stake_netuid);
Expand All @@ -214,7 +203,7 @@ impl<T: Config> Pallet<T> {
// replace a node if we reach the max allowed modules for the network
if Self::global_n() >= Self::get_max_allowed_modules() {
// get the least staked network (subnet)
let least_staked_netuid: u16 = Self::least_staked_netuid();
let (least_staked_netuid, _) = Self::least_staked_netuid();

// deregister the lowest priority node
Self::remove_module(
Expand Down
5 changes: 1 addition & 4 deletions pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ impl<T: Config> Pallet<T> {
})
.collect();

// enabling self voting (if enabled)
if !subnet_params.self_vote {
weights = mask_diag_sparse(&weights);
}
weights = mask_diag_sparse(&weights);

// Normalize remaining weights.
inplace_row_normalize_sparse(&mut weights);
Expand Down
23 changes: 4 additions & 19 deletions pallets/subspace/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ impl<T: Config> Pallet<T> {
vote_threshold: VoteThresholdSubnet::<T>::get(netuid),
vote_mode: VoteModeSubnet::<T>::get(netuid),
trust_ratio: TrustRatio::<T>::get(netuid),
self_vote: SelfVote::<T>::get(netuid),
founder_share: FounderShare::<T>::get(netuid),
incentive_ratio: IncentiveRatio::<T>::get(netuid),
founder: Founder::<T>::get(netuid),
Expand All @@ -155,7 +154,6 @@ impl<T: Config> Pallet<T> {
Self::set_trust_ratio(netuid, params.trust_ratio);
Self::set_vote_threshold_subnet(netuid, params.vote_threshold);
Self::set_vote_mode_subnet(netuid, params.vote_mode);
Self::set_self_vote(netuid, params.self_vote);
Self::set_incentive_ratio(netuid, params.incentive_ratio);
}

Expand All @@ -177,7 +175,7 @@ impl<T: Config> Pallet<T> {
}

// get the least staked network
pub fn least_staked_netuid() -> u16 {
pub fn least_staked_netuid() -> (u16, u64) {
let mut min_stake: u64 = u64::MAX;
let mut min_stake_netuid: u16 = u16::MAX;
for (netuid, net_stake) in <TotalStake<T> as IterableStorageMap<u16, u64>>::iter() {
Expand All @@ -186,7 +184,7 @@ impl<T: Config> Pallet<T> {
min_stake_netuid = netuid;
}
}
min_stake_netuid
(min_stake_netuid, min_stake)
}

pub fn address_vector(netuid: u16) -> Vec<Vec<u8>> {
Expand Down Expand Up @@ -267,19 +265,6 @@ impl<T: Config> Pallet<T> {
Founder::<T>::get(netuid) == *key
}

pub fn set_self_vote(netuid: u16, self_vote: bool) {
SelfVote::<T>::insert(netuid, self_vote);
}

pub fn get_self_vote(netuid: u16) -> bool {
SelfVote::<T>::get(netuid)
}

pub fn market_cap() -> u64 {
let total_stake: u64 = Self::total_stake();
total_stake
}

pub fn get_unit_emission() -> u64 {
UnitEmission::<T>::get()
}
Expand All @@ -290,7 +275,7 @@ impl<T: Config> Pallet<T> {

// Returns the total amount of stake in the staking table.
pub fn get_total_emission_per_block() -> u64 {
let market_cap: u64 = Self::market_cap();
let total_stake: u64 = Self::total_stake();
let unit_emission: u64 = Self::get_unit_emission();
let mut emission_per_block: u64 = unit_emission; // assuming 8 second block times
let halving_total_stake_checkpoints: Vec<u64> =
Expand All @@ -300,7 +285,7 @@ impl<T: Config> Pallet<T> {
.collect();
for (i, having_stake) in halving_total_stake_checkpoints.iter().enumerate() {
let halving_factor = 2u64.pow((i) as u32);
if market_cap < *having_stake {
if total_stake < *having_stake {
emission_per_block /= halving_factor;
break;
}
Expand Down
24 changes: 9 additions & 15 deletions pallets/subspace/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl<T: Config> Pallet<T> {

let min_allowed_length: usize = Self::get_min_allowed_weights(netuid) as usize;
let max_allowed_length: usize = Self::get_max_allowed_weights(netuid) as usize;
let self_vote = Self::get_self_vote(netuid);
if self_vote {
ensure!(
!Self::is_self_weight(uid, &uids, &values),
Error::<T>::NoSelfWeight
);
}

// TODO: fix this function
ensure!(
!Self::is_self_weight(uid, &uids),
Error::<T>::NoSelfWeight
);

ensure!(
uids.len() >= min_allowed_length,
Error::<T>::NotSettingEnoughWeights
Expand Down Expand Up @@ -136,14 +136,8 @@ impl<T: Config> Pallet<T> {
}

// Returns true if the uids and weights correspond to a self weight on the uid.
pub fn is_self_weight(uid: u16, uids: &[u16], weights: &[u16]) -> bool {
if weights.len() != 1 {
return false;
}
if uid != uids[0] {
return false;
}
true
pub fn is_self_weight(uid: u16, uids: &[u16]) -> bool {
uids.contains(&uid)
}

#[cfg(debug_assertions)]
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/tests/subnets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn test_global_max_allowed_subnets() {
for i in 1..(2 * max_allowed_subnets) {
let netuid = i;
stake += i as u64;
let least_staked_netuid = SubspaceModule::least_staked_netuid();
let (least_staked_netuid, _) = SubspaceModule::least_staked_netuid();

if i > 1 {
println!("least staked netuid {}", least_staked_netuid);
Expand Down
7 changes: 3 additions & 4 deletions pallets/subspace/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn test_is_self_weight_weights_length_not_one() {
let weights: Vec<u16> = Vec::from_iter((0..max_allowed).map(|id| id + 1));

let expected = false;
let result = SubspaceModule::is_self_weight(uid, &uids, &weights);
let result = SubspaceModule::is_self_weight(uid, &uids);

assert_eq!(
expected, result,
Expand All @@ -241,7 +241,7 @@ fn test_is_self_weight_uid_not_in_uids() {
let weights: Vec<u16> = vec![0];

let expected = false;
let result = SubspaceModule::is_self_weight(uid, &uids, &weights);
let result = SubspaceModule::is_self_weight(uid, &uids);

assert_eq!(
expected, result,
Expand All @@ -259,10 +259,9 @@ fn test_is_self_weight_uid_in_uids() {

let uids: Vec<u16> = Vec::from_iter((0..max_allowed).map(|id| id + 1));
let uid: u16 = uids[0];
let weights: Vec<u16> = vec![0];

let expected = true;
let result = SubspaceModule::is_self_weight(uid, &uids, &weights);
let result = SubspaceModule::is_self_weight(uid, &uids);

assert_eq!(
expected, result,
Expand Down

0 comments on commit 5a48cdd

Please sign in to comment.