Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Jan 7, 2025
1 parent 44d8a50 commit 7b40bac
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 317 deletions.
6 changes: 3 additions & 3 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,9 +958,9 @@ pub mod pallet {
Ok(())
}

/// The extrinsic sets the target stake per interval.
/// It is only callable by the root account.
/// The extrinsic will call the Subtensor pallet to set target stake per interval.
// The extrinsic sets the target stake per interval.
// It is only callable by the root account.
// The extrinsic will call the Subtensor pallet to set target stake per interval.
// #[pallet::call_index(47)]
// #[pallet::weight((0, DispatchClass::Operational, Pays::No))]
// pub fn sudo_set_target_stakes_per_interval(
Expand Down
12 changes: 7 additions & 5 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ benchmarks! {
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);

assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));
}: add_stake(RawOrigin::Signed( coldkey.clone() ), hotkey, amount)
}: add_stake(RawOrigin::Signed( coldkey.clone() ), hotkey, netuid, amount)

benchmark_remove_stake{
let caller: T::AccountId = whitelisted_caller::<AccountIdOf<T>>();
Expand Down Expand Up @@ -161,10 +161,10 @@ benchmarks! {
let u64_staked_amt = 100_000_000_000;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), u64_staked_amt);

assert_ok!( Subtensor::<T>::add_stake(RawOrigin::Signed( coldkey.clone() ).into() , hotkey.clone(), u64_staked_amt));
assert_ok!( Subtensor::<T>::add_stake(RawOrigin::Signed( coldkey.clone() ).into() , hotkey.clone(), netuid, u64_staked_amt));

let amount_unstaked: u64 = u64_staked_amt - 1;
}: remove_stake(RawOrigin::Signed( coldkey.clone() ), hotkey.clone(), amount_unstaked)
}: remove_stake(RawOrigin::Signed( coldkey.clone() ), hotkey.clone(), netuid, amount_unstaked)

benchmark_serve_axon{
let caller: T::AccountId = whitelisted_caller::<AccountIdOf<T>>();
Expand Down Expand Up @@ -289,25 +289,27 @@ benchmarks! {
let seed : u32 = 1;

let coldkey: T::AccountId = account("Test", 0, seed);
let hotkey: T::AccountId = account("TestHotkey", 0, seed);

Subtensor::<T>::set_network_rate_limit(1);

let amount: u64 = 1;
let amount_to_be_staked = 100_000_000_000_000u64;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
}: register_network(RawOrigin::Signed(coldkey))
}: register_network(RawOrigin::Signed(coldkey), hotkey.clone())

