Skip to content

Reimplement StakeDelta #1600

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

Draft
wants to merge 6 commits into
base: devnet-ready
Choose a base branch
from
Draft

Conversation

shamil-gadelshin
Copy link
Collaborator

Description

This PR reimplements "stake delta" feature that was removed previously. It saves stake movement into the separate storage by hotkey, coldkey, and netuid. During the epoch() function execution stake delta is used as a "damper" to counter large stake movement effects on the epoch.

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

The PR changes the epoch calculations.

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

Additional Notes

The original PR comes as a draft to gather feedback.

}

pub fn total(&self) -> i128 {
self.coldkey_stake_deltas.values().sum()
Copy link
Contributor

Choose a reason for hiding this comment

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

We should consider if it helps the runtime to update this on every add/remove that way calling total() is faster.

Might be worse runtime depending on where we need to update it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The data structure is not finalized (probably, we'll use StorageNMap). We'll reconsider the total operation after that.

pub fn get_stake_delta_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
let total_stake = StakeDeltaSinceLastEmissionDrain::<T>::get(netuid, hotkey).total();

total_stake.abs().saturated_into()
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we know if the sum is positive or negative here?

I think we use it like total_stake.max(0), so we discard the negatives. But abs isn't the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we remove the stake this epoch we will get a negative value. But we should mitigate the stake movement without taking the movement direction into account (add or remove), shouldn't we? We only dampen the movement. Please, correct me if I'm wrong.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here we return only an unsigned int so we can't tell if the delta was positive or negative.

We only truly care about positive deltas anyway (at least right now).

Copy link
Collaborator Author

@shamil-gadelshin shamil-gadelshin May 14, 2025

Choose a reason for hiding this comment

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

Here we return only an unsigned int so we can't tell if the delta was positive or negative.

Yes, because I was going to reduce any movement effect ("punish any stake movement for the epoch"). I chose "abs" because otherwise "remove_stake" will create a negative value and we'll increase the emissions, which doesn't seem to be a desired outcome.

We only truly care about positive deltas anyway (at least right now).

Ok. I will go with total_stake.max(0) then. Thanks!

@shamil-gadelshin shamil-gadelshin marked this pull request as ready for review May 19, 2025 11:14
@shamil-gadelshin shamil-gadelshin added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label May 19, 2025
@shamil-gadelshin
Copy link
Collaborator Author

The last changes address PR comments, introduce additional places for stake delta effects, add tests, and simplify stake delta storage map by removing the coldkey from the storage.

@shamil-gadelshin shamil-gadelshin marked this pull request as draft May 21, 2025 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-cargo-audit This PR fails cargo audit but needs to be merged anyway
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants