Skip to content

Commit

Permalink
Phragmén Validator Election (paritytech#1915)
Browse files Browse the repository at this point in the history
* adds first draft phragmen with tuned tests and sr-primitives

* fix a few docs and code styles

* clean, organize and finish remaining test cases

* more and more tests

* update edge cases behavior and relavent tests

* fix global test issues

* updated wasm files

* all tests fixed

* cleanup

* fix some global issues

* fix global tests

* bump versions

* fix typo

* first step toward extracting phragmen

* Fix most of the grumbles.

* fix rest of the grumbles

* spaces to tabs

* update wasm

* Removed nightly feature.

* More tests

* Fix broken tests.

* Bump and update wasm.
  • Loading branch information
kianenigma authored and MTDK1 committed Apr 12, 2019
1 parent f3d135c commit 8deace5
Show file tree
Hide file tree
Showing 11 changed files with 1,147 additions and 383 deletions.
82 changes: 81 additions & 1 deletion core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ macro_rules! create_runtime_str {
#[cfg(feature = "std")]
pub use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "std")]
use serde_derive::{Serialize, Deserialize};
pub use serde_derive::{Serialize, Deserialize};

/// Complex storage builder stuff.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -251,6 +251,86 @@ impl From<codec::Compact<Perbill>> for Perbill {
}
}

/// Perquintill is parts-per-quintillion. It stores a value between 0 and 1 in fixed point and
/// provides a means to multiply some other value by that.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq)]
pub struct Perquintill(u64);

const QUINTILLION: u64 = 1_000_000_000_000_000_000;

impl Perquintill {
/// Nothing.
pub fn zero() -> Self { Self(0) }

/// Everything.
pub fn one() -> Self { Self(QUINTILLION) }

/// Construct new instance where `x` is in quintillionths. Value equivalent to `x / 1,000,000,000,000,000,000`.
pub fn from_quintillionths(x: u64) -> Self { Self(x.min(QUINTILLION)) }

/// Construct new instance where `x` is in billionths. Value equivalent to `x / 1,000,000,000`.
pub fn from_billionths(x: u64) -> Self { Self(x.min(1_000_000_000) * 1_000_000_000 ) }

/// Construct new instance where `x` is in millionths. Value equivalent to `x / 1,000,000`.
pub fn from_millionths(x: u64) -> Self { Self(x.min(1_000_000) * 1000_000_000_000) }

/// Construct new instance where `x` is denominator and the nominator is 1.
pub fn from_xth(x: u64) -> Self { Self(QUINTILLION / x.min(QUINTILLION)) }

#[cfg(feature = "std")]
/// Construct new instance whose value is equal to `x` (between 0 and 1).
pub fn from_fraction(x: f64) -> Self { Self((x.max(0.0).min(1.0) * QUINTILLION as f64) as u64) }
}

impl ::rstd::ops::Deref for Perquintill {
type Target = u64;

fn deref(&self) -> &u64 {
&self.0
}
}

impl<N> ::rstd::ops::Mul<N> for Perquintill
where
N: traits::As<u64>
{
type Output = N;
fn mul(self, b: N) -> Self::Output {
<N as traits::As<u64>>::sa(b.as_().saturating_mul(self.0) / QUINTILLION)
}
}

#[cfg(feature = "std")]
impl From<f64> for Perquintill {
fn from(x: f64) -> Perquintill {
Perquintill::from_fraction(x)
}
}

#[cfg(feature = "std")]
impl From<f32> for Perquintill {
fn from(x: f32) -> Perquintill {
Perquintill::from_fraction(x as f64)
}
}

impl codec::CompactAs for Perquintill {
type As = u64;
fn encode_as(&self) -> &u64 {
&self.0
}
fn decode_from(x: u64) -> Perquintill {
Perquintill(x)
}
}

impl From<codec::Compact<Perquintill>> for Perquintill {
fn from(x: codec::Compact<Perquintill>) -> Perquintill {
x.0
}
}

