Skip to content

Commit

Permalink
Merge branch 'main' into feat/check-metadata-hash-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
camfairchild committed Jul 18, 2024
2 parents c80df3d + 5adbbd9 commit 4a4adfd
Show file tree
Hide file tree
Showing 15 changed files with 1,158 additions and 291 deletions.
4 changes: 2 additions & 2 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ parameter_types! {
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
pub const InitialNetworkRateLimit: u64 = 0;
pub const InitialTargetStakesPerInterval: u16 = 1;
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
pub const InitialKeySwapCost: u64 = 1_000_000_000;
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
Expand Down Expand Up @@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test {
type InitialSubnetLimit = InitialSubnetLimit;
type InitialNetworkRateLimit = InitialNetworkRateLimit;
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
type HotkeySwapCost = InitialHotkeySwapCost;
type KeySwapCost = InitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
Expand Down
80 changes: 27 additions & 53 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,33 +314,33 @@ benchmarks! {
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
}: dissolve_network(RawOrigin::Signed(coldkey), 1)

swap_hotkey {
let seed: u32 = 1;
let coldkey: T::AccountId = account("Alice", 0, seed);
let old_hotkey: T::AccountId = account("Bob", 0, seed);
let new_hotkey: T::AccountId = account("Charlie", 0, seed);

let netuid = 1u16;
Subtensor::<T>::init_new_network(netuid, 100);
Subtensor::<T>::set_min_burn(netuid, 1);
Subtensor::<T>::set_max_burn(netuid, 1);
Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
Subtensor::<T>::set_max_registrations_per_block(netuid, 256);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));

let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
for i in 0..max_uids - 1 {
let coldkey: T::AccountId = account("Axon", 0, i);
let hotkey: T::AccountId = account("Hotkey", 0, i);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
}
}: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
// swap_hotkey {
// let seed: u32 = 1;
// let coldkey: T::AccountId = account("Alice", 0, seed);
// let old_hotkey: T::AccountId = account("Bob", 0, seed);
// let new_hotkey: T::AccountId = account("Charlie", 0, seed);

// let netuid = 1u16;
// Subtensor::<T>::init_new_network(netuid, 100);
// Subtensor::<T>::set_min_burn(netuid, 1);
// Subtensor::<T>::set_max_burn(netuid, 1);
// Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
// Subtensor::<T>::set_max_registrations_per_block(netuid, 256);

// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
// assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));

// let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
// for i in 0..max_uids - 1 {
// let coldkey: T::AccountId = account("Axon", 0, i);
// let hotkey: T::AccountId = account("Hotkey", 0, i);

// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
// assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
// }
// }: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)

commit_weights {
let tempo: u16 = 1;
Expand Down Expand Up @@ -429,30 +429,4 @@ reveal_weights {

}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)

