-
Notifications
You must be signed in to change notification settings - Fork 47
fix: denomination arithmetic #794
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9988d89
test: denomination arithmetic
rflechtner 36f4d07
fix: avoid dropping decimals in fixed conversion
rflechtner 9807ea3
fix: adress overflow issues on refund_account
rflechtner 4b8d224
refactor: convert_to_fixed
rflechtner 694334c
fix: mitigate overflow on denominations exceeding the capacity of fixed
rflechtner e94104e
feat: constant to limit maximum allowed denomination
rflechtner 08b71c4
refactor: use U256 for infallible scaling (#796)
rflechtner bf42e60
chore: document safe denomination limit
rflechtner 3a2306b
feat: add integrity test hook
rflechtner a86d1c0
test: additional integrity test to check for denomination that overfl…
rflechtner 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
135 changes: 135 additions & 0 deletions
135
pallets/pallet-bonded-coins/src/tests/curves/arithmetic.rs
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| use crate::{ | ||
| curves::convert_to_fixed, | ||
| mock::{runtime::Test, Float}, | ||
| }; | ||
| use frame_support::assert_ok; | ||
| use sp_runtime::ArithmeticError; | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_basic() { | ||
| let x = 1000u128; | ||
| let denomination = 2u8; // 10^2 = 100 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| // Test runtime uses I75F53 for CurveParameterTypeOf, which is what we'll cover | ||
| // in testing. | ||
| let expected = Float::from_num(10); // 1000 / 100 = 10 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_with_remainder() { | ||
| let x = 1050u128; | ||
| let denomination = 2u8; // 10^2 = 100 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(10.5); // 1050 / 100 = 10.5 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_smaller_than_denomination() { | ||
| let x = 1050u128; | ||
| let denomination = 6u8; // 10^6 = 1000000 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(0.00105); // 1050 / 1000000 = 0.00105 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_large_value() { | ||
| let x = 1_000_000_000_000_000u128; | ||
| let denomination = 12u8; // 10^12 = 1_000_000_000_000 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(1000); // 1_000_000_000_000_000 / 1_000_000_000_000 = 1000 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_small_denomination() { | ||
| let x = 12345u128; | ||
| let denomination = 1u8; // 10^1 = 10 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(1234.5); // 12345 / 10 = 1234.5 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_overflow() { | ||
| let x = u128::MAX; | ||
| let denomination = 0u8; // 10^0 = 1, no scaling | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination); | ||
| assert!(result.is_err()); | ||
| assert_eq!(result.unwrap_err(), ArithmeticError::Overflow); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_denomination_overflow() { | ||
| let x = 1000u128; | ||
| let denomination = 128u8; // 10^128 overflows | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination); | ||
| assert!(result.is_err()); | ||
| assert_eq!(result.unwrap_err(), ArithmeticError::Overflow); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_overflow_avoided() { | ||
| let x = u128::MAX; // around 3.4e+38 | ||
| let denomination = 17u8; // I75F53 should handle around 1.8e+22, 38 - 23 -> 17 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination); | ||
| assert_ok!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_handles_large_denomination() { | ||
| let x = u128::MAX; // around 3.4e+38 | ||
| let denomination = 22u8; // I75F53 should handle around 1.8e+22; this is the maximum safe denomination | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination); | ||
| assert_ok!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_very_large_denomination() { | ||
| let denomination = 30u8; // I75F53 should handle around 1.8e+22, this can lead to overflow | ||
|
|
||
| // multiple of denomination would not result in remainder = 0 | ||
| assert_ok!(convert_to_fixed::<Test>(10u128.pow(31), denomination)); | ||
|
|
||
| // non-multiples of denomination could lead to overflow of remainder | ||
| assert_ok!(convert_to_fixed::<Test>(11u128.pow(31), denomination)); | ||
| assert_ok!(convert_to_fixed::<Test>(10u128.pow(29), denomination)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_zero_denomination() { | ||
| let x = 1000u128; | ||
| let denomination = 0u8; // 10^0 = 1 | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(1000); // 1000 / 1 = 1000 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_convert_to_fixed_zero_input() { | ||
| let x = 0u128; | ||
| let denomination = 10u8; // 10^10 = large divisor | ||
|
|
||
| let result = convert_to_fixed::<Test>(x, denomination).unwrap(); | ||
| let expected = Float::from_num(0); // 0 / any number = 0 | ||
|
|
||
| assert_eq!(result, expected); | ||
| } |
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| mod arithmetic; | ||
| mod lmsr; | ||
| mod polynomial; | ||
| mod square_root; |
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.
Uh oh!
There was an error while loading. Please reload this page.