Remove sp-runtime dependency from sp-phragmen (#5786)

* Remove sp-runtime dependency from sp-phragmen

* Use regular derive(Debug) instead of RuntimeDebug
This commit is contained in:
Stanislav Tkach
2020-04-28 18:21:10 +03:00
committed by GitHub
parent 4fa5941f44
commit d870eb23e1
11 changed files with 35 additions and 33 deletions
+1
View File
@@ -7561,6 +7561,7 @@ dependencies = [
"parity-scale-codec",
"rand 0.7.3",
"serde",
"sp-arithmetic",
"sp-phragmen",
"sp-phragmen-compact",
"sp-runtime",
+2 -2
View File
@@ -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
};
+3 -2
View File
@@ -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",
]
@@ -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>
@@ -164,7 +164,7 @@ fn struct_def(
PartialEq,
Eq,
Clone,
_phragmen::sp_runtime::RuntimeDebug,
Debug,
_phragmen::codec::Encode,
_phragmen::codec::Decode,
)]
+2 -2
View File
@@ -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<A: IdentifierT>(winners: Vec<WithApprovalOf<A>>) -> Ve
#[cfg(test)]
mod tests {
use super::*;
use sp_runtime::Perbill;
use sp_arithmetic::Perbill;
#[test]
fn into_staked_works() {
+15 -12
View File
@@ -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> = (A, ExtendedBalance);
const DEN: u128 = u128::max_value();
/// A candidate entity for phragmen election.
#[derive(Clone, Default, RuntimeDebug)]
#[derive(Clone, Default, Debug)]
struct Candidate<AccountId> {
/// Identifier.
who: AccountId,
@@ -120,7 +123,7 @@ struct Candidate<AccountId> {
}
/// A voter entity.
#[derive(Clone, Default, RuntimeDebug)]
#[derive(Clone, Default, Debug)]
struct Voter<AccountId> {
/// Identifier.
who: AccountId,
@@ -133,7 +136,7 @@ struct Voter<AccountId> {
}
/// A candidate being backed by a voter.
#[derive(Clone, Default, RuntimeDebug)]
#[derive(Clone, Default, Debug)]
struct Edge<AccountId> {
/// Identifier.
who: AccountId,
@@ -144,7 +147,7 @@ struct Edge<AccountId> {
}
/// Final result of the phragmen election.
#[derive(RuntimeDebug)]
#[derive(Debug)]
pub struct PhragmenResult<AccountId, T: PerThing> {
/// 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<AccountId, T: PerThing> {
}
/// 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<AccountId, T: PerThing> {
/// 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<AccountId> {
/// Voter's identifier
@@ -301,7 +304,7 @@ impl<AccountId> StakedAssignment<AccountId> {
///
/// 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<AccountId> {
/// Total support.
@@ -334,7 +337,7 @@ pub fn elect<AccountId, R>(
initial_candidates: Vec<AccountId>,
initial_voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
) -> Option<PhragmenResult<AccountId, R>> where
AccountId: Default + Ord + Member,
AccountId: Default + Ord + Clone,
R: PerThing,
{
// return structures
@@ -561,7 +564,7 @@ pub fn build_support_map<AccountId>(
winners: &[AccountId],
assignments: &[StakedAssignment<AccountId>],
) -> (SupportMap<AccountId>, u32) where
AccountId: Default + Ord + Member,
AccountId: Default + Ord + Clone,
{
let mut errors = 0;
// Initialize the support of each candidate.
+3 -5
View File
@@ -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<A> {
@@ -77,7 +75,7 @@ pub(crate) fn elect_float<A, FS>(
initial_voters: Vec<(A, Vec<A>)>,
stake_of: FS,
) -> Option<_PhragmenResult<A>> where
A: Default + Ord + Member + Copy,
A: Default + Ord + Copy,
for<'r> FS: Fn(&'r A) -> VoteWeight,
{
let mut elected_candidates: Vec<(A, ExtendedBalance)>;
+1 -2
View File
@@ -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,
+1 -1
View File
@@ -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::*,
+2 -2
View File
@@ -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;