-
Notifications
You must be signed in to change notification settings - Fork 4
fix: reject negative TAO amounts in staking and transfer params #9
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,15 +36,20 @@ impl TransferParams { | |
| /// let params = TransferParams::new_tao( | ||
| /// "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", | ||
| /// 1.5 | ||
| /// ); | ||
| /// ).unwrap(); | ||
| /// assert_eq!(params.amount_rao, 1_500_000_000); | ||
| /// ``` | ||
| pub fn new_tao(dest: &str, amount_tao: f64) -> Self { | ||
| Self { | ||
| pub fn new_tao(dest: &str, amount_tao: f64) -> Result<Self, BittensorError> { | ||
| if !amount_tao.is_finite() || amount_tao < 0.0 { | ||
| return Err(BittensorError::InvalidAmount { | ||
| reason: format!("TAO amount must be a non-negative finite number, got {}", amount_tao), | ||
| }); | ||
| } | ||
| Ok(Self { | ||
| dest: dest.to_string(), | ||
| amount_rao: (amount_tao * 1_000_000_000.0) as u64, | ||
| keep_alive: true, | ||
| } | ||
| }) | ||
|
Comment on lines
+42
to
+52
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. Validate the scaled RAO range as well, not just the raw TAO input. The current guard still accepts large finite TAO amounts whose scaled RAO value no longer fits in 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /// Create new transfer params with amount in RAO | ||
|
|
@@ -84,7 +89,7 @@ impl TransferParams { | |
| /// let params = TransferParams::new_tao( | ||
| /// "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty", | ||
| /// 1.0 | ||
| /// ); | ||
| /// )?; | ||
| /// let result = transfer(client, signer, params).await?; | ||
| /// Ok(()) | ||
| /// } | ||
|
|
@@ -218,11 +223,35 @@ mod tests { | |
| #[test] | ||
| fn test_transfer_params_tao() { | ||
| let params = | ||
| TransferParams::new_tao("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 1.5); | ||
| TransferParams::new_tao("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 1.5) | ||
| .unwrap(); | ||
| assert_eq!(params.amount_rao, 1_500_000_000); | ||
| assert!(params.keep_alive); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_transfer_params_negative_tao() { | ||
| let result = | ||
| TransferParams::new_tao("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", -1.0); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_transfer_params_nan_tao() { | ||
| let result = | ||
| TransferParams::new_tao("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", f64::NAN); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_transfer_params_infinity_tao() { | ||
| let result = TransferParams::new_tao( | ||
| "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", | ||
| f64::INFINITY, | ||
| ); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_transfer_params_rao() { | ||
| let params = | ||
|
|
@@ -234,6 +263,7 @@ mod tests { | |
| fn test_transfer_params_builder() { | ||
| let params = | ||
| TransferParams::new_tao("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 1.0) | ||
| .unwrap() | ||
| .keep_alive(false); | ||
|
|
||
| assert!(!params.keep_alive); | ||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: one-covenant/bittensor-rs
Length of output: 109
🏁 Script executed:
Repository: one-covenant/bittensor-rs
Length of output: 1004
🏁 Script executed:
Repository: one-covenant/bittensor-rs
Length of output: 51
🏁 Script executed:
Repository: one-covenant/bittensor-rs
Length of output: 1243
🏁 Script executed:
Repository: one-covenant/bittensor-rs
Length of output: 222
Add upper-bound validation before casting
amount_taoto prevent silent overflow.The current validation checks
is_finite()and>= 0.0, but this does not prevent values from overflowing when scaled by 1,000,000,000. For example, anyamount_tao > ~18.4 billionwill exceedu64::MAXafter scaling, causing silent truncation or wraparound in theas u64cast. Add a check to reject values above the representable ceiling (approximately 18.446 TAO) and add a regression test for oversized positive inputs.🤖 Prompt for AI Agents