benchmark_dissolve_network {
let seed : u32 = 1;

let coldkey: T::AccountId = account("Test", 0, seed);
let hotkey: T::AccountId = account("TestHotkey", 0, seed);

Subtensor::<T>::set_network_rate_limit(0);

let amount: u64 = 1;
let amount_to_be_staked = 100_000_000_000_000u64;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone()));
}: dissolve_network(RawOrigin::Root, coldkey.clone(), 1)


Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T: Config> Pallet<T> {
let mechid: u16 = SubnetMechanism::<T>::get(*netuid);
log::debug!("{:?} - mechid: {:?}", netuid, mechid);
// 6.2: Get the subnet emission TAO.
let subnet_emission: u64 = *tao_in_map.get(&netuid).unwrap_or(&0);
let subnet_emission: u64 = *tao_in_map.get(netuid).unwrap_or(&0);
log::debug!("{:?} subnet_emission: {:?}", netuid, subnet_emission );
if mechid == 0 {
// The mechanism is Stable (FOR TESTING PURPOSES ONLY)
Expand Down
478 changes: 239 additions & 239 deletions pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pallets/subtensor/src/rpc_info/dynamic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl<T: Config> Pallet<T> {
let blocks_since_last_step: u64 = current_block.saturating_sub(last_step);
Some(DynamicInfo {
netuid: netuid.into(),
owner_hotkey: SubnetOwnerHotkey::<T>::get(netuid).into(),
owner_coldkey: SubnetOwner::<T>::get(netuid).into(),
owner_hotkey: SubnetOwnerHotkey::<T>::get(netuid),
owner_coldkey: SubnetOwner::<T>::get(netuid),
subnet_name: Self::get_name_for_subnet(netuid).into_iter().map(Compact).collect(),
token_symbol: Self::get_symbol_for_subnet(netuid).into_iter().map(Compact).collect(),
tempo: Tempo::<T>::get(netuid).into(),
Expand Down
34 changes: 17 additions & 17 deletions pallets/subtensor/src/rpc_info/show_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<T: Config> Pallet<T> {
for hotkey in hotkeys.clone() {
let last_emission: Compact<u64> =
LastHotkeyEmissionOnNetuid::<T>::get(hotkey.clone(), netuid).into();
hotkeys_emissions.push(last_emission.into());
hotkeys_emissions.push(last_emission);
}
result.push(hotkeys_emissions.clone());
}
Expand Down Expand Up @@ -146,22 +146,22 @@ impl<T: Config> Pallet<T> {
let emission_history: Vec<Vec<Compact<u64>>> = Self::get_emissions_history(hotkeys.clone());
Some(SubnetState {
netuid: netuid.into(),
hotkeys: hotkeys.into(),
coldkeys: coldkeys.into(),
active: active.into(),
validator_permit: validator_permit.into(),
pruning_score: pruning_score.into(),
last_update: last_update.into(),
emission: emission.into(),
dividends: dividends.into(),
incentives: incentives.into(),
consensus: consensus.into(),
trust: trust.into(),
rank: rank.into(),
block_at_registration: block_at_registration.into(),
alpha_stake: alpha_stake.into(),
tao_stake: tao_stake.into(),
total_stake: total_stake.into(),
hotkeys,
coldkeys,
active,
validator_permit,
pruning_score,
last_update,
emission,
dividends,
incentives,
consensus,
trust,
rank,
block_at_registration,
alpha_stake,
tao_stake,
total_stake,
emission_history,
})
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/rpc_info/stake_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl<T: Config> Pallet<T> {
let mut stake_info_for_coldkey: Vec<StakeInfo<T>> = Vec::new();
for netuid_i in netuids.clone().iter() {
for hotkey_i in staking_hotkeys.clone().iter() {
let alpha: u64 = Self::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_i, &coldkey_i, *netuid_i);
let alpha: u64 = Self::get_stake_for_hotkey_and_coldkey_on_subnet( hotkey_i, coldkey_i, *netuid_i);
let emission: u64 = HotkeyDividendsPerSubnet::<T>::get( *netuid_i, &hotkey_i);
let is_registered: bool = Self::is_hotkey_registered_on_network(*netuid_i, &hotkey_i);
let is_registered: bool = Self::is_hotkey_registered_on_network(*netuid_i, hotkey_i);
stake_info_for_coldkey.push(StakeInfo {
hotkey: hotkey_i.clone(),
coldkey: coldkey_i.clone(),
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<T: Config> Pallet<T> {
let tao_unstaked: u64 = Self::unstake_from_subnet(&hotkey, &coldkey, *netuid, alpha_unstaked);

// Increment total
total_tao_unstaked += tao_unstaked;
total_tao_unstaked = total_tao_unstaked.saturating_add(tao_unstaked);

// If the stake is below the minimum, we clear the nomination from storage.
Self::clear_small_nomination_if_required(&hotkey, &coldkey, *netuid);
Expand Down
60 changes: 28 additions & 32 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use frame_support::IterableStorageDoubleMap;
use share_pool::{SharePool, SharePoolDataOperations};
use sp_std::ops::Neg;
use substrate_fixed::types::{I64F64, I96F32};
Expand All @@ -18,7 +17,8 @@ impl<T: Config> Pallet<T> {
/// # Returns
/// * `u64` - The total alpha issuance for the specified subnet.
pub fn get_alpha_issuance(netuid: u16) -> u64 {
return SubnetAlphaIn::<T>::get(netuid) + SubnetAlphaOut::<T>::get(netuid);
SubnetAlphaIn::<T>::get(netuid)
.saturating_add(SubnetAlphaOut::<T>::get(netuid))
}

/// Calculates the price of alpha for a given subnet.
Expand All @@ -33,13 +33,17 @@ impl<T: Config> Pallet<T> {
/// # Returns
/// * `I96F32` - The price of alpha for the specified subnet.
pub fn get_alpha_price(netuid: u16) -> I96F32 {
if SubnetAlphaIn::<T>::get(netuid) == 0 {
return I96F32::from_num(0);
} else{
return I96F32::from_num(SubnetTAO::<T>::get(netuid) )/ I96F32::from_num(SubnetAlphaIn::<T>::get(netuid));
}
I96F32::from_num(
if SubnetAlphaIn::<T>::get(netuid) == 0 {
0
} else {
SubnetTAO::<T>::get(netuid)
}
)
.checked_div(I96F32::from_num(SubnetAlphaIn::<T>::get(netuid)))
.unwrap_or(I96F32::from_num(0))
}

/// Retrieves the global global weight as a normalized value between 0 and 1.
///
/// This function performs the following steps:
Expand Down Expand Up @@ -92,36 +96,28 @@ impl<T: Config> Pallet<T> {
/// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet.
///
pub fn get_stake_weights_for_network(netuid: u16) -> (Vec<I64F64>, Vec<I64F64>, Vec<I64F64>) {
// Step 1: Get the subnet size (number of neurons).
let n: u16 = Self::get_subnetwork_n(netuid);

// Step 2: Retrieve all hotkeys (neuron keys) on this subnet.
let hotkeys: Vec<(u16, T::AccountId)> = <Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid).collect();
// Retrieve the global global weight.
let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight());

// Step 3: Calculate
let mut alpha_stake: Vec<I64F64> = vec![I64F64::from_num(0); n as usize];
for (uid_i, hotkey) in &hotkeys {
let alpha: u64 = Self::get_inherited_for_hotkey_on_subnet(hotkey, netuid);
alpha_stake[*uid_i as usize] = I64F64::from_num(alpha);
}
// Step 1: Retrieve all hotkeys (neuron keys) on this subnet.
// Step 2: Get stake of all hotkeys (neurons)
let alpha_stake: Vec<I64F64> = Keys::<T>::iter_prefix(netuid)
.map(|(_uid, hotkey)| I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, netuid)))
.collect();

// Step 4: Calculate the global tao stake vector.
// Step 3: Calculate the global tao stake vector.
// Initialize a vector to store global tao stakes for each neuron.
let mut tao_stake: Vec<I64F64> = vec![I64F64::from_num(0); n as usize];
for (uid_i, hotkey) in &hotkeys {
let tao: u64 = Self::get_inherited_for_hotkey_on_subnet(hotkey, 0);
tao_stake[*uid_i as usize] = I64F64::from_num(tao);
}
let tao_stake: Vec<I64F64> = Keys::<T>::iter_prefix(netuid)
.map(|(_uid, hotkey)| I64F64::from_num(Self::get_inherited_for_hotkey_on_subnet(&hotkey, 0)))
.collect();

// Step 5: Combine alpha and root tao stakes.
// Retrieve the global global weight.
let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight());
// Step 4: Combine alpha and root tao stakes.
// Calculate the weighted average of alpha and global tao stakes for each neuron.
let total_stake: Vec<I64F64> = alpha_stake
.iter()
.zip(tao_stake.iter())
.map(|(alpha_i, tao_i)| {
alpha_i + tao_i * tao_weight
alpha_i.saturating_add(tao_i.saturating_mul(tao_weight))
})
.collect();

Expand Down Expand Up @@ -274,7 +270,7 @@ impl<T: Config> Pallet<T> {
netuid: u16,
) -> u64 {
let alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
alpha_share_pool.try_get_value(&coldkey).unwrap_or(0)
alpha_share_pool.try_get_value(coldkey).unwrap_or(0)
}

/// Retrieves the total stake (alpha) for a given hotkey on a specific subnet.
Expand Down Expand Up @@ -351,7 +347,7 @@ impl<T: Config> Pallet<T> {
amount: u64
) {
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
let _ = alpha_share_pool.update_value_for_one(&coldkey, amount as i64);
let _ = alpha_share_pool.update_value_for_one(coldkey, amount as i64);
}

/// Sell shares in the hotkey on a given subnet
Expand All @@ -371,7 +367,7 @@ impl<T: Config> Pallet<T> {
amount: u64
) {
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
let _ = alpha_share_pool.update_value_for_one(&coldkey, (amount as i64).neg());
let _ = alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg());
}

/// Swaps TAO for the alpha token on the subnet.
Expand Down
35 changes: 20 additions & 15 deletions primitives/share-pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::result_unit_err)]

use sp_std::ops::Neg;
use sp_std::marker;
Expand Down Expand Up @@ -66,18 +67,22 @@ where
/// Every key's associated value effectively updates with this operation
pub fn update_value_for_all(&mut self, update: i64) -> Result<(), ()> {
let shared_value: I64F64 = self.state_ops.get_shared_value();
if update > 0 {
self.state_ops.set_shared_value(
shared_value
.checked_add(I64F64::from_num(update))
.ok_or_else(|| {})?,
);
} else if update < 0 {
self.state_ops.set_shared_value(
shared_value
.checked_sub(I64F64::from_num(update.neg()))
.ok_or_else(|| {})?,
);
match update.cmp(&0) {
sp_std::cmp::Ordering::Greater => {
self.state_ops.set_shared_value(
shared_value
.checked_add(I64F64::from_num(update))
.ok_or({})?,
);
}
sp_std::cmp::Ordering::Less => {
self.state_ops.set_shared_value(
shared_value
.checked_sub(I64F64::from_num(update.neg()))
.ok_or({})?,
);
}
_ => { }
}

Ok(())
Expand Down Expand Up @@ -106,18 +111,18 @@ where

let shares_per_update: I64F64 = I64F64::from_num(update)
.checked_div(value_per_share)
.ok_or_else(|| {})?;
.ok_or({})?;

self.state_ops.set_denominator(
denominator
.checked_add(shares_per_update)
.ok_or_else(|| {})?,
.ok_or({})?,
);
self.state_ops.set_share(
key,
current_share
.checked_add(shares_per_update)
.ok_or_else(|| {})?,
.ok_or({})?,
);
}

Expand Down

0 comments on commit 7b40bac

Please sign in to comment.