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 7 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 demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
bonding_duration: 90, // 90 days per bond.
early_era_slash: 10000,
session_reward: 100,
offline_slash_grace: 0,
}),
democracy: Some(DemocracyConfig {
launch_period: 120 * 24 * 14, // 2 weeks per public referendum
Expand Down
7 changes: 4 additions & 3 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ mod tests {
reclaim_rebate: 0,
early_era_slash: 0,
session_reward: 0,
offline_slash_grace: 0,
}),
democracy: Some(Default::default()),
council: Some(Default::default()),
Expand Down Expand Up @@ -250,7 +251,7 @@ mod tests {
// Blake
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("c563199c60df7d914262b1775b284870f3a5da2f24b56d2c6288b37c815a6cd9").into(),
hex!("6be281d6389f65b144f08acac551207f21bfbc136077b3d385b21fac31cb8818").into(),
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -266,7 +267,7 @@ mod tests {
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("83f71d5475f63350825b0301de322233d3711a9f3fcfd74050d1534af47a36b3").into(),
hex!("0e39bc5df47bf469d563b1da5d8cd492e7a79c4615229e8b62da1829e92abd5f").into(),
vec![
BareExtrinsic {
signed: bob(),
Expand All @@ -289,7 +290,7 @@ mod tests {
// Blake
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
// Keccak
hex!("06d026c0d687ec583660a6052de6f89acdb24ea964d06be3831c837c3c426966").into(),
hex!("7b6e9f810656d5d34d0034918e029ca370a189d7d69a16215875e82906440b96").into(),
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand Down
2 changes: 1 addition & 1 deletion demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Convert<AccountId, SessionKey> for SessionKeyConversion {
}

impl session::Trait for Concrete {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = SessionKeyConversion;
type OnSessionChange = Staking;
}
Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl staking::Trait for Test {
type OnAccountKill = Contract;
}
impl session::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = Staking;
}
Expand Down Expand Up @@ -109,6 +109,7 @@ fn new_test_ext(existential_deposit: u64, gas_price: u64) -> runtime_io::TestExt
reclaim_rebate: 0,
early_era_slash: 0,
session_reward: 0,
offline_slash_grace: 0,
}.build_storage()
.unwrap(),
);
Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ mod tests {
type Header = Header;
}
impl session::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = staking::Module<Test>;
}
Expand Down Expand Up @@ -697,6 +697,7 @@ mod tests {
reclaim_rebate: 0,
early_era_slash: 0,
session_reward: 0,
offline_slash_grace: 0,
}.build_storage().unwrap());
t.extend(democracy::GenesisConfig::<Test>{
launch_period: 1,
Expand Down
23 changes: 12 additions & 11 deletions substrate/runtime/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ mod tests {
type Header = Header;
}
impl session::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = staking::Module<Test>;
}
Expand Down Expand Up @@ -438,6 +438,7 @@ mod tests {
reclaim_rebate: 0,
early_era_slash: 0,
session_reward: 0,
offline_slash_grace: 0,
}.build_storage().unwrap());
t.extend(GenesisConfig::<Test>{
launch_period: 1,
Expand Down Expand Up @@ -499,7 +500,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (10, 0));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 2);
});
Expand Down Expand Up @@ -577,19 +578,19 @@ mod tests {
System::set_block_number(1);
assert_ok!(Democracy::vote(&1, 0, true));
assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);
assert_eq!(Staking::bonding_duration(), 4);

System::set_block_number(2);
assert_ok!(Democracy::vote(&1, 1, true));
assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);
assert_eq!(Staking::bonding_duration(), 3);

