Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pending childkeys #1050

Merged
merged 13 commits into from
Dec 6, 2024
4 changes: 2 additions & 2 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@ pub mod pallet {
/// The extrinsic will call the Subtensor pallet to set the weights min stake.
#[pallet::call_index(42)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_weights_min_stake(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
pub fn sudo_set_stake_threshold(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
ensure_root(origin)?;
pallet_subtensor::Pallet::<T>::set_weights_min_stake(min_stake);
pallet_subtensor::Pallet::<T>::set_stake_threshold(min_stake);
Ok(())
}

Expand Down
12 changes: 6 additions & 6 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,23 +682,23 @@ fn test_sudo_set_max_allowed_validators() {
}

#[test]
fn test_sudo_set_weights_min_stake() {
fn test_sudo_set_stake_threshold() {
new_test_ext().execute_with(|| {
let to_be_set: u64 = 10;
let init_value: u64 = SubtensorModule::get_weights_min_stake();
let init_value: u64 = SubtensorModule::get_stake_threshold();
assert_eq!(
AdminUtils::sudo_set_weights_min_stake(
AdminUtils::sudo_set_stake_threshold(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
to_be_set
),
Err(DispatchError::BadOrigin)
);
assert_eq!(SubtensorModule::get_weights_min_stake(), init_value);
assert_ok!(AdminUtils::sudo_set_weights_min_stake(
assert_eq!(SubtensorModule::get_stake_threshold(), init_value);
assert_ok!(AdminUtils::sudo_set_stake_threshold(
<<Test as Config>::RuntimeOrigin>::root(),
to_be_set
));
assert_eq!(SubtensorModule::get_weights_min_stake(), to_be_set);
assert_eq!(SubtensorModule::get_stake_threshold(), to_be_set);
});
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl<T: Config> Pallet<T> {

// Check to see if the hotkey has enough stake to set weights.
ensure!(
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake(),
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_stake_threshold(),
Error::<T>::NotEnoughStakeToSetWeights
);

Expand Down
3 changes: 3 additions & 0 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ impl<T: Config> Pallet<T> {
);
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
}

// 4.5 Apply pending childkeys of this subnet for the next epoch
Self::do_set_pending_children(*netuid);
} else {
// No epoch, increase blocks since last step and continue
Self::set_blocks_since_last_step(
Expand Down
35 changes: 32 additions & 3 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ pub mod pallet {
vec![]
}
#[pallet::type_value]
/// Default pending childkeys
pub fn DefaultPendingChildkeys<T: Config>() -> (Vec<(u64, T::AccountId)>, u64) {
(vec![], 0)
}
#[pallet::type_value]
/// Default account linkage
pub fn DefaultProportion<T: Config>() -> u64 {
0
Expand Down Expand Up @@ -576,7 +581,7 @@ pub mod pallet {
}
#[pallet::type_value]
/// Default minimum stake for weights.
pub fn DefaultWeightsMinStake<T: Config>() -> u64 {
pub fn DefaultStakeThreshold<T: Config>() -> u64 {
0
}
#[pallet::type_value]
Expand Down Expand Up @@ -677,6 +682,18 @@ pub mod pallet {
T::InitialColdkeySwapScheduleDuration::get()
}

#[pallet::type_value]
/// Default value for applying pending items (e.g. childkeys).
pub fn DefaultPendingCooldown<T: Config>() -> u64 {
7200
}

#[pallet::type_value]
/// Default minimum stake for setting childkeys.
pub fn DefaultChildkeysMinStake<T: Config>() -> u64 {
1_000_000_000_000
}

#[pallet::storage]
pub type ColdkeySwapScheduleDuration<T: Config> =
StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultColdkeySwapScheduleDuration<T>>;
Expand Down Expand Up @@ -824,6 +841,18 @@ pub mod pallet {
DefaultStakeDelta<T>,
>;
#[pallet::storage]
/// DMAP ( netuid, parent ) --> (Vec<(proportion,child)>, cool_down_block)
pub type PendingChildKeys<T: Config> = StorageDoubleMap<
_,
Identity,
u16,
Blake2_128Concat,
T::AccountId,
(Vec<(u64, T::AccountId)>, u64),
ValueQuery,
DefaultPendingChildkeys<T>,
>;
#[pallet::storage]
/// DMAP ( parent, netuid ) --> Vec<(proportion,child)>
pub type ChildKeys<T: Config> = StorageDoubleMap<
_,
Expand Down Expand Up @@ -1270,7 +1299,7 @@ pub mod pallet {
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
#[pallet::storage]
/// ITEM( weights_min_stake )
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
pub type StakeThreshold<T> = StorageValue<_, u64, ValueQuery, DefaultStakeThreshold<T>>;
#[pallet::storage]
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block, first_reveal_block, last_reveal_block)> | Stores a queue of commits for an account on a given netuid.
pub type WeightCommits<T: Config> = StorageDoubleMap<
Expand Down Expand Up @@ -1342,7 +1371,7 @@ pub mod pallet {
/// Is the caller allowed to set weights
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
// Blacklist weights transactions for low stake peers.
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold()
}

/// Helper function to check if register is allowed
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ mod dispatches {
netuid: u16,
children: Vec<(u64, T::AccountId)>,
) -> DispatchResultWithPostInfo {
Self::do_set_children(origin, hotkey, netuid, children)?;
Self::do_schedule_children(origin, hotkey, netuid, children)?;
Ok(().into())
}

Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ mod errors {
/// The caller is requesting to set weights but the caller has less than minimum stake
/// required to set weights (less than WeightsMinStake).
NotEnoughStakeToSetWeights,
/// The parent hotkey doesn't have enough own stake to set childkeys.
NotEnoughStakeToSetChildkeys,
/// The caller is requesting adding more stake than there exists in the coldkey account.
/// See: "[add_stake()]"
NotEnoughBalanceToStake,
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod events {
/// setting the RAO recycled for registration.
RAORecycledForRegistrationSet(u16, u64),
/// min stake is set for validators to set weights.
WeightsMinStake(u64),
StakeThresholdSet(u64),
/// setting the minimum required stake amount for senate registration.
SenateRequiredStakePercentSet(u64),
/// setting the adjustment alpha on a subnet.
Expand Down Expand Up @@ -179,6 +179,8 @@ mod events {
/// The account ID of the coldkey
coldkey: T::AccountId,
},
/// Setting of children of a hotkey have been scheduled
SetChildrenScheduled(T::AccountId, u16, u64, Vec<(u64, T::AccountId)>),
/// The children of a hotkey have been set
SetChildren(T::AccountId, u16, Vec<(u64, T::AccountId)>),
/// The hotkey emission tempo has been set
Expand Down
48 changes: 48 additions & 0 deletions pallets/subtensor/src/migrations/migrate_stake_threshold.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::*;
use crate::HasMigrationRun;
use frame_support::pallet_prelude::ValueQuery;
use frame_support::storage_alias;
use frame_support::{traits::Get, weights::Weight};
use scale_info::prelude::string::String;

/// Module containing deprecated storage format for WeightsMinStake
pub mod deprecated_weights_min_stake {
use super::*;

#[storage_alias]
pub(super) type WeightsMinStake<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;
}

pub fn migrate_stake_threshold<T: Config>() -> Weight {
let migration_name = b"migrate_stake_threshold".to_vec();
let mut weight = T::DbWeight::get().reads(1);

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

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

let min_stake = deprecated_weights_min_stake::WeightsMinStake::<T>::get();
StakeThreshold::<T>::set(min_stake);
deprecated_weights_min_stake::WeightsMinStake::<T>::kill();

weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));

HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed successfully.",
String::from_utf8_lossy(&migration_name)
);

gztensor marked this conversation as resolved.
Show resolved Hide resolved
weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod migrate_fix_total_coldkey_stake;
pub mod migrate_init_total_issuance;
pub mod migrate_populate_owned_hotkeys;
pub mod migrate_populate_staking_hotkeys;
pub mod migrate_stake_threshold;
pub mod migrate_to_v1_separate_emission;
pub mod migrate_to_v2_fixed_total_stake;
pub mod migrate_total_issuance;
Expand Down
7 changes: 7 additions & 0 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ impl<T: Config> Pallet<T> {
let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake);

// Check if stake lowered below MinStake and remove Pending children if it did
if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::<T>::get() {
Self::get_all_subnet_netuids().iter().for_each(|netuid| {
PendingChildKeys::<T>::remove(netuid, &hotkey);
})
}

// Set last block for rate limiting
let block: u64 = Self::get_current_block_as_u64();
Self::set_last_tx_block(&coldkey, block);
Expand Down
Loading
Loading