diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 03aed6027..f6b132148 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -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( diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarks.rs index 61ca4c148..07131acc4 100644 --- a/pallets/subtensor/src/benchmarks.rs +++ b/pallets/subtensor/src/benchmarks.rs @@ -127,7 +127,7 @@ benchmarks! { Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked); assert_ok!(Subtensor::::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::>(); @@ -161,10 +161,10 @@ benchmarks! { let u64_staked_amt = 100_000_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), u64_staked_amt); - assert_ok!( Subtensor::::add_stake(RawOrigin::Signed( coldkey.clone() ).into() , hotkey.clone(), u64_staked_amt)); + assert_ok!( Subtensor::::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::>(); @@ -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::::set_network_rate_limit(1); let amount: u64 = 1; let amount_to_be_staked = 100_000_000_000_000u64; Subtensor::::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::::set_network_rate_limit(0); let amount: u64 = 1; let amount_to_be_staked = 100_000_000_000_000u64; Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked); - assert_ok!(Subtensor::::register_network(RawOrigin::Signed(coldkey.clone()).into())); + assert_ok!(Subtensor::::register_network(RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone())); }: dissolve_network(RawOrigin::Root, coldkey.clone(), 1) diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index 012bd78e5..ff97c5ace 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -103,7 +103,7 @@ impl Pallet { let mechid: u16 = SubnetMechanism::::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) diff --git a/pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs b/pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs index 0523561cc..58b658a9a 100644 --- a/pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs +++ b/pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs @@ -168,151 +168,151 @@ mod v0 { impl Pallet { #[cfg(feature = "try-runtime")] fn check_null_stake_invariants( - old_storage: v0::OldStorage, + _old_storage: v0::OldStorage, ) -> Result<(), sp_runtime::TryRuntimeError> { - let null_account = &DefaultAccount::::get(); - - let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8"; - let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1"; - let migration_coldkey = "5GeRjQYsobRWFnrbBmGe5ugme3rfnDVF69N45YtdBpUFsJG8"; - - let taostats_old_hk_account = &get_account_id_from_ss58::(taostats_old_hotkey); - let taostats_new_hk_account = &get_account_id_from_ss58::(taostats_new_hotkey); - let migration_ck_account = &get_account_id_from_ss58::(migration_coldkey); - - let old = old_storage; - let null_stake_total = old - .old_null_stake_taostats - .saturating_add(old.old_null_stake_datura) - .saturating_add(old.old_migration_stake_taostats) - .saturating_add(old.old_migration_stake_datura); - - match ( - taostats_old_hk_account, - taostats_new_hk_account, - migration_ck_account, - ) { - (Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_ck_acct)) => { - // Check the pending emission is added to new the TaoStats hotkey - assert_eq!( - PendingdHotkeyEmission::::get(taostats_new_hk_acct), - old.expected_taostats_new_hk_pending_emission - ); + // let null_account = &DefaultAccount::::get(); + + // let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8"; + // let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1"; + // let migration_coldkey = "5GeRjQYsobRWFnrbBmGe5ugme3rfnDVF69N45YtdBpUFsJG8"; + + // let taostats_old_hk_account = &get_account_id_from_ss58::(taostats_old_hotkey); + // let taostats_new_hk_account = &get_account_id_from_ss58::(taostats_new_hotkey); + // let migration_ck_account = &get_account_id_from_ss58::(migration_coldkey); + + // let old = old_storage; + // let null_stake_total = old + // .old_null_stake_taostats + // .saturating_add(old.old_null_stake_datura) + // .saturating_add(old.old_migration_stake_taostats) + // .saturating_add(old.old_migration_stake_datura); + + // match ( + // taostats_old_hk_account, + // taostats_new_hk_account, + // migration_ck_account, + // ) { + // (Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_ck_acct)) => { + // // Check the pending emission is added to new the TaoStats hotkey + // assert_eq!( + // PendingdHotkeyEmission::::get(taostats_new_hk_acct), + // old.expected_taostats_new_hk_pending_emission + // ); - assert_eq!(PendingdHotkeyEmission::::get(taostats_old_hk_acct), 0); + // assert_eq!(PendingdHotkeyEmission::::get(taostats_old_hk_acct), 0); - assert_eq!(Stake::::get(taostats_old_hk_acct, null_account), 0); + // assert_eq!(Stake::::get(taostats_old_hk_acct, null_account), 0); - assert!(StakingHotkeys::::get(migration_ck_acct).contains(taostats_old_hk_acct)); + // assert!(StakingHotkeys::::get(migration_ck_acct).contains(taostats_old_hk_acct)); - assert_eq!( - Self::get_stake_for_coldkey_and_hotkey(null_account, taostats_old_hk_acct), - 0 - ); + // assert_eq!( + // Self::get_stake_for_coldkey_and_hotkey(null_account, taostats_old_hk_acct), + // 0 + // ); - // TODO: Fix this with proper total hotkey stake - // Check the total hotkey stake is the same - // assert_eq!( - // TotalHotkeyStake::::get(taostats_old_hk_acct), - // old.old_null_stake_taostats - // .saturating_add(old.old_migration_stake_taostats) - // ); - - let new_null_stake_taostats = - Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, taostats_old_hk_acct); - - assert_eq!( - new_null_stake_taostats, - old.old_null_stake_taostats - .saturating_add(old.old_migration_stake_taostats) - ); - } - _ => { - log::warn!("Failed to get account id from ss58 for taostats hotkeys"); - return Err("Failed to get account id from ss58 for taostats hotkeys".into()); - } - } + // // TODO: Fix this with proper total hotkey stake + // // Check the total hotkey stake is the same + // // assert_eq!( + // // TotalHotkeyStake::::get(taostats_old_hk_acct), + // // old.old_null_stake_taostats + // // .saturating_add(old.old_migration_stake_taostats) + // // ); - let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB"; - let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi"; - - let datura_old_hk_account = &get_account_id_from_ss58::(datura_old_hotkey); - let datura_new_hk_account = &get_account_id_from_ss58::(datura_new_hotkey); - - match ( - datura_old_hk_account, - datura_new_hk_account, - migration_ck_account, - ) { - (Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_ck_acct)) => { - // Check the pending emission is added to new Datura hotkey - assert_eq!( - crate::PendingdHotkeyEmission::::get(datura_new_hk_acct), - old.expected_datura_new_hk_pending_emission - ); + // let new_null_stake_taostats = + // Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, taostats_old_hk_acct); - // Check the pending emission is removed from old ones - assert_eq!(PendingdHotkeyEmission::::get(datura_old_hk_acct), 0); + // assert_eq!( + // new_null_stake_taostats, + // old.old_null_stake_taostats + // .saturating_add(old.old_migration_stake_taostats) + // ); + // } + // _ => { + // log::warn!("Failed to get account id from ss58 for taostats hotkeys"); + // return Err("Failed to get account id from ss58 for taostats hotkeys".into()); + // } + // } - // Check the stake entry is removed - assert_eq!(Stake::::get(datura_old_hk_acct, null_account), 0); + // let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB"; + // let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi"; - assert!(StakingHotkeys::::get(migration_ck_acct).contains(datura_old_hk_acct)); + // let datura_old_hk_account = &get_account_id_from_ss58::(datura_old_hotkey); + // let datura_new_hk_account = &get_account_id_from_ss58::(datura_new_hotkey); - assert_eq!( - Self::get_stake_for_coldkey_and_hotkey(null_account, datura_old_hk_acct), - 0 - ); + // match ( + // datura_old_hk_account, + // datura_new_hk_account, + // migration_ck_account, + // ) { + // (Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_ck_acct)) => { + // // Check the pending emission is added to new Datura hotkey + // assert_eq!( + // crate::PendingdHotkeyEmission::::get(datura_new_hk_acct), + // old.expected_datura_new_hk_pending_emission + // ); - // TODO: Fix - // Check the total hotkey stake is the same - // assert_eq!( - // TotalHotkeyStake::::get(datura_old_hk_acct), - // old.old_null_stake_datura - // .saturating_add(old.old_migration_stake_datura) - // ); - - let new_null_stake_datura = - Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, datura_old_hk_acct); - - assert_eq!( - new_null_stake_datura, - old.old_null_stake_datura - .saturating_add(old.old_migration_stake_datura) - ); - } - _ => { - log::warn!("Failed to get account id from ss58 for datura hotkeys"); - return Err("Failed to get account id from ss58 for datura hotkeys".into()); - } - } + // // Check the pending emission is removed from old ones + // assert_eq!(PendingdHotkeyEmission::::get(datura_old_hk_acct), 0); + + // // Check the stake entry is removed + // assert_eq!(Stake::::get(datura_old_hk_acct, null_account), 0); + + // assert!(StakingHotkeys::::get(migration_ck_acct).contains(datura_old_hk_acct)); + + // assert_eq!( + // Self::get_stake_for_coldkey_and_hotkey(null_account, datura_old_hk_acct), + // 0 + // ); + + // // TODO: Fix + // // Check the total hotkey stake is the same + // // assert_eq!( + // // TotalHotkeyStake::::get(datura_old_hk_acct), + // // old.old_null_stake_datura + // // .saturating_add(old.old_migration_stake_datura) + // // ); + + // let new_null_stake_datura = + // Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, datura_old_hk_acct); - // match migration_ck_account { - // Ok(migration_ck_acct) => { - // // Check the migration key has stake with both *old* hotkeys // assert_eq!( - // TotalColdkeyStake::::get(migration_ck_acct), - // null_stake_total + // new_null_stake_datura, + // old.old_null_stake_datura + // .saturating_add(old.old_migration_stake_datura) // ); // } // _ => { - // log::warn!("Failed to get account id from ss58 for migration coldkey"); - // return Err("Failed to get account id from ss58 for migration coldkey".into()); + // log::warn!("Failed to get account id from ss58 for datura hotkeys"); + // return Err("Failed to get account id from ss58 for datura hotkeys".into()); // } // } - // Check the total issuance is the SAME following migration (no TAO issued) - let expected_total_issuance = old.total_issuance_before; - let expected_total_stake = old.total_stake_before; - assert_eq!(Self::get_total_issuance(), expected_total_issuance); - - // Check total stake is the SAME following the migration (no new TAO staked) - assert_eq!(TotalStake::::get(), expected_total_stake); - // Check the total stake maps are updated following the migration (removal of old null_account stake entries) - // assert_eq!(TotalColdkeyStake::::get(null_account), 0); - - // Check staking hotkeys is updated - assert_eq!(StakingHotkeys::::get(null_account), vec![]); + // // match migration_ck_account { + // // Ok(migration_ck_acct) => { + // // // Check the migration key has stake with both *old* hotkeys + // // assert_eq!( + // // TotalColdkeyStake::::get(migration_ck_acct), + // // null_stake_total + // // ); + // // } + // // _ => { + // // log::warn!("Failed to get account id from ss58 for migration coldkey"); + // // return Err("Failed to get account id from ss58 for migration coldkey".into()); + // // } + // // } + + // // Check the total issuance is the SAME following migration (no TAO issued) + // let expected_total_issuance = old.total_issuance_before; + // let expected_total_stake = old.total_stake_before; + // assert_eq!(Self::get_total_issuance(), expected_total_issuance); + + // // Check total stake is the SAME following the migration (no new TAO staked) + // assert_eq!(TotalStake::::get(), expected_total_stake); + // // Check the total stake maps are updated following the migration (removal of old null_account stake entries) + // // assert_eq!(TotalColdkeyStake::::get(null_account), 0); + + // // Check staking hotkeys is updated + // assert_eq!(StakingHotkeys::::get(null_account), vec![]); Ok(()) } @@ -327,122 +327,122 @@ pub mod migration { pub struct Migration(PhantomData); - #[cfg(feature = "try-runtime")] - fn get_old_storage_values() -> Result { - log::info!("Getting old storage values for migration"); - - let null_account = &DefaultAccount::::get(); - let migration_coldkey = "5GeRjQYsobRWFnrbBmGe5ugme3rfnDVF69N45YtdBpUFsJG8"; - let migration_account = &get_account_id_from_ss58::(migration_coldkey); - - let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8"; - let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1"; - - let taostats_old_hk_account = &get_account_id_from_ss58::(taostats_old_hotkey); - let taostats_new_hk_account = &get_account_id_from_ss58::(taostats_new_hotkey); - - let total_issuance_before = crate::Pallet::::get_total_issuance(); - let mut expected_taostats_new_hk_pending_emission: u64 = 0; - let mut expected_datura_new_hk_pending_emission: u64 = 0; - let (old_null_stake_taostats, old_migration_stake_taostats) = match ( - taostats_old_hk_account, - taostats_new_hk_account, - migration_account, - ) { - (Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_acct)) => { - expected_taostats_new_hk_pending_emission = - expected_taostats_new_hk_pending_emission - .saturating_add(PendingdHotkeyEmission::::get(taostats_old_hk_acct)) - .saturating_add(PendingdHotkeyEmission::::get(taostats_new_hk_acct)); - - Ok::<(u64, u64), sp_runtime::TryRuntimeError>(( - crate::Pallet::::get_stake_for_coldkey_and_hotkey( - null_account, - taostats_old_hk_acct, - ), - crate::Pallet::::get_stake_for_coldkey_and_hotkey( - migration_acct, - taostats_old_hk_acct, - ), - )) - } - _ => { - log::warn!("Failed to get account id from ss58 for taostats hotkeys"); - Err("Failed to get account id from ss58 for taostats hotkeys".into()) - } - }?; - - let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB"; - let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi"; - - let datura_old_hk_account = &get_account_id_from_ss58::(datura_old_hotkey); - let datura_new_hk_account = &get_account_id_from_ss58::(datura_new_hotkey); - - let (old_null_stake_datura, old_migration_stake_datura) = match ( - datura_old_hk_account, - datura_new_hk_account, - migration_account, - ) { - (Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_acct)) => { - expected_datura_new_hk_pending_emission = expected_datura_new_hk_pending_emission - .saturating_add(PendingdHotkeyEmission::::get(datura_old_hk_acct)) - .saturating_add(PendingdHotkeyEmission::::get(datura_new_hk_acct)); - - Ok::<(u64, u64), sp_runtime::TryRuntimeError>(( - crate::Pallet::::get_stake_for_coldkey_and_hotkey( - null_account, - datura_old_hk_acct, - ), - crate::Pallet::::get_stake_for_coldkey_and_hotkey( - migration_acct, - datura_old_hk_acct, - ), - )) - } - _ => { - log::warn!("Failed to get account id from ss58 for datura hotkeys"); - Err("Failed to get account id from ss58 for datura hotkeys".into()) - } - }?; - - let total_stake_before: u64 = crate::Pallet::::get_total_stake(); - - let result = v0::OldStorage { - total_issuance_before, - total_stake_before, - expected_taostats_new_hk_pending_emission, - expected_datura_new_hk_pending_emission, - old_migration_stake_taostats, - old_null_stake_taostats, - old_migration_stake_datura, - old_null_stake_datura, - }; - - log::info!("Got old storage values for migration"); - - Ok(result) - } + // #[cfg(feature = "try-runtime")] + // fn get_old_storage_values() -> Result { + // log::info!("Getting old storage values for migration"); + + // let null_account = &DefaultAccount::::get(); + // let migration_coldkey = "5GeRjQYsobRWFnrbBmGe5ugme3rfnDVF69N45YtdBpUFsJG8"; + // let migration_account = &get_account_id_from_ss58::(migration_coldkey); + + // let taostats_old_hotkey = "5Hddm3iBFD2GLT5ik7LZnT3XJUnRnN8PoeCFgGQgawUVKNm8"; + // let taostats_new_hotkey = "5GKH9FPPnWSUoeeTJp19wVtd84XqFW4pyK2ijV2GsFbhTrP1"; + + // let taostats_old_hk_account = &get_account_id_from_ss58::(taostats_old_hotkey); + // let taostats_new_hk_account = &get_account_id_from_ss58::(taostats_new_hotkey); + + // let total_issuance_before = crate::Pallet::::get_total_issuance(); + // let mut expected_taostats_new_hk_pending_emission: u64 = 0; + // let mut expected_datura_new_hk_pending_emission: u64 = 0; + // let (old_null_stake_taostats, old_migration_stake_taostats) = match ( + // taostats_old_hk_account, + // taostats_new_hk_account, + // migration_account, + // ) { + // (Ok(taostats_old_hk_acct), Ok(taostats_new_hk_acct), Ok(migration_acct)) => { + // expected_taostats_new_hk_pending_emission = + // expected_taostats_new_hk_pending_emission + // .saturating_add(PendingdHotkeyEmission::::get(taostats_old_hk_acct)) + // .saturating_add(PendingdHotkeyEmission::::get(taostats_new_hk_acct)); + + // Ok::<(u64, u64), sp_runtime::TryRuntimeError>(( + // crate::Pallet::::get_stake_for_coldkey_and_hotkey( + // null_account, + // taostats_old_hk_acct, + // ), + // crate::Pallet::::get_stake_for_coldkey_and_hotkey( + // migration_acct, + // taostats_old_hk_acct, + // ), + // )) + // } + // _ => { + // log::warn!("Failed to get account id from ss58 for taostats hotkeys"); + // Err("Failed to get account id from ss58 for taostats hotkeys".into()) + // } + // }?; + + // let datura_old_hotkey = "5FKstHjZkh4v3qAMSBa1oJcHCLjxYZ8SNTSz1opTv4hR7gVB"; + // let datura_new_hotkey = "5GP7c3fFazW9GXK8Up3qgu2DJBk8inu4aK9TZy3RuoSWVCMi"; + + // let datura_old_hk_account = &get_account_id_from_ss58::(datura_old_hotkey); + // let datura_new_hk_account = &get_account_id_from_ss58::(datura_new_hotkey); + + // let (old_null_stake_datura, old_migration_stake_datura) = match ( + // datura_old_hk_account, + // datura_new_hk_account, + // migration_account, + // ) { + // (Ok(datura_old_hk_acct), Ok(datura_new_hk_acct), Ok(migration_acct)) => { + // expected_datura_new_hk_pending_emission = expected_datura_new_hk_pending_emission + // .saturating_add(PendingdHotkeyEmission::::get(datura_old_hk_acct)) + // .saturating_add(PendingdHotkeyEmission::::get(datura_new_hk_acct)); + + // Ok::<(u64, u64), sp_runtime::TryRuntimeError>(( + // crate::Pallet::::get_stake_for_coldkey_and_hotkey( + // null_account, + // datura_old_hk_acct, + // ), + // crate::Pallet::::get_stake_for_coldkey_and_hotkey( + // migration_acct, + // datura_old_hk_acct, + // ), + // )) + // } + // _ => { + // log::warn!("Failed to get account id from ss58 for datura hotkeys"); + // Err("Failed to get account id from ss58 for datura hotkeys".into()) + // } + // }?; + + // let total_stake_before: u64 = crate::Pallet::::get_total_stake(); + + // let result = v0::OldStorage { + // total_issuance_before, + // total_stake_before, + // expected_taostats_new_hk_pending_emission, + // expected_datura_new_hk_pending_emission, + // old_migration_stake_taostats, + // old_null_stake_taostats, + // old_migration_stake_datura, + // old_null_stake_datura, + // }; + + // log::info!("Got old storage values for migration"); + + // Ok(result) + // } impl OnRuntimeUpgrade for Migration { - /// Runs the migration to fix the pending emissions. - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - use codec::Encode; - - // Get the old storage values - match get_old_storage_values::() { - Ok(old_storage) => { - log::info!("Successfully got old storage values for migration"); - let encoded = old_storage.encode(); - - Ok(encoded) - } - Err(e) => { - log::error!("Failed to get old storage values for migration: {:?}", e); - Err("Failed to get old storage values for migration".into()) - } - } - } + // Runs the migration to fix the pending emissions. + // #[cfg(feature = "try-runtime")] + // fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + // use codec::Encode; + + // // Get the old storage values + // match get_old_storage_values::() { + // Ok(old_storage) => { + // log::info!("Successfully got old storage values for migration"); + // let encoded = old_storage.encode(); + + // Ok(encoded) + // } + // Err(e) => { + // log::error!("Failed to get old storage values for migration: {:?}", e); + // Err("Failed to get old storage values for migration".into()) + // } + // } + // } // Runs the migrate function for the fix_pending_emission migration fn on_runtime_upgrade() -> Weight { diff --git a/pallets/subtensor/src/rpc_info/dynamic_info.rs b/pallets/subtensor/src/rpc_info/dynamic_info.rs index e44e5a4f8..dc48eea32 100644 --- a/pallets/subtensor/src/rpc_info/dynamic_info.rs +++ b/pallets/subtensor/src/rpc_info/dynamic_info.rs @@ -38,8 +38,8 @@ impl Pallet { let blocks_since_last_step: u64 = current_block.saturating_sub(last_step); Some(DynamicInfo { netuid: netuid.into(), - owner_hotkey: SubnetOwnerHotkey::::get(netuid).into(), - owner_coldkey: SubnetOwner::::get(netuid).into(), + owner_hotkey: SubnetOwnerHotkey::::get(netuid), + owner_coldkey: SubnetOwner::::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::::get(netuid).into(), diff --git a/pallets/subtensor/src/rpc_info/show_subnet.rs b/pallets/subtensor/src/rpc_info/show_subnet.rs index 2dd45bfbf..8dc6435da 100644 --- a/pallets/subtensor/src/rpc_info/show_subnet.rs +++ b/pallets/subtensor/src/rpc_info/show_subnet.rs @@ -57,7 +57,7 @@ impl Pallet { for hotkey in hotkeys.clone() { let last_emission: Compact = LastHotkeyEmissionOnNetuid::::get(hotkey.clone(), netuid).into(); - hotkeys_emissions.push(last_emission.into()); + hotkeys_emissions.push(last_emission); } result.push(hotkeys_emissions.clone()); } @@ -146,22 +146,22 @@ impl Pallet { let emission_history: Vec>> = 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, }) } diff --git a/pallets/subtensor/src/rpc_info/stake_info.rs b/pallets/subtensor/src/rpc_info/stake_info.rs index c4a2d57a6..b05628124 100644 --- a/pallets/subtensor/src/rpc_info/stake_info.rs +++ b/pallets/subtensor/src/rpc_info/stake_info.rs @@ -32,9 +32,9 @@ impl Pallet { let mut stake_info_for_coldkey: Vec> = 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::::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(), diff --git a/pallets/subtensor/src/staking/remove_stake.rs b/pallets/subtensor/src/staking/remove_stake.rs index 7783a4ebe..2398d32b3 100644 --- a/pallets/subtensor/src/staking/remove_stake.rs +++ b/pallets/subtensor/src/staking/remove_stake.rs @@ -213,7 +213,7 @@ impl Pallet { 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); diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index ba7e41bf8..19addd3c6 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -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}; @@ -18,7 +17,8 @@ impl Pallet { /// # Returns /// * `u64` - The total alpha issuance for the specified subnet. pub fn get_alpha_issuance(netuid: u16) -> u64 { - return SubnetAlphaIn::::get(netuid) + SubnetAlphaOut::::get(netuid); + SubnetAlphaIn::::get(netuid) + .saturating_add(SubnetAlphaOut::::get(netuid)) } /// Calculates the price of alpha for a given subnet. @@ -33,13 +33,17 @@ impl Pallet { /// # Returns /// * `I96F32` - The price of alpha for the specified subnet. pub fn get_alpha_price(netuid: u16) -> I96F32 { - if SubnetAlphaIn::::get(netuid) == 0 { - return I96F32::from_num(0); - } else{ - return I96F32::from_num(SubnetTAO::::get(netuid) )/ I96F32::from_num(SubnetAlphaIn::::get(netuid)); - } + I96F32::from_num( + if SubnetAlphaIn::::get(netuid) == 0 { + 0 + } else { + SubnetTAO::::get(netuid) + } + ) + .checked_div(I96F32::from_num(SubnetAlphaIn::::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: @@ -92,36 +96,28 @@ impl Pallet { /// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet. /// pub fn get_stake_weights_for_network(netuid: u16) -> (Vec, Vec, Vec) { - // 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)> = as IterableStorageDoubleMap>::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 = 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 = Keys::::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 = 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 = Keys::::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 = 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(); @@ -274,7 +270,7 @@ impl Pallet { 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. @@ -351,7 +347,7 @@ impl Pallet { 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 @@ -371,7 +367,7 @@ impl Pallet { 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. diff --git a/primitives/share-pool/src/lib.rs b/primitives/share-pool/src/lib.rs index 361d3e97b..fcc706024 100644 --- a/primitives/share-pool/src/lib.rs +++ b/primitives/share-pool/src/lib.rs @@ -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; @@ -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(()) @@ -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({})?, ); }