-
Notifications
You must be signed in to change notification settings - Fork 249
Root claim #2053
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
Merged
Merged
Root claim #2053
Changes from 36 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
2f7ea4b
Add root claim infrastructure
shamil-gadelshin c579597
Alter coinbase.rs
shamil-gadelshin 6c614fe
Alter tests
shamil-gadelshin e04c8e9
Remove comments
shamil-gadelshin f607993
Change TaoDividendsPerSubnet -> AlphaDividendsPerSubnet
shamil-gadelshin 8877c40
Fix: PendingAlphaSwapped only needs to increase for non-subsidized su…
gztensor e7605df
Update tests
shamil-gadelshin c48a9c4
Update coinbase.rs
shamil-gadelshin 9fa484a
Cargo fmt
shamil-gadelshin 7f5dba4
Remove TaoDividendsPerSubnet
shamil-gadelshin 28e2f74
Remove PendingAlphaSwapped
shamil-gadelshin eb32237
Rename RootDebt -> RootClaimed
shamil-gadelshin 2e613b0
Fix RootClaimable map
shamil-gadelshin b125673
Add root claim tests.
shamil-gadelshin 0151f49
Add basic test with drain_emissions
shamil-gadelshin 83b2b36
Update tests.
shamil-gadelshin c482f2f
Update tests
shamil-gadelshin 55c1b1d
Simplify tests
shamil-gadelshin a11d7a9
Add disproportional stake test
shamil-gadelshin e3cc8e1
Add test with removed stake.
shamil-gadelshin dd44dd8
Fix existing tests
shamil-gadelshin 0b13d2e
Update tests
shamil-gadelshin 8d1140d
Fix swap claim root
shamil-gadelshin 65bb4f0
Add basic run_coinbase test
shamil-gadelshin 1accdfa
Fix block indices to claim
shamil-gadelshin c09bcb3
Optimize RootClaimable
shamil-gadelshin fc161b3
Update benchmarks
shamil-gadelshin b39d51c
Add coldkey to the new map.
shamil-gadelshin 57bcae3
Add root staking maps migration.
shamil-gadelshin cc7a1df
Add run_coinbase test for root claim.
shamil-gadelshin 78e45af
Refactor root claim code.
shamil-gadelshin dcaa3f7
Merge branch 'devnet-ready' into root-claim2
shamil-gadelshin d0b2dac
Fix merge issues
shamil-gadelshin 0f82356
Remove obsolete code
shamil-gadelshin 239df82
Fix typo
shamil-gadelshin 69e222b
Add sudo_set_num_root_claims extrinsic
shamil-gadelshin c5d9f31
Update claim_root benchmark.
shamil-gadelshin 99345b1
Refactor code
shamil-gadelshin cc324ec
Add migration
shamil-gadelshin 8d63857
Merge branch 'devnet-ready' into root-claim2
shamil-gadelshin 1f1c7e7
Fix merge conflicts
shamil-gadelshin 3d3ef0d
Update pallets/subtensor/src/staking/claim_root.rs
shamil-gadelshin ab5a274
Handle case for stake mismatch.
shamil-gadelshin db3a0f0
Add proxy type.
shamil-gadelshin c213dd0
Add swap_coldkey support for root claim
shamil-gadelshin d6203c0
Add swap_hotkey support for root claim
shamil-gadelshin 7a2ebae
Remove PendingRootDivs map
shamil-gadelshin b0e346e
Claim all during network deregistration.
shamil-gadelshin d68d3aa
Modify default claim root key number
shamil-gadelshin 29e99d7
Introduce root claim threshold for subnets.
shamil-gadelshin 0143105
Merge branch 'devnet-ready' into root-claim2
shamil-gadelshin fb17282
Fix benchmarks
shamil-gadelshin 55f30b2
auto-update benchmark weights
github-actions[bot] 558f908
Apply review suggestions
shamil-gadelshin 28deede
Merge branch 'devnet-ready' into root-claim2
shamil-gadelshin efbe782
auto-update benchmark weights
github-actions[bot] f08ef46
Enable zero as sudo_set_num_root_claims param value.
shamil-gadelshin 91e5776
Add subnets to claim_root extrinsic.
shamil-gadelshin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -214,27 +214,14 @@ impl<T: Config> Pallet<T> { | |||||
| // Get pending alpha as original alpha_out - root_alpha. | ||||||
| let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha); | ||||||
| log::debug!("pending_alpha: {pending_alpha:?}"); | ||||||
| // Sell root emission through the pool (do not pay fees) | ||||||
|
|
||||||
| let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false); | ||||||
| if !subsidized { | ||||||
| let swap_result = Self::swap_alpha_for_tao( | ||||||
| *netuid_i, | ||||||
| tou64!(root_alpha).into(), | ||||||
| T::SwapInterface::min_price().into(), | ||||||
| true, | ||||||
| ); | ||||||
| if let Ok(ok_result) = swap_result { | ||||||
| let root_tao: u64 = ok_result.amount_paid_out; | ||||||
| // Accumulate root divs for subnet. | ||||||
| PendingRootDivs::<T>::mutate(*netuid_i, |total| { | ||||||
| *total = total.saturating_add(root_tao.into()); | ||||||
| }); | ||||||
| } | ||||||
| PendingRootAlphaDivs::<T>::mutate(*netuid_i, |total| { | ||||||
| *total = total.saturating_add(tou64!(root_alpha).into()); | ||||||
| }); | ||||||
| } | ||||||
| // Accumulate alpha emission in pending. | ||||||
| PendingAlphaSwapped::<T>::mutate(*netuid_i, |total| { | ||||||
| *total = total.saturating_add(tou64!(root_alpha).into()); | ||||||
| }); | ||||||
|
|
||||||
| // Accumulate alpha emission in pending. | ||||||
| PendingEmission::<T>::mutate(*netuid_i, |total| { | ||||||
| *total = total.saturating_add(tou64!(pending_alpha).into()); | ||||||
|
|
@@ -266,25 +253,15 @@ impl<T: Config> Pallet<T> { | |||||
| PendingEmission::<T>::insert(netuid, AlphaCurrency::ZERO); | ||||||
|
|
||||||
| // Get and drain the subnet pending root divs. | ||||||
shamil-gadelshin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| let pending_tao = PendingRootDivs::<T>::get(netuid); | ||||||
| PendingRootDivs::<T>::insert(netuid, TaoCurrency::ZERO); | ||||||
|
|
||||||
| // Get this amount as alpha that was swapped for pending root divs. | ||||||
| let pending_swapped = PendingAlphaSwapped::<T>::get(netuid); | ||||||
| PendingAlphaSwapped::<T>::insert(netuid, AlphaCurrency::ZERO); | ||||||
| let pending_root_alpha = PendingRootAlphaDivs::<T>::get(netuid); | ||||||
| PendingRootAlphaDivs::<T>::insert(netuid, AlphaCurrency::ZERO); | ||||||
|
|
||||||
| // Get owner cut and drain. | ||||||
| let owner_cut = PendingOwnerCut::<T>::get(netuid); | ||||||
| PendingOwnerCut::<T>::insert(netuid, AlphaCurrency::ZERO); | ||||||
|
|
||||||
| // Drain pending root divs, alpha emission, and owner cut. | ||||||
shamil-gadelshin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Self::drain_pending_emission( | ||||||
| netuid, | ||||||
| pending_alpha, | ||||||
| pending_tao, | ||||||
| pending_swapped, | ||||||
| owner_cut, | ||||||
| ); | ||||||
| Self::drain_pending_emission(netuid, pending_alpha, pending_root_alpha, owner_cut); | ||||||
| } else { | ||||||
| // Increment | ||||||
| BlocksSinceLastStep::<T>::mutate(netuid, |total| *total = total.saturating_add(1)); | ||||||
|
|
@@ -327,7 +304,7 @@ impl<T: Config> Pallet<T> { | |||||
|
|
||||||
| pub fn calculate_dividend_distribution( | ||||||
| pending_alpha: AlphaCurrency, | ||||||
| pending_tao: TaoCurrency, | ||||||
| pending_root_alpha: AlphaCurrency, | ||||||
| tao_weight: U96F32, | ||||||
| stake_map: BTreeMap<T::AccountId, (AlphaCurrency, AlphaCurrency)>, | ||||||
| dividends: BTreeMap<T::AccountId, U96F32>, | ||||||
|
|
@@ -338,7 +315,7 @@ impl<T: Config> Pallet<T> { | |||||
| log::debug!("dividends: {dividends:?}"); | ||||||
| log::debug!("stake_map: {stake_map:?}"); | ||||||
| log::debug!("pending_alpha: {pending_alpha:?}"); | ||||||
| log::debug!("pending_tao: {pending_tao:?}"); | ||||||
| log::debug!("pending_root_alpha: {pending_root_alpha:?}"); | ||||||
| log::debug!("tao_weight: {tao_weight:?}"); | ||||||
|
|
||||||
| // Setup. | ||||||
|
|
@@ -390,22 +367,22 @@ impl<T: Config> Pallet<T> { | |||||
| log::debug!("total_root_divs: {total_root_divs:?}"); | ||||||
| log::debug!("total_alpha_divs: {total_alpha_divs:?}"); | ||||||
|
|
||||||
| // Compute root divs as TAO. Here we take | ||||||
| let mut tao_dividends: BTreeMap<T::AccountId, U96F32> = BTreeMap::new(); | ||||||
| // Compute root alpha divs. Here we take | ||||||
shamil-gadelshin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| let mut root_alpha_dividends: BTreeMap<T::AccountId, U96F32> = BTreeMap::new(); | ||||||
| for (hotkey, root_divs) in root_dividends { | ||||||
| // Root proportion. | ||||||
| let root_share: U96F32 = root_divs.checked_div(total_root_divs).unwrap_or(zero); | ||||||
| log::debug!("hotkey: {hotkey:?}, root_share: {root_share:?}"); | ||||||
| // Root proportion in TAO | ||||||
| let root_tao: U96F32 = asfloat!(pending_tao).saturating_mul(root_share); | ||||||
| log::debug!("hotkey: {hotkey:?}, root_tao: {root_tao:?}"); | ||||||
| // Root proportion in alpha | ||||||
| let root_alpha: U96F32 = asfloat!(pending_root_alpha).saturating_mul(root_share); | ||||||
| log::debug!("hotkey: {hotkey:?}, root_alpha: {root_alpha:?}"); | ||||||
| // Record root dividends as TAO. | ||||||
| tao_dividends | ||||||
| root_alpha_dividends | ||||||
| .entry(hotkey) | ||||||
| .and_modify(|e| *e = root_tao) | ||||||
| .or_insert(root_tao); | ||||||
| .and_modify(|e| *e = root_alpha) | ||||||
| .or_insert(root_alpha); | ||||||
| } | ||||||
| log::debug!("tao_dividends: {tao_dividends:?}"); | ||||||
| log::debug!("root_alpha_dividends: {root_alpha_dividends:?}"); | ||||||
|
|
||||||
| // Compute proportional alpha divs using the pending alpha and total alpha divs from the epoch. | ||||||
| let mut prop_alpha_dividends: BTreeMap<T::AccountId, U96F32> = BTreeMap::new(); | ||||||
|
|
@@ -425,7 +402,7 @@ impl<T: Config> Pallet<T> { | |||||
| } | ||||||
| log::debug!("prop_alpha_dividends: {prop_alpha_dividends:?}"); | ||||||
|
|
||||||
| (prop_alpha_dividends, tao_dividends) | ||||||
| (prop_alpha_dividends, root_alpha_dividends) | ||||||
| } | ||||||
|
|
||||||
| fn get_owner_hotkeys(netuid: NetUid, coldkey: &T::AccountId) -> Vec<T::AccountId> { | ||||||
|
|
@@ -465,7 +442,7 @@ impl<T: Config> Pallet<T> { | |||||
| owner_cut: AlphaCurrency, | ||||||
| incentives: BTreeMap<T::AccountId, AlphaCurrency>, | ||||||
| alpha_dividends: BTreeMap<T::AccountId, U96F32>, | ||||||
| tao_dividends: BTreeMap<T::AccountId, U96F32>, | ||||||
| root_alpha_dividends: BTreeMap<T::AccountId, U96F32>, | ||||||
| ) { | ||||||
| // Distribute the owner cut. | ||||||
| if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) { | ||||||
|
|
@@ -565,37 +542,31 @@ impl<T: Config> Pallet<T> { | |||||
| TotalHotkeyAlphaLastEpoch::<T>::insert(hotkey, netuid, total_hotkey_alpha); | ||||||
| } | ||||||
|
|
||||||
| // Distribute root tao divs. | ||||||
| let _ = TaoDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None); | ||||||
| for (hotkey, mut root_tao) in tao_dividends { | ||||||
| // Distribute root alpha divs. | ||||||
| for (hotkey, mut root_alpha) in root_alpha_dividends { | ||||||
| // Get take prop | ||||||
| let tao_take: U96F32 = Self::get_hotkey_take_float(&hotkey).saturating_mul(root_tao); | ||||||
| let alpha_take: U96F32 = | ||||||
| Self::get_hotkey_take_float(&hotkey).saturating_mul(root_alpha); | ||||||
| // Remove take prop from root_tao | ||||||
shamil-gadelshin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| root_tao = root_tao.saturating_sub(tao_take); | ||||||
| root_alpha = root_alpha.saturating_sub(alpha_take); | ||||||
| // Give the validator their take. | ||||||
| log::debug!("hotkey: {hotkey:?} tao_take: {tao_take:?}"); | ||||||
| let validator_stake = Self::increase_stake_for_hotkey_and_coldkey_on_subnet( | ||||||
| log::debug!("hotkey: {hotkey:?} alpha_take: {alpha_take:?}"); | ||||||
| let _validator_stake = Self::increase_stake_for_hotkey_and_coldkey_on_subnet( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is there a reason we keep the variable here? |
||||||
| &hotkey, | ||||||
| &Owner::<T>::get(hotkey.clone()), | ||||||
| NetUid::ROOT, | ||||||
| tou64!(tao_take).into(), | ||||||
| netuid, | ||||||
camfairchild marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| tou64!(alpha_take).into(), | ||||||
| ); | ||||||
| // Give rest to nominators. | ||||||
| log::debug!("hotkey: {hotkey:?} root_tao: {root_tao:?}"); | ||||||
| Self::increase_stake_for_hotkey_on_subnet( | ||||||
|
|
||||||
| Self::increase_root_claimable_for_hotkey_and_subnet( | ||||||
| &hotkey, | ||||||
| NetUid::ROOT, | ||||||
| tou64!(root_tao).into(), | ||||||
| netuid, | ||||||
| tou64!(root_alpha).into(), | ||||||
| ); | ||||||
|
|
||||||
| // Record root dividends for this validator on this subnet. | ||||||
shamil-gadelshin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| TaoDividendsPerSubnet::<T>::mutate(netuid, hotkey.clone(), |divs| { | ||||||
| *divs = divs.saturating_add(tou64!(root_tao).into()); | ||||||
| }); | ||||||
| // Update the total TAO on the subnet with root tao dividends. | ||||||
| SubnetTAO::<T>::mutate(NetUid::ROOT, |total| { | ||||||
| *total = total | ||||||
| .saturating_add(validator_stake.to_u64().into()) | ||||||
| .saturating_add(tou64!(root_tao).into()); | ||||||
| AlphaDividendsPerSubnet::<T>::mutate(netuid, hotkey.clone(), |divs| { | ||||||
| *divs = divs.saturating_add(tou64!(root_alpha).into()); | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -617,7 +588,7 @@ impl<T: Config> Pallet<T> { | |||||
|
|
||||||
| pub fn calculate_dividend_and_incentive_distribution( | ||||||
| netuid: NetUid, | ||||||
| pending_tao: TaoCurrency, | ||||||
| pending_root_alpha: AlphaCurrency, | ||||||
| pending_validator_alpha: AlphaCurrency, | ||||||
| hotkey_emission: Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)>, | ||||||
| tao_weight: U96F32, | ||||||
|
|
@@ -633,33 +604,32 @@ impl<T: Config> Pallet<T> { | |||||
|
|
||||||
| let stake_map = Self::get_stake_map(netuid, dividends.keys().collect::<Vec<_>>()); | ||||||
|
|
||||||
| let (alpha_dividends, tao_dividends) = Self::calculate_dividend_distribution( | ||||||
| let (alpha_dividends, root_alpha_dividends) = Self::calculate_dividend_distribution( | ||||||
| pending_validator_alpha, | ||||||
| pending_tao, | ||||||
| pending_root_alpha, | ||||||
| tao_weight, | ||||||
| stake_map, | ||||||
| dividends, | ||||||
| ); | ||||||
|
|
||||||
| (incentives, (alpha_dividends, tao_dividends)) | ||||||
| (incentives, (alpha_dividends, root_alpha_dividends)) | ||||||
| } | ||||||
|
|
||||||
| pub fn drain_pending_emission( | ||||||
| netuid: NetUid, | ||||||
| pending_alpha: AlphaCurrency, | ||||||
| pending_tao: TaoCurrency, | ||||||
| pending_swapped: AlphaCurrency, | ||||||
| pending_root_alpha: AlphaCurrency, | ||||||
| owner_cut: AlphaCurrency, | ||||||
| ) { | ||||||
| log::debug!( | ||||||
| "Draining pending alpha emission for netuid {netuid:?}, pending_alpha: {pending_alpha:?}, pending_tao: {pending_tao:?}, pending_swapped: {pending_swapped:?}, owner_cut: {owner_cut:?}" | ||||||
| "Draining pending alpha emission for netuid {netuid:?}, pending_alpha: {pending_alpha:?}, pending_root_alpha: {pending_root_alpha:?}, owner_cut: {owner_cut:?}" | ||||||
| ); | ||||||
|
|
||||||
| let tao_weight = Self::get_tao_weight(); | ||||||
|
|
||||||
| // Run the epoch. | ||||||
| let hotkey_emission: Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)> = | ||||||
| Self::epoch_with_mechanisms(netuid, pending_alpha.saturating_add(pending_swapped)); | ||||||
| Self::epoch_with_mechanisms(netuid, pending_alpha.saturating_add(pending_root_alpha)); | ||||||
| log::debug!("hotkey_emission: {hotkey_emission:?}"); | ||||||
|
|
||||||
| // Compute the pending validator alpha. | ||||||
|
|
@@ -676,18 +646,18 @@ impl<T: Config> Pallet<T> { | |||||
|
|
||||||
| let pending_validator_alpha = if !incentive_sum.is_zero() { | ||||||
| pending_alpha | ||||||
| .saturating_add(pending_swapped) | ||||||
| .saturating_add(pending_root_alpha) | ||||||
| .saturating_div(2.into()) | ||||||
| .saturating_sub(pending_swapped) | ||||||
| .saturating_sub(pending_root_alpha) | ||||||
| } else { | ||||||
| // If the incentive is 0, then Validators get 100% of the alpha. | ||||||
| pending_alpha | ||||||
| }; | ||||||
|
|
||||||
| let (incentives, (alpha_dividends, tao_dividends)) = | ||||||
| let (incentives, (alpha_dividends, root_alpha_dividends)) = | ||||||
| Self::calculate_dividend_and_incentive_distribution( | ||||||
| netuid, | ||||||
| pending_tao, | ||||||
| pending_root_alpha, | ||||||
| pending_validator_alpha, | ||||||
| hotkey_emission, | ||||||
| tao_weight, | ||||||
|
|
@@ -698,7 +668,7 @@ impl<T: Config> Pallet<T> { | |||||
| owner_cut, | ||||||
| incentives, | ||||||
| alpha_dividends, | ||||||
| tao_dividends, | ||||||
| root_alpha_dividends, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PendingRootAlphaDivs::<T>::remove(netuid)is called twice: line 318 & 351