Skip to content

Commit 238e33e

Browse files
authored
Merge pull request #2218 from opentensor/root-claim-upgrade2
Root claim upgrade (part 1)
2 parents 3dd0e0f + 7634bc4 commit 238e33e

File tree

6 files changed

+341
-62
lines changed

6 files changed

+341
-62
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ impl<T: Config> Pallet<T> {
615615
}
616616

617617
// Distribute root alpha divs.
618+
let _ = RootAlphaDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
618619
for (hotkey, mut root_alpha) in root_alpha_dividends {
619620
// Get take prop
620621
let alpha_take: U96F32 =
@@ -637,7 +638,7 @@ impl<T: Config> Pallet<T> {
637638
);
638639

639640
// Record root alpha dividends for this validator on this subnet.
640-
AlphaDividendsPerSubnet::<T>::mutate(netuid, hotkey.clone(), |divs| {
641+
RootAlphaDividendsPerSubnet::<T>::mutate(netuid, &hotkey, |divs| {
641642
*divs = divs.saturating_add(tou64!(root_alpha).into());
642643
});
643644
}

pallets/subtensor/src/lib.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,11 @@ pub mod pallet {
334334
Swap,
335335
/// Keep all alpha emission.
336336
Keep,
337-
}
338-
339-
/// Enum for the per-coldkey root claim frequency setting.
340-
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
341-
pub enum RootClaimFrequencyEnum {
342-
/// Claim automatically.
343-
#[default]
344-
Auto,
345-
/// Only claim manually; Never automatically.
346-
Manual,
337+
/// Keep all alpha emission for specified subnets.
338+
KeepSubnets {
339+
/// Subnets to keep alpha emissions (swap everything else).
340+
subnets: BTreeSet<NetUid>,
341+
},
347342
}
348343

349344
/// Default minimum root claim amount.
@@ -1206,7 +1201,7 @@ pub mod pallet {
12061201
DefaultAccountLinkage<T>,
12071202
>;
12081203

1209-
/// --- DMAP ( netuid, hotkey ) --> u64 | Last total dividend this hotkey got on tempo.
1204+
/// --- DMAP ( netuid, hotkey ) --> u64 | Last alpha dividend this hotkey got on tempo.
12101205
#[pallet::storage]
12111206
pub type AlphaDividendsPerSubnet<T: Config> = StorageDoubleMap<
12121207
_,
@@ -1219,6 +1214,19 @@ pub mod pallet {
12191214
DefaultZeroAlpha<T>,
12201215
>;
12211216

1217+
/// --- DMAP ( netuid, hotkey ) --> u64 | Last root alpha dividend this hotkey got on tempo.
1218+
#[pallet::storage]
1219+
pub type RootAlphaDividendsPerSubnet<T: Config> = StorageDoubleMap<
1220+
_,
1221+
Identity,
1222+
NetUid,
1223+
Blake2_128Concat,
1224+
T::AccountId,
1225+
AlphaCurrency,
1226+
ValueQuery,
1227+
DefaultZeroAlpha<T>,
1228+
>;
1229+
12221230
/// ==================
12231231
/// ==== Coinbase ====
12241232
/// ==================

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ mod dispatches {
712712
///
713713
#[pallet::call_index(2)]
714714
#[pallet::weight((Weight::from_parts(340_800_000, 0)
715-
.saturating_add(T::DbWeight::get().reads(25_u64))
715+
.saturating_add(T::DbWeight::get().reads(27_u64))
716716
.saturating_add(T::DbWeight::get().writes(16_u64)), DispatchClass::Normal, Pays::Yes))]
717717
pub fn add_stake(
718718
origin: OriginFor<T>,
@@ -1587,7 +1587,7 @@ mod dispatches {
15871587
/// - Thrown if key has hit transaction rate limit
15881588
#[pallet::call_index(84)]
15891589
#[pallet::weight((Weight::from_parts(358_500_000, 0)
1590-
.saturating_add(T::DbWeight::get().reads(41_u64))
1590+
.saturating_add(T::DbWeight::get().reads(44_u64))
15911591
.saturating_add(T::DbWeight::get().writes(26_u64)), DispatchClass::Normal, Pays::Yes))]
15921592
pub fn unstake_all_alpha(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
15931593
Self::do_unstake_all_alpha(origin, hotkey)
@@ -1701,7 +1701,7 @@ mod dispatches {
17011701
#[pallet::call_index(87)]
17021702
#[pallet::weight((
17031703
Weight::from_parts(351_300_000, 0)
1704-
.saturating_add(T::DbWeight::get().reads(37_u64))
1704+
.saturating_add(T::DbWeight::get().reads(40_u64))
17051705
.saturating_add(T::DbWeight::get().writes(24_u64)),
17061706
DispatchClass::Normal,
17071707
Pays::Yes
@@ -1766,7 +1766,7 @@ mod dispatches {
17661766
///
17671767
#[pallet::call_index(88)]
17681768
#[pallet::weight((Weight::from_parts(402_900_000, 0)
1769-
.saturating_add(T::DbWeight::get().reads(25_u64))
1769+
.saturating_add(T::DbWeight::get().reads(27_u64))
17701770
.saturating_add(T::DbWeight::get().writes(16_u64)), DispatchClass::Normal, Pays::Yes))]
17711771
pub fn add_stake_limit(
17721772
origin: OriginFor<T>,
@@ -1830,7 +1830,7 @@ mod dispatches {
18301830
///
18311831
#[pallet::call_index(89)]
18321832
#[pallet::weight((Weight::from_parts(377_400_000, 0)
1833-
.saturating_add(T::DbWeight::get().reads(29_u64))
1833+
.saturating_add(T::DbWeight::get().reads(31_u64))
18341834
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
18351835
pub fn remove_stake_limit(
18361836
origin: OriginFor<T>,
@@ -1874,7 +1874,7 @@ mod dispatches {
18741874
#[pallet::call_index(90)]
18751875
#[pallet::weight((
18761876
Weight::from_parts(411_500_000, 0)
1877-
.saturating_add(T::DbWeight::get().reads(37_u64))
1877+
.saturating_add(T::DbWeight::get().reads(40_u64))
18781878
.saturating_add(T::DbWeight::get().writes(24_u64)),
18791879
DispatchClass::Normal,
18801880
Pays::Yes
@@ -2052,7 +2052,7 @@ mod dispatches {
20522052
/// Without limit_price it remove all the stake similar to `remove_stake` extrinsic
20532053
#[pallet::call_index(103)]
20542054
#[pallet::weight((Weight::from_parts(395_300_000, 10142)
2055-
.saturating_add(T::DbWeight::get().reads(29_u64))
2055+
.saturating_add(T::DbWeight::get().reads(31_u64))
20562056
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
20572057
pub fn remove_stake_full_limit(
20582058
origin: T::RuntimeOrigin,
@@ -2372,6 +2372,10 @@ mod dispatches {
23722372
) -> DispatchResult {
23732373
let coldkey: T::AccountId = ensure_signed(origin)?;
23742374

2375+
if let RootClaimTypeEnum::KeepSubnets { subnets } = &new_root_claim_type {
2376+
ensure!(!subnets.is_empty(), Error::<T>::InvalidSubnetNumber);
2377+
}
2378+
23752379
Self::maybe_add_coldkey_index(&coldkey);
23762380

23772381
Self::change_root_claim_type(&coldkey, new_root_claim_type);

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,48 +157,52 @@ impl<T: Config> Pallet<T> {
157157
return; // no-op
158158
}
159159

160-
match root_claim_type {
161-
// Increase stake on root
162-
RootClaimTypeEnum::Swap => {
163-
// Swap the alpha owed to TAO
164-
let owed_tao = match Self::swap_alpha_for_tao(
165-
netuid,
166-
owed_u64.into(),
167-
T::SwapInterface::min_price::<TaoCurrency>(),
168-
true,
169-
) {
170-
Ok(owed_tao) => owed_tao,
171-
Err(err) => {
172-
log::error!("Error swapping alpha for TAO: {err:?}");
173-
174-
return;
175-
}
176-
};
177-
178-
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
179-
hotkey,
180-
coldkey,
181-
NetUid::ROOT,
182-
owed_tao.amount_paid_out.to_u64().into(),
183-
);
184-
185-
Self::add_stake_adjust_root_claimed_for_hotkey_and_coldkey(
186-
hotkey,
187-
coldkey,
188-
owed_tao.amount_paid_out.into(),
189-
);
190-
}
191-
RootClaimTypeEnum::Keep => {
192-
// Increase the stake with the alpha owned
193-
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
194-
hotkey,
195-
coldkey,
196-
netuid,
197-
owed_u64.into(),
198-
);
199-
}
160+
let swap = match root_claim_type {
161+
RootClaimTypeEnum::Swap => true,
162+
RootClaimTypeEnum::Keep => false,
163+
RootClaimTypeEnum::KeepSubnets { subnets } => !subnets.contains(&netuid),
200164
};
201165

166+
if swap {
167+
// Increase stake on root. Swap the alpha owed to TAO
168+
let owed_tao = match Self::swap_alpha_for_tao(
169+
netuid,
170+
owed_u64.into(),
171+
T::SwapInterface::min_price::<TaoCurrency>(),
172+
true,
173+
) {
174+
Ok(owed_tao) => owed_tao,
175+
Err(err) => {
176+
log::error!("Error swapping alpha for TAO: {err:?}");
177+
178+
return;
179+
}
180+
};
181+
182+
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
183+
hotkey,
184+
coldkey,
185+
NetUid::ROOT,
186+
owed_tao.amount_paid_out.to_u64().into(),
187+
);
188+
189+
Self::add_stake_adjust_root_claimed_for_hotkey_and_coldkey(
190+
hotkey,
191+
coldkey,
192+
owed_tao.amount_paid_out.into(),
193+
);
194+
} else
195+
/* Keep */
196+
{
197+
// Increase the stake with the alpha owned
198+
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
199+
hotkey,
200+
coldkey,
201+
netuid,
202+
owed_u64.into(),
203+
);
204+
}
205+
202206
// Increase root claimed by owed amount.
203207
RootClaimed::<T>::mutate((netuid, hotkey, coldkey), |root_claimed| {
204208
*root_claimed = root_claimed.saturating_add(owed_u64.into());

0 commit comments

Comments
 (0)