Skip to content

Commit 180e6a9

Browse files
committed
Fix test_share_based_staking
1 parent 830b041 commit 180e6a9

File tree

8 files changed

+143
-117
lines changed

8 files changed

+143
-117
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub mod pallet {
8181
use sp_std::collections::vec_deque::VecDeque;
8282
use sp_std::vec;
8383
use sp_std::vec::Vec;
84-
use substrate_fixed::types::I64F64;
84+
use substrate_fixed::types::U64F64;
8585
use subtensor_macros::freeze_struct;
8686

8787
#[cfg(not(feature = "std"))]
@@ -715,8 +715,8 @@ pub mod pallet {
715715

716716
#[pallet::type_value]
717717
/// Default value for Share Pool variables
718-
pub fn DefaultSharePoolZero<T: Config>() -> I64F64 {
719-
I64F64::from_num(0)
718+
pub fn DefaultSharePoolZero<T: Config>() -> U64F64 {
719+
U64F64::from_num(0)
720720
}
721721

722722
#[pallet::storage]
@@ -944,7 +944,7 @@ pub mod pallet {
944944
T::AccountId,
945945
Identity,
946946
u16,
947-
I64F64,
947+
U64F64,
948948
ValueQuery,
949949
DefaultSharePoolZero<T>,
950950
>;
@@ -956,7 +956,7 @@ pub mod pallet {
956956
NMapKey<Blake2_128Concat, T::AccountId>, // cold
957957
NMapKey<Identity, u16>, // subnet
958958
),
959-
I64F64, // Shares
959+
U64F64, // Shares
960960
ValueQuery,
961961
>;
962962
#[pallet::storage] // --- DMAP ( netuid ) --> token_symbol | Returns the token symbol for a subnet.

pallets/subtensor/src/macros/genesis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ mod genesis {
6666
Alpha::<T>::insert(
6767
// Lock the initial funds making this key the owner.
6868
(hotkey.clone(), hotkey.clone(), netuid),
69-
I64F64::from_num(1_000_000_000),
69+
U64F64::from_num(1_000_000_000),
7070
);
7171
TotalHotkeyAlpha::<T>::insert(hotkey.clone(), netuid, 1_000_000_000);
72-
TotalHotkeyShares::<T>::insert(hotkey.clone(), netuid, I64F64::from_num(1_000_000_000));
72+
TotalHotkeyShares::<T>::insert(hotkey.clone(), netuid, U64F64::from_num(1_000_000_000));
7373
// TotalColdkeyAlpha::<T>::insert(hotkey.clone(), netuid, 1_000_000_000);
7474
SubnetAlphaOut::<T>::insert(netuid, 1_000_000_000);
7575
let mut staking_hotkeys = StakingHotkeys::<T>::get(hotkey.clone());

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use frame_support::IterableStorageMap;
44
use frame_support::{traits::Get, weights::Weight};
55
use log;
66
use sp_runtime::format;
7-
use substrate_fixed::types::I64F64;
7+
use substrate_fixed::types::U64F64;
88

99
pub fn migrate_rao<T: Config>() -> Weight {
1010
let migration_name = b"migrate_rao".to_vec();
@@ -42,10 +42,10 @@ pub fn migrate_rao<T: Config>() -> Weight {
4242
});
4343
// Set all the stake on root 0 subnet.
4444
Alpha::<T>::mutate((hotkey.clone(), coldkey.clone(), 0), |total| {
45-
*total = total.saturating_add(I64F64::from_num(stake))
45+
*total = total.saturating_add(U64F64::from_num(stake))
4646
});
4747
TotalHotkeyShares::<T>::mutate(hotkey.clone(), 0, |total| {
48-
*total = total.saturating_add(I64F64::from_num(stake))
48+
*total = total.saturating_add(U64F64::from_num(stake))
4949
});
5050
// Set the total stake on the hotkey
5151
TotalHotkeyAlpha::<T>::mutate(hotkey.clone(), 0, |total| {

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use share_pool::{SharePool, SharePoolDataOperations};
33
use sp_std::ops::Neg;
4-
use substrate_fixed::types::{I64F64, I96F32};
4+
use substrate_fixed::types::{I64F64, I96F32, U64F64};
55

66
impl<T: Config> Pallet<T> {
77

@@ -334,7 +334,7 @@ impl<T: Config> Pallet<T> {
334334
amount: u64
335335
) {
336336
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
337-
let _ = alpha_share_pool.update_value_for_all(amount as i64);
337+
alpha_share_pool.update_value_for_all(amount as i64);
338338
}
339339

340340
/// Decrease hotkey stake on a subnet.
@@ -352,7 +352,7 @@ impl<T: Config> Pallet<T> {
352352
amount: u64
353353
) {
354354
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
355-
let _ = alpha_share_pool.update_value_for_all((amount as i64).neg());
355+
alpha_share_pool.update_value_for_all((amount as i64).neg());
356356
}
357357

358358
/// Buys shares in the hotkey on a given subnet
@@ -372,7 +372,7 @@ impl<T: Config> Pallet<T> {
372372
amount: u64
373373
) {
374374
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
375-
let _ = alpha_share_pool.update_value_for_one(coldkey, amount as i64);
375+
alpha_share_pool.update_value_for_one(coldkey, amount as i64);
376376
}
377377

378378
/// Sell shares in the hotkey on a given subnet
@@ -392,7 +392,11 @@ impl<T: Config> Pallet<T> {
392392
amount: u64
393393
) {
394394
let mut alpha_share_pool = Self::get_alpha_share_pool(hotkey.clone(), netuid);
395-
let _ = alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg());
395+
if let Ok(value) = alpha_share_pool.try_get_value(&coldkey) {
396+
if value >= amount {
397+
alpha_share_pool.update_value_for_one(coldkey, (amount as i64).neg());
398+
}
399+
}
396400
}
397401

398402
/// Swaps TAO for the alpha token on the subnet.
@@ -656,39 +660,39 @@ impl<T: Config> HotkeyAlphaSharePoolDataOperations<T> {
656660
type AlphaShareKey<T> = <T as frame_system::Config>::AccountId;
657661

658662
impl<T: Config> SharePoolDataOperations<AlphaShareKey<T>> for HotkeyAlphaSharePoolDataOperations<T> {
659-
fn get_shared_value(&self) -> I64F64 {
660-
I64F64::from_num(crate::TotalHotkeyAlpha::<T>::get(&self.hotkey, self.netuid))
663+
fn get_shared_value(&self) -> U64F64 {
664+
U64F64::from_num(crate::TotalHotkeyAlpha::<T>::get(&self.hotkey, self.netuid))
661665
}
662666

663-
fn get_share(&self, key: &AlphaShareKey<T>) -> I64F64 {
667+
fn get_share(&self, key: &AlphaShareKey<T>) -> U64F64 {
664668
crate::Alpha::<T>::get((&(self.hotkey), key, self.netuid))
665669
}
666670

667-
fn try_get_share(&self, key: &AlphaShareKey<T>) -> Result<I64F64, ()> {
671+
fn try_get_share(&self, key: &AlphaShareKey<T>) -> Result<U64F64, ()> {
668672
crate::Alpha::<T>::try_get((&(self.hotkey), key, self.netuid))
669673
}
670674

671-
fn get_denominator(&self) -> I64F64 {
675+
fn get_denominator(&self) -> U64F64 {
672676
crate::TotalHotkeyShares::<T>::get(&(self.hotkey), self.netuid)
673677
}
674678

675-
fn set_shared_value(&mut self, value: I64F64) {
679+
fn set_shared_value(&mut self, value: U64F64) {
676680
if value != 0 {
677681
crate::TotalHotkeyAlpha::<T>::insert(&(self.hotkey), self.netuid, value.to_num::<u64>());
678682
} else {
679683
crate::TotalHotkeyAlpha::<T>::remove(&(self.hotkey), self.netuid);
680684
}
681685
}
682686

683-
fn set_share(&mut self, key: &AlphaShareKey<T>, share: I64F64) {
687+
fn set_share(&mut self, key: &AlphaShareKey<T>, share: U64F64) {
684688
if share != 0 {
685689
crate::Alpha::<T>::insert((&self.hotkey, key, self.netuid), share);
686690
} else {
687691
crate::Alpha::<T>::remove((&self.hotkey, key, self.netuid));
688692
}
689693
}
690694

691-
fn set_denominator(&mut self, update: I64F64) {
695+
fn set_denominator(&mut self, update: U64F64) {
692696
if update != 0 {
693697
crate::TotalHotkeyShares::<T>::insert(&self.hotkey, self.netuid, update);
694698
} else {

pallets/subtensor/src/swap/swap_coldkey.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use frame_support::weights::Weight;
33
use sp_core::Get;
4-
use substrate_fixed::types::I64F64;
4+
use substrate_fixed::types::U64F64;
55

66
impl<T: Config> Pallet<T> {
77
/// Swaps the coldkey associated with a set of hotkeys from an old coldkey to a new coldkey.
@@ -169,9 +169,9 @@ impl<T: Config> Pallet<T> {
169169
// 3.1 Swap Alpha
170170
for netuid in Self::get_all_subnet_netuids() {
171171
// Get the stake on the old (hot,coldkey) account.
172-
let old_alpha: I64F64 = Alpha::<T>::get((&hotkey, old_coldkey, netuid));
172+
let old_alpha: U64F64 = Alpha::<T>::get((&hotkey, old_coldkey, netuid));
173173
// Get the stake on the new (hot,coldkey) account.
174-
let new_alpha: I64F64 = Alpha::<T>::get((&hotkey, new_coldkey, netuid));
174+
let new_alpha: U64F64 = Alpha::<T>::get((&hotkey, new_coldkey, netuid));
175175
// Add the stake to new account.
176176
Alpha::<T>::insert(
177177
(&hotkey, new_coldkey, netuid),

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use frame_support::weights::Weight;
33
use sp_core::Get;
4-
use substrate_fixed::types::I64F64;
4+
use substrate_fixed::types::U64F64;
55

66
impl<T: Config> Pallet<T> {
77
/// Swaps the hotkey of a coldkey account.
@@ -327,9 +327,9 @@ impl<T: Config> Pallet<T> {
327327
// 3.1 Swap Alpha
328328
for netuid in Self::get_all_subnet_netuids() {
329329
// Get the stake on the old (hot,coldkey) account.
330-
let old_alpha: I64F64 = Alpha::<T>::get((&old_hotkey, coldkey.clone(), netuid));
330+
let old_alpha: U64F64 = Alpha::<T>::get((&old_hotkey, coldkey.clone(), netuid));
331331
// Get the stake on the new (hot,coldkey) account.
332-
let new_alpha: I64F64 = Alpha::<T>::get((&new_hotkey, coldkey.clone(), netuid));
332+
let new_alpha: U64F64 = Alpha::<T>::get((&new_hotkey, coldkey.clone(), netuid));
333333
// Add the stake to new account.
334334
Alpha::<T>::insert(
335335
(&new_hotkey, coldkey.clone(), netuid),

pallets/subtensor/src/tests/staking2.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,15 @@ fn test_share_based_staking() {
302302
);
303303

304304
// Test removing more stake than available
305-
let excessive_amount = primary_after_removal + 1000;
305+
let available_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
306+
&primary_hotkey,
307+
&primary_coldkey,
308+
netuid
309+
);
310+
let excessive_amount = available_stake + 1000;
306311
log::info!(
307312
"Attempting to remove excessive stake: {} + 1000 = {}",
308-
primary_after_removal, excessive_amount
313+
available_stake, excessive_amount
309314
);
310315
SubtensorModule::decrease_stake_for_hotkey_and_coldkey_on_subnet(
311316
&primary_hotkey,
@@ -323,7 +328,7 @@ fn test_share_based_staking() {
323328
after_excessive_removal
324329
);
325330
assert!(
326-
after_excessive_removal == primary_after_removal,
331+
after_excessive_removal == available_stake,
327332
"Removing more stake performs no action"
328333
);
329334

0 commit comments

Comments
 (0)