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

Phragmén Validator Election #1915

Merged
merged 27 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
merge conflicts with master
  • Loading branch information
kianenigma committed Mar 14, 2019
commit cac9d3f46946dd9d1345dba0eb0998d66ec95c02
45 changes: 44 additions & 1 deletion core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,50 @@ impl From<codec::Compact<Perquintill>> for Perquintill {
}
}

/// Ed25519 signature verify.
/// Signature verify that can work with any known signature types..
#[derive(Eq, PartialEq, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum MultiSignature {
/// An Ed25519 signature.
Ed25519(ed25519::Signature),
/// An Sr25519 signature.
Sr25519(sr25519::Signature),
}

impl Default for MultiSignature {
fn default() -> Self {
MultiSignature::Ed25519(Default::default())
}
}

/// Public key for any known crypto algorithm.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum MultiSigner {
/// An Ed25519 identity.
Ed25519(ed25519::Public),
/// An Sr25519 identity.
Sr25519(sr25519::Public),
}

impl Default for MultiSigner {
fn default() -> Self {
MultiSigner::Ed25519(Default::default())
}
}

impl Verify for MultiSignature {
type Signer = MultiSigner;
fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &Self::Signer) -> bool {
match (self, signer) {
(MultiSignature::Ed25519(ref sig), &MultiSigner::Ed25519(ref who)) => sig.verify(msg, who),
(MultiSignature::Sr25519(ref sig), &MultiSigner::Sr25519(ref who)) => sig.verify(msg, who),
_ => false,
}
}
}

/// Signature verify that can work with any known signature types..
#[derive(Eq, PartialEq, Clone, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub struct AnySignature(H512);
Expand Down
Binary file not shown.
8 changes: 4 additions & 4 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ 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.into(), x.1.into(), STASH, StakerStatus::Validator)).collect(),
invulnerables: initial_authorities.iter().map(|x| x.1.into()).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 {
launch_period: 10 * MINUTES, // 1 day per public referendum
Expand Down Expand Up @@ -267,8 +267,8 @@ pub fn testnet_genesis(
current_offline_slash: 0,
current_session_reward: 0,
offline_slash_grace: 0,
stakers: initial_authorities.iter().map(|x| (x.0.into(), x.1.into(), STASH, StakerStatus::Validator)).collect(),
invulnerables: initial_authorities.iter().map(|x| x.1.into()).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 {
launch_period: 9,
Expand Down
29 changes: 15 additions & 14 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,13 @@ mod tests {
]
);

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 mut digest = generic::Digest::<Log>::default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No un-commented code please.

// 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 @@ -583,14 +584,14 @@ mod tests {
phase: Phase::Finalization,
event: Event::session(session::RawEvent::NewSession(1))
},
EventRecord {
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 { // TODO: this might be wrong.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same and no TODO without an issue in the code.

// 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
Binary file not shown.
6 changes: 4 additions & 2 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ decl_storage! {
config(stakers): Vec<(T::AccountId, T::AccountId, BalanceOf<T>, StakerStatus<T::AccountId>)>;
build(|storage: &mut primitives::StorageOverlay, _: &mut primitives::ChildrenStorageOverlay, config: &GenesisConfig<T>| {
with_storage(storage, || {
println!("Staker: {:?}", config.stakers);

for &(ref stash, ref controller, balance, ref status) in &config.stakers {
let _ = <Module<T>>::bond(
T::Origin::from(Some(stash.clone()).into()),
Expand Down Expand Up @@ -683,6 +685,7 @@ impl<T: Trait> Module<T> {
.min_by_key(|c| c.exposure.total)
.map(|c| c.exposure.total)
.unwrap_or_default();
<SlotStake<T>>::put(&slot_stake);

// Clear Stakers and reduce their slash_count.
for v in <session::Module<T>>::validators().iter() {
Expand All @@ -692,7 +695,7 @@ impl<T: Trait> Module<T> {
<SlashCount<T>>::insert(v, slash_count - 1);
}
}

println!("Elected : {:?}", elected_candidates);
// Populate Stakers.
for candidate in &elected_candidates {
<Stakers<T>>::insert(candidate.who.clone(), candidate.exposure.clone());
Expand All @@ -703,7 +706,6 @@ impl<T: Trait> Module<T> {
&elected_candidates.into_iter().map(|i| i.who).collect::<Vec<_>>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just iterated this list above, maybe merge both iterations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new function decomposition its not the same + this is passing the whole Vec inside, I don't get exactly what you mean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you refactored and saying that my comment does not apply anymore, what should I do without seeing the new code?(nevertheless, you were iterating this list twice and that does not need to be done)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for reference, you now iterate the same vector 3 times :D
This could all be done in one iteration.

);

<SlotStake<T>>::put(&slot_stake);
slot_stake
}

Expand Down
13 changes: 8 additions & 5 deletions srml/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@ impl ExtBuilder {
balances: if self.monied {
if self.reward > 0 {
vec![
(1, 10 * balance_factor),(2, 20 * balance_factor),
(3, 300 * balance_factor),(4, 400 * balance_factor),
(10, balance_factor), (11, balance_factor * 1000),
(20, balance_factor), (21, balance_factor * 2000),
(100, 2000 * balance_factor), (101, 2000 * balance_factor)
(1, 10 * balance_factor),
(2, 20 * balance_factor),
(3, 300 * balance_factor),
(4, 400 * balance_factor),
(10, balance_factor),
(11, balance_factor * 1000),
(20, balance_factor),
(21, balance_factor * 2000)
]
} else {
vec![
Expand Down
3 changes: 3 additions & 0 deletions srml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ fn basic_setup_works() {
(10, ValidatorPrefs { unstake_threshold: 3, validator_payment: 0 })
]);

// Account 100 is the default nominator
assert_eq!(Staking::stakers(100), Exposure { total: 500, own: 500, others: vec![] });

// Account 10 is exposed by 100 * balance_factor from their own stash in account 11
assert_eq!(Staking::stakers(10), Exposure { total: 1500, own: 1000, others: vec![ IndividualExposure { who: 100, value: 500 }] });
assert_eq!(Staking::stakers(20), Exposure { total: 2500, own: 2000, others: vec![ IndividualExposure { who: 100, value: 500 }] });
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.