Skip to content

Commit

Permalink
Merge branch 'brent/remove-f64' (#436)
Browse files Browse the repository at this point in the history
* brent/remove-f64:
  changelog: #436
  [ci] wasm checksums update
  replace floating point arithm from token module with rust_decimal
  • Loading branch information
tzemanovic committed Nov 16, 2022
2 parents 0724c22 + 072c44f commit a9e9042
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
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 @@ -7,6 +7,7 @@ use std::str::FromStr;

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

Expand Down Expand Up @@ -38,7 +39,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 @@ -110,21 +110,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 @@ -227,7 +222,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 @@ -470,11 +465,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
10 changes: 5 additions & 5 deletions tests/src/e2e/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ fn transfer_token(
ALBERT,
&receiver,
NAM,
&Amount::from(100000_f64),
&Amount::whole(100000),
port_channel_id_a,
None,
None,
Expand Down Expand Up @@ -814,7 +814,7 @@ fn transfer_back(
BERTHA,
&receiver,
NAM,
&Amount::from(50000_f64),
&Amount::whole(50000),
port_channel_id_b,
Some(sub_prefix),
None,
Expand Down Expand Up @@ -873,7 +873,7 @@ fn transfer_timeout(
ALBERT,
&receiver,
NAM,
&Amount::from(100000_f64),
&Amount::whole(100000),
port_channel_id_a,
None,
Some(Duration::new(5, 0)),
Expand Down Expand Up @@ -918,7 +918,7 @@ fn transfer_timeout_on_close(
BERTHA,
&receiver,
NAM,
&Amount::from(100000_f64),
&Amount::whole(100000),
port_channel_id_b,
None,
None,
Expand Down Expand Up @@ -967,7 +967,7 @@ fn try_transfer_on_close(
ALBERT,
&receiver,
NAM,
&Amount::from(100000_f64),
&Amount::whole(100000),
port_channel_id_a,
None,
None,
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
20 changes: 10 additions & 10 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"tx_bond.wasm": "tx_bond.06959b7b98f314dc338dfd064f455bba4a1073feb376548953202cb9914b02df.wasm",
"tx_ibc.wasm": "tx_ibc.df535cd260ff98e492a14406be62a9d56d42c42d165e8e052118acf41af2f6c0.wasm",
"tx_bond.wasm": "tx_bond.7bdd7bcc4cc1191546d3eaa01820d6f5cf4eb71735eafff1c4421f0d2e1f723d.wasm",
"tx_ibc.wasm": "tx_ibc.79779eb0a993632c0be997b72f500de68fa42f7c25d09f949999fc8013a808ca.wasm",
"tx_init_account.wasm": "tx_init_account.8a7fcbea9d6f74eb7b59436a24b6d2846e7294029b209add9b57a7d71aafd929.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.0fd3d58630358fd644c6467a3a4b1f78bb392bfadfe72d5564cb02fe467b22c7.wasm",
"tx_init_validator.wasm": "tx_init_validator.d4edc590cd2613b989003fe64bd96cb178d6ad1ee5caa251130334c6ccaa4996.wasm",
"tx_transfer.wasm": "tx_transfer.8c22c05837dfed33f291377c94ee6feac96200355013684d419b3f18f41cde44.wasm",
"tx_unbond.wasm": "tx_unbond.250076716cd7e88991c4bcfba759ea66134a005e06026f6452196f22fa6551c0.wasm",
"tx_update_vp.wasm": "tx_update_vp.99a29ce8e54b0252a962dfe84a6e404f1f83a25991ea361baedeb18f9f59a0d8.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.baa03c2847e53122675d38384634baf811c564b1ec6e049103724a2913245334.wasm",
"tx_withdraw.wasm": "tx_withdraw.505c8f7181483e7f76d1c253c23958dfbb32b0c58b8d3676cb476ab794a87eee.wasm",
"vp_masp.wasm": "vp_masp.ae5ef6cd9718e23323a264e28b2da74ec52fa56c812b36fa48bb650eacb527a9.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.1449056604bac3a63fc65076d6c6fc5305edcfeb3fdc4fc14f9b2b60918f882b.wasm",
"tx_init_validator.wasm": "tx_init_validator.f015f967260d78cfe04f6c81b8981be34c941bcaa912aa0e2a92979df1493414.wasm",
"tx_transfer.wasm": "tx_transfer.ca4c05c17cbab7cb856d7b56d220dd1189b3b1c24721392ec7a2ebcbfa3b2147.wasm",
"tx_unbond.wasm": "tx_unbond.e911e9e4072094fc90edf1edbd86c2a53deeed1c0b870696d7b643fa11829bc4.wasm",
"tx_update_vp.wasm": "tx_update_vp.a8593dcb7c3c7a6cfab8dccae3a3de37fe7a394111c1be7ac82afe91a0a04e9c.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.3e90ca4d15da7ee35ba9a3a99cd856ef2b2696e141dcc2ef9a03eba41c4a45f6.wasm",
"tx_withdraw.wasm": "tx_withdraw.358d9d445b8d975b13d1b37ed87dd316045928bdc5013c964a50f606b81ac437.wasm",
"vp_masp.wasm": "vp_masp.b4eaaa09b374d098a2953fea201310e79758205f2ded90573dd29ec0a4d7fdda.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.bf7474247822663a744f84aa5f248c014ce49ecf93f213c0e4d498a716145399.wasm",
"vp_token.wasm": "vp_token.eef6be5850d6b2ec91abd0e1a2e06976a4e3f7b7ec8f8bb5cff786f58957cf19.wasm",
"vp_user.wasm": "vp_user.aa6eb1e0bd92bd4e5dc5ae1bd4b0e48494a64a6b071677c84a5d0fb7a97ee341.wasm"
Expand Down

0 comments on commit a9e9042

Please sign in to comment.