Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions primitives/core/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
5 changes: 3 additions & 2 deletions primitives/phragmen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -30,5 +31,5 @@ std = [
"codec/std",
"serde",
"sp-std/std",
"sp-runtime/std",
"sp-arithmetic/std",
]
8 changes: 4 additions & 4 deletions primitives/phragmen/compact/src/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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))
})
Expand All @@ -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,
);
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion primitives/phragmen/compact/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn struct_def(
PartialEq,
Eq,
Clone,
_phragmen::sp_runtime::RuntimeDebug,
Debug,
_phragmen::codec::Encode,
_phragmen::codec::Decode,
)]
Expand Down
4 changes: 2 additions & 2 deletions primitives/phragmen/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
27 changes: 15 additions & 12 deletions primitives/phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -120,7 +123,7 @@ struct Candidate<AccountId> {
}

/// A voter entity.
#[derive(Clone, Default, RuntimeDebug)]
#[derive(Clone, Default, Debug)]
struct Voter<AccountId> {
/// Identifier.
who: AccountId,
Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions primitives/phragmen/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)>;
Expand Down
3 changes: 1 addition & 2 deletions primitives/phragmen/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion primitives/phragmen/src/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down
4 changes: 2 additions & 2 deletions primitives/phragmen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;

Expand Down