Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/limit nominators per delegate #367

Closed
wants to merge 4 commits into from

Conversation

gztensor
Copy link
Contributor

Description

This PR completes the feature of limiting the number of nominators per hotkey. Default value is set to 128, but it can be adjusted via admin extrinsic. Initially the code migrates by initializing counters using Stake state map, so that add_stake transaction don't need to run O(n) counting operation.

Related Issue(s)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe):

Breaking Change

n/a

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run cargo fmt and cargo clippy to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Screenshots (if applicable)

n/a

Additional Notes

n/a

@@ -548,3 +548,37 @@ pub fn migrate_stake_to_substake<T: Config>() -> Weight {
log::info!("Final weight: {:?}", weight); // Debug print
weight
}

pub fn migrate_nominator_counters<T: Config>() -> Weight {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check that this is only migrating nominations and not self delegations i.e when the cold key owns the hot key

#[pallet::storage] // --- ITEM ( delegate_limit ) --> Maximmu number of nominators per subnet validator
pub type DelegateLimit<T> = StorageValue<_, u32, ValueQuery, DefaultDelegateLimit<T>>;
#[pallet::storage] // --- ITEM ( delegate_limit ) --> Maximum number of nominators per hotkey
pub type NominatorLimit<T> = StorageValue<_, u32, ValueQuery, DefaultNominatorLimit<T>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think NominationLimit would be a more appropriate name ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not, because we are counting unique nominators here, and one nominator can make multiple nominations that would only increase the counter by 1.

#[pallet::storage] // --- ITEM ( delegate_limit ) --> Maximum number of nominators per hotkey
pub type NominatorLimit<T> = StorageValue<_, u32, ValueQuery, DefaultNominatorLimit<T>>;
#[pallet::storage] // --- MAP ( hot ) --> count | Number of nominators per hotkey
pub type NominatorCount<T: Config> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above NominationCount

@@ -258,7 +258,7 @@ impl<T: Config> Pallet<T> {
.collect()
}

pub fn get_delegate_limit() -> u32 {
DelegateLimit::<T>::get()
pub fn get_nominator_limit() -> u32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_nomination_limit

@@ -162,7 +162,7 @@ pub mod pallet {
#[pallet::constant] // Initial default delegation take.
type InitialDefaultTake: Get<u16>;
#[pallet::constant] // Initial limit on number of nominators per subnet validator
type InitialDelegateLimit: Get<u32>;
type InitialNominatorLimit: Get<u32>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InitialNominationLimit

@@ -357,7 +357,7 @@ impl pallet_subtensor::Config for Test {
type InitialSubnetLimit = InitialSubnetLimit;
type InitialNetworkRateLimit = InitialNetworkRateLimit;
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
type InitialDelegateLimit = InitialDelegateLimit;
type InitialNominatorLimit = InitialNominatorLimit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type InitialNominatorLimit = InitialNominatorLimit;
type InitialNominationLimit = InitialNominationLimit;

}

// Nominate 11th time - fails
let nominator11 = U256::from(11);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add this case given that we already test for it in the previous test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! Removing it.

@@ -644,7 +644,7 @@ parameter_types! {
pub const SubtensorInitialScalingLawPower: u16 = 50; // 0.5
pub const SubtensorInitialMaxAllowedValidators: u16 = 128;
pub const SubtensorInitialTempo: u16 = 99;
pub const SubtensorInitialDelegateLimit: u32 = 128; // Limits the number of nominators per subnet validator
pub const SubtensorInitialNominatorLimit: u32 = 128; // Limits the number of nominators per subnet validator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const SubtensorInitialNominatorLimit: u32 = 128; // Limits the number of nominators per subnet validator
pub const SubtensorInitialNominationLimit: u32 = 128; // Limits the number of nominators per subnet validator

@@ -696,7 +696,7 @@ impl pallet_subtensor::Config for Runtime {
type InitialValidatorPruneLen = SubtensorInitialValidatorPruneLen;
type InitialScalingLawPower = SubtensorInitialScalingLawPower;
type InitialTempo = SubtensorInitialTempo;
type InitialDelegateLimit = SubtensorInitialDelegateLimit;
type InitialNominatorLimit = SubtensorInitialNominatorLimit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type InitialNominatorLimit = SubtensorInitialNominatorLimit;
type InitialNominationLimit = SubtensorInitialNominationLimit;

@@ -886,8 +886,8 @@ impl
SubtensorModule::set_tempo(netuid, tempo);
}

fn set_delegate_limit(limit: u32) {
SubtensorModule::set_delegate_limit(limit);
fn set_nominator_limit(limit: u32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn set_nominator_limit(limit: u32) {
fn set_nomination_limit(limit: u32) {

@distributedstatemachine distributedstatemachine changed the base branch from dynamic to development April 26, 2024 08:23
@distributedstatemachine distributedstatemachine changed the base branch from development to dynamic April 26, 2024 08:23
@distributedstatemachine
Copy link
Contributor

Also i think this PR should be going into development not dynamic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red team feature development, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants