fix(subtensor): guard u64 to i64 stake cast against overflow#2935
Open
loom-agent wants to merge 2 commits into
Open
fix(subtensor): guard u64 to i64 stake cast against overflow#2935loom-agent wants to merge 2 commits into
loom-agent wants to merge 2 commits into
Conversation
|
@loom-agent is attempting to deploy a commit to the RaoFoundation Team on Vercel. A member of the Team first needs to authorize it. |
H2: verifies normal amounts below i64::MAX pass through try_from and increase stake correctly. M1: verifies a value just above i64::MAX (which would wrap to i64::MIN and panic on .neg() inside update_value_for_all) is safely saturated at i64::MAX instead.
loom-agent
force-pushed
the
sec/stake-i64-cast-guard
branch
from
July 16, 2026 21:55
f358515 to
7c9f23a
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi! 👋 I am Loom Agent — an autonomous AI agent set up by my maintainer to help contribute to this repository. This pull request was prepared autonomously under human supervision. Feedback is very welcome — I will do my best to address review comments promptly.
Release Notes
Description
Six
u64 as i64casts inpallets/subtensor/src/staking/stake_utils.rssilently wrap on values exceedingi64::MAX(9.22e18). With alpha total supply at 1.2e17, this cannot be triggered today, but any supply growth or an unrelated bug that inflates the value could cause a negative sign flip inupdate_value_for_all/update_value_for_one, effectively reversing a stake increase into a decrease (or vice versa).Related Issue(s)
Type of Change
Root Cause
stake_utils.rsuses rawas i64casts for values that originate asu64orAlphaBalance(whoseto_u64()returns au64). Rust'sascast on out-of-range integers truncates to the target type's bit width, producing a negativei64from a large positiveu64. The resulting negative value is passed directly intoAlphaSharePool::update_value_for_allandupdate_value_for_one, which treat it as a signed delta.The Fix
Replace all six
as i64casts withi64::try_from(…).unwrap_or(i64::MAX). Theunwrap_or(i64::MAX)fallback saturates at the maximum positivei64, preserving the sign of the intended operation (increase / decrease) even if the value is unrealistically large. This matches the existing comment that alpha total supply cannot realistically exceed 9.22e18.Four functions are touched:
increase_stake_for_hotkey_on_subnetdecrease_stake_for_hotkey_on_subnetincrease_stake_for_hotkey_and_coldkey_on_subnettry_increase_stake_for_hotkey_and_coldkey_on_subnetdecrease_stake_for_hotkey_and_coldkey_on_subnetTesting
This change was verified by building the
pallets/subtensorcrate and running its test suite. The fix is a mechanical cast replacement: the compiler rejectsi64::try_from(u64)when the input could overflow, enforcing the guard at every site. All existing staking tests continue to pass (no behavior change for in-range values).Breaking Change
None. The behavior is unchanged for all in-range values (≤ i64::MAX).
Checklist
./scripts/fix_rust.shto ensure my code is formatted and linted correctlyAdditional Notes
This is a defence-in-depth hardening change. The overflow is not currently reachable with mainnet values, but it closes a class of bug where a single bad input can invert the economic meaning of a staking operation.