/// Signature verify that can work with any known signature types..
#[derive(Eq, PartialEq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
Expand Down
Binary file not shown.
6 changes: 3 additions & 3 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use primitives::{ed25519::Public as AuthorityId, ed25519, sr25519, Pair, crypto::UncheckedInto};
use node_primitives::AccountId;
use node_runtime::{ConsensusConfig, CouncilSeatsConfig, CouncilVotingConfig, DemocracyConfig,
SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, TreasuryConfig,
SessionConfig, StakingConfig, StakerStatus, TimestampConfig, BalancesConfig, TreasuryConfig,
SudoConfig, ContractConfig, GrandpaConfig, IndicesConfig, FeesConfig, Permill, Perbill};
pub use node_runtime::GenesisConfig;
use substrate_service;
Expand Down Expand Up @@ -113,7 +113,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
bonding_duration: 60 * MINUTES,
offline_slash_grace: 4,
minimum_validator_count: 4,
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH)).collect(),
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)).collect(),
invulnerables: initial_authorities.iter().map(|x| x.1.clone()).collect(),
}),
democracy: Some(DemocracyConfig {
Expand Down Expand Up @@ -267,7 +267,7 @@ pub fn testnet_genesis(
current_offline_slash: 0,
current_session_reward: 0,
offline_slash_grace: 0,
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH)).collect(),
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)).collect(),
invulnerables: initial_authorities.iter().map(|x| x.1.clone()).collect(),
}),
democracy: Some(DemocracyConfig {
Expand Down
24 changes: 21 additions & 3 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod tests {
use node_primitives::{Hash, BlockNumber, AccountId};
use runtime_primitives::traits::{Header as HeaderT, Hash as HashT};
use runtime_primitives::{generic, generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
use {balances, indices, session, system, consensus, timestamp, treasury, contract};
use {balances, indices, session, system, staking, consensus, timestamp, treasury, contract};
use contract::ContractAddressFor;
use system::{EventRecord, Phase};
use node_runtime::{Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances,
Expand Down Expand Up @@ -296,7 +296,11 @@ mod tests {
staking: Some(StakingConfig {
sessions_per_era: 2,
current_era: 0,
stakers: vec![(dave(), alice(), 111), (eve(), bob(), 101), (ferdie(), charlie(), 100)],
stakers: vec![
(dave(), alice(), 111, staking::StakerStatus::Validator),
(eve(), bob(), 100, staking::StakerStatus::Validator),
(ferdie(), charlie(), 100, staking::StakerStatus::Validator)
],
validator_count: 3,
minimum_validator_count: 0,
bonding_duration: 0,
Expand Down Expand Up @@ -441,7 +445,13 @@ mod tests {
]
);

let digest = generic::Digest::<Log>::default();
// let mut digest = generic::Digest::<Log>::default();
// digest.push(Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![
// (Keyring::Charlie.to_raw_public().into(), 1),
// (Keyring::Bob.to_raw_public().into(), 1),
// (Keyring::Alice.to_raw_public().into(), 1),
// ])));
let digest = generic::Digest::<Log>::default(); // TODO test this
assert_eq!(Header::decode(&mut &block2.0[..]).unwrap().digest, digest);

(block1, block2)
Expand Down Expand Up @@ -574,6 +584,14 @@ mod tests {
phase: Phase::Finalization,
event: Event::session(session::RawEvent::NewSession(1))
},
// EventRecord { // TODO: this might be wrong.
// phase: Phase::Finalization,
// event: Event::grandpa(::grandpa::RawEvent::NewAuthorities(vec![
// (Keyring::Charlie.to_raw_public().into(), 1),
// (Keyring::Bob.to_raw_public().into(), 1),
// (Keyring::Alice.to_raw_public().into(), 1),
// ])),
// },
EventRecord {
phase: Phase::Finalization,
event: Event::treasury(treasury::RawEvent::Spending(0))
Expand Down
3 changes: 2 additions & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ pub use timestamp::Call as TimestampCall;
pub use balances::Call as BalancesCall;
pub use runtime_primitives::{Permill, Perbill};
pub use support::StorageValue;
pub use staking::StakerStatus;

/// Runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 35,
impl_version: 35,
impl_version: 36,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
Binary file not shown.
62 changes: 0 additions & 62 deletions srml/staking/Staking.md

This file was deleted.

Loading

0 comments on commit 8deace5

Please sign in to comment.