schedule_coldkey_swap {
let seed: u32 = 1;
let old_coldkey: T::AccountId = account("OldColdkey", 0, seed);
let new_coldkey: T::AccountId = account("NewColdkey", 0, seed + 1);
let hotkey: T::AccountId = account("Hotkey", 0, seed);

let netuid = 1u16;
let tempo = 1u16;
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
let nonce = 0;

// Initialize the network
Subtensor::<T>::init_new_network(netuid, tempo);
Subtensor::<T>::set_network_registration_allowed(netuid, true);

// Add balance to the old coldkey account
let amount_to_be_staked: u64 = 1000000u32.into();
Subtensor::<T>::add_balance_to_coldkey_account(&old_coldkey.clone(), amount_to_be_staked+1000000000);
// Burned register the hotkey with the old coldkey
assert_ok!(Subtensor::<T>::burned_register(
RawOrigin::Signed(old_coldkey.clone()).into(),
netuid,
hotkey.clone()
));

}: schedule_coldkey_swap(RawOrigin::Signed(old_coldkey.clone()), new_coldkey.clone(), vec![], block_number, nonce)
}
2 changes: 2 additions & 0 deletions pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ mod errors {
CommitRevealEnabled,
/// Attemtping to commit/reveal weights when disabled.
CommitRevealDisabled,
/// Not able to join the senate.
CouldNotJoinSenate,
/// Attempting to set alpha high/low while disabled
LiquidAlphaDisabled,
/// Alpha high is too low: alpha_high > 0.8
Expand Down
7 changes: 7 additions & 0 deletions pallets/subtensor/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ mod events {
MinDelegateTakeSet(u16),
/// the target stakes per interval is set by sudo/admin transaction
TargetStakesPerIntervalSet(u64),
/// a member of the senate is adjusted
SenateAdjusted {
/// the account ID of the old senate member, if any
old_member: Option<T::AccountId>,
/// the account ID of the new senate member
new_member: T::AccountId,
},
/// A coldkey has been swapped
ColdkeySwapped {
/// the account ID of old coldkey
Expand Down
55 changes: 40 additions & 15 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub mod pallet {

/// Tracks version for migrations. Should be monotonic with respect to the
/// order of migrations. (i.e. always increasing)
const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);

/// Minimum balance required to perform a coldkey swap
pub const MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP: u64 = 100_000_000; // 0.1 TAO in RAO
Expand Down Expand Up @@ -247,7 +247,7 @@ pub mod pallet {
type InitialTargetStakesPerInterval: Get<u64>;
/// Cost of swapping a hotkey.
#[pallet::constant]
type HotkeySwapCost: Get<u64>;
type KeySwapCost: Get<u64>;
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
#[pallet::constant]
type AlphaHigh: Get<u16>;
Expand Down Expand Up @@ -1140,6 +1140,9 @@ pub mod pallet {
DefaultBonds<T>,
>;

#[pallet::storage] // --- Storage for migration run status
pub type HasMigrationRun<T: Config> = StorageMap<_, Identity, Vec<u8>, bool, ValueQuery>;

/// ==================
/// ==== Genesis =====
/// ==================
Expand Down Expand Up @@ -1419,7 +1422,9 @@ pub mod pallet {
// Populate OwnedHotkeys map for coldkey swap. Doesn't update storage vesion.
.saturating_add(migration::migrate_populate_owned::<T>())
// Populate StakingHotkeys map for coldkey swap. Doesn't update storage vesion.
.saturating_add(migration::migrate_populate_staking_hotkeys::<T>());
.saturating_add(migration::migrate_populate_staking_hotkeys::<T>())
// Fix total coldkey stake.
.saturating_add(migration::migrate_fix_total_coldkey_stake::<T>());

weight
}
Expand Down Expand Up @@ -2038,6 +2043,15 @@ pub mod pallet {
Self::do_root_register(origin, hotkey)
}

/// Attempt to adjust the senate membership to include a hotkey
#[pallet::call_index(63)]
#[pallet::weight((Weight::from_parts(0, 0)
.saturating_add(T::DbWeight::get().reads(0))
.saturating_add(T::DbWeight::get().writes(0)), DispatchClass::Normal, Pays::Yes))]
pub fn adjust_senate(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
Self::do_adjust_senate(origin, hotkey)
}

/// User register a new subnetwork via burning token
#[pallet::call_index(7)]
#[pallet::weight((Weight::from_parts(177_000_000, 0)
Expand All @@ -2052,17 +2066,17 @@ pub mod pallet {
}

/// The extrinsic for user to change its hotkey
#[pallet::call_index(70)]
#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
.saturating_add(T::DbWeight::get().reads(272))
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
pub fn swap_hotkey(
origin: OriginFor<T>,
hotkey: T::AccountId,
new_hotkey: T::AccountId,
) -> DispatchResultWithPostInfo {
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
}
///#[pallet::call_index(70)]
///#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
///.saturating_add(T::DbWeight::get().reads(272))
///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
///pub fn swap_hotkey(
/// origin: OriginFor<T>,
/// hotkey: T::AccountId,
/// new_hotkey: T::AccountId,
///) -> DispatchResultWithPostInfo {
/// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
///}
/// The extrinsic for user to change the coldkey associated with their account.
///
Expand All @@ -2089,7 +2103,6 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
Self::do_swap_coldkey(origin, &new_coldkey)
}

/// Unstakes all tokens associated with a hotkey and transfers them to a new coldkey.
///
/// # Arguments
Expand All @@ -2105,6 +2118,7 @@ pub mod pallet {
/// # Weight
///
/// Weight is calculated based on the number of database reads and writes.
#[cfg(test)]
#[pallet::call_index(72)]
#[pallet::weight((Weight::from_parts(21_000_000, 0)
.saturating_add(T::DbWeight::get().reads(3))
Expand Down Expand Up @@ -2244,6 +2258,17 @@ pub mod pallet {
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
Self::user_remove_network(origin, netuid)
}

/// Sets values for liquid alpha
#[pallet::call_index(64)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_hotfix_swap_coldkey_delegates(
_origin: OriginFor<T>,
_old_coldkey: T::AccountId,
_new_coldkey: T::AccountId,
) -> DispatchResult {
Ok(())
}
}

