Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pub mod pallet {
Swap,
/// Keep all alpha emission.
Keep,
/// Delegate choice to subnet.
Delegated,
}

/// Enum for the per-coldkey root claim frequency setting.
Expand All @@ -359,7 +361,12 @@ pub mod pallet {
/// This is set by the user. Either swap to TAO or keep as alpha.
#[pallet::type_value]
pub fn DefaultRootClaimType<T: Config>() -> RootClaimTypeEnum {
RootClaimTypeEnum::default()
RootClaimTypeEnum::Delegated
}
/// Default value for delegate claim type storage
#[pallet::type_value]
pub fn DefaultValidatorClaimType<T: Config>() -> RootClaimTypeEnum {
RootClaimTypeEnum::Keep
}

/// Default number of root claims per claim call.
Expand Down Expand Up @@ -2244,6 +2251,17 @@ pub mod pallet {
ValueQuery,
DefaultRootClaimType<T>,
>;
#[pallet::storage] // -- MAP ( hotkey, netuid ) --> delegate_claim_type enum
pub type ValidatorClaimType<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Identity,
NetUid,
RootClaimTypeEnum,
ValueQuery,
DefaultValidatorClaimType<T>,
>;
#[pallet::storage] // --- MAP ( u64 ) --> coldkey | Maps coldkeys that have stake to an index
pub type StakingColdkeysByIndex<T: Config> =
StorageMap<_, Identity, u64, T::AccountId, OptionQuery>;
Expand Down
36 changes: 36 additions & 0 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,5 +2421,41 @@ mod dispatches {

Ok(())
}

/// --- Sets delegate claim type for a hotkey on a subnet.
#[pallet::call_index(125)]
#[pallet::weight((
Weight::from_parts(5_711_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
DispatchClass::Operational,
Pays::Yes
))]
pub fn set_validator_claim_type(
origin: OriginFor<T>,
hotkey: T::AccountId,
netuid: NetUid,
new_claim_type: RootClaimTypeEnum,
) -> DispatchResult {
let coldkey: T::AccountId = ensure_signed(origin)?;
ensure!(
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
Error::<T>::NonAssociatedColdKey
);

// Ensure the delegate claim type is not Delegated.
ensure!(
matches!(
new_claim_type,
RootClaimTypeEnum::Swap | RootClaimTypeEnum::Keep
),
Error::<T>::InvalidRootClaimType
);

ValidatorClaimType::<T>::insert(hotkey.clone(), netuid, new_claim_type.clone());
Self::deposit_event(Event::ValidatorClaimTypeSet {
hotkey: hotkey.clone(),
root_claim_type: new_claim_type,
});
Ok(())
}
}
}
2 changes: 2 additions & 0 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,7 @@ mod errors {
InvalidRootClaimThreshold,
/// Exceeded subnet limit number or zero.
InvalidSubnetNumber,
/// Delegates cant set delegated as claim type
InvalidRootClaimType,
}
}
10 changes: 10 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ mod events {
root_claim_type: RootClaimTypeEnum,
},

/// Root claim type for a coldkey has been set.
/// Parameters:
/// (coldkey, u8)
ValidatorClaimTypeSet {
/// delegate hotkey
hotkey: T::AccountId,
/// root claim type enum
root_claim_type: RootClaimTypeEnum,
},

/// Subnet lease dividends have been distributed.
SubnetLeaseDividendsDistributed {
/// The lease ID
Expand Down
19 changes: 16 additions & 3 deletions pallets/subtensor/src/staking/claim_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<T: Config> Pallet<T> {
hotkey: &T::AccountId,
coldkey: &T::AccountId,
netuid: NetUid,
root_claim_type: RootClaimTypeEnum,
mut root_claim_type: RootClaimTypeEnum,
ignore_minimum_condition: bool,
) {
// Subtract the root claimed.
Expand Down Expand Up @@ -157,6 +157,11 @@ impl<T: Config> Pallet<T> {
return; // no-op
}

// If root_claim_type is Delegated, switch to the delegate's actual claim type.
if root_claim_type == RootClaimTypeEnum::Delegated {
root_claim_type = ValidatorClaimType::<T>::get(hotkey, netuid);
}

match root_claim_type {
// Increase stake on root
RootClaimTypeEnum::Swap => {
Expand All @@ -170,11 +175,13 @@ impl<T: Config> Pallet<T> {
Ok(owed_tao) => owed_tao,
Err(err) => {
log::error!("Error swapping alpha for TAO: {err:?}");

return;
}
};

// Importantly measures swap as flow.
Self::record_tao_outflow(netuid, owed_tao.amount_paid_out.into());

Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
hotkey,
coldkey,
Expand All @@ -189,14 +196,20 @@ impl<T: Config> Pallet<T> {
);
}
RootClaimTypeEnum::Keep => {
// Increase the stake with the alpha owned
// Increase the stake with the alpha owed
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
hotkey,
coldkey,
netuid,
owed_u64.into(),
);
}
// Add Delegated arm for completeness, but it should never reach here due to switch above.
RootClaimTypeEnum::Delegated => {
// Should not reach here. Added for completeness.
log::error!("Delegated root_claim_type should have been switched. Skipping.");
return;
}
};

// Increase root claimed by owed amount.
Expand Down
Loading
Loading