System::set_block_number(3);
assert_ok!(Democracy::vote(&1, 2, true));
assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);
assert_eq!(Staking::bonding_duration(), 2);
});
}
Expand All @@ -610,7 +611,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (10, 0));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 2);
});
Expand All @@ -625,7 +626,7 @@ mod tests {
assert_ok!(Democracy::cancel_referendum(r));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 1);
});
Expand All @@ -643,7 +644,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (0, 10));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 1);
});
Expand All @@ -664,7 +665,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (110, 100));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 2);
});
Expand All @@ -681,7 +682,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (60, 50));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 1);
});
Expand All @@ -702,7 +703,7 @@ mod tests {
assert_eq!(Democracy::tally(r), (100, 50));

assert_eq!(Democracy::end_block(System::block_number()), Ok(()));
Staking::on_session_change(0, Vec::new());
Staking::on_session_change(0, true);

assert_eq!(Staking::era_length(), 2);
});
Expand Down
9 changes: 5 additions & 4 deletions substrate/runtime/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ mod tests {
type Header = Header;
}
impl session::Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = staking::Module<Test>;
}
Expand Down Expand Up @@ -287,6 +287,7 @@ mod tests {
reclaim_rebate: 0,
early_era_slash: 0,
session_reward: 0,
offline_slash_grace: 0,
}.build_storage().unwrap());
let xt = primitives::testing::TestXt((1, 0, Call::transfer(2.into(), 69)));
let mut t = runtime_io::TestExternalities::from(t);
Expand Down Expand Up @@ -317,8 +318,8 @@ mod tests {
// Blake
// state_root: hex!("02532989c613369596025dfcfc821339fc9861987003924913a5a1382f87034a").into(),
// Keccak
state_root: hex!("8fad93b6b9e5251a2e4913598fd0d74a138c0e486eb1133ff8081b429b0c56f2").into(),
extrinsics_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").into(), // REVIEW: I expected this to be wrong with a different hasher?
state_root: hex!("06efddda99014ce420dc903e6c8b7f87a1c96e699fbb43d26dc5f3203ae94ee0").into(),
extrinsics_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").into(),
digest: Digest { logs: vec![], },
},
extrinsics: vec![],
Expand Down Expand Up @@ -351,7 +352,7 @@ mod tests {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("8fad93b6b9e5251a2e4913598fd0d74a138c0e486eb1133ff8081b429b0c56f2").into(),
state_root: hex!("06efddda99014ce420dc903e6c8b7f87a1c96e699fbb43d26dc5f3203ae94ee0").into(),
extrinsics_root: [0u8; 32].into(),
digest: Digest { logs: vec![], },
},
Expand Down
5 changes: 4 additions & 1 deletion substrate/runtime/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use codec::{Codec, Encode};
pub use integer_sqrt::IntegerSquareRoot;
pub use num_traits::{Zero, One, Bounded};
pub use num_traits::ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign,
RemAssign, Shl, Shr};

