Skip to content

Add TotalAlphaStaked counter per netuid - #3085

Merged
unarbos merged 2 commits into
release-448from
feat/alpha-accounting-staking-total
Aug 18, 2026
Merged

Add TotalAlphaStaked counter per netuid#3085
unarbos merged 2 commits into
release-448from
feat/alpha-accounting-staking-total

Conversation

@UnArbosFour

Copy link
Copy Markdown
Contributor

Description

Adds TotalAlphaStaked, an O(1) per-subnet aggregate of all alpha currently staked across hotkeys.

The maintained invariant is:

TotalAlphaStaked[netuid] == sum(TotalHotkeyAlpha[hotkey, netuid])

Changes

  • Adds TotalAlphaStaked, keyed by netuid.
  • Synchronizes the aggregate whenever TotalHotkeyAlpha changes through the shared staking pool logic.
  • Covers stake additions and removals, full unstaking, cross-subnet stake movement, emissions, recycling, and alpha burns.
  • Ensures incentives burned because they target subnet-owner UIDs do not increase the aggregate.
  • Initializes the aggregate for genesis stake.
  • Adds a one-time runtime migration that backfills each subnet by summing its existing TotalHotkeyAlpha entries.
  • Accounts for migration and incremental cleanup database weights.
  • Clears the entry during subnet dissolution and alpha/stake cleanup.
  • Bumps the runtime spec version to 448.
  • Regenerates Python SDK metadata to expose the new storage entry.

Motivation

Consumers currently need to iterate over every TotalHotkeyAlpha entry to determine the total alpha staked on a subnet. Maintaining this aggregate makes that value directly queryable while preserving the existing per-hotkey accounting as the source of truth.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Breaking Change

None.

Testing

Added invariant-based coverage for:

  • Genesis initialization
  • First and subsequent stake additions
  • Multiple hotkeys on one subnet
  • Partial and complete stake removal
  • Zero and rejected mutations
  • Stake movement within and between subnets
  • Coinbase emission distribution
  • Owner-directed incentive burns
  • Alpha recycling and explicit burns
  • Destructive alpha cleanup
  • Subnet dissolution
  • Migration backfill, weight accounting, and idempotency

The tests compare TotalAlphaStaked against the sum of all TotalHotkeyAlpha entries for the relevant subnet.

Additional Notes

Zero-valued TotalAlphaStaked entries are permitted. Subnet dissolution explicitly removes the entry along with the subnet’s other state.

@vercel

vercel Bot commented Aug 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
subtensor Ready Ready Preview Aug 18, 2026 7:29pm

Request Review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review — see the sticky summary comment for the verdict and the inline comments below for specific findings.

Comment on lines +1608 to +1610
let previous = TotalHotkeyAlpha::<T>::get(&self.hotkey, self.netuid);
let value = AlphaBalance::from(value);
TotalAlphaStaked::<T>::mutate(self.netuid, |total| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Stake mutations add unaccounted database I/O

This shared path now adds a TotalHotkeyAlpha read plus a TotalAlphaStaked read/write to every affected staking operation, but the PR does not update or regenerate the corresponding dispatch weights. Repeated calls can therefore consume more database resources than the block weight system charges. Regenerate the affected benchmarks/weights and include the resulting weight changes.

@github-actions

github-actions Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

🛡️ AI Review — Skeptic (security review)

VERDICT: VULNERABLE

VERY HIGH scrutiny (47-day-old account and one public repo), tempered by repository write access and substantive merged contributions; release-448 target, no Gittensor association found.

The prior weight-accounting concern remains after the latest merge commit.

Findings

Sev File Finding
MEDIUM pallets/subtensor/src/staking/stake_utils.rs:1610 Stake mutations add unaccounted database I/O inline

Prior-comment reconciliation

  • bace116e: not addressed — Cleanup-specific metering was added, but the benchmark-generated extrinsic weights for ordinary stake-pool mutations still do not account for the new read and aggregate mutation.

Conclusion

The aggregate accounting appears internally consistent, but ordinary stake mutations remain undercharged for the newly added storage I/O.


📜 Previous run (superseded)
Sev File Finding Status
MEDIUM pallets/subtensor/src/staking/stake_utils.rs:1610 Stake mutations add unaccounted database I/O ➡️ Carried forward to current findings
Cleanup-specific metering was added, but the benchmark-generated extrinsic weights for ordinary stake-pool mutations still do not account for the new read and aggregate mutation.

# 🔍 AI Review — Auditor (domain review) has not yet run on this PR.

@github-actions

Copy link
Copy Markdown
Contributor

🔄 AI review updated — Skeptic: VULNERABLE

@UnArbosFour
UnArbosFour changed the base branch from main to release-448 August 18, 2026 19:25

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review — see the sticky summary comment for the verdict and the inline comments below for specific findings.

Comment on lines +1608 to +1610
let previous = TotalHotkeyAlpha::<T>::get(&self.hotkey, self.netuid);
let value = AlphaBalance::from(value);
TotalAlphaStaked::<T>::mutate(self.netuid, |total| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Stake mutations add unaccounted database I/O

set_shared_value now adds a TotalHotkeyAlpha read plus a TotalAlphaStaked read/write to every shared-pool mutation. The PR adjusts cleanup metering, but does not regenerate or conservatively update the weights of the many extrinsics that reach this method. Those calls can therefore consume more block resources than they declare. Regenerate the affected benchmarks/weights, or explicitly add the new database costs to every reachable weighted path.

@github-actions

Copy link
Copy Markdown
Contributor

🔄 AI review updated — Skeptic: VULNERABLE

@unarbos
unarbos merged commit 011738f into release-448 Aug 18, 2026
95 of 100 checks passed
unarbos added a commit that referenced this pull request Aug 18, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>

#3085 added TotalAlphaStaked storage, so the SDK read, stub, query page, and catalogs can ship again. Regenerated docs for the merged limit-order and staking changes.
@unarbos
unarbos deleted the feat/alpha-accounting-staking-total branch August 18, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants