Skip to content

Commit 8eb7bd9

Browse files
constconst
const
authored and
const
committed
merge childkeys
1 parent 8941385 commit 8eb7bd9

File tree

18 files changed

+162
-2053
lines changed

18 files changed

+162
-2053
lines changed

pallets/admin-utils/src/benchmarking.rs

+8
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,13 @@ mod benchmarks {
248248
_(RawOrigin::Root, 1u64/*emission_tempo*/)/*set_hotkey_emission_tempo*/;
249249
}
250250

251+
#[benchmark]
252+
fn sudo_set_network_max_stake() {
253+
T::Subtensor::init_new_network(1u16 /*netuid*/, 1u16 /*tempo*/);
254+
255+
#[extrinsic_call]
256+
_(RawOrigin::Root, 1u16/*netuid*/, 1_000_000_000_000_000u64/*max_stake*/)/*sudo_set_network_max_stake*/;
257+
}
258+
251259
//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
252260
}

pallets/admin-utils/src/lib.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,9 @@ pub mod pallet {
10501050
///
10511051
/// # Errors
10521052
/// * `DispatchError::BadOrigin` - If the origin is not the root account.
1053+
// #[pallet::weight(T::WeightInfo::sudo_set_hotkey_emission_tempo())]
10531054
#[pallet::call_index(52)]
1054-
#[pallet::weight(T::WeightInfo::sudo_set_hotkey_emission_tempo())]
1055+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
10551056
pub fn sudo_set_hotkey_emission_tempo(
10561057
origin: OriginFor<T>,
10571058
emission_tempo: u64,
@@ -1064,6 +1065,57 @@ pub mod pallet {
10641065
);
10651066
Ok(())
10661067
}
1068+
1069+
/// Sets the maximum stake allowed for a specific network.
1070+
///
1071+
/// This function allows the root account to set the maximum stake for a given network.
1072+
/// It updates the network's maximum stake value and logs the change.
1073+
///
1074+
/// # Arguments
1075+
///
1076+
/// * `origin` - The origin of the call, which must be the root account.
1077+
/// * `netuid` - The unique identifier of the network.
1078+
/// * `max_stake` - The new maximum stake value to set.
1079+
///
1080+
/// # Returns
1081+
///
1082+
/// Returns `Ok(())` if the operation is successful, or an error if it fails.
1083+
///
1084+
/// # Example
1085+
///
1086+
///
1087+
/// # Notes
1088+
///
1089+
/// - This function can only be called by the root account.
1090+
/// - The `netuid` should correspond to an existing network.
1091+
///
1092+
/// # TODO
1093+
///
1094+
// - Consider adding a check to ensure the `netuid` corresponds to an existing network.
1095+
// - Implement a mechanism to gradually adjust the max stake to prevent sudden changes.
1096+
// #[pallet::weight(T::WeightInfo::sudo_set_network_max_stake())]
1097+
#[pallet::call_index(53)]
1098+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1099+
pub fn sudo_set_network_max_stake(
1100+
origin: OriginFor<T>,
1101+
netuid: u16,
1102+
max_stake: u64,
1103+
) -> DispatchResult {
1104+
// Ensure the call is made by the root account
1105+
ensure_root(origin)?;
1106+
1107+
// Set the new maximum stake for the specified network
1108+
T::Subtensor::set_network_max_stake(netuid, max_stake);
1109+
1110+
// Log the change
1111+
log::trace!(
1112+
"NetworkMaxStakeSet( netuid: {:?}, max_stake: {:?} )",
1113+
netuid,
1114+
max_stake
1115+
);
1116+
1117+
Ok(())
1118+
}
10671119
}
10681120
}
10691121

@@ -1167,4 +1219,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
11671219
alpha_high: u16,
11681220
) -> Result<(), DispatchError>;
11691221
fn set_hotkey_emission_tempo(emission_tempo: u64);
1222+
fn set_network_max_stake(netuid: u16, max_stake: u64);
11701223
}

pallets/admin-utils/src/weights.rs

