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

Commit

Permalink
Self-Vote for Staking (among others.) (#2078)
Browse files Browse the repository at this point in the history
* initial doc for the staking module

* Remove md style links.

* Remove todos.

* Add rust code types

* Rename and fix review notes.

* Add new md file

* Final touches.

* Migrate compleatly to rustdoc

* Update link

* Fix heading

* Final touches wrt the new template.

* Remove empty prereq.

* Fix more reviews

* Some final nits.

* Fix some side issues.

* Fix another set of reviews

* Fix + stabilize leftover reivews.

* Remove unused test parameters

* Fix typo.

* Merge redundant loops

* Adds phantom self-vote

* Fix broken tests.

* Refactor some names to match the reference.

* Remove redundant inner loops from election round.

* Introduce phragmen post-processing.

* Some fixes and todos.

* Fix some tests with new phragmen params

* Fix test

* Bump spec

* Fix wasm build

* Fix tests and phragmen fallback. Avoid double-controlling

* Fix and rebuild wasm

* Whitespaces, whitespaces everywhere.

* Rebuild

* Disable post-processing.

* Identify by stash, not controller account.

* Couple of fixes

* Fix first test

* Fix invulnerability_should_work

* Fix a couple more tests

* Fix more tests

* Fix more tests

* Fix more tests

* Fix some tests

* Fix update-ledger.

* Fix update-ledger.

* Fix another test

* Fix another test

* Fix rest of staking tests

* Remove printlns

* Rebuild wasm

* Fix & tests for auth/val syncing

* Fix up threading for tests

* Remove superfluous asserts
  • Loading branch information
kianenigma authored and gavofyork committed Mar 22, 2019
1 parent d1e25d1 commit 3628a93
Show file tree
Hide file tree
Showing 13 changed files with 1,397 additions and 709 deletions.
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.

Binary file not shown.
2 changes: 0 additions & 2 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
current_era: 0,
offline_slash: Perbill::from_billionths(1_000_000),
session_reward: Perbill::from_billionths(2_065),
current_offline_slash: 0,
current_session_reward: 0,
validator_count: 7,
sessions_per_era: 12,
Expand Down Expand Up @@ -263,7 +262,6 @@ pub fn testnet_genesis(
bonding_duration: 2 * 60 * 12,
offline_slash: Perbill::zero(),
session_reward: Perbill::zero(),
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, StakerStatus::Validator)).collect(),
Expand Down
17 changes: 1 addition & 16 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ mod tests {
bonding_duration: 0,
offline_slash: Perbill::zero(),
session_reward: Perbill::zero(),
current_offline_slash: 0,
current_session_reward: 0,
offline_slash_grace: 0,
invulnerables: vec![alice(), bob(), charlie()],
Expand Down Expand Up @@ -443,13 +442,7 @@ 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 digest = generic::Digest::<Log>::default(); // TODO test this
let digest = generic::Digest::<Log>::default();
assert_eq!(Header::decode(&mut &block2.0[..]).unwrap().digest, digest);

(block1, block2)
Expand Down Expand Up @@ -578,14 +571,6 @@ 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
4 changes: 2 additions & 2 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 38,
impl_version: 40,
spec_version: 39,
impl_version: 39,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions srml/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ impl<T: Trait> Module<T> {
}
}

/// Set a single authority by index.
pub fn set_authority_count(count: u32) {
Self::save_original_authorities(None);
AuthorityStorageVec::<T::SessionKey>::set_count(count);
}

/// Set a single authority by index.
pub fn set_authority(index: u32, key: &T::SessionKey) {
let current_authority = AuthorityStorageVec::<T::SessionKey>::item(index);
Expand Down
1 change: 1 addition & 0 deletions srml/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ timestamp = { package = "srml-timestamp", path = "../timestamp", default-feature
[dev-dependencies]
substrate-primitives = { path = "../../core/primitives" }
runtime_io = { package = "sr-io", path = "../../core/sr-io" }
lazy_static = "1.0"

[features]
default = ["std"]
Expand Down
51 changes: 44 additions & 7 deletions srml/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,19 @@ impl<T: Trait> Module<T> {
<LastLengthChange<T>>::put(block_number);
}

T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);

// Update any changes in session keys.
for (i, v) in Self::validators().into_iter().enumerate() {
let v = Self::validators();
<consensus::Module<T>>::set_authority_count(v.len() as u32);
for (i, v) in v.into_iter().enumerate() {
<consensus::Module<T>>::set_authority(
i as u32,
&<NextKeyFor<T>>::get(&v)
.or_else(|| T::ConvertAccountIdToSessionKey::convert(v))
.unwrap_or_default()
);
};

T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);
}

/// Get the time that should have elapsed over a session if everything was working perfectly.
Expand Down Expand Up @@ -224,6 +226,7 @@ impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
use srml_support::{impl_outer_origin, assert_ok};
use runtime_io::with_externalities;
use substrate_primitives::{H256, Blake2Hasher};
Expand All @@ -235,6 +238,17 @@ mod tests {
pub enum Origin for Test {}
}

thread_local!{
static NEXT_VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
}

pub struct TestOnSessionChange;
impl OnSessionChange<u64> for TestOnSessionChange {
fn on_session_change(_elapsed: u64, _should_reward: bool) {
NEXT_VALIDATORS.with(|v| Session::set_validators(&*v.borrow()));
}
}

#[derive(Clone, Eq, PartialEq)]
pub struct Test;
impl consensus::Trait for Test {
Expand All @@ -261,7 +275,7 @@ mod tests {
}
impl Trait for Test {
type ConvertAccountIdToSessionKey = ConvertUintAuthorityId;
type OnSessionChange = ();
type OnSessionChange = TestOnSessionChange;
type Event = ();
}

Expand All @@ -273,14 +287,14 @@ mod tests {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
t.extend(consensus::GenesisConfig::<Test>{
code: vec![],
authorities: vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)],
authorities: NEXT_VALIDATORS.with(|l| l.borrow().iter().cloned().map(UintAuthorityId).collect()),
}.build_storage().unwrap().0);
t.extend(timestamp::GenesisConfig::<Test>{
period: 5,
}.build_storage().unwrap().0);
t.extend(GenesisConfig::<Test>{
session_length: 2,
validators: vec![1, 2, 3],
validators: NEXT_VALIDATORS.with(|l| l.borrow().clone()),
keys: vec![],
}.build_storage().unwrap().0);
runtime_io::TestExternalities::new(t)
Expand All @@ -289,12 +303,35 @@ mod tests {
#[test]
fn simple_setup_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1).into(), UintAuthorityId(2).into(), UintAuthorityId(3).into()]);
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
assert_eq!(Session::length(), 2);
assert_eq!(Session::validators(), vec![1, 2, 3]);
});
}

#[test]
fn authorities_should_track_validators() {
with_externalities(&mut new_test_ext(), || {
NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2]);
assert_ok!(Session::force_new_session(false));
Session::check_rotate_session(1);
assert_eq!(Session::validators(), vec![1, 2]);
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2)]);

NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2, 4]);
assert_ok!(Session::force_new_session(false));
Session::check_rotate_session(2);
assert_eq!(Session::validators(), vec![1, 2, 4]);
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(4)]);

NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2, 3]);
assert_ok!(Session::force_new_session(false));
Session::check_rotate_session(3);
assert_eq!(Session::validators(), vec![1, 2, 3]);
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
});
}

#[test]
fn should_work_with_early_exit() {
with_externalities(&mut new_test_ext(), || {
Expand Down
Loading

0 comments on commit 3628a93

Please sign in to comment.