// ---- Subtensor helper functions.
Expand Down
80 changes: 80 additions & 0 deletions pallets/subtensor/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use alloc::string::String;
use frame_support::traits::DefensiveResult;
use frame_support::{
pallet_prelude::{Identity, OptionQuery},
Expand All @@ -24,6 +25,85 @@ pub mod deprecated_loaded_emission_format {
StorageMap<Pallet<T>, Identity, u16, Vec<(AccountIdOf<T>, u64)>, OptionQuery>;
}

/// Migrates and fixes the total coldkey stake.
///
/// This function iterates through all staking hotkeys, calculates the total stake for each coldkey,
/// and updates the `TotalColdkeyStake` storage accordingly. The migration is only performed if the
/// on-chain storage version is 6.
///
/// # Returns
/// The weight of the migration process.
pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
// Initialize the weight with one read operation.
let mut weight = T::DbWeight::get().reads(1);

// Iterate through all staking hotkeys.
for (coldkey, hotkey_vec) in StakingHotkeys::<T>::iter() {
// Init the zero value.
let mut coldkey_stake_sum: u64 = 0;
weight = weight.saturating_add(T::DbWeight::get().reads(1));

// Calculate the total stake for the current coldkey.
for hotkey in hotkey_vec {
// Cant fail on retrieval.
coldkey_stake_sum =
coldkey_stake_sum.saturating_add(Stake::<T>::get(hotkey, coldkey.clone()));
weight = weight.saturating_add(T::DbWeight::get().reads(1));
}
// Update the `TotalColdkeyStake` storage with the calculated stake sum.
// Cant fail on insert.
TotalColdkeyStake::<T>::insert(coldkey.clone(), coldkey_stake_sum);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}
weight
}

/// Migrates and fixes the total coldkey stake.
///
/// This function checks if the migration has already run, and if not, it performs the migration
/// to fix the total coldkey stake. It also marks the migration as completed after running.
///
/// # Returns
/// The weight of the migration process.
pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
let migration_name = b"fix_total_coldkey_stake_v7".to_vec();

// Initialize the weight with one read operation.
let mut weight = T::DbWeight::get().reads(1);

// Check if the migration has already run
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return Weight::zero();
}

log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// Run the migration
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());

// Mark the migration as completed
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

// Set the storage version to 7
StorageVersion::new(7).put::<Pallet<T>>();
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed. Storage version set to 7.",
String::from_utf8_lossy(&migration_name)
);

// Return the migration weight.
weight
}
/// Performs migration to update the total issuance based on the sum of stakes and total balances.
/// This migration is applicable only if the current storage version is 5, after which it updates the storage version to 6.
///
Expand Down
Loading

0 comments on commit 4a4adfd

Please sign in to comment.