Skip to content

Commit 7e7252f

Browse files
authored
Merge pull request #1984 from opentensor/feat/subsubnets
Subsubnets
2 parents bcc1c10 + d55e68c commit 7e7252f

40 files changed

+4003
-749
lines changed

common/src/lib.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,162 @@ pub mod time {
224224
pub const DAYS: BlockNumber = HOURS * 24;
225225
}
226226

227+
#[freeze_struct("8e576b32bb1bb664")]
228+
#[repr(transparent)]
229+
#[derive(
230+
Deserialize,
231+
Serialize,
232+
Clone,
233+
Copy,
234+
Decode,
235+
DecodeWithMemTracking,
236+
Default,
237+
Encode,
238+
Eq,
239+
Hash,
240+
MaxEncodedLen,
241+
Ord,
242+
PartialEq,
243+
PartialOrd,
244+
RuntimeDebug,
245+
)]
246+
#[serde(transparent)]
247+
pub struct SubId(u8);
248+
249+
impl SubId {
250+
pub const MAIN: SubId = Self(0);
251+
}
252+
253+
impl From<u8> for SubId {
254+
fn from(value: u8) -> Self {
255+
Self(value)
256+
}
257+
}
258+
259+
impl From<SubId> for u16 {
260+
fn from(val: SubId) -> Self {
261+
u16::from(val.0)
262+
}
263+
}
264+
265+
impl From<SubId> for u64 {
266+
fn from(val: SubId) -> Self {
267+
u64::from(val.0)
268+
}
269+
}
270+
271+
impl From<SubId> for u8 {
272+
fn from(val: SubId) -> Self {
273+
u8::from(val.0)
274+
}
275+
}
276+
277+
impl Display for SubId {
278+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
279+
Display::fmt(&self.0, f)
280+
}
281+
}
282+
283+
impl CompactAs for SubId {
284+
type As = u8;
285+
286+
fn encode_as(&self) -> &Self::As {
287+
&self.0
288+
}
289+
290+
fn decode_from(v: Self::As) -> Result<Self, CodecError> {
291+
Ok(Self(v))
292+
}
293+
}
294+
295+
impl From<Compact<SubId>> for SubId {
296+
fn from(c: Compact<SubId>) -> Self {
297+
c.0
298+
}
299+
}
300+
301+
impl TypeInfo for SubId {
302+
type Identity = <u8 as TypeInfo>::Identity;
303+
fn type_info() -> scale_info::Type {
304+
<u8 as TypeInfo>::type_info()
305+
}
306+
}
307+
308+
#[freeze_struct("2d995c5478e16d4d")]
309+
#[repr(transparent)]
310+
#[derive(
311+
Deserialize,
312+
Serialize,
313+
Clone,
314+
Copy,
315+
Decode,
316+
DecodeWithMemTracking,
317+
Default,
318+
Encode,
319+
Eq,
320+
Hash,
321+
MaxEncodedLen,
322+
Ord,
323+
PartialEq,
324+
PartialOrd,
325+
RuntimeDebug,
326+
)]
327+
#[serde(transparent)]
328+
pub struct NetUidStorageIndex(u16);
329+
330+
impl NetUidStorageIndex {
331+
pub const ROOT: NetUidStorageIndex = Self(0);
332+
}
333+
334+
impl Display for NetUidStorageIndex {
335+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
336+
Display::fmt(&self.0, f)
337+
}
338+
}
339+
340+
impl CompactAs for NetUidStorageIndex {
341+
type As = u16;
342+
343+
fn encode_as(&self) -> &Self::As {
344+
&self.0
345+
}
346+
347+
fn decode_from(v: Self::As) -> Result<Self, CodecError> {
348+
Ok(Self(v))
349+
}
350+
}
351+
352+
impl From<Compact<NetUidStorageIndex>> for NetUidStorageIndex {
353+
fn from(c: Compact<NetUidStorageIndex>) -> Self {
354+
c.0
355+
}
356+
}
357+
358+
impl From<NetUid> for NetUidStorageIndex {
359+
fn from(val: NetUid) -> Self {
360+
val.0.into()
361+
}
362+
}
363+
364+
impl From<NetUidStorageIndex> for u16 {
365+
fn from(val: NetUidStorageIndex) -> Self {
366+
val.0
367+
}
368+
}
369+
370+
impl From<u16> for NetUidStorageIndex {
371+
fn from(value: u16) -> Self {
372+
Self(value)
373+
}
374+
}
375+
376+
impl TypeInfo for NetUidStorageIndex {
377+
type Identity = <u16 as TypeInfo>::Identity;
378+
fn type_info() -> scale_info::Type {
379+
<u16 as TypeInfo>::type_info()
380+
}
381+
}
382+
227383
#[cfg(test)]
228384
mod tests {
229385
use super::*;

pallets/admin-utils/src/lib.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use pallet::*;
99
// - we could use a type parameter for `AuthorityId`, but there is
1010
// no sense for this as GRANDPA's `AuthorityId` is not a parameter -- it's always the same
1111
use sp_consensus_grandpa::AuthorityList;
12-
use sp_runtime::{DispatchResult, RuntimeAppPublic, traits::Member};
12+
use sp_runtime::{DispatchResult, RuntimeAppPublic, Vec, traits::Member};
1313

1414
mod benchmarking;
1515

@@ -28,7 +28,7 @@ pub mod pallet {
2828
use pallet_subtensor::utils::rate_limiting::TransactionType;
2929
use sp_runtime::BoundedVec;
3030
use substrate_fixed::types::I96F32;
31-
use subtensor_runtime_common::{NetUid, TaoCurrency};
31+
use subtensor_runtime_common::{NetUid, SubId, TaoCurrency};
3232

3333
/// The main data structure of the module.
3434
#[pallet::pallet]
@@ -1846,6 +1846,58 @@ pub mod pallet {
18461846
log::debug!("OwnerHyperparamRateLimitSet( limit: {limit:?} ) ");
18471847
Ok(())
18481848
}
1849+
1850+
/// Sets the desired number of subsubnets in a subnet
1851+
#[pallet::call_index(76)]
1852+
#[pallet::weight(Weight::from_parts(15_000_000, 0)
1853+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
1854+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1855+
pub fn sudo_set_subsubnet_count(
1856+
origin: OriginFor<T>,
1857+
netuid: NetUid,
1858+
subsub_count: SubId,
1859+
) -> DispatchResult {
1860+
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
1861+
origin,
1862+
netuid,
1863+
&[TransactionType::SubsubnetParameterUpdate],
1864+
)?;
1865+
1866+
pallet_subtensor::Pallet::<T>::do_set_subsubnet_count(netuid, subsub_count)?;
1867+
1868+
pallet_subtensor::Pallet::<T>::record_owner_rl(
1869+
maybe_owner,
1870+
netuid,
1871+
&[TransactionType::SubsubnetParameterUpdate],
1872+
);
1873+
Ok(())
1874+
}
1875+
1876+
/// Sets the emission split between subsubnets in a subnet
1877+
#[pallet::call_index(77)]
1878+
#[pallet::weight(Weight::from_parts(15_000_000, 0)
1879+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
1880+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1881+
pub fn sudo_set_subsubnet_emission_split(
1882+
origin: OriginFor<T>,
1883+
netuid: NetUid,
1884+
maybe_split: Option<Vec<u16>>,
1885+
) -> DispatchResult {
1886+
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
1887+
origin,
1888+
netuid,
1889+
&[TransactionType::SubsubnetParameterUpdate],
1890+
)?;
1891+
1892+
pallet_subtensor::Pallet::<T>::do_set_emission_split(netuid, maybe_split)?;
1893+
1894+
pallet_subtensor::Pallet::<T>::record_owner_rl(
1895+
maybe_owner,
1896+
netuid,
1897+
&[TransactionType::SubsubnetParameterUpdate],
1898+
);
1899+
Ok(())
1900+
}
18491901
}
18501902
}
18511903

pallets/admin-utils/src/tests/mod.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use pallet_subtensor::Event;
1111
use sp_consensus_grandpa::AuthorityId as GrandpaId;
1212
use sp_core::{Get, Pair, U256, ed25519};
1313
use substrate_fixed::types::I96F32;
14-
use subtensor_runtime_common::{Currency, NetUid, TaoCurrency};
14+
use subtensor_runtime_common::{Currency, NetUid, SubId, TaoCurrency};
1515

1616
use crate::Error;
1717
use crate::pallet::PrecompileEnable;
@@ -827,7 +827,7 @@ fn test_sudo_set_bonds_moving_average() {
827827
let netuid = NetUid::from(1);
828828
let to_be_set: u64 = 10;
829829
add_network(netuid, 10);
830-
let init_value: u64 = SubtensorModule::get_bonds_moving_average(netuid);
830+
let init_value: u64 = SubtensorModule::get_bonds_moving_average(netuid.into());
831831
assert_eq!(
832832
AdminUtils::sudo_set_bonds_moving_average(
833833
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
@@ -845,15 +845,18 @@ fn test_sudo_set_bonds_moving_average() {
845845
Err(Error::<Test>::SubnetDoesNotExist.into())
846846
);
847847
assert_eq!(
848-
SubtensorModule::get_bonds_moving_average(netuid),
848+
SubtensorModule::get_bonds_moving_average(netuid.into()),
849849
init_value
850850
);
851851
assert_ok!(AdminUtils::sudo_set_bonds_moving_average(
852852
<<Test as Config>::RuntimeOrigin>::root(),
853853
netuid,
854854
to_be_set
855855
));
856-
assert_eq!(SubtensorModule::get_bonds_moving_average(netuid), to_be_set);
856+
assert_eq!(
857+
SubtensorModule::get_bonds_moving_average(netuid.into()),
858+
to_be_set
859+
);
857860
});
858861
}
859862

@@ -2227,3 +2230,42 @@ fn test_sudo_set_max_burn() {
22272230
);
22282231
});
22292232
}
2233+
2234+
#[test]
2235+
fn test_sudo_set_subsubnet_count() {
2236+
new_test_ext().execute_with(|| {
2237+
let netuid = NetUid::from(1);
2238+
let ss_count_ok = SubId::from(8);
2239+
let ss_count_bad = SubId::from(9);
2240+
2241+
let sn_owner = U256::from(1324);
2242+
add_network(netuid, 10);
2243+
// Set the Subnet Owner
2244+
SubnetOwner::<Test>::insert(netuid, sn_owner);
2245+
2246+
assert_eq!(
2247+
AdminUtils::sudo_set_subsubnet_count(
2248+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
2249+
netuid,
2250+
ss_count_ok
2251+
),
2252+
Err(DispatchError::BadOrigin)
2253+
);
2254+
assert_noop!(
2255+
AdminUtils::sudo_set_subsubnet_count(RuntimeOrigin::root(), netuid, ss_count_bad),
2256+
pallet_subtensor::Error::<Test>::InvalidValue
2257+
);
2258+
2259+
assert_ok!(AdminUtils::sudo_set_subsubnet_count(
2260+
<<Test as Config>::RuntimeOrigin>::root(),
2261+
netuid,
2262+
ss_count_ok
2263+
));
2264+
2265+
assert_ok!(AdminUtils::sudo_set_subsubnet_count(
2266+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2267+
netuid,
2268+
ss_count_ok
2269+
));
2270+
});
2271+
}

0 commit comments

Comments
 (0)