Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Bound Unsigned when signed is not supported. (#6367)
Browse files Browse the repository at this point in the history
* bound unsigned when necessary

* convert more type to AtLeast32BitUnsigned

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* doc refactor

* line reorganize

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
  • Loading branch information
gui1117 and kianenigma committed Jun 25, 2020
1 parent 482c1bc commit 4be954a
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 74 deletions.
4 changes: 2 additions & 2 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{Parameter, decl_module, decl_event, decl_storage, decl_error, ensure};
use sp_runtime::traits::{Member, AtLeast32Bit, Zero, StaticLookup};
use sp_runtime::traits::{Member, AtLeast32Bit, AtLeast32BitUnsigned, Zero, StaticLookup};
use frame_system::{self as system, ensure_signed};
use sp_runtime::traits::One;

Expand All @@ -144,7 +144,7 @@ pub trait Trait: frame_system::Trait {
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

/// The units in which we record balances.
type Balance: Member + Parameter + AtLeast32Bit + Default + Copy;
type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy;

/// The arithmetic type of asset identifier.
type AssetId: Parameter + AtLeast32Bit + Default + Copy;
Expand Down
6 changes: 3 additions & 3 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ use frame_support::{
use sp_runtime::{
RuntimeDebug, DispatchResult, DispatchError,
traits::{
Zero, AtLeast32Bit, StaticLookup, Member, CheckedAdd, CheckedSub,
Zero, AtLeast32BitUnsigned, StaticLookup, Member, CheckedAdd, CheckedSub,
MaybeSerializeDeserialize, Saturating, Bounded,
},
};
Expand All @@ -180,7 +180,7 @@ pub use self::imbalances::{PositiveImbalance, NegativeImbalance};

pub trait Subtrait<I: Instance = DefaultInstance>: frame_system::Trait {
/// The balance of an account.
type Balance: Parameter + Member + AtLeast32Bit + Codec + Default + Copy +
type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy +
MaybeSerializeDeserialize + Debug;

/// The minimum amount required to keep an account open.
Expand All @@ -192,7 +192,7 @@ pub trait Subtrait<I: Instance = DefaultInstance>: frame_system::Trait {

pub trait Trait<I: Instance = DefaultInstance>: frame_system::Trait {
/// The balance of an account.
type Balance: Parameter + Member + AtLeast32Bit + Codec + Default + Copy +
type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy +
MaybeSerializeDeserialize + Debug;

/// Handler for the unbalanced reduction when removing a dust account.
Expand Down
25 changes: 8 additions & 17 deletions frame/generic-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,17 @@ use codec::{Decode, Encode, HasCompact, Input, Output, Error as CodecError};
use sp_runtime::{RuntimeDebug, DispatchResult, DispatchError};
use sp_runtime::traits::{
CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, One, Saturating, AtLeast32Bit,
Zero, Bounded,
Zero, Bounded, AtLeast32BitUnsigned
};

use sp_std::prelude::*;
use sp_std::{cmp, result, fmt::Debug};
use frame_support::{
decl_event, decl_module, decl_storage, ensure, decl_error,
traits::{
Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, ReservableCurrency,
SignedImbalance, WithdrawReason, WithdrawReasons, TryDrop, BalanceStatus,
Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency,
ReservableCurrency, SignedImbalance, WithdrawReason, WithdrawReasons, TryDrop,
BalanceStatus,
},
Parameter, StorageMap,
};
Expand All @@ -178,25 +179,15 @@ mod tests;
pub use self::imbalances::{NegativeImbalance, PositiveImbalance};

pub trait Trait: frame_system::Trait {
type Balance: Parameter
+ Member
+ AtLeast32Bit
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug;
type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy + Debug +
MaybeSerializeDeserialize;
type AssetId: Parameter + Member + AtLeast32Bit + Default + Copy;
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}

pub trait Subtrait: frame_system::Trait {
type Balance: Parameter
+ Member
+ AtLeast32Bit
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug;
type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy + Debug +
MaybeSerializeDeserialize;
type AssetId: Parameter + Member + AtLeast32Bit + Default + Copy;
}

Expand Down
4 changes: 2 additions & 2 deletions frame/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use pallet_session::historical::IdentificationTuple;
use sp_runtime::{
offchain::storage::StorageValueRef,
RuntimeDebug,
traits::{Convert, Member, Saturating, AtLeast32Bit}, Perbill,
traits::{Convert, Member, Saturating, AtLeast32BitUnsigned}, Perbill,
transaction_validity::{
TransactionValidity, ValidTransaction, InvalidTransaction, TransactionSource,
TransactionPriority,
Expand Down Expand Up @@ -160,7 +160,7 @@ struct HeartbeatStatus<BlockNumber> {
pub sent_at: BlockNumber,
}

impl<BlockNumber: PartialEq + AtLeast32Bit + Copy> HeartbeatStatus<BlockNumber> {
impl<BlockNumber: PartialEq + AtLeast32BitUnsigned + Copy> HeartbeatStatus<BlockNumber> {
/// Returns true if heartbeat has been recently sent.
///
/// Parameters:
Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! The staking rate in NPoS is the total amount of tokens staked by nominators and validators,
//! divided by the total token supply.

use sp_runtime::{Perbill, traits::AtLeast32Bit, curve::PiecewiseLinear};
use sp_runtime::{Perbill, traits::AtLeast32BitUnsigned, curve::PiecewiseLinear};

/// The total payout to all validators (and their nominators) per era and maximum payout.
///
Expand All @@ -34,7 +34,7 @@ pub fn compute_total_payout<N>(
npos_token_staked: N,
total_tokens: N,
era_duration: u64
) -> (N, N) where N: AtLeast32Bit + Clone {
) -> (N, N) where N: AtLeast32BitUnsigned + Clone {
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;

Expand Down
8 changes: 4 additions & 4 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ use sp_runtime::{
Percent, Perbill, PerU16, PerThing, RuntimeDebug, DispatchError,
curve::PiecewiseLinear,
traits::{
Convert, Zero, StaticLookup, CheckedSub, Saturating, SaturatedConversion, AtLeast32Bit,
Dispatchable,
Convert, Zero, StaticLookup, CheckedSub, Saturating, SaturatedConversion,
AtLeast32BitUnsigned, Dispatchable,
},
transaction_validity::{
TransactionValidityError, TransactionValidity, ValidTransaction, InvalidTransaction,
Expand Down Expand Up @@ -493,7 +493,7 @@ pub struct StakingLedger<AccountId, Balance: HasCompact> {

impl<
AccountId,
Balance: HasCompact + Copy + Saturating + AtLeast32Bit,
Balance: HasCompact + Copy + Saturating + AtLeast32BitUnsigned,
> StakingLedger<AccountId, Balance> {
/// Remove entries from `unlocking` that are sufficiently old and reduce the
/// total by the sum of their balances.
Expand Down Expand Up @@ -544,7 +544,7 @@ impl<
}

impl<AccountId, Balance> StakingLedger<AccountId, Balance> where
Balance: AtLeast32Bit + Saturating + Copy,
Balance: AtLeast32BitUnsigned + Saturating + Copy,
{
/// Slash the validator for a given amount of balance. This can grow the value
/// of the slash in the case that the validator has less than `minimum_balance`
Expand Down
7 changes: 4 additions & 3 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_core::u32_trait::Value as U32;
use sp_runtime::{
RuntimeDebug, ConsensusEngineId, DispatchResult, DispatchError, traits::{
MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero,
BadOrigin
BadOrigin, AtLeast32BitUnsigned
},
};
use crate::dispatch::Parameter;
Expand Down Expand Up @@ -788,7 +788,7 @@ pub enum SignedImbalance<B, P: Imbalance<B>>{
impl<
P: Imbalance<B, Opposite=N>,
N: Imbalance<B, Opposite=P>,
B: AtLeast32Bit + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
B: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
> SignedImbalance<B, P> {
pub fn zero() -> Self {
SignedImbalance::Positive(P::zero())
Expand Down Expand Up @@ -851,7 +851,8 @@ impl<
/// Abstraction over a fungible assets system.
pub trait Currency<AccountId> {
/// The balance of an account.
type Balance: AtLeast32Bit + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;
type Balance: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug +
Default;

/// The opaque token type for an imbalance. This is returned by unbalanced operations
/// and must be dealt with. It may be dropped but cannot be cloned.
Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ use sp_runtime::{
};
use crate::dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, DispatchError};
use sp_runtime::traits::SaturatedConversion;
use sp_arithmetic::{Perbill, traits::{BaseArithmetic, Saturating}};
use sp_arithmetic::{Perbill, traits::{BaseArithmetic, Saturating, Unsigned}};
use smallvec::{smallvec, SmallVec};

/// Re-export priority as type
Expand Down Expand Up @@ -571,7 +571,7 @@ pub type WeightToFeeCoefficients<T> = SmallVec<[WeightToFeeCoefficient<T>; 4]>;
/// An implementor should only implement the `polynomial` function.
pub trait WeightToFeePolynomial {
/// The type that is returned as result from polynomial evaluation.
type Balance: BaseArithmetic + From<u32> + Copy;
type Balance: BaseArithmetic + From<u32> + Copy + Unsigned;

/// Returns a polynomial that describes the weight to fee conversion.
///
Expand Down Expand Up @@ -611,7 +611,7 @@ pub trait WeightToFeePolynomial {
pub struct IdentityFee<T>(sp_std::marker::PhantomData<T>);

impl<T> WeightToFeePolynomial for IdentityFee<T> where
T: BaseArithmetic + From<u32> + Copy
T: BaseArithmetic + From<u32> + Copy + Unsigned
{
type Balance = T;

Expand Down
7 changes: 4 additions & 3 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ use sp_runtime::{
self, CheckEqual, AtLeast32Bit, Zero, Lookup, LookupError,
SimpleBitOps, Hash, Member, MaybeDisplay, BadOrigin,
MaybeSerialize, MaybeSerializeDeserialize, MaybeMallocSizeOf, StaticLookup, One, Bounded,
Dispatchable,
Dispatchable, AtLeast32BitUnsigned
},
offchain::storage_lock::BlockNumberProvider,
};
Expand Down Expand Up @@ -181,8 +181,9 @@ pub trait Trait: 'static + Eq + Clone {

/// The block number type used by the runtime.
type BlockNumber:
Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + AtLeast32Bit
+ Default + Bounded + Copy + sp_std::hash::Hash + sp_std::str::FromStr + MaybeMallocSizeOf;
Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay +
AtLeast32BitUnsigned + Default + Bounded + Copy + sp_std::hash::Hash +
sp_std::str::FromStr + MaybeMallocSizeOf;

/// The output of the `Hashing` function.
type Hash:
Expand Down
6 changes: 3 additions & 3 deletions frame/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use sp_std::prelude::*;
use sp_std::fmt::Debug;
use codec::{Encode, Decode};
use sp_runtime::{DispatchResult, RuntimeDebug, traits::{
StaticLookup, Zero, AtLeast32Bit, MaybeSerializeDeserialize, Convert
StaticLookup, Zero, AtLeast32BitUnsigned, MaybeSerializeDeserialize, Convert
}};
use frame_support::{decl_module, decl_event, decl_storage, decl_error, ensure};
use frame_support::traits::{
Expand Down Expand Up @@ -92,8 +92,8 @@ pub struct VestingInfo<Balance, BlockNumber> {
}

impl<
Balance: AtLeast32Bit + Copy,
BlockNumber: AtLeast32Bit + Copy,
Balance: AtLeast32BitUnsigned + Copy,
BlockNumber: AtLeast32BitUnsigned + Copy,
> VestingInfo<Balance, BlockNumber> {
/// Amount locked at block `n`.
pub fn locked_at<
Expand Down
Loading

0 comments on commit 4be954a

Please sign in to comment.