From 2e586d728fa4a85d4187fc8aa33f84d0b7b8e833 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 30 Apr 2024 19:15:44 +0400 Subject: [PATCH 01/11] typo --- pallets/subtensor/src/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 42c2a1ab9..588b67c8d 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -453,7 +453,7 @@ impl Pallet { // We remove the balance from the hotkey. Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed); - // We add the balancer to the coldkey. If the above fails we will not credit this coldkey. + // We add the balance to the coldkey. If the above fails we will not credit this coldkey. Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed); // Set last block for rate limiting From 25300669336e5097af0531691b3a21024bc76811 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 08:03:58 -0400 Subject: [PATCH 02/11] use removed stake to update balance in clear nom --- pallets/subtensor/src/staking.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 588b67c8d..fd4cc400c 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -679,17 +679,24 @@ impl Pallet { /// It also removes the stake entry for the hotkey-coldkey pairing and adjusts the TotalStake /// and TotalIssuance by subtracting the removed stake amount. /// + /// Returns the amount of stake that was removed. + /// /// # Arguments /// /// * `coldkey` - A reference to the AccountId of the coldkey involved in the staking. /// * `hotkey` - A reference to the AccountId of the hotkey associated with the coldkey. - pub fn empty_stake_on_coldkey_hotkey_account(coldkey: &T::AccountId, hotkey: &T::AccountId) { + pub fn empty_stake_on_coldkey_hotkey_account( + coldkey: &T::AccountId, + hotkey: &T::AccountId, + ) -> u64 { let current_stake: u64 = Stake::::get(hotkey, coldkey); TotalColdkeyStake::::mutate(coldkey, |old| *old = old.saturating_sub(current_stake)); TotalHotkeyStake::::mutate(hotkey, |stake| *stake = stake.saturating_sub(current_stake)); Stake::::remove(hotkey, coldkey); TotalStake::::mutate(|stake| *stake = stake.saturating_sub(current_stake)); TotalIssuance::::mutate(|issuance| *issuance = issuance.saturating_sub(current_stake)); + + current_stake } /// Clears the nomination for an account, if it is a nominator account and the stake is below the minimum required threshold. @@ -704,9 +711,9 @@ impl Pallet { if stake < Self::get_nominator_min_required_stake() { // Remove the stake from the nominator account. (this is a more forceful unstake operation which ) // Actually deletes the staking account. - Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey); + let cleared_stake = Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey); // Add the stake to the coldkey account. - Self::add_balance_to_coldkey_account(coldkey, stake); + Self::add_balance_to_coldkey_account(coldkey, cleared_stake); } } } From e380c6b8e620b7fd4d691a7183c716fd2134a412 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 09:11:52 -0400 Subject: [PATCH 03/11] add tests --- pallets/subtensor/tests/staking.rs | 57 +++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/pallets/subtensor/tests/staking.rs b/pallets/subtensor/tests/staking.rs index f968ac55e..3633a26ac 100644 --- a/pallets/subtensor/tests/staking.rs +++ b/pallets/subtensor/tests/staking.rs @@ -2698,16 +2698,55 @@ fn test_remove_stake_below_minimum_threshold() { stake_amount_to_remove )); - // Nomination stake cannot stake below min threshold. - assert_noop!( - SubtensorModule::remove_stake( - <::RuntimeOrigin>::signed(coldkey2), - hotkey1, - stake_amount_to_remove - ), - Error::::NomStakeBelowMinimumThreshold + // Nomination stake cannot unstake below min threshold, + // without unstaking all and removing the nomination. + let total_hotkey_stake_before = SubtensorModule::get_total_stake_for_hotkey(&hotkey1); + let bal_before = Balances::free_balance(coldkey2); + let staked_before = SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey2, &hotkey1); + let total_network_stake_before = SubtensorModule::get_total_stake(); + let total_issuance_before = SubtensorModule::get_total_issuance(); + // check the premise of the test is correct + assert!(initial_stake - stake_amount_to_remove < minimum_threshold); + assert_ok!(SubtensorModule::remove_stake( + <::RuntimeOrigin>::signed(coldkey2), + hotkey1, + stake_amount_to_remove + )); + + // Has no stake now + assert_eq!( + SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey2, &hotkey1), + 0 ); - }) + let stake_removed = staked_before; // All stake was removed + // Has the full balance + assert_eq!(Balances::free_balance(coldkey2), bal_before + stake_removed); + + // Stake map entry is removed + assert_eq!( + Stake::::try_get(hotkey1, coldkey2).is_err(), + true // Entry was removed + ); + // Stake tracking is updated + assert_eq!( + TotalColdkeyStake::::try_get(coldkey2).unwrap(), + 0 // Did not have any stake before; Entry is NOT removed + ); + assert_eq!( + TotalHotkeyStake::::try_get(hotkey1).unwrap(), + total_hotkey_stake_before - stake_removed // Stake was removed from hotkey1 tracker + ); + assert_eq!( + TotalStake::::try_get().unwrap(), + total_network_stake_before - stake_removed + ); + + // Total issuance is the same + assert_eq!( + SubtensorModule::get_total_issuance(), + total_issuance_before // Nothing was issued + ); + }); } // Verify delegate take can be decreased From dd25a7ce613b0d3cec2c50beb16a318a5e862510 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 09:12:21 -0400 Subject: [PATCH 04/11] Sweep nom stake below minimum --- pallets/subtensor/src/staking.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index fd4cc400c..1c99fa040 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -436,26 +436,18 @@ impl Pallet { Error::::UnstakeRateLimitExceeded ); - // If this is a nomination stake, check if total stake after removing will be above - // the minimum required stake. - - // If coldkey is not owner of the hotkey, it's a nomination stake. - if !Self::coldkey_owns_hotkey(&coldkey, &hotkey) { - let total_stake_after_remove = - Stake::::get(&hotkey, &coldkey).saturating_sub(stake_to_be_removed); - - ensure!( - total_stake_after_remove >= NominatorMinRequiredStake::::get(), - Error::::NomStakeBelowMinimumThreshold - ); - } - // We remove the balance from the hotkey. Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed); // We add the balance to the coldkey. If the above fails we will not credit this coldkey. Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed); + // If the stake is below the minimum, we clear the nomination from storage. + // Not, this only applies to nominator stakes. + // If the coldkey does not own the hotkey, it's a nominator stake. + let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey); + Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake); + // Set last block for rate limiting let block: u64 = Self::get_current_block_as_u64(); Self::set_last_tx_block(&coldkey, block); From a4a8b87ba734bf03f7c72e2612c94473a5328d69 Mon Sep 17 00:00:00 2001 From: shibshib Date: Mon, 29 Apr 2024 23:35:30 +0000 Subject: [PATCH 05/11] Fix hotregs hotpatch rebased off of main --- pallets/subtensor/src/lib.rs | 2 +- pallets/subtensor/tests/mock.rs | 2 +- pallets/subtensor/tests/registration.rs | 93 +++++++++++++++++++------ 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 8d424782b..ae403b30f 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1988,7 +1988,7 @@ where Pallet::::get_registrations_this_interval(*netuid); let max_registrations_per_interval = Pallet::::get_target_registrations_per_interval(*netuid); - if registrations_this_interval >= max_registrations_per_interval { + if registrations_this_interval >= (max_registrations_per_interval * 3) { // If the registration limit for the interval is exceeded, reject the transaction return InvalidTransaction::ExhaustsResources.into(); } diff --git a/pallets/subtensor/tests/mock.rs b/pallets/subtensor/tests/mock.rs index 9b964fcb0..f90cd5d88 100644 --- a/pallets/subtensor/tests/mock.rs +++ b/pallets/subtensor/tests/mock.rs @@ -141,7 +141,7 @@ parameter_types! { pub const InitialAdjustmentInterval: u16 = 100; pub const InitialAdjustmentAlpha: u64 = 0; // no weight to previous value. pub const InitialMaxRegistrationsPerBlock: u16 = 3; - pub const InitialTargetRegistrationsPerInterval: u16 = 2; + pub const InitialTargetRegistrationsPerInterval: u16 = 3; pub const InitialPruningScore : u16 = u16::MAX; pub const InitialRegistrationRequirement: u16 = u16::MAX; // Top 100% pub const InitialMinDifficulty: u64 = 1; diff --git a/pallets/subtensor/tests/registration.rs b/pallets/subtensor/tests/registration.rs index 63ef2b947..fa4494f2c 100644 --- a/pallets/subtensor/tests/registration.rs +++ b/pallets/subtensor/tests/registration.rs @@ -290,18 +290,18 @@ fn test_burned_registration_under_limit() { let netuid: u16 = 1; let hotkey_account_id: U256 = U256::from(1); let coldkey_account_id = U256::from(667); - let who: ::AccountId = hotkey_account_id; - let block_number: u64 = 0; + let who: ::AccountId = coldkey_account_id; + let burn_cost = 1000; + // Set the burn cost + SubtensorModule::set_burn(netuid, burn_cost); - let (nonce, work) = SubtensorModule::create_work_for_block_number( - netuid, - block_number, - 129123813, - &hotkey_account_id, - ); + add_network(netuid, 13, 0); // Add the network + // Give it some TAO to the coldkey balance; more than the burn cost + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, burn_cost + 10_000); - let max_registrants = 2; - SubtensorModule::set_target_registrations_per_interval(netuid, max_registrants); + let target_registrants = 2; + let max_registrants = target_registrants * 3; // Maximum is 3 times the target + SubtensorModule::set_target_registrations_per_interval(netuid, target_registrants); let call_burned_register: pallet_subtensor::Call = pallet_subtensor::Call::burned_register { @@ -317,21 +317,15 @@ fn test_burned_registration_under_limit() { extension.validate(&who, &call_burned_register.into(), &info, 10); assert_ok!(burned_register_result); - add_network(netuid, 13, 0); //actually call register - assert_ok!(SubtensorModule::register( - <::RuntimeOrigin>::signed(hotkey_account_id), + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), netuid, - block_number, - nonce, - work, hotkey_account_id, - coldkey_account_id )); let current_registrants = SubtensorModule::get_registrations_this_interval(netuid); - let target_registrants = SubtensorModule::get_target_registrations_per_interval(netuid); - assert!(current_registrants <= target_registrants); + assert!(current_registrants <= max_registrants); }); } @@ -340,11 +334,15 @@ fn test_burned_registration_rate_limit_exceeded() { new_test_ext(1).execute_with(|| { let netuid: u16 = 1; let hotkey_account_id: U256 = U256::from(1); - let who: ::AccountId = hotkey_account_id; - let max_registrants = 1; + let coldkey_account_id = U256::from(667); + let who: ::AccountId = coldkey_account_id; - SubtensorModule::set_target_registrations_per_interval(netuid, max_registrants); - SubtensorModule::set_registrations_this_interval(netuid, 1); + let target_registrants = 1; + let max_registrants = target_registrants * 3; // Maximum is 3 times the target + + SubtensorModule::set_target_registrations_per_interval(netuid, target_registrants); + // Set the current registrations to the maximum; should not be able to register more + SubtensorModule::set_registrations_this_interval(netuid, max_registrants); let call_burned_register: pallet_subtensor::Call = pallet_subtensor::Call::burned_register { @@ -369,6 +367,55 @@ fn test_burned_registration_rate_limit_exceeded() { }); } +#[test] +fn test_burned_registration_rate_allows_burn_adjustment() { + // We need to be able to register more than the *target* registrations per interval + new_test_ext().execute_with(|| { + let netuid: u16 = 1; + let hotkey_account_id: U256 = U256::from(1); + let coldkey_account_id = U256::from(667); + let who: ::AccountId = coldkey_account_id; + + let burn_cost = 1000; + // Set the burn cost + SubtensorModule::set_burn(netuid, burn_cost); + + add_network(netuid, 13, 0); // Add the network + // Give it some TAO to the coldkey balance; more than the burn cost + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, burn_cost + 10_000); + + let target_registrants = 1; // Target is 1, but we can register more than that, up to some maximum. + SubtensorModule::set_target_registrations_per_interval(netuid, target_registrants); + // Set the current registrations to above the target; we should be able to register at least 1 more + SubtensorModule::set_registrations_this_interval(netuid, target_registrants); + + // Register one more, so the current registrations are above the target + let call_burned_register: pallet_subtensor::Call = + pallet_subtensor::Call::burned_register { + netuid, + hotkey: hotkey_account_id, + }; + + let info: DispatchInfo = + DispatchInfoOf::<::RuntimeCall>::default(); + let extension = SubtensorSignedExtension::::new(); + //does not actually call register + let burned_register_result = + extension.validate(&who, &call_burned_register.into(), &info, 10); + assert_ok!(burned_register_result); + + //actually call register + assert_ok!(SubtensorModule::burned_register( + <::RuntimeOrigin>::signed(coldkey_account_id), + netuid, + hotkey_account_id + )); + + let current_registrants = SubtensorModule::get_registrations_this_interval(netuid); + assert!(current_registrants > target_registrants); // Should be able to register more than the target + }); +} + #[test] fn test_burned_registration_ok() { new_test_ext(1).execute_with(|| { From ac0be46971696743f6447263a6b370fd88fb7b82 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 09:43:14 -0400 Subject: [PATCH 06/11] fix test for POW register --- pallets/subtensor/tests/registration.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pallets/subtensor/tests/registration.rs b/pallets/subtensor/tests/registration.rs index fa4494f2c..540e004ec 100644 --- a/pallets/subtensor/tests/registration.rs +++ b/pallets/subtensor/tests/registration.rs @@ -249,9 +249,10 @@ fn test_registration_rate_limit_exceeded() { let coldkey_account_id = U256::from(667); let who: ::AccountId = hotkey_account_id; - let max_registrants = 1; - SubtensorModule::set_target_registrations_per_interval(netuid, max_registrants); - SubtensorModule::set_registrations_this_interval(netuid, 1); + let target_registrants = 1; + let max_registrants = target_registrants * 3; + SubtensorModule::set_target_registrations_per_interval(netuid, target_registrants); + SubtensorModule::set_registrations_this_interval(netuid, max_registrants); let (nonce, work) = SubtensorModule::create_work_for_block_number( netuid, @@ -370,7 +371,7 @@ fn test_burned_registration_rate_limit_exceeded() { #[test] fn test_burned_registration_rate_allows_burn_adjustment() { // We need to be able to register more than the *target* registrations per interval - new_test_ext().execute_with(|| { + new_test_ext(1).execute_with(|| { let netuid: u16 = 1; let hotkey_account_id: U256 = U256::from(1); let coldkey_account_id = U256::from(667); From 94a55e6844881a5477c981bb7812d9f715b5b7db Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 09:47:15 -0400 Subject: [PATCH 07/11] bump spec --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index b7cc7bea4..921237ae7 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -137,7 +137,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 149, + spec_version: 150, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From dc401bc363bf4c94d26a02d388aea136bdf10434 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 09:59:23 -0400 Subject: [PATCH 08/11] Update pallets/subtensor/src/staking.rs --- pallets/subtensor/src/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 1c99fa040..dd7f78367 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -443,7 +443,7 @@ impl Pallet { Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed); // If the stake is below the minimum, we clear the nomination from storage. - // Not, this only applies to nominator stakes. + // This only applies to nominator stakes. // If the coldkey does not own the hotkey, it's a nominator stake. let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey); Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake); From d30411e89d92fba6973772f240a4e7ed8220cb81 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 23 May 2024 18:47:48 +0400 Subject: [PATCH 09/11] remove applied migrations --- runtime/src/lib.rs | 13 +- .../src/migrations/account_data_migration.rs | 205 ------------------ .../src/migrations/init_storage_versions.rs | 26 --- runtime/src/migrations/mod.rs | 4 +- 4 files changed, 4 insertions(+), 244 deletions(-) delete mode 100644 runtime/src/migrations/account_data_migration.rs delete mode 100644 runtime/src/migrations/init_storage_versions.rs diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 921237ae7..0c09e2145 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -10,16 +10,12 @@ mod migrations; use codec::{Decode, Encode, MaxEncodedLen}; -use migrations::{account_data_migration, init_storage_versions}; use pallet_commitments::CanCommit; use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, }; -use frame_support::{ - pallet_prelude::{DispatchError, DispatchResult, Get}, - traits::OnRuntimeUpgrade, -}; +use frame_support::pallet_prelude::{DispatchError, DispatchResult, Get}; use frame_system::{EnsureNever, EnsureRoot, RawOrigin}; use pallet_registry::CanRegisterIdentity; @@ -1171,12 +1167,7 @@ pub type SignedExtra = ( pallet_commitments::CommitmentsSignedExtension, ); -type Migrations = ( - init_storage_versions::Migration, - account_data_migration::Migration, - pallet_multisig::migrations::v1::MigrateToV1, - pallet_preimage::migration::v1::Migration, -); +type Migrations = (); // Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = diff --git a/runtime/src/migrations/account_data_migration.rs b/runtime/src/migrations/account_data_migration.rs deleted file mode 100644 index 5db5c3261..000000000 --- a/runtime/src/migrations/account_data_migration.rs +++ /dev/null @@ -1,205 +0,0 @@ -use crate::*; -use frame_support::log; -use pallet_balances::ExtraFlags; - -#[cfg(feature = "try-runtime")] -use sp_std::collections::btree_map::BTreeMap; - -mod prev { - use super::*; - use frame_support::{pallet_prelude::ValueQuery, storage_alias, Blake2_128Concat}; - - #[derive( - Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen, - )] - pub struct AccountDataStruct { - pub free: Balance, - pub reserved: Balance, - pub misc_frozen: Balance, - pub fee_frozen: Balance, - } - - #[derive( - Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen, - )] - pub struct AccountStruct { - pub nonce: u32, - pub consumers: u32, - pub providers: u32, - pub sufficients: u32, - pub data: AccountDataStruct, - } - - #[storage_alias] - pub type Account = StorageMap< - frame_system::pallet::Pallet, - Blake2_128Concat, - AccountId, - AccountStruct, - ValueQuery, - >; -} - -#[cfg(feature = "try-runtime")] -#[derive(Encode, Decode)] -enum PreUpgradeInfo { - MigrationAlreadyOccured, - MigrationShouldRun( - BTreeMap>>, - ), -} - -const TARGET: &str = "runtime::account_data_migration"; -pub struct Migration; -impl OnRuntimeUpgrade for Migration { - /// Save pre-upgrade account ids to check are decodable post-upgrade. - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - // Skip migration if it already took place. - if migration_already_occured() { - return Ok(PreUpgradeInfo::MigrationAlreadyOccured.encode()); - } - - log::info!(target: TARGET, "pre-upgrade"); - // Save the expected post-migration account state. - let mut expected_account: BTreeMap< - AccountId, - frame_system::AccountInfo>, - > = BTreeMap::new(); - - for (acc_id, acc) in prev::Account::::iter() { - let expected_data = pallet_balances::AccountData { - free: acc.data.free, - reserved: acc.data.reserved, - frozen: acc.data.misc_frozen.saturating_add(acc.data.fee_frozen), - flags: ExtraFlags::default(), - }; - - // `ensure_upgraded` bumps the consumers if there is a non zero reserved balance and no frozen balance. - // https://github.com/paritytech/polkadot-sdk/blob/305d311d5c732fcc4629f3295768f1ed44ef434c/substrate/frame/balances/src/lib.rs#L785 - let expected_consumers = if acc.data.reserved > 0 && expected_data.frozen == 0 { - acc.consumers + 1 - } else { - acc.consumers - }; - let expected_acc = frame_system::AccountInfo { - nonce: acc.nonce, - consumers: expected_consumers, - providers: acc.providers, - sufficients: acc.sufficients, - data: expected_data, - }; - expected_account.insert(acc_id, expected_acc); - } - - Ok(PreUpgradeInfo::MigrationShouldRun(expected_account).encode()) - } - - /// Migrates Account storage to the new format, and calls `ensure_upgraded` for them. - fn on_runtime_upgrade() -> Weight { - // Skip migration if it already took place. - if migration_already_occured() { - log::warn!( - target: TARGET, - "Migration already completed and can be removed.", - ); - return ::DbWeight::get().reads_writes(0u64, 0u64); - } - - // Pull the storage in the previous format into memory - let accounts = prev::Account::::iter().collect::>(); - log::info!(target: TARGET, "Migrating {} accounts...", accounts.len()); - - for (acc_id, acc_info) in accounts.clone().into_iter() { - let prev_data = acc_info.clone().data; - - // Move account to new data format - let new_data = pallet_balances::AccountData { - free: prev_data.free, - reserved: prev_data.reserved, - frozen: prev_data.misc_frozen.saturating_add(prev_data.fee_frozen), - flags: ExtraFlags::old_logic(), - }; - let new_account = frame_system::AccountInfo { - nonce: acc_info.nonce, - consumers: acc_info.consumers, - providers: acc_info.providers, - sufficients: acc_info.sufficients, - data: new_data, - }; - frame_system::pallet::Account::::insert(acc_id.clone(), new_account); - - // Ensure upgraded - pallet_balances::Pallet::::ensure_upgraded(&acc_id); - } - - log::info!(target: TARGET, "Migrated {} accounts ✅", accounts.len()); - - // R/W not important for solo chain. - ::DbWeight::get().reads_writes(0u64, 0u64) - } - - /// Ensures post-upgrade that every Account entry matches what is expected. - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { - use frame_support::ensure; - - log::info!(target: TARGET, "Running post-upgrade..."); - - let pre_upgrade_data: PreUpgradeInfo = - Decode::decode(&mut &state[..]).expect("decoding state failed"); - - match pre_upgrade_data { - PreUpgradeInfo::MigrationAlreadyOccured => Ok(()), - PreUpgradeInfo::MigrationShouldRun(expected_accounts) => { - // Ensure the actual post-migration state matches the expected - for (acc_id, acc) in frame_system::pallet::Account::::iter() { - let expected = expected_accounts.get(&acc_id).expect("account not found"); - - // New system logic nukes the account if no providers or sufficients. - if acc.providers > 0 || acc.sufficients > 0 { - ensure!(acc.nonce == expected.nonce, "nonce mismatch"); - ensure!(acc.consumers == expected.consumers, "consumers mismatch"); - ensure!(acc.providers == expected.providers, "providers mismatch"); - ensure!( - acc.sufficients == expected.sufficients, - "sufficients mismatch" - ); - ensure!(acc.data.free == expected.data.free, "data.free mismatch"); - ensure!( - acc.data.reserved == expected.data.reserved, - "data.reserved mismatch" - ); - ensure!( - acc.data.frozen == expected.data.frozen, - "data.frozen mismatch" - ); - ensure!(acc.data.flags == expected.data.flags, "data.flags mismatch"); - } - } - - log::info!(target: TARGET, "post-upgrade success ✅"); - Ok(()) - } - } - } -} - -/// Check if the migration already took place. The migration is all-or-nothing, so if one -/// account is migrated we know that the rest also must be migrated. -/// -/// We can use the nonce to check if it's been migrated, because it being zero meant that -/// the decode succeeded and this account has been migrated already. -/// -/// Performance note: While this may appear poor for performance due to touching all keys, -/// these keys will need to be touched anyway, so it's fine to just touch them loading them into -/// the storage overlay here. -fn migration_already_occured() -> bool { - for (_, acc) in frame_system::pallet::Account::::iter() { - if acc.nonce > 0 { - return true; - }; - } - - false -} diff --git a/runtime/src/migrations/init_storage_versions.rs b/runtime/src/migrations/init_storage_versions.rs deleted file mode 100644 index 9ad0f9b2a..000000000 --- a/runtime/src/migrations/init_storage_versions.rs +++ /dev/null @@ -1,26 +0,0 @@ -use crate::*; - -/// Init the on-chain storage versions of pallets added to the runtime prior to this being an automatic process. -pub struct Migration; - -impl OnRuntimeUpgrade for Migration { - fn on_runtime_upgrade() -> Weight { - use frame_support::traits::GetStorageVersion; - use frame_support::traits::StorageVersion; - - if Triumvirate::on_chain_storage_version() == StorageVersion::new(0) { - Triumvirate::current_storage_version().put::(); - } - if TriumvirateMembers::on_chain_storage_version() == StorageVersion::new(0) { - TriumvirateMembers::current_storage_version().put::(); - } - if SenateMembers::on_chain_storage_version() == StorageVersion::new(0) { - SenateMembers::current_storage_version().put::(); - } - if Scheduler::on_chain_storage_version() == StorageVersion::new(0) { - Scheduler::current_storage_version().put::(); - } - - ::DbWeight::get().reads_writes(4, 4) - } -} diff --git a/runtime/src/migrations/mod.rs b/runtime/src/migrations/mod.rs index 494f81fa9..e5a1281f7 100644 --- a/runtime/src/migrations/mod.rs +++ b/runtime/src/migrations/mod.rs @@ -1,2 +1,2 @@ -pub mod account_data_migration; -pub mod init_storage_versions; +//! Module for standalone migrations + From 53a02dfde5d839ddf2ab7855da540060245990f1 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 23 May 2024 18:55:43 +0400 Subject: [PATCH 10/11] fix: fmt --- runtime/src/migrations/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/src/migrations/mod.rs b/runtime/src/migrations/mod.rs index e5a1281f7..8d54881c1 100644 --- a/runtime/src/migrations/mod.rs +++ b/runtime/src/migrations/mod.rs @@ -1,2 +1 @@ //! Module for standalone migrations - From 970e3ec97681ea0b97f857e328e2159e935c17fe Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 23 May 2024 11:13:17 -0400 Subject: [PATCH 11/11] revert change to initial mock config --- pallets/subtensor/tests/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/tests/mock.rs b/pallets/subtensor/tests/mock.rs index f90cd5d88..9b964fcb0 100644 --- a/pallets/subtensor/tests/mock.rs +++ b/pallets/subtensor/tests/mock.rs @@ -141,7 +141,7 @@ parameter_types! { pub const InitialAdjustmentInterval: u16 = 100; pub const InitialAdjustmentAlpha: u64 = 0; // no weight to previous value. pub const InitialMaxRegistrationsPerBlock: u16 = 3; - pub const InitialTargetRegistrationsPerInterval: u16 = 3; + pub const InitialTargetRegistrationsPerInterval: u16 = 2; pub const InitialPruningScore : u16 = u16::MAX; pub const InitialRegistrationRequirement: u16 = u16::MAX; // Top 100% pub const InitialMinDifficulty: u64 = 1;