Skip to content

Commit 91e5776

Browse files
Add subnets to claim_root extrinsic.
1 parent f08ef46 commit 91e5776

File tree

6 files changed

+228
-47
lines changed

6 files changed

+228
-47
lines changed

pallets/subtensor/src/benchmarks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use sp_runtime::{
1414
BoundedVec, Percent,
1515
traits::{BlakeTwo256, Hash},
1616
};
17+
use sp_std::collections::btree_set::BTreeSet;
1718
use sp_std::vec;
1819
use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency};
1920

@@ -1630,7 +1631,7 @@ mod pallet_benchmarks {
16301631
),);
16311632

16321633
#[extrinsic_call]
1633-
_(RawOrigin::Signed(coldkey.clone()));
1634+
_(RawOrigin::Signed(coldkey.clone()), BTreeSet::from([netuid]));
16341635

16351636
// Verification
16361637
let new_stake =

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub const ALPHA_MAP_BATCH_SIZE: usize = 30;
5858

5959
pub const MAX_NUM_ROOT_CLAIMS: u64 = 50;
6060

61+
pub const MAX_SUBNET_CLAIMS: usize = 5;
62+
6163
pub const MAX_ROOT_CLAIM_THRESHOLD: u64 = 10_000_000;
6264

6365
#[allow(deprecated)]
@@ -89,6 +91,7 @@ pub mod pallet {
8991
use sp_core::{ConstU32, H160, H256};
9092
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
9193
use sp_std::collections::btree_map::BTreeMap;
94+
use sp_std::collections::btree_set::BTreeSet;
9295
use sp_std::collections::vec_deque::VecDeque;
9396
use sp_std::vec;
9497
use sp_std::vec::Vec;

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ mod dispatches {
1515
use crate::MAX_CRV3_COMMIT_SIZE_BYTES;
1616
use crate::MAX_NUM_ROOT_CLAIMS;
1717
use crate::MAX_ROOT_CLAIM_THRESHOLD;
18+
use crate::MAX_SUBNET_CLAIMS;
19+
1820
/// Dispatchable functions allow users to interact with the pallet and invoke state changes.
1921
/// These functions materialize as "extrinsics", which are often compared to transactions.
2022
/// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
@@ -2331,12 +2333,21 @@ mod dispatches {
23312333
DispatchClass::Normal,
23322334
Pays::Yes
23332335
))]
2334-
pub fn claim_root(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
2336+
pub fn claim_root(
2337+
origin: OriginFor<T>,
2338+
subnets: BTreeSet<NetUid>,
2339+
) -> DispatchResultWithPostInfo {
23352340
let coldkey: T::AccountId = ensure_signed(origin)?;
23362341

2342+
ensure!(!subnets.is_empty(), Error::<T>::InvalidSubnetNumber);
2343+
ensure!(
2344+
subnets.len() <= MAX_SUBNET_CLAIMS,
2345+
Error::<T>::InvalidSubnetNumber
2346+
);
2347+
23372348
Self::maybe_add_coldkey_index(&coldkey);
23382349

2339-
let weight = Self::do_root_claim(coldkey);
2350+
let weight = Self::do_root_claim(coldkey, Some(subnets));
23402351
Ok((Some(weight), Pays::Yes).into())
23412352
}
23422353

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,7 @@ mod errors {
264264
InvalidNumRootClaim,
265265
/// Invalid value of root claim threshold
266266
InvalidRootClaimThreshold,
267+
/// Exceeded subnet limit number or zero.
268+
InvalidSubnetNumber,
267269
}
268270
}

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ impl<T: Config> Pallet<T> {
210210
.saturating_add(T::DbWeight::get().reads(7_u64))
211211
.saturating_add(T::DbWeight::get().writes(5_u64))
212212
}
213-
pub fn root_claim_all(hotkey: &T::AccountId, coldkey: &T::AccountId) -> Weight {
213+
pub fn root_claim_all(
214+
hotkey: &T::AccountId,
215+
coldkey: &T::AccountId,
216+
subnets: Option<BTreeSet<NetUid>>,
217+
) -> Weight {
214218
let mut weight = Weight::default();
215219

216220
let root_claim_type = RootClaimType::<T>::get(coldkey);
@@ -220,10 +224,19 @@ impl<T: Config> Pallet<T> {
220224
let root_claimable = RootClaimable::<T>::get(hotkey);
221225
weight.saturating_accrue(T::DbWeight::get().reads(1));
222226

223-
root_claimable.iter().for_each(|(netuid, _)| {
227+
for (netuid, _) in root_claimable.iter() {
228+
let skip = subnets
229+
.as_ref()
230+
.map(|subnets| !subnets.contains(netuid))
231+
.unwrap_or(false);
232+
233+
if skip {
234+
continue;
235+
}
236+
224237
Self::root_claim_on_subnet(hotkey, coldkey, *netuid, root_claim_type.clone(), false);
225238
weight.saturating_accrue(Self::root_claim_on_subnet_weight(root_claim_type.clone()));
226-
});
239+
}
227240

228241
weight
229242
}
@@ -278,15 +291,15 @@ impl<T: Config> Pallet<T> {
278291
}
279292
}
280293

281-
pub fn do_root_claim(coldkey: T::AccountId) -> Weight {
294+
pub fn do_root_claim(coldkey: T::AccountId, subnets: Option<BTreeSet<NetUid>>) -> Weight {
282295
let mut weight = Weight::default();
283296

284297
let hotkeys = StakingHotkeys::<T>::get(&coldkey);
285298
weight.saturating_accrue(T::DbWeight::get().reads(1));
286299

287300
hotkeys.iter().for_each(|hotkey| {
288301
weight.saturating_accrue(T::DbWeight::get().reads(1));
289-
weight.saturating_accrue(Self::root_claim_all(hotkey, &coldkey));
302+
weight.saturating_accrue(Self::root_claim_all(hotkey, &coldkey, subnets.clone()));
290303
});
291304

292305
Self::deposit_event(Event::RootClaimed { coldkey });
@@ -321,7 +334,7 @@ impl<T: Config> Pallet<T> {
321334
for i in coldkeys_to_claim.iter() {
322335
weight.saturating_accrue(T::DbWeight::get().reads(1));
323336
if let Ok(coldkey) = StakingColdkeysByIndex::<T>::try_get(i) {
324-
weight.saturating_accrue(Self::do_root_claim(coldkey.clone()));
337+
weight.saturating_accrue(Self::do_root_claim(coldkey.clone(), None));
325338
}
326339

327340
continue;

0 commit comments

Comments
 (0)