-
Notifications
You must be signed in to change notification settings - Fork 3
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
use the unsigned fixed type for balances: U64F64
#353
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5ffa812
use unsigned fixed type for balances
clangenb 91d16f8
add I64F64_to_U64F64 and fix test compilation
clangenb 8ba5f24
[pallet-balances] more meaningful errors
clangenb b3d7183
fix clippy
clangenb 52764ee
fix comment
clangenb e90ec7c
fix comment
clangenb 64d0d17
Merge branch 'cl/unsigned-balance-type' of github.com:encointer/palle…
clangenb dfbec6b
[primitives/balances] better way to transform the I64F64 to U64F64
clangenb f8bf641
[primitives/balances] rename `I64F64_to_u64F64` to `to_U64F64`
clangenb f3c4ab3
[balances] fix `can_deposit_works` test and make it more understandable
clangenb f289d6e
[primitives/balances] better comment about type conversion
clangenb 9f15ad7
[primitives/balances] fix typo
clangenb 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 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 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 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 |
---|---|---|
|
@@ -38,7 +38,7 @@ const LOG: &str = "encointer::demurrage"; | |
// We're working with fixpoint here. | ||
|
||
/// Encointer balances are fixpoint values | ||
pub type BalanceType = I64F64; | ||
pub type BalanceType = U64F64; | ||
|
||
/// Demurrage is the rate of evanescence of balances per block | ||
/// it must be positive | ||
|
@@ -137,11 +137,14 @@ pub fn demurrage_factor(demurrage_per_block: Demurrage, elapsed_blocks: u32) -> | |
// demurrage >= 0; hence exponent <= 0 | ||
let exponent = match (-demurrage_per_block.abs()).checked_mul(elapsed_blocks.into()) { | ||
Some(exp) => exp, | ||
None => return 0.into(), | ||
None => return 0u64.into(), | ||
}; | ||
|
||
// exponent <= 0; hence return value [0, 1) | ||
exp(exponent).unwrap_or_else(|_| 0.into()) | ||
let f: I64F64 = exp(exponent).unwrap_or_else(|_| 0.into()); | ||
|
||
// Safe conversion. The result of an exponential function can't be negative. | ||
BalanceType::from_le_bytes(f.to_le_bytes()) | ||
} | ||
|
||
/// Our BalanceType is I64F64, so the smallest possible number is | ||
|
@@ -163,8 +166,8 @@ impl Convert<BalanceType, u128> for EncointerBalanceConverter { | |
|
||
result += (bits >> 64) as u128 * ONE_ENCOINTER_BALANCE_UNIT; | ||
|
||
result += BalanceType::from_bits((bits as u64) as i128) // <- to truncate | ||
.saturating_mul_int(ONE_ENCOINTER_BALANCE_UNIT as i128) | ||
result += BalanceType::from_bits((bits as u64) as u128) // <- to truncate | ||
.saturating_mul_int(ONE_ENCOINTER_BALANCE_UNIT) | ||
.to_num::<u128>(); | ||
result | ||
} | ||
|
@@ -215,26 +218,26 @@ mod tests { | |
|
||
#[test] | ||
fn demurrage_works() { | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
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. Implicit type is i32, which can obviously not be |
||
assert_abs_diff_eq(bal.apply_demurrage(DEFAULT_DEMURRAGE, ONE_YEAR).principal, 0.5); | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_when_principal_is_zero_works() { | ||
let bal = BalanceEntry::<u32>::new(0.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(0u32.into(), 0); | ||
assert_abs_diff_eq(bal.apply_demurrage(DEFAULT_DEMURRAGE, ONE_YEAR).principal, 0f64); | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_when_demurrage_is_negative_works() { | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
assert_abs_diff_eq(bal.apply_demurrage(-DEFAULT_DEMURRAGE, ONE_YEAR).principal, 0.5); | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_with_overflowing_values_works() { | ||
let demurrage = Demurrage::from_num(0.000048135220872218395); | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
|
||
// This produced a overflow before: https://github.com/encointer/encointer-node/issues/290 | ||
assert_abs_diff_eq(bal.apply_demurrage(demurrage, ONE_YEAR).principal, 0f64); | ||
|
@@ -249,31 +252,31 @@ mod tests { | |
#[test] | ||
fn apply_demurrage_with_block_number_bigger_than_u32max_does_not_overflow() { | ||
let demurrage = Demurrage::from_num(DEFAULT_DEMURRAGE); | ||
let bal = BalanceEntry::<u64>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u64>::new(1u32.into(), 0); | ||
|
||
assert_abs_diff_eq(bal.apply_demurrage(demurrage, u32::MAX as u64 + 1).principal, 0f64); | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_with_block_number_not_monotonically_rising_just_updates_last_block() { | ||
let demurrage = Demurrage::from_num(DEFAULT_DEMURRAGE); | ||
let bal = BalanceEntry::<u32>::new(1.into(), 1); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 1); | ||
|
||
assert_eq!(bal.apply_demurrage(demurrage, 0), BalanceEntry::<u32>::new(1.into(), 0)) | ||
assert_eq!(bal.apply_demurrage(demurrage, 0), BalanceEntry::<u32>::new(1u32.into(), 0)) | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_with_zero_demurrage_works() { | ||
let demurrage = Demurrage::from_num(0.0); | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
|
||
assert_abs_diff_eq(bal.apply_demurrage(demurrage, ONE_YEAR).principal, 1f64); | ||
} | ||
|
||
#[test] | ||
fn apply_demurrage_with_zero_elapsed_blocks_works() { | ||
let demurrage = Demurrage::from_num(DEFAULT_DEMURRAGE); | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
|
||
assert_abs_diff_eq(bal.apply_demurrage(demurrage, 0).principal, 1f64); | ||
} | ||
|
@@ -285,7 +288,7 @@ mod tests { | |
// | ||
// Not critical as we safeguard against this with `validate_demurrage`. | ||
let demurrage = Demurrage::from_num(i64::MAX - 1); | ||
let bal = BalanceEntry::<u32>::new(1.into(), 0); | ||
let bal = BalanceEntry::<u32>::new(1u32.into(), 0); | ||
|
||
assert_abs_diff_eq(bal.apply_demurrage(demurrage, 1).principal, 0f64); | ||
} | ||
|
This file contains 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 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
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.
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.
Validate only validated if the value is negative; type safety gives us this guarantee now already.