diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 3c944740b7..6269e03360 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -7561,6 +7561,7 @@ dependencies = [ "parity-scale-codec", "rand 0.7.3", "serde", + "sp-arithmetic", "sp-phragmen", "sp-phragmen-compact", "sp-runtime", diff --git a/substrate/primitives/core/src/testing.rs b/substrate/primitives/core/src/testing.rs index b5e6f4c7af..9e83090bcf 100644 --- a/substrate/primitives/core/src/testing.rs +++ b/substrate/primitives/core/src/testing.rs @@ -16,10 +16,10 @@ //! Types that should only be used for testing! -use crate::crypto::{KeyTypeId, CryptoTypePublicPair}; +use crate::crypto::KeyTypeId; #[cfg(feature = "std")] use crate::{ - crypto::{Pair, Public}, + crypto::{Pair, Public, CryptoTypePublicPair}, ed25519, sr25519, traits::BareCryptoStoreError }; diff --git a/substrate/primitives/phragmen/Cargo.toml b/substrate/primitives/phragmen/Cargo.toml index ff3b331406..29fc0ed41e 100644 --- a/substrate/primitives/phragmen/Cargo.toml +++ b/substrate/primitives/phragmen/Cargo.toml @@ -15,13 +15,14 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] } sp-std = { version = "2.0.0-dev", default-features = false, path = "../std" } -sp-runtime = { version = "2.0.0-dev", default-features = false, path = "../../primitives/runtime" } sp-phragmen-compact = { version = "2.0.0-dev", path = "./compact" } +sp-arithmetic = { version = "2.0.0-dev", default-features = false, path = "../arithmetic" } [dev-dependencies] substrate-test-utils = { version = "2.0.0-dev", path = "../../test-utils" } rand = "0.7.3" sp-phragmen = { version = "2.0.0-dev", path = "." } +sp-runtime = { version = "2.0.0-dev", path = "../../primitives/runtime" } [features] default = ["std"] @@ -30,5 +31,5 @@ std = [ "codec/std", "serde", "sp-std/std", - "sp-runtime/std", + "sp-arithmetic/std", ] diff --git a/substrate/primitives/phragmen/compact/src/assignment.rs b/substrate/primitives/phragmen/compact/src/assignment.rs index 587e482ccb..4630a494fc 100644 --- a/substrate/primitives/phragmen/compact/src/assignment.rs +++ b/substrate/primitives/phragmen/compact/src/assignment.rs @@ -91,7 +91,7 @@ fn into_impl(count: usize) -> TokenStream2 { } // defensive only. Since Percent doesn't have `Sub`. - let p2 = _phragmen::sp_runtime::traits::Saturating::saturating_sub( + let p2 = _phragmen::sp_arithmetic::traits::Saturating::saturating_sub( Accuracy::one(), p1, ); @@ -115,7 +115,7 @@ fn into_impl(count: usize) -> TokenStream2 { let mut inners_parsed = inners .iter() .map(|(ref t_idx, p)| { - sum = _phragmen::sp_runtime::traits::Saturating::saturating_add(sum, *p); + sum = _phragmen::sp_arithmetic::traits::Saturating::saturating_add(sum, *p); let target = target_at(*t_idx).ok_or(_phragmen::Error::CompactInvalidIndex)?; Ok((target, *p)) }) @@ -126,7 +126,7 @@ fn into_impl(count: usize) -> TokenStream2 { } // defensive only. Since Percent doesn't have `Sub`. - let p_last = _phragmen::sp_runtime::traits::Saturating::saturating_sub( + let p_last = _phragmen::sp_arithmetic::traits::Saturating::saturating_sub( Accuracy::one(), sum, ); @@ -163,7 +163,7 @@ pub(crate) fn assignment( #voter_type: _phragmen::codec::Codec + Default + Copy, #target_type: _phragmen::codec::Codec + Default + Copy, Accuracy: - _phragmen::codec::Codec + Default + Clone + _phragmen::sp_runtime::PerThing + + _phragmen::codec::Codec + Default + Clone + _phragmen::sp_arithmetic::PerThing + PartialOrd, > #ident<#voter_type, #target_type, Accuracy> diff --git a/substrate/primitives/phragmen/compact/src/lib.rs b/substrate/primitives/phragmen/compact/src/lib.rs index 114aeaeb32..9406f944c3 100644 --- a/substrate/primitives/phragmen/compact/src/lib.rs +++ b/substrate/primitives/phragmen/compact/src/lib.rs @@ -164,7 +164,7 @@ fn struct_def( PartialEq, Eq, Clone, - _phragmen::sp_runtime::RuntimeDebug, + Debug, _phragmen::codec::Encode, _phragmen::codec::Decode, )] diff --git a/substrate/primitives/phragmen/src/helpers.rs b/substrate/primitives/phragmen/src/helpers.rs index 216de9243e..6b1497e3ad 100644 --- a/substrate/primitives/phragmen/src/helpers.rs +++ b/substrate/primitives/phragmen/src/helpers.rs @@ -17,7 +17,7 @@ //! Helper methods for phragmen. use crate::{Assignment, ExtendedBalance, VoteWeight, IdentifierT, StakedAssignment, WithApprovalOf}; -use sp_runtime::PerThing; +use sp_arithmetic::PerThing; use sp_std::prelude::*; /// Converts a vector of ratio assignments into ones with absolute budget value. @@ -57,7 +57,7 @@ pub fn to_without_backing(winners: Vec>) -> Ve #[cfg(test)] mod tests { use super::*; - use sp_runtime::Perbill; + use sp_arithmetic::Perbill; #[test] fn into_staked_works() { diff --git a/substrate/primitives/phragmen/src/lib.rs b/substrate/primitives/phragmen/src/lib.rs index f45732d5b8..cf972eb1ed 100644 --- a/substrate/primitives/phragmen/src/lib.rs +++ b/substrate/primitives/phragmen/src/lib.rs @@ -34,8 +34,11 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::{prelude::*, collections::btree_map::BTreeMap, fmt::Debug, cmp::Ordering, convert::TryFrom}; -use sp_runtime::{helpers_128bit::multiply_by_rational, PerThing, Rational128, RuntimeDebug, SaturatedConversion}; -use sp_runtime::traits::{Zero, Member, Saturating, Bounded}; +use sp_arithmetic::{ + PerThing, Rational128, + helpers_128bit::multiply_by_rational, + traits::{Zero, Saturating, Bounded, SaturatedConversion}, +}; #[cfg(test)] mod mock; @@ -60,7 +63,7 @@ pub use helpers::*; #[doc(hidden)] pub use codec; #[doc(hidden)] -pub use sp_runtime; +pub use sp_arithmetic; // re-export the compact solution type. pub use sp_phragmen_compact::generate_compact_solution_type; @@ -107,7 +110,7 @@ pub type WithApprovalOf = (A, ExtendedBalance); const DEN: u128 = u128::max_value(); /// A candidate entity for phragmen election. -#[derive(Clone, Default, RuntimeDebug)] +#[derive(Clone, Default, Debug)] struct Candidate { /// Identifier. who: AccountId, @@ -120,7 +123,7 @@ struct Candidate { } /// A voter entity. -#[derive(Clone, Default, RuntimeDebug)] +#[derive(Clone, Default, Debug)] struct Voter { /// Identifier. who: AccountId, @@ -133,7 +136,7 @@ struct Voter { } /// A candidate being backed by a voter. -#[derive(Clone, Default, RuntimeDebug)] +#[derive(Clone, Default, Debug)] struct Edge { /// Identifier. who: AccountId, @@ -144,7 +147,7 @@ struct Edge { } /// Final result of the phragmen election. -#[derive(RuntimeDebug)] +#[derive(Debug)] pub struct PhragmenResult { /// Just winners zipped with their approval stake. Note that the approval stake is merely the /// sub of their received stake and could be used for very basic sorting and approval voting. @@ -155,7 +158,7 @@ pub struct PhragmenResult { } /// A voter's stake assignment among a set of targets, represented as ratios. -#[derive(RuntimeDebug, Clone, Default)] +#[derive(Debug, Clone, Default)] #[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))] pub struct Assignment { /// Voter's identifier. @@ -223,7 +226,7 @@ where /// A voter's stake assignment among a set of targets, represented as absolute values in the scale /// of [`ExtendedBalance`]. -#[derive(RuntimeDebug, Clone, Default)] +#[derive(Debug, Clone, Default)] #[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))] pub struct StakedAssignment { /// Voter's identifier @@ -301,7 +304,7 @@ impl StakedAssignment { /// /// This, at the current version, resembles the `Exposure` defined in the Staking pallet, yet /// they do not necessarily have to be the same. -#[derive(Default, RuntimeDebug)] +#[derive(Default, Debug)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Eq, PartialEq))] pub struct Support { /// Total support. @@ -334,7 +337,7 @@ pub fn elect( initial_candidates: Vec, initial_voters: Vec<(AccountId, VoteWeight, Vec)>, ) -> Option> where - AccountId: Default + Ord + Member, + AccountId: Default + Ord + Clone, R: PerThing, { // return structures @@ -561,7 +564,7 @@ pub fn build_support_map( winners: &[AccountId], assignments: &[StakedAssignment], ) -> (SupportMap, u32) where - AccountId: Default + Ord + Member, + AccountId: Default + Ord + Clone, { let mut errors = 0; // Initialize the support of each candidate. diff --git a/substrate/primitives/phragmen/src/mock.rs b/substrate/primitives/phragmen/src/mock.rs index 5aab5ff2f7..cf9c90334b 100644 --- a/substrate/primitives/phragmen/src/mock.rs +++ b/substrate/primitives/phragmen/src/mock.rs @@ -19,11 +19,9 @@ #![cfg(test)] use crate::{elect, PhragmenResult, Assignment, VoteWeight, ExtendedBalance}; -use sp_runtime::{ - assert_eq_error_rate, PerThing, - traits::{Member, SaturatedConversion, Zero, One} -}; +use sp_arithmetic::{PerThing, traits::{SaturatedConversion, Zero, One}}; use sp_std::collections::btree_map::BTreeMap; +use sp_runtime::assert_eq_error_rate; #[derive(Default, Debug)] pub(crate) struct _Candidate { @@ -77,7 +75,7 @@ pub(crate) fn elect_float( initial_voters: Vec<(A, Vec)>, stake_of: FS, ) -> Option<_PhragmenResult> where - A: Default + Ord + Member + Copy, + A: Default + Ord + Copy, for<'r> FS: Fn(&'r A) -> VoteWeight, { let mut elected_candidates: Vec<(A, ExtendedBalance)>; diff --git a/substrate/primitives/phragmen/src/node.rs b/substrate/primitives/phragmen/src/node.rs index 92ef325a34..432c537052 100644 --- a/substrate/primitives/phragmen/src/node.rs +++ b/substrate/primitives/phragmen/src/node.rs @@ -16,11 +16,10 @@ //! (very) Basic implementation of a graph node used in the reduce algorithm. -use sp_runtime::RuntimeDebug; use sp_std::{cell::RefCell, fmt, prelude::*, rc::Rc}; /// The role that a node can accept. -#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, RuntimeDebug)] +#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)] pub(crate) enum NodeRole { /// A voter. This is synonym to a nominator in a staking context. Voter, diff --git a/substrate/primitives/phragmen/src/reduce.rs b/substrate/primitives/phragmen/src/reduce.rs index 1f6f6c3b99..ac4441ddc0 100644 --- a/substrate/primitives/phragmen/src/reduce.rs +++ b/substrate/primitives/phragmen/src/reduce.rs @@ -48,7 +48,7 @@ use crate::node::{Node, NodeId, NodeRef, NodeRole}; use crate::{ExtendedBalance, IdentifierT, StakedAssignment}; -use sp_runtime::traits::{Bounded, Zero}; +use sp_arithmetic::traits::{Bounded, Zero}; use sp_std::{ collections::btree_map::{BTreeMap, Entry::*}, prelude::*, diff --git a/substrate/primitives/phragmen/src/tests.rs b/substrate/primitives/phragmen/src/tests.rs index 9d16d67495..720a0a3f75 100644 --- a/substrate/primitives/phragmen/src/tests.rs +++ b/substrate/primitives/phragmen/src/tests.rs @@ -24,7 +24,7 @@ use crate::{ Support, StakedAssignment, Assignment, PhragmenResult, ExtendedBalance, }; use substrate_test_utils::assert_eq_uvec; -use sp_runtime::{Perbill, Permill, Percent, PerU16}; +use sp_arithmetic::{Perbill, Permill, Percent, PerU16}; #[test] fn float_phragmen_poc_works() { @@ -648,7 +648,7 @@ mod compact { // these need to come from the same dev-dependency `sp-phragmen`, not from the crate. use sp_phragmen::{Assignment, StakedAssignment, Error as PhragmenError, ExtendedBalance}; use sp_std::{convert::{TryInto, TryFrom}, fmt::Debug}; - use sp_runtime::Percent; + use sp_arithmetic::Percent; type Accuracy = Percent;