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

use the unsigned fixed type for balances: U64F64 #353

Merged
merged 12 commits into from
Sep 22, 2023
2 changes: 1 addition & 1 deletion balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ impl<T: Config> Pallet<T> {

/// Returns the community-specific demurrage if it is set. Otherwise returns the
/// the demurrage defined in the genesis config
fn demurrage(cid: &CommunityIdentifier) -> BalanceType {
fn demurrage(cid: &CommunityIdentifier) -> Demurrage {
<DemurragePerBlock<T>>::try_get(cid).unwrap_or_else(|_| T::DefaultDemurrage::get())
}

Expand Down
2 changes: 1 addition & 1 deletion balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRunt
frame_support::parameter_types! {
pub const DefaultDemurrage: Demurrage = Demurrage::from_bits(0x0000000000000000000001E3F0A8A973_i128);
/// 0.000005
pub const ExistentialDeposit: BalanceType = BalanceType::from_bits(0x0000000000000000000053e2d6238da4_i128);
pub const ExistentialDeposit: BalanceType = BalanceType::from_bits(0x0000000000000000000053e2d6238da4_u128);
}

frame_support::construct_runtime!(
Expand Down
91 changes: 34 additions & 57 deletions balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{Balance as EncointerBalanceStorage, *};
use crate::mock::{Balances, DefaultDemurrage};
use approx::{assert_abs_diff_eq, assert_relative_eq};
use encointer_primitives::{
balances::to_U64F64,
communities::CommunityIdentifier,
fixed::{traits::LossyInto, transcendental::exp},
};
Expand Down Expand Up @@ -214,7 +215,7 @@ fn demurrage_should_work() {
System::set_block_number(1);
assert_eq!(
EncointerBalances::balance(cid, &alice),
exp::<BalanceType, BalanceType>(-DefaultDemurrage::get()).unwrap()
to_U64F64(exp::<Demurrage, Demurrage>(-DefaultDemurrage::get()).unwrap()).unwrap()
);
//one year later
System::set_block_number(86400 / 5 * 356);
Expand Down Expand Up @@ -309,7 +310,7 @@ fn transfer_all_works() {

let balance: f64 = EncointerBalances::balance(cid, &bob).lossy_into();
let demurrage_factor: f64 =
exp::<BalanceType, BalanceType>(Demurrage::from_num(3.0) * -DefaultDemurrage::get())
exp::<Demurrage, Demurrage>(Demurrage::from_num(3.0) * -DefaultDemurrage::get())
.unwrap()
.lossy_into();
assert_relative_eq!(balance, 50.0 * demurrage_factor, epsilon = 1.0e-9);
Expand Down Expand Up @@ -493,67 +494,43 @@ mod impl_fungibles {
let ferdie = AccountKeyring::Ferdie.to_account_id();
assert_ok!(EncointerBalances::issue(cid, &alice, BalanceType::from_num(50)));

assert!(
EncointerBalances::can_deposit(wrong_cid, &alice, 10, Provenance::Minted) ==
DepositConsequence::UnknownAsset
assert_eq!(
EncointerBalances::can_deposit(wrong_cid, &alice, 10, Provenance::Minted),
DepositConsequence::UnknownAsset
);

assert_ok!(EncointerBalances::issue(
cid,
&alice,
BalanceType::from_num(4.5 * 10f64.powf(18f64))
));
assert_ok!(EncointerBalances::issue(
cid,
&bob,
BalanceType::from_num(4.5 * 10f64.powf(18f64))
));

assert!(
EncointerBalances::can_deposit(
cid,
&ferdie,
fungible(BalanceType::from_num(4.5 * 10f64.powf(18f64))),
Provenance::Minted
) == DepositConsequence::Overflow
);

// in the very weird case where some some balances are negative we need to test for overflow of
// and account balance, because now an account can overflow but the total issuance does not.
assert_ok!(EncointerBalances::burn(
cid,
&bob,
BalanceType::from_num(4.5 * 10f64.powf(18f64))
// u64::Max is uneven, so subtract 1 to be explicit about the result.
BalanceType::from_num((u64::MAX - 1) / 2)
));

assert_ok!(EncointerBalances::issue(
cid,
&bob,
BalanceType::from_num(-4.5 * 10f64.powf(18f64))
));

assert_ok!(EncointerBalances::issue(
cid,
&alice,
BalanceType::from_num(4.5 * 10f64.powf(18f64))
// Subtract the 50 we issued to Alice above.
BalanceType::from_num((u64::MAX - 1) / 2 - 50)
));

assert!(
assert_eq!(
EncointerBalances::can_deposit(
cid,
&alice,
fungible(BalanceType::from_num(4.5 * 10f64.powf(18f64))),
Provenance::Minted
) == DepositConsequence::Overflow
&ferdie,
fungible(BalanceType::from_num(2)),
Provenance::Minted,
),
DepositConsequence::Overflow
);

assert!(
assert_eq!(
EncointerBalances::can_deposit(
cid,
&alice,
fungible(BalanceType::from_num(1)),
Provenance::Minted
) == DepositConsequence::Success
),
DepositConsequence::Success
);
})
}
Expand All @@ -568,29 +545,29 @@ mod impl_fungibles {
assert_ok!(EncointerBalances::issue(cid, &alice, BalanceType::from_num(10)));
assert_ok!(EncointerBalances::issue(cid, &bob, BalanceType::from_num(1)));

assert!(
EncointerBalances::can_withdraw(wrong_cid, &alice, 10) ==
WithdrawConsequence::UnknownAsset
assert_eq!(
EncointerBalances::can_withdraw(wrong_cid, &alice, 10),
WithdrawConsequence::UnknownAsset
);

assert!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(12))) ==
WithdrawConsequence::Underflow
assert_eq!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(12))),
WithdrawConsequence::Underflow
);

assert!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(0))) ==
WithdrawConsequence::Success
assert_eq!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(0))),
WithdrawConsequence::Success
);

