Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/brent/remove-f64' (#436) into main
Browse files Browse the repository at this point in the history
evil: remove f64s from ibc tests

* namada/brent/remove-f64:
  changelog: #436
  [ci] wasm checksums update
  replace floating point arithm from token module with rust_decimal
  • Loading branch information
juped committed Nov 29, 2022
2 parents 75f2e1b + 072c44f commit b202ece
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 @@ -694,7 +694,7 @@ fn transfer_token(
ALBERT,
&receiver,
NAM,
&Amount::from(100000_f64),
&Amount::whole(100000),
port_channel_id_a,
None,
None,
Expand Down Expand Up @@ -813,7 +813,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 @@ -872,7 +872,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 @@ -917,7 +917,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 @@ -966,7 +966,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.ad0fbb5688576d4e6ba81ce51cb2775c752832e714e03ef3ff341b1ed3c672eb.wasm",
"tx_ibc.wasm": "tx_ibc.d9e61a3e49ef86e02260aa7d85e71901afb81a1bb25324521a2fddeb89a26690.wasm",
"tx_bond.wasm": "tx_bond.60394584ebd1a373716e772b1d57b74c4c162d0a235851090ef10adff8f6885c.wasm",
"tx_ibc.wasm": "tx_ibc.37414564f5e8a9b6ff207b2d2ad3ba42887aaba072361683f092a0cdaeaa8dd3.wasm",
"tx_init_account.wasm": "tx_init_account.597c88a3703a935ce75cbe05041bf016e9bf0e4f3227deb0dbdf2218034289ca.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.8257da180d218c8284d93a4f7193572772f83767ab40dfa965a67bd10e4691c5.wasm",
"tx_init_validator.wasm": "tx_init_validator.088dea2cad55cf9ad3c6aee35127fa7c3df9d1a715f94d6d466ebf430b603c4f.wasm",
"tx_transfer.wasm": "tx_transfer.d6b415807c79a3e39a4bda780d043caad565a6a3c770d7ea7eebc3eb82b2be15.wasm",
"tx_unbond.wasm": "tx_unbond.f6fab49c63982e14bb811d45c3a6ca784459a474c700b772a8275ba7bab41eb6.wasm",
"tx_update_vp.wasm": "tx_update_vp.99a29ce8e54b0252a962dfe84a6e404f1f83a25991ea361baedeb18f9f59a0d8.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.674cf7f54399b5f484eed3b66aec16fb5ffdb7da3245f50132385568ff0ba7e6.wasm",
"tx_withdraw.wasm": "tx_withdraw.9b865e8fbc3b7fb02c8f25b78ee478595938bd6fc2a9cc8252bbf823158144ae.wasm",
"vp_masp.wasm": "vp_masp.ea95ae1607b33bbb00d0a6c8233685661fe1c7795f2cadc600223afbc7778ee2.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.3590dfbb3790b65d68bceaf1fcacbfc3761159b310f62b062f3e7b24e92a0398.wasm",
"tx_init_validator.wasm": "tx_init_validator.c6b781121996cbb671e81ef7d9bad1f9eb253826000a3fcab3565d94ee1ca7a9.wasm",
"tx_transfer.wasm": "tx_transfer.06d21e40d7287527b78d49ecfd7b76b8eb31ce599a33d9a32ebbe3910185f7c9.wasm",
"tx_unbond.wasm": "tx_unbond.fa037deb85ce33d2f863cc729ec2149238530446fadf2e8132914ca2e3ddba8a.wasm",
"tx_update_vp.wasm": "tx_update_vp.a8593dcb7c3c7a6cfab8dccae3a3de37fe7a394111c1be7ac82afe91a0a04e9c.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.46d964ae4bf0c26ba96fe5bb4393572c4ef2477d176ff86231df6986ed4dc7bc.wasm",
"tx_withdraw.wasm": "tx_withdraw.16af8ac9148ce20e1c8bce42c7afd3a2d9d619b6a8f48ede86ca728d0694d3cd.wasm",
"vp_masp.wasm": "vp_masp.58280e10bd139ea951b4abf77d97f399276a399d4a441d738bb58db55fdf229b.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.8a47910a17f80f22faae362874f943d4161568ad0e51c9530f98527734de2f36.wasm",
"vp_token.wasm": "vp_token.0e7afe1e9a563baebc91526596d799d65253361fcbc8d1ef16cf19fe3d3d007f.wasm",
"vp_user.wasm": "vp_user.7ef9a459b7784b888375ca9233dc7dbdb89df1f3ef81d030e11b444c6aa726e6.wasm"
Expand Down

0 comments on commit b202ece

Please sign in to comment.