Skip to content

fix: validate negative TAO amounts in staking#7

Open
OpenSourceSoul wants to merge 1 commit intoone-covenant:mainfrom
OpenSourceSoul:fix/negative-tao-validation
Open

fix: validate negative TAO amounts in staking#7
OpenSourceSoul wants to merge 1 commit intoone-covenant:mainfrom
OpenSourceSoul:fix/negative-tao-validation

Conversation

@OpenSourceSoul
Copy link
Copy Markdown

@OpenSourceSoul OpenSourceSoul commented Feb 1, 2026

Problem
StakeParams::new_tao() accepted negative TAO amounts without validation. When a negative f64 is cast to u64, it produces a massive value (close to u64::MAX), causing incorrect staking amounts.

Solution

  • Add validation to reject negative amounts in new_tao()
  • Return Result<Self, BittensorError> for proper error handling
  • Added tests for negative amount rejection and zero amount handling

Changes

  • bittensor-rs/src/extrinsics/staking.rs: 48 insertions, 20 deletions

This prevents users from accidentally staking massive amounts when passing negative values.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Staking operations now include enhanced input validation to prevent processing of negative amounts, providing clear error messages for invalid inputs.
  • Tests

    • Added comprehensive test coverage for edge cases, including zero amounts and negative amount rejection.

✏️ Tip: You can customize this high-level summary in your review settings.

Prevent negative TAO amounts from causing u64 overflow when converted
to RAO. Previously, a negative value like -1.0 would produce a massive
u64 value (close to u64::MAX) due to how floating point to integer
conversion works in Rust.

Changes:
- Add validation in new_tao() to reject negative amounts
- Return Result type instead of Self for error handling
- Update docstring examples to use unwrap()
- Add tests for negative amount rejection and zero amount handling
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 1, 2026

Walkthrough

The StakeParams::new_tao method is refactored to return a Result<Self, BittensorError> instead of Self, introducing validation to reject negative amounts. Error handling is updated at call sites, documentation examples are modified to reflect the new behavior, and tests are added to verify zero amounts are permitted while negative amounts are properly rejected.

Changes

Cohort / File(s) Summary
Staking validation and error handling
bittensor-rs/src/extrinsics/staking.rs
Refactored StakeParams::new_tao to return Result<Self, BittensorError> with validation that rejects negative amounts. Updated call sites to handle the Result type, modified documentation examples to use error-handling patterns, and added tests for zero amounts (allowed) and negative amounts (rejected with error).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • Issue #8: Directly addresses the same code-level fix to StakeParams::new_tao — adding non-negative validation, changing return type to Result, and introducing corresponding tests.

Poem

🐰 A stake must be worthy, never less than the ground,
No negative values in validation we've found,
Result types and Errors now guard the path true,
Each tao will validate before passing on through!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding validation for negative TAO amounts in the staking module, which is the core objective of this pull request.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@OpenSourceSoul
Copy link
Copy Markdown
Author

hi! @distributedstatemachine check please

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.

1 participant