Skip to content

Commit 29e99d7

Browse files
Introduce root claim threshold for subnets.
1 parent d68d3aa commit 29e99d7

File tree

6 files changed

+127
-10
lines changed

6 files changed

+127
-10
lines changed

pallets/subtensor/src/benchmarks.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,4 +1656,23 @@ mod pallet_benchmarks {
16561656
#[extrinsic_call]
16571657
_(RawOrigin::Root, 100);
16581658
}
1659+
1660+
#[benchmark]
1661+
fn sudo_set_root_claim_threshold() {
1662+
let coldkey: T::AccountId = whitelisted_caller();
1663+
let hotkey: T::AccountId = account("A", 0, 1);
1664+
1665+
let netuid = Subtensor::<T>::get_next_netuid();
1666+
1667+
let lock_cost = Subtensor::<T>::get_network_lock_cost();
1668+
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey, lock_cost.into());
1669+
1670+
assert_ok!(Subtensor::<T>::register_network(
1671+
RawOrigin::Signed(coldkey.clone()).into(),
1672+
hotkey.clone()
1673+
));
1674+
1675+
#[extrinsic_call]
1676+
_(RawOrigin::Root, netuid, 100);
1677+
}
16591678
}

pallets/subtensor/src/lib.rs

Lines changed: 8 additions & 2 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_ROOT_CLAIM_THRESHOLD: u64 = 10_000_000;
62+
6163
#[allow(deprecated)]
6264
#[deny(missing_docs)]
6365
#[import_section(errors::errors)]
@@ -350,8 +352,8 @@ pub mod pallet {
350352
/// Default minimum root claim amount.
351353
/// This is the minimum amount of root claim that can be made.
352354
/// Any amount less than this will not be claimed.
353-
pub fn DefaultMinRootClaimAmount<T: Config>() -> u64 {
354-
500_000
355+
pub fn DefaultMinRootClaimAmount<T: Config>() -> I96F32 {
356+
500_000u64.into()
355357
}
356358

357359
#[pallet::type_value]
@@ -1901,6 +1903,10 @@ pub mod pallet {
19011903
ValueQuery,
19021904
>;
19031905

1906+
#[pallet::storage] // --- MAP(netuid ) --> Root claim threshold
1907+
pub type RootClaimableThreshold<T: Config> =
1908+
StorageMap<_, Blake2_128Concat, NetUid, I96F32, ValueQuery, DefaultMinRootClaimAmount<T>>;
1909+
19041910
#[pallet::storage] // --- MAP ( hot ) --> MAP(netuid ) --> claimable_dividends | Root claimable dividends.
19051911
pub type RootClaimable<T: Config> = StorageMap<
19061912
_,

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ mod dispatches {
1212
use sp_core::ecdsa::Signature;
1313
use sp_runtime::{Percent, traits::Saturating};
1414

15-
use crate::MAX_NUM_ROOT_CLAIMS;
16-
1715
use crate::MAX_CRV3_COMMIT_SIZE_BYTES;
16+
use crate::MAX_NUM_ROOT_CLAIMS;
17+
use crate::MAX_ROOT_CLAIM_THRESHOLD;
1818
/// Dispatchable functions allow users to interact with the pallet and invoke state changes.
1919
/// These functions materialize as "extrinsics", which are often compared to transactions.
2020
/// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
@@ -2484,5 +2484,37 @@ mod dispatches {
24842484

24852485
Ok(())
24862486
}
2487+
2488+
/// --- Sets the root claim type for the coldkey.
2489+
/// # Args:
2490+
/// * 'origin': (<T as frame_system::Config>Origin):
2491+
/// - The signature of the caller's coldkey.
2492+
///
2493+
/// # Event:
2494+
/// * RootClaimTypeSet;
2495+
/// - On the successfully setting the root claim type for the coldkey.
2496+
///
2497+
#[pallet::call_index(124)]
2498+
#[pallet::weight((
2499+
Weight::from_parts(4_000_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
2500+
DispatchClass::Operational,
2501+
Pays::Yes
2502+
))]
2503+
pub fn sudo_set_root_claim_threshold(
2504+
origin: OriginFor<T>,
2505+
netuid: NetUid,
2506+
new_value: u64,
2507+
) -> DispatchResult {
2508+
Self::ensure_subnet_owner_or_root(origin, netuid)?;
2509+
2510+
ensure!(
2511+
new_value <= I96F32::from(MAX_ROOT_CLAIM_THRESHOLD),
2512+
Error::<T>::InvalidRootClaimThreshold
2513+
);
2514+
2515+
RootClaimableThreshold::<T>::set(netuid, new_value.into());
2516+
2517+
Ok(())
2518+
}
24872519
}
24882520
}

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,7 @@ mod errors {
266266
ChildParentInconsistency,
267267
/// Invalid number of root claims
268268
InvalidNumRootClaim,
269+
/// Invalid value of root claim threshold
270+
InvalidRootClaimThreshold,
269271
}
270272
}

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<T: Config> Pallet<T> {
135135
let owed: I96F32 = Self::get_root_owed_for_hotkey_coldkey_float(hotkey, coldkey, netuid);
136136

137137
if !ignore_minimum_condition
138-
&& owed < I96F32::saturating_from_num(DefaultMinRootClaimAmount::<T>::get())
138+
&& owed < I96F32::saturating_from_num(RootClaimableThreshold::<T>::get(&netuid))
139139
{
140140
log::debug!(
141141
"root claim on subnet {netuid} is skipped: {owed:?} for h={hotkey:?},c={coldkey:?} "

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ use crate::tests::mock::{
44
RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext, run_to_block,
55
};
66
use crate::{
7-
Error, MAX_NUM_ROOT_CLAIMS, NetworksAdded, NumRootClaim, NumStakingColdkeys,
8-
PendingRootAlphaDivs, RootClaimable, StakingColdkeys, StakingColdkeysByIndex, SubnetAlphaIn,
9-
SubnetMechanism, SubnetTAO, SubtokenEnabled, Tempo, pallet,
7+
DefaultMinRootClaimAmount, Error, MAX_NUM_ROOT_CLAIMS, MAX_ROOT_CLAIM_THRESHOLD, NetworksAdded,
8+
NumRootClaim, NumStakingColdkeys, PendingRootAlphaDivs, RootClaimable, RootClaimableThreshold,
9+
StakingColdkeys, StakingColdkeysByIndex, SubnetAlphaIn, SubnetMechanism, SubnetTAO,
10+
SubtokenEnabled, Tempo, pallet,
1011
};
1112
use crate::{RootClaimType, RootClaimTypeEnum, RootClaimed};
1213
use approx::assert_abs_diff_eq;
14+
use frame_support::dispatch::RawOrigin;
1315
use frame_support::pallet_prelude::Weight;
14-
use frame_support::{assert_noop, assert_ok};
16+
use frame_support::traits::Get;
17+
use frame_support::{assert_err, assert_noop, assert_ok};
1518
use sp_core::{H256, U256};
1619
use sp_runtime::DispatchError;
1720
use substrate_fixed::types::{I96F32, U96F32};
@@ -1297,6 +1300,61 @@ fn test_claim_root_on_network_deregistration() {
12971300
assert!(!RootClaimed::<Test>::contains_key((
12981301
&hotkey, &coldkey, netuid
12991302
)));
1300-
assert!(!RootClaimable::<Test>::get(&hotkey).contains_key(&netuid));
1303+
assert!(!RootClaimable::<Test>::get(hotkey).contains_key(&netuid));
1304+
});
1305+
}
1306+
1307+
#[test]
1308+
fn test_claim_root_threshold() {
1309+
new_test_ext(1).execute_with(|| {
1310+
let owner_coldkey = U256::from(1001);
1311+
let hotkey = U256::from(1002);
1312+
let netuid = add_dynamic_network(&hotkey, &owner_coldkey);
1313+
1314+
assert_eq!(
1315+
RootClaimableThreshold::<Test>::get(netuid),
1316+
DefaultMinRootClaimAmount::<Test>::get()
1317+
);
1318+
1319+
let threshold = 1000u64;
1320+
assert_ok!(SubtensorModule::sudo_set_root_claim_threshold(
1321+
RawOrigin::Root.into(),
1322+
netuid,
1323+
threshold
1324+
));
1325+
assert_eq!(
1326+
RootClaimableThreshold::<Test>::get(netuid),
1327+
I96F32::from(threshold)
1328+
);
1329+
1330+
let threshold = 2000u64;
1331+
assert_ok!(SubtensorModule::sudo_set_root_claim_threshold(
1332+
RawOrigin::Signed(owner_coldkey).into(),
1333+
netuid,
1334+
threshold
1335+
));
1336+
assert_eq!(
1337+
RootClaimableThreshold::<Test>::get(netuid),
1338+
I96F32::from(threshold)
1339+
);
1340+
1341+
// Errors
1342+
assert_err!(
1343+
SubtensorModule::sudo_set_root_claim_threshold(
1344+
RawOrigin::Signed(hotkey).into(),
1345+
netuid,
1346+
threshold
1347+
),
1348+
DispatchError::BadOrigin,
1349+
);
1350+
1351+
assert_err!(
1352+
SubtensorModule::sudo_set_root_claim_threshold(
1353+
RawOrigin::Signed(owner_coldkey).into(),
1354+
netuid,
1355+
MAX_ROOT_CLAIM_THRESHOLD + 1
1356+
),
1357+
Error::<Test>::InvalidRootClaimThreshold,
1358+
);
13011359
});
13021360
}

0 commit comments

Comments
 (0)