assert!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(2))) ==
WithdrawConsequence::BalanceLow
assert_eq!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(2))),
WithdrawConsequence::BalanceLow
);

assert!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(1))) ==
WithdrawConsequence::Success
assert_eq!(
EncointerBalances::can_withdraw(cid, &bob, fungible(BalanceType::from_num(1))),
WithdrawConsequence::Success
);
})
}
Expand Down
14 changes: 4 additions & 10 deletions communities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
use codec::Encode;
use core::marker::PhantomData;
use encointer_primitives::{
balances::{BalanceEntry, BalanceType, Demurrage},
balances::{BalanceEntry, Demurrage},
common::PalletString,
communities::{
consts::*, validate_nominal_income, CommunityIdentifier,
CommunityMetadata as CommunityMetadataType, Degree, GeoHash, Location, LossyFrom,
NominalIncome as NominalIncomeType,
consts::*, CommunityIdentifier, CommunityMetadata as CommunityMetadataType, Degree,
GeoHash, Location, LossyFrom, NominalIncome as NominalIncomeType,
},
fixed::transcendental::{asin, cos, powi, sin, sqrt},
scheduler::CeremonyPhaseType,
Expand Down Expand Up @@ -106,9 +105,6 @@ pub mod pallet {
community_metadata
.validate()
.map_err(|_| <Error<T>>::InvalidCommunityMetadata)?;
if let Some(i) = nominal_income {
validate_nominal_income(&i).map_err(|_| <Error<T>>::InvalidNominalIncome)?;
}
Comment on lines -109 to -111
Copy link
Member Author

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.


let cid = CommunityIdentifier::new(location, bootstrappers.clone())
.map_err(|_| Error::<T>::InvalidLocation)?;
Expand Down Expand Up @@ -275,7 +271,7 @@ pub mod pallet {
pub fn update_demurrage(
origin: OriginFor<T>,
cid: CommunityIdentifier,
demurrage: BalanceType,
demurrage: Demurrage,
) -> DispatchResultWithPostInfo {
T::CommunityMaster::ensure_origin(origin)?;

Expand Down Expand Up @@ -481,8 +477,6 @@ impl<T: Config> Pallet<T> {
nominal_income: NominalIncomeType,
) -> DispatchResultWithPostInfo {
Self::ensure_cid_exists(&cid)?;
validate_nominal_income(&nominal_income).map_err(|_| <Error<T>>::InvalidNominalIncome)?;
Self::ensure_cid_exists(&cid)?;

<NominalIncome<T>>::insert(cid, nominal_income);

Expand Down
22 changes: 11 additions & 11 deletions democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn proposal_submission_works() {
let cid = create_cid();
let block = System::block_number();
let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32));

assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice()),
Expand All @@ -109,7 +109,7 @@ fn proposal_submission_fails_if_proposal_in_enactment_queue() {
new_test_ext().execute_with(|| {
let cid = create_cid();
let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32));

EnactmentQueue::<TestRuntime>::insert(proposal_action.get_identifier(), 100);

Expand Down Expand Up @@ -254,7 +254,7 @@ fn eligible_reputations_works_with_cids() {
let alice = alice();

let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32));
assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice.clone()),
proposal_action
Expand Down Expand Up @@ -384,15 +384,15 @@ fn do_update_proposal_state_fails_with_wrong_state() {
let proposal: Proposal<BlockNumber> = Proposal {
start: BlockNumber::from(1u64),
start_cindex: 1,
action: ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32)),
action: ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32)),
state: ProposalState::Cancelled,
};
Proposals::<TestRuntime>::insert(1, proposal);

