-
Notifications
You must be signed in to change notification settings - Fork 318
feat: net TAO flow for emission allocation #2634
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
+130
−4
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,45 @@ impl<T: Config> Pallet<T> { | |
| SubnetTaoFlow::<T>::remove(netuid); | ||
| } | ||
|
|
||
| pub fn record_protocol_inflow(netuid: NetUid, tao: TaoBalance) { | ||
| SubnetProtocolFlow::<T>::mutate(netuid, |flow| { | ||
| *flow = flow.saturating_add(u64::from(tao) as i64); | ||
| }); | ||
| } | ||
|
|
||
| pub fn record_protocol_outflow(netuid: NetUid, tao: TaoBalance) { | ||
| SubnetProtocolFlow::<T>::mutate(netuid, |flow| { | ||
| *flow = flow.saturating_sub(u64::from(tao) as i64); | ||
| }); | ||
| } | ||
|
|
||
| pub fn reset_protocol_flow(netuid: NetUid) { | ||
| SubnetProtocolFlow::<T>::remove(netuid); | ||
| } | ||
|
|
||
| fn get_ema_protocol_flow(netuid: NetUid) -> I64F64 { | ||
|
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. This method is a getter, it should not modify state |
||
| let current_block: u64 = Self::get_current_block_as_u64(); | ||
|
|
||
| let block_flow = I64F64::saturating_from_num(SubnetProtocolFlow::<T>::get(netuid)); | ||
| let (last_block, last_block_ema) = | ||
| SubnetEmaProtocolFlow::<T>::get(netuid).unwrap_or((0, I64F64::saturating_from_num(0))); | ||
|
|
||
| if last_block != current_block { | ||
| let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get()) | ||
| .safe_div(I64F64::saturating_from_num(i64::MAX)); | ||
| let one = I64F64::saturating_from_num(1); | ||
| let ema_flow = (one.saturating_sub(flow_alpha)) | ||
| .saturating_mul(last_block_ema) | ||
| .saturating_add(flow_alpha.saturating_mul(block_flow)); | ||
| SubnetEmaProtocolFlow::<T>::insert(netuid, (current_block, ema_flow)); | ||
|
|
||
| Self::reset_protocol_flow(netuid); | ||
| ema_flow | ||
| } else { | ||
| last_block_ema | ||
| } | ||
| } | ||
|
|
||
| // Update SubnetEmaTaoFlow if needed and return its value for | ||
| // the current block | ||
| #[allow(dead_code)] | ||
|
|
@@ -177,12 +216,23 @@ impl<T: Config> Pallet<T> { | |
| // Implementation of shares that uses TAO flow | ||
| #[allow(dead_code)] | ||
| fn get_shares_flow(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> { | ||
| // Get raw flows | ||
| let ema_flows = subnets_to_emit_to | ||
| let net_flow_enabled = NetTaoFlowEnabled::<T>::get(); | ||
|
|
||
| // Always update the protocol EMA (keeps it warm for when toggled on) | ||
| let ema_flows: BTreeMap<NetUid, I64F64> = subnets_to_emit_to | ||
| .iter() | ||
| .map(|netuid| (*netuid, Self::get_ema_flow(*netuid))) | ||
| .map(|netuid| { | ||
| let user_ema = Self::get_ema_flow(*netuid); | ||
|
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. Do not call |
||
| let protocol_ema = Self::get_ema_protocol_flow(*netuid); | ||
| let net = if net_flow_enabled { | ||
| user_ema.saturating_sub(protocol_ema) | ||
| } else { | ||
| user_ema | ||
| }; | ||
| (*netuid, net) | ||
| }) | ||
| .collect(); | ||
| log::debug!("EMA flows: {ema_flows:?}"); | ||
| log::debug!("EMA flows (net_flow_enabled={net_flow_enabled}): {ema_flows:?}"); | ||
|
|
||
| // Clip the EMA flow with lower limit L | ||
| // z[i] = max{S[i] − L, 0} | ||
|
|
||
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
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.
Step numbers in ai generated comments are fragile, I suggest we do not refer to them.