-12
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub trait WeightInfo {
6262
fn sudo_set_tempo() -> Weight;
6363
fn sudo_set_commit_reveal_weights_interval() -> Weight;
6464
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
65-
fn sudo_set_hotkey_emission_tempo() -> Weight;
66-
6765
}
6866

6967
/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
@@ -807,14 +805,4 @@ impl WeightInfo for () {
807805
.saturating_add(RocksDbWeight::get().reads(1_u64))
808806
.saturating_add(RocksDbWeight::get().writes(1_u64))
809807
}
810-
/// Storage: `SubtensorModule::HotkeyEmissionTempo` (r:0 w:1)
811-
/// Proof: `SubtensorModule::HotkeyEmissionTempo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
812-
fn sudo_set_hotkey_emission_tempo() -> Weight {
813-
// Proof Size summary in bytes:
814-
// Measured: `0`
815-
// Estimated: `0`
816-
// Minimum execution time: 5_000_000 picoseconds.
817-
Weight::from_parts(6_000_000, 0)
818-
.saturating_add(T::DbWeight::get().writes(1_u64))
819-
}
820808
}

pallets/admin-utils/tests/mock.rs

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ parameter_types! {
114114
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
115115
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
116116
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
117+
pub const InitialHotkeyEmissionTempo: u64 = 1;
118+
pub const InitialNetworkMaxStake: u64 = 500_000_000_000_000; // 500_000 TAO
117119
}
118120

119121
impl pallet_subtensor::Config for Test {
@@ -169,6 +171,8 @@ impl pallet_subtensor::Config for Test {
169171
type AlphaHigh = InitialAlphaHigh;
170172
type AlphaLow = InitialAlphaLow;
171173
type LiquidAlphaOn = InitialLiquidAlphaOn;
174+
type InitialHotkeyEmissionTempo = InitialHotkeyEmissionTempo;
175+
type InitialNetworkMaxStake = InitialNetworkMaxStake;
172176
}
173177

174178
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
@@ -478,6 +482,10 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
478482
fn set_hotkey_emission_tempo(emission_tempo: u64) {
479483
SubtensorModule::set_hotkey_emission_tempo(emission_tempo)
480484
}
485+
fn set_network_max_stake(netuid: u16, max_stake: u64) {
486+
SubtensorModule::set_network_max_stake(netuid, max_stake)
487+
}
488+
481489
fn do_set_alpha_values(
482490
origin: RuntimeOrigin,
483491
netuid: u16,

pallets/subtensor/src/coinbase/block_step.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use super::*;
2-
use frame_support::storage::IterableStorageDoubleMap;
32
use frame_support::storage::IterableStorageMap;
4-
use sp_runtime::Saturating;
53
use substrate_fixed::types::I110F18;
6-
use substrate_fixed::types::I64F64;
7-
use substrate_fixed::types::I96F32;
84

95
impl<T: Config> Pallet<T> {
106
/// Executes the necessary operations for each block.

pallets/subtensor/src/coinbase/run_coinbase.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use super::*;
22
use frame_support::storage::IterableStorageDoubleMap;
3-
use frame_support::storage::IterableStorageMap;
4-
use sp_runtime::Saturating;
5-
use substrate_fixed::types::I110F18;
63
use substrate_fixed::types::I64F64;
74
use substrate_fixed::types::I96F32;
85

@@ -190,8 +187,8 @@ impl<T: Config> Pallet<T> {
190187
mining_emission: u64,
191188
) {
192189
// --- 1. First, calculate the hotkey's share of the emission.
193-
let take_proportion: I64F64 =
194-
I64F64::from_num(Delegates::<T>::get(hotkey)).saturating_div(I64F64::from_num(u16::MAX));
190+
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
191+
.saturating_div(I64F64::from_num(u16::MAX));
195192
let hotkey_take: u64 = take_proportion
196193
.saturating_mul(I64F64::from_num(validating_emission))
197194
.to_num::<u64>();
@@ -274,8 +271,8 @@ impl<T: Config> Pallet<T> {
274271
let total_hotkey_stake: u64 = Self::get_total_stake_for_hotkey(hotkey);
275272

276273
// --- 5 Calculate the emission take for the hotkey.
277-
let take_proportion: I64F64 =
278-
I64F64::from_num(Delegates::<T>::get(hotkey)).saturating_div(I64F64::from_num(u16::MAX));
274+
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
275+
.saturating_div(I64F64::from_num(u16::MAX));
279276
let hotkey_take: u64 =
280277
(take_proportion.saturating_mul(I64F64::from_num(emission))).to_num::<u64>();
281278

@@ -287,11 +284,15 @@ impl<T: Config> Pallet<T> {
287284

288285
// --- 8 Iterate over each nominator.
289286
for (nominator, nominator_stake) in
290-
<Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64>>::iter_prefix(hotkey)
287+
<Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64>>::iter_prefix(
288+
hotkey,
289+
)
291290
{
292291
// --- 9 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
293292
// If it was, skip this nominator as they will not receive their proportion of the emission.
294-
if LastAddStakeIncrease::<T>::get(hotkey, nominator.clone()) > last_hotkey_emission_drain {
293+
if LastAddStakeIncrease::<T>::get(hotkey, nominator.clone())
294+
> last_hotkey_emission_drain
295+
{
295296
continue;
296297
}
297298

@@ -365,9 +366,10 @@ impl<T: Config> Pallet<T> {
365366
if tempo == 0 {
366367
return u64::MAX;
367368
}
368-
(tempo as u64).saturating_sub(
369-
(block_number.saturating_add((netuid as u64).saturating_add(1)))
370-
% (tempo as u64).saturating_add(1),
371-
)
369+
let netuid_plus_one = (netuid as u64).saturating_add(1);
370+
let block_plus_netuid = block_number.saturating_add(netuid_plus_one);
371+
let tempo_plus_one = (tempo as u64).saturating_add(1);
372+
let remainder = block_plus_netuid.rem_euclid(tempo_plus_one);
373+
(tempo as u64).saturating_sub(remainder)
372374
}
373375
}

pallets/subtensor/src/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ pub mod pallet {
502502
#[pallet::type_value]
503503
/// Default value for network immunity period.
504504
pub fn DefaultHotkeyEmissionTempo<T: Config>() -> u64 {
505-
7200
505+
T::InitialHotkeyEmissionTempo::get()
506506
}
507507
#[pallet::type_value]
508508
/// Default value for rate limiting
@@ -549,6 +549,11 @@ pub mod pallet {
549549
pub fn DefaultAlphaValues<T: Config>() -> (u16, u16) {
550550
(45875, 58982)
551551
}
552+
#[pallet::type_value]
553+
/// Default value for network max stake.
554+
pub fn DefaultNetworkMaxStake<T: Config>() -> u64 {
555+
T::InitialNetworkMaxStake::get()
556+
}
552557

553558
#[pallet::storage]
554559
pub type SenateRequiredStakePercentage<T> =
@@ -926,6 +931,10 @@ pub mod pallet {
926931
/// MAP ( netuid ) --> (alpha_low, alpha_high)
927932
pub type AlphaValues<T> =
928933
StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;
934+
/// MAP ( netuid ) --> max stake allowed on a subnet.
935+
#[pallet::storage]
936+
pub type NetworkMaxStake<T> =
937+
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultNetworkMaxStake<T>>;
929938

930939
/// =======================================
931940
/// ==== Subnetwork Consensus Storage ====

pallets/subtensor/src/macros/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,11 @@ mod config {
168168
/// A flag to indicate if Liquid Alpha is enabled.
169169
#[pallet::constant]
170170
type LiquidAlphaOn: Get<bool>;
171+
/// Initial network max stake.
172+
#[pallet::constant]
173+
type InitialNetworkMaxStake: Get<u64>;
174+
/// Initial hotkey emission tempo.
175+
#[pallet::constant]
176+
type InitialHotkeyEmissionTempo: Get<u64>;
171177
}
172178
}

pallets/subtensor/src/macros/events.rs

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ mod events {
171171
/// The account ID of the coldkey
172172
coldkey: T::AccountId,
173173
},
174+
/// The children of a hotkey have been set
174175
SetChildren(T::AccountId, u16, Vec<(u64, T::AccountId)>),
176+
/// The hotkey emission tempo has been set
177+
HotkeyEmissionTempoSet(u64),
178+
/// The network maximum stake has been set
179+
NetworkMaxStakeSet(u16, u64),
175180
}
176181
}

pallets/subtensor/src/rpc_info/neuron_info.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ impl<T: Config> Pallet<T> {
118118
})
119119
.collect::<Vec<(Compact<u16>, Compact<u16>)>>();
120120
let stake: Vec<(T::AccountId, Compact<u64>)> = vec![(
121-
coldkey,
122-
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid).into(),
121+
coldkey.clone(),
122+
Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid).into(),
123123
)];
124124
let neuron = NeuronInfo {
125125
hotkey: hotkey.clone(),

pallets/subtensor/src/staking/set_children.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use substrate_fixed::types::I96F32;
32

43
impl<T: Config> Pallet<T> {
54
/// ---- The implementation for the extrinsic do_set_child_singular: Sets a single child.

pallets/subtensor/src/subnets/registration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@ impl<T: Config> Pallet<T> {
550550
// Copy the hotkey_bytes into the first half of full_bytes
551551
full_bytes[..32].copy_from_slice(hotkey_bytes);
552552
let keccak_256_seal_hash_vec: [u8; 32] = keccak_256(&full_bytes[..]);
553-
let hash_u64: u64 = u64::from_le_bytes(keccak_256_seal_hash_vec[0..8].try_into().unwrap());
553+
let hash_u64: u64 = u64::from_le_bytes(keccak_256_seal_hash_vec[0..8].try_into().unwrap_or_default());
554554
hash_u64
555555
}
556-
556+
557557
pub fn create_seal_hash(block_number_u64: u64, nonce_u64: u64, hotkey: &T::AccountId) -> H256 {
558558
let nonce = nonce_u64.to_le_bytes();
559559
let block_hash_at_number: H256 = Self::get_block_hash_from_u64(block_number_u64);

pallets/subtensor/src/utils.rs

+37
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ impl<T: Config> Pallet<T> {
703703
pub fn get_hotkey_emission_tempo() -> u64 {
704704
HotkeyEmissionTempo::<T>::get()
705705
}
706+
706707
/// Sets the hotkey emission tempo.
707708
///
708709
/// # Arguments
@@ -711,4 +712,40 @@ impl<T: Config> Pallet<T> {
711712
HotkeyEmissionTempo::<T>::set(emission_tempo);
712713
Self::deposit_event(Event::HotkeyEmissionTempoSet(emission_tempo));
713714
}
715+
716+
pub fn get_pending_hotkey_emission(hotkey: &T::AccountId) -> u64 {
717+
PendingdHotkeyEmission::<T>::get(hotkey)
718+
}
719+
720+
/// Retrieves the maximum stake allowed for a given network.
721+
///
722+
/// # Arguments
723+
///
724+
/// * `netuid` - The unique identifier of the network.
725+
///
726+
/// # Returns
727+
///
728+
/// * `u64` - The maximum stake allowed for the specified network.
729+
pub fn get_network_max_stake(netuid: u16) -> u64 {
730+
NetworkMaxStake::<T>::get(netuid)
731+
}
732+
733+
/// Sets the maximum stake allowed for a given network.
734+
///
735+
/// # Arguments
736+
///
737+
/// * `netuid` - The unique identifier of the network.
738+
/// * `max_stake` - The new maximum stake value to set.
739+
///
740+
/// # Effects
741+
///
742+
/// * Updates the NetworkMaxStake storage.
743+
/// * Emits a NetworkMaxStakeSet event.
744+
pub fn set_network_max_stake(netuid: u16, max_stake: u64) {
745+
// Update the NetworkMaxStake storage with the new max_stake value
746+
NetworkMaxStake::<T>::insert(netuid, max_stake);
747+
748+
// Emit an event to notify listeners about the change
749+
Self::deposit_event(Event::NetworkMaxStakeSet(netuid, max_stake));
750+
}
714751
}

0 commit comments

Comments
 (0)