/// A lazy value.
pub trait Lazy<T: ?Sized> {
Expand Down Expand Up @@ -132,6 +133,7 @@ pub trait SimpleArithmetic:
Mul<Self, Output = Self> + MulAssign<Self> +
Div<Self, Output = Self> + DivAssign<Self> +
Rem<Self, Output = Self> + RemAssign<Self> +
Shl<u32, Output = Self> + Shr<u32, Output = Self> +
CheckedAdd +
CheckedSub +
CheckedMul +
Expand All @@ -145,6 +147,7 @@ impl<T:
Mul<Self, Output = Self> + MulAssign<Self> +
Div<Self, Output = Self> + DivAssign<Self> +
Rem<Self, Output = Self> + RemAssign<Self> +
Shl<u32, Output = Self> + Shr<u32, Output = Self> +
CheckedAdd +
CheckedSub +
CheckedMul +
Expand Down
62 changes: 17 additions & 45 deletions substrate/runtime/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ use runtime_support::dispatch::Result;
use std::collections::HashMap;

/// A session has changed.
pub trait OnSessionChange<T, A> {
pub trait OnSessionChange<T> {
/// Session has changed.
fn on_session_change(time_elapsed: T, bad_validators: Vec<A>);
fn on_session_change(time_elapsed: T, should_reward: bool);
}

impl<T, A> OnSessionChange<T, A> for () {
fn on_session_change(_: T, _: Vec<A>) {}
impl<T> OnSessionChange<T> for () {
fn on_session_change(_: T, _: bool) {}
}

pub trait Trait: timestamp::Trait {
// the position of the required timestamp-set extrinsic.
const NOTE_OFFLINE_POSITION: u32;
const NOTE_MISSED_PROPOSAL_POSITION: u32;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you update a comment above?


type ConvertAccountIdToSessionKey: Convert<Self::AccountId, Self::SessionKey>;
type OnSessionChange: OnSessionChange<Self::Moment, Self::AccountId>;
type OnSessionChange: OnSessionChange<Self::Moment>;
}

decl_module! {
Expand All @@ -83,7 +83,7 @@ decl_module! {
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum PrivCall {
fn set_length(new: T::BlockNumber) -> Result = 0;
fn force_new_session(normal_rotation: bool) -> Result = 1;
fn force_new_session(apply_rewards: bool) -> Result = 1;
}
}

Expand Down Expand Up @@ -142,18 +142,18 @@ impl<T: Trait> Module<T> {
}

/// Forces a new session.
pub fn force_new_session(normal_rotation: bool) -> Result {
<ForcingNewSession<T>>::put(normal_rotation);
pub fn force_new_session(apply_rewards: bool) -> Result {
<ForcingNewSession<T>>::put(apply_rewards);
Ok(())
}

/// Notes which of the validators appear to be online from the point of the view of the block author.
pub fn note_offline(aux: &T::PublicAux, offline_val_indices: Vec<u32>) -> Result {
assert!(aux.is_empty());
assert!(
<system::Module<T>>::extrinsic_index() == T::NOTE_OFFLINE_POSITION,
<system::Module<T>>::extrinsic_index() == T::NOTE_MISSED_PROPOSAL_POSITION,
"note_offline extrinsic must be at position {} in the block",
T::NOTE_OFFLINE_POSITION
T::NOTE_MISSED_PROPOSAL_POSITION
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you update error message?

);

let vs = Self::validators();
Expand Down Expand Up @@ -181,15 +181,15 @@ impl<T: Trait> Module<T> {
// check block number and call next_session if necessary.
let block_number = <system::Module<T>>::block_number();
let is_final_block = ((block_number - Self::last_length_change()) % Self::length()).is_zero();
let bad_validators = <BadValidators<T>>::take().unwrap_or_default();
let should_end_session = <ForcingNewSession<T>>::take().is_some() || !bad_validators.is_empty() || is_final_block;
let (should_end_session, apply_rewards) = <ForcingNewSession<T>>::take()
.map_or((is_final_block, is_final_block), |apply_rewards| (true, apply_rewards));
if should_end_session {
Self::rotate_session(is_final_block, bad_validators);
Self::rotate_session(is_final_block, apply_rewards);
}
}

/// Move onto next session: register the new authority set.
pub fn rotate_session(is_final_block: bool, bad_validators: Vec<T::AccountId>) {
pub fn rotate_session(is_final_block: bool, apply_rewards: bool) {
let now = <timestamp::Module<T>>::get();
let time_elapsed = now.clone() - Self::current_start();

Expand All @@ -209,7 +209,7 @@ impl<T: Trait> Module<T> {
<LastLengthChange<T>>::put(block_number);
}

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

// Update any changes in session keys.
Self::validators().iter().enumerate().for_each(|(i, v)| {
Expand Down Expand Up @@ -314,7 +314,7 @@ mod tests {
type Moment = u64;
}
impl Trait for Test {
const NOTE_OFFLINE_POSITION: u32 = 1;
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = ();
}
Expand Down Expand Up @@ -350,34 +350,6 @@ mod tests {
});
}

#[test]
fn should_rotate_on_bad_validators() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(2);
assert_eq!(Session::blocks_remaining(), 0);
Timestamp::set_timestamp(0);
assert_ok!(Session::set_length(3));
Session::check_rotate_session();
assert_eq!(Session::current_index(), 1);
assert_eq!(Session::length(), 3);
assert_eq!(Session::current_start(), 0);
assert_eq!(Session::ideal_session_duration(), 15);
// ideal end = 0 + 15 * 3 = 15

System::set_block_number(3);
assert_eq!(Session::blocks_remaining(), 2);
Timestamp::set_timestamp(9); // no bad validators. session not rotated.
Session::check_rotate_session();

System::set_block_number(4);
::system::ExtrinsicIndex::<Test>::put(1);
assert_eq!(Session::blocks_remaining(), 1);
Session::note_offline(&0, vec![1]).unwrap(); // bad validator -> session rotate
Session::check_rotate_session();
assert_eq!(Session::current_index(), 2);
});
}

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