let proposal2: Proposal<BlockNumber> = Proposal {
start: BlockNumber::from(1u64),
start_cindex: 1,
action: ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32)),
action: ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32)),
state: ProposalState::Approved,
};
Proposals::<TestRuntime>::insert(2, proposal2);
Expand Down Expand Up @@ -570,7 +570,7 @@ fn test_get_electorate_works() {
));

let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32));
assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice.clone()),
proposal_action
Expand Down Expand Up @@ -638,7 +638,7 @@ fn enactment_updates_proposal_metadata_and_enactment_queue() {
));

let proposal_action2 =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(100u32));
assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice.clone()),
proposal_action2
Expand Down Expand Up @@ -668,7 +668,7 @@ fn proposal_happy_flow() {
let cid2 = register_test_community::<TestRuntime>(None, 10.0, 10.0);
let alice = alice();
let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(13037i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(13037u32));
assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice.clone()),
proposal_action
Expand Down Expand Up @@ -700,7 +700,7 @@ fn proposal_happy_flow() {

assert_eq!(EncointerDemocracy::proposals(1).unwrap().state, ProposalState::Enacted);
assert_eq!(EncointerDemocracy::enactment_queue(proposal_action.get_identifier()), None);
assert_eq!(EncointerCommunities::nominal_income(cid), NominalIncomeType::from(13037i32));
assert_eq!(EncointerCommunities::nominal_income(cid), NominalIncomeType::from(13037u32));
});
}

Expand All @@ -710,7 +710,7 @@ fn enact_update_nominal_income_works() {
let cid = create_cid();
let alice = alice();
let proposal_action =
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(13037i32));
ProposalAction::UpdateNominalIncome(cid, NominalIncomeType::from(13037u32));
assert_ok!(EncointerDemocracy::submit_proposal(
RuntimeOrigin::signed(alice.clone()),
proposal_action
Expand All @@ -724,7 +724,7 @@ fn enact_update_nominal_income_works() {

assert_eq!(EncointerDemocracy::proposals(1).unwrap().state, ProposalState::Enacted);
assert_eq!(EncointerDemocracy::enactment_queue(proposal_action.get_identifier()), None);
assert_eq!(EncointerCommunities::nominal_income(cid), NominalIncomeType::from(13037i32));
assert_eq!(EncointerCommunities::nominal_income(cid), NominalIncomeType::from(13037u32));
});
}

Expand Down
Loading