Skip to content
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

Replace f64 with rust_decimal in token conversions #436

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/436-remove-f64.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactored token decimal formatting.
([#436](https://github.com/anoma/namada/pull/436))
5 changes: 4 additions & 1 deletion proof_of_stake/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ pub enum ValidationError {
UnbondingLenTooShort(u64, u64),
}

/// The number of fundamental units per whole token of the native staking token
pub const TOKENS_PER_NAM: u64 = 1_000_000;

/// From Tendermint: <https://github.com/tendermint/tendermint/blob/master/spec/abci/apps.md#updating-the-validator-set>
const MAX_TOTAL_VOTING_POWER: i64 = i64::MAX / 8;

/// Assuming token amount is `u64` in micro units.
const TOKEN_MAX_AMOUNT: u64 = u64::MAX / 1_000_000;
const TOKEN_MAX_AMOUNT: u64 = u64::MAX / TOKENS_PER_NAM;

impl PosParams {
/// Validate PoS parameters values. Returns an empty list if the values are
Expand Down
29 changes: 12 additions & 17 deletions shared/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
use std::str::FromStr;

use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use rust_decimal::prelude::{Decimal, ToPrimitive};
use serde::{Deserialize, Serialize};
use thiserror::Error;

Expand Down Expand Up @@ -37,7 +38,6 @@ pub struct Amount {
pub const MAX_DECIMAL_PLACES: u32 = 6;
/// Decimal scale of token [`Amount`] and [`Change`].
pub const SCALE: u64 = 1_000_000;
const SCALE_F64: f64 = SCALE as f64;

/// A change in tokens amount
pub type Change = i128;
Expand Down Expand Up @@ -109,21 +109,16 @@ impl<'de> serde::Deserialize<'de> for Amount {
}
}

impl From<Amount> for f64 {
/// Warning: `f64` loses precision and it should not be used when exact
/// values are required.
impl From<Amount> for Decimal {
fn from(amount: Amount) -> Self {
amount.micro as f64 / SCALE_F64
Into::<Decimal>::into(amount.micro) / Into::<Decimal>::into(SCALE)
}
}

impl From<f64> for Amount {
/// Warning: `f64` loses precision and it should not be used when exact
/// values are required.
fn from(micro: f64) -> Self {
Self {
micro: (micro * SCALE_F64).round() as u64,
}
impl From<Decimal> for Amount {
fn from(micro: Decimal) -> Self {
let res = (micro * Into::<Decimal>::into(SCALE)).to_u64().unwrap();
Self { micro: res }
}
}

Expand Down Expand Up @@ -205,7 +200,7 @@ impl FromStr for Amount {
match rust_decimal::Decimal::from_str(s) {
Ok(decimal) => {
let scale = decimal.scale();
if scale > 6 {
if scale > MAX_DECIMAL_PLACES {
return Err(AmountParseError::ScaleTooLarge(scale));
}
let whole =
Expand Down Expand Up @@ -440,11 +435,11 @@ mod tests {
/// The upper limit is set to `2^51`, because then the float is
/// starting to lose precision.
#[test]
fn test_token_amount_f64_conversion(raw_amount in 0..2_u64.pow(51)) {
fn test_token_amount_decimal_conversion(raw_amount in 0..2_u64.pow(51)) {
let amount = Amount::from(raw_amount);
// A round-trip conversion to and from f64 should be an identity
let float = f64::from(amount);
let identity = Amount::from(float);
// A round-trip conversion to and from Decimal should be an identity
let decimal = Decimal::from(amount);
let identity = Amount::from(decimal);
assert_eq!(amount, identity);
}
}
Expand Down
18 changes: 11 additions & 7 deletions tests/src/native_vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ pub mod testing {

use crate::tx::{self, tx_host_env};

const TOKENS_PER_NAM: i128 =
namada::ledger::pos::namada_proof_of_stake::parameters::TOKENS_PER_NAM
as i128;

#[derive(Clone, Debug, Default)]
pub struct TestValidator {
pub address: Option<Address>,
Expand Down Expand Up @@ -940,9 +944,9 @@ pub mod testing {
// We convert the tokens from micro units to whole tokens
// with division by 10^6
let vp_before =
params.votes_per_token * ((total_delta) / 1_000_000);
params.votes_per_token * (total_delta / TOKENS_PER_NAM);
let vp_after = params.votes_per_token
* ((total_delta + token_delta) / 1_000_000);
* ((total_delta + token_delta) / TOKENS_PER_NAM);
// voting power delta
let vp_delta = vp_after - vp_before;

Expand Down Expand Up @@ -1001,12 +1005,12 @@ pub mod testing {
let total_delta = validator_total_deltas
.get(epoch)
.unwrap_or_default();
// We convert the tokens from micro units to whole
// We convert the tokens from micro units to whole
// tokens with division by 10^6
let vp_before = params.votes_per_token
* ((total_delta) / 1_000_000);
* (total_delta / TOKENS_PER_NAM);
let vp_after = params.votes_per_token
* ((total_delta + token_delta) / 1_000_000);
* ((total_delta + token_delta) / TOKENS_PER_NAM);
// voting power delta
let vp_delta_at_unbonding =
vp_after - vp_before - vp_delta - total_vp_delta;
Expand Down Expand Up @@ -1080,9 +1084,9 @@ pub mod testing {
// We convert the tokens from micro units to whole tokens
// with division by 10^6
let vp_before = params.votes_per_token
* ((total_delta_cur) / 1_000_000);
* (total_delta_cur / TOKENS_PER_NAM);
let vp_after = params.votes_per_token
* ((total_delta_cur + token_delta) / 1_000_000);
* ((total_delta_cur + token_delta) / TOKENS_PER_NAM);
// voting power delta
let vp_delta = vp_after - vp_before;

Expand Down
31 changes: 17 additions & 14 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"tx_bond.wasm": "tx_bond.04d6847800dad11990b42e8f2981a4a79d06d6d0c981c3d70c929e5b6a4f348b.wasm",
"tx_ibc.wasm": "tx_ibc.6ab530398ed8e276a8af7f231edbfae984b7e84eeb854714ba9339c5bed9d330.wasm",
"tx_init_account.wasm": "tx_init_account.578d987351e6ae42baa7849ae167e3ba33f3a62dba51cd47b0fa6d3ea6e4f128.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.71e27610210622fa53c3de58351761cca839681a4f450d4eff6b46bde3ae85a5.wasm",
"tx_init_validator.wasm": "tx_init_validator.269f065ff683782db2fdcac6e2485e80cbebb98929671a42eeb01703e0bbd8f5.wasm",
"tx_transfer.wasm": "tx_transfer.784325cf7763faf8d75797960cda6fbabbd343f3c6f7e6785f60f5e0911a6bb5.wasm",
"tx_unbond.wasm": "tx_unbond.ed13fa636d138ac4e35f2b4f31a6b4d3bed67e6b998dc6325f90711a2aca3704.wasm",
"tx_update_vp.wasm": "tx_update_vp.c4050e597116203eba5afde946a014afb067bdeaaae417377214a80c38a3786b.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.ece325881aad1c8a29f715c2f435c3335e08e51eed837c00ce0f7bbaddbefe50.wasm",
"tx_withdraw.wasm": "tx_withdraw.408fc10b3744c398258124e5e48e3449f6baf82a263df26911586a3382fbceb9.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.ae9a681dc2c1bd244b0575474fa4a364af56fa75833950693ca52ab25018c97d.wasm",
"vp_token.wasm": "vp_token.468de153dc5ce3af208bd762de3e85be48bc631012ec5f0947af95168da6cb93.wasm",
"vp_user.wasm": "vp_user.c101016a85a72f40da7f33e5d9061cfd2e3274eaac75a71c59c9ab4ed9896ffd.wasm"
}
"tx_bond.wasm": "tx_bond.059b1256240d15a64cf419f3a2d24af1496211c386d85acc3733095bc2e5da9b.wasm",
"tx_ibc.wasm": "tx_ibc.4eeb30bc1e6a32b8efe8958eab568082a238db01eb98340f28a9fa41371a3753.wasm",
"tx_init_account.wasm": "tx_init_account.85d017ac76e51f359fa07e753c0e6fcbd3341e7661492cbf2801cf3c41480dd4.wasm",
"tx_init_nft.wasm": "tx_init_nft.fbeb1687a364b2c249c9fd69588ff0985bd9c1f8f4c93e5328de1e9ba527d991.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.43b52414649bb0fec86dfb35d001656c1825bc5906e450e8b0c3a60aaa5f3d45.wasm",
"tx_init_validator.wasm": "tx_init_validator.6ccb7fcf246cb7a2f97a5dfdcefe16ee1add72a832081c2572adc2d7a355cf56.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.f2ed21521c676f04be4c6278cc60bec83265c3750437c87d9ea681b830767d71.wasm",
"tx_transfer.wasm": "tx_transfer.b6fc342f4a76918874e6d037a3864e4369dbba7cd7d558622e7a723e3d854da3.wasm",
"tx_unbond.wasm": "tx_unbond.6e7316d08bf8ab9a6fb1889f64a5a2265ee0399661dbb48e33555170545d1c7c.wasm",
"tx_update_vp.wasm": "tx_update_vp.6d291dadb43545a809ba33fe26582b7984c67c65f05e363a93dbc62e06a33484.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.d0a87d58f64f46586ae3d83852deee269e22520f4780875c05aaf1731044c356.wasm",
"tx_withdraw.wasm": "tx_withdraw.e5dcc5ef2362018c1fa5ea02912528ee929aa7b6fefcf06f4ccf7509bfa52283.wasm",
"vp_nft.wasm": "vp_nft.9be5a821bc7b3075b917e8ead45893502d82cc7417e6af50dfd3f6baf36243e0.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.8e2e45ff165d40dc8249188aca108e5cba86ac5dd1cd989b0237cadd4b66bfdf.wasm",
"vp_token.wasm": "vp_token.4a0446f20e7436de1e889c640a11644d1a1295c4d29e45b24582df2b9ed3176e.wasm",
"vp_user.wasm": "vp_user.eb1d6f1f524c28571ad0f21f75371aa635257313cea2702b9a70e5022fe6c3ef.wasm"
}