Skip to content

Commit 0cb0eeb

Browse files
kianenigmagui1117
andauthored
Allow Staking tests to run with session length other than 1 (#7719)
* fix periodic session * Allow staking tests to run with session lengths other than 1. * Update frame/staking/src/mock.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Fix all tests with session length 5. * Test for active != current * Better doc * Update frame/staking/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * also set the timestamp properly. * trigger CI * Revert "trigger CI" This reverts commit 0f254944cdad848aa6e63bd8a618db95447a8e68. * Update frame/staking/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
1 parent b513d96 commit 0cb0eeb

File tree

2 files changed

+65
-58
lines changed

2 files changed

+65
-58
lines changed

src/lib.rs

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
//! # Session Module
1919
//!
20-
//! The Session module allows validators to manage their session keys, provides a function for changing
21-
//! the session length, and handles session rotation.
20+
//! The Session module allows validators to manage their session keys, provides a function for
21+
//! changing the session length, and handles session rotation.
2222
//!
2323
//! - [`session::Config`](./trait.Config.html)
2424
//! - [`Call`](./enum.Call.html)
@@ -29,34 +29,39 @@
2929
//! ### Terminology
3030
//! <!-- Original author of paragraph: @gavofyork -->
3131
//!
32-
//! - **Session:** A session is a period of time that has a constant set of validators. Validators can only join
33-
//! or exit the validator set at a session change. It is measured in block numbers. The block where a session is
34-
//! ended is determined by the `ShouldEndSession` trait. When the session is ending, a new validator set
35-
//! can be chosen by `OnSessionEnding` implementations.
36-
//! - **Session key:** A session key is actually several keys kept together that provide the various signing
37-
//! functions required by network authorities/validators in pursuit of their duties.
38-
//! - **Validator ID:** Every account has an associated validator ID. For some simple staking systems, this
39-
//! may just be the same as the account ID. For staking systems using a stash/controller model,
40-
//! the validator ID would be the stash account ID of the controller.
32+
//! - **Session:** A session is a period of time that has a constant set of validators. Validators
33+
//! can only join or exit the validator set at a session change. It is measured in block numbers.
34+
//! The block where a session is ended is determined by the `ShouldEndSession` trait. When the
35+
//! session is ending, a new validator set can be chosen by `OnSessionEnding` implementations.
36+
//!
37+
//! - **Session key:** A session key is actually several keys kept together that provide the various
38+
//! signing functions required by network authorities/validators in pursuit of their duties.
39+
//! - **Validator ID:** Every account has an associated validator ID. For some simple staking
40+
//! systems, this may just be the same as the account ID. For staking systems using a
41+
//! stash/controller model, the validator ID would be the stash account ID of the controller.
42+
//!
4143
//! - **Session key configuration process:** Session keys are set using `set_keys` for use not in
42-
//! the next session, but the session after next. They are stored in `NextKeys`, a mapping between
43-
//! the caller's `ValidatorId` and the session keys provided. `set_keys` allows users to set their
44-
//! session key prior to being selected as validator.
45-
//! It is a public call since it uses `ensure_signed`, which checks that the origin is a signed account.
46-
//! As such, the account ID of the origin stored in `NextKeys` may not necessarily be associated with
47-
//! a block author or a validator. The session keys of accounts are removed once their account balance is zero.
44+
//! the next session, but the session after next. They are stored in `NextKeys`, a mapping between
45+
//! the caller's `ValidatorId` and the session keys provided. `set_keys` allows users to set their
46+
//! session key prior to being selected as validator. It is a public call since it uses
47+
//! `ensure_signed`, which checks that the origin is a signed account. As such, the account ID of
48+
//! the origin stored in `NextKeys` may not necessarily be associated with a block author or a
49+
//! validator. The session keys of accounts are removed once their account balance is zero.
50+
//!
4851
//! - **Session length:** This pallet does not assume anything about the length of each session.
49-
//! Rather, it relies on an implementation of `ShouldEndSession` to dictate a new session's start.
50-
//! This pallet provides the `PeriodicSessions` struct for simple periodic sessions.
51-
//! - **Session rotation configuration:** Configure as either a 'normal' (rewardable session where rewards are
52-
//! applied) or 'exceptional' (slashable) session rotation.
52+
//! Rather, it relies on an implementation of `ShouldEndSession` to dictate a new session's start.
53+
//! This pallet provides the `PeriodicSessions` struct for simple periodic sessions.
54+
//!
55+
//! - **Session rotation configuration:** Configure as either a 'normal' (rewardable session where
56+
//! rewards are applied) or 'exceptional' (slashable) session rotation.
57+
//!
5358
//! - **Session rotation process:** At the beginning of each block, the `on_initialize` function
54-
//! queries the provided implementation of `ShouldEndSession`. If the session is to end the newly
55-
//! activated validator IDs and session keys are taken from storage and passed to the
56-
//! `SessionHandler`. The validator set supplied by `SessionManager::new_session` and the corresponding session
57-
//! keys, which may have been registered via `set_keys` during the previous session, are written
58-
//! to storage where they will wait one session before being passed to the `SessionHandler`
59-
//! themselves.
59+
//! queries the provided implementation of `ShouldEndSession`. If the session is to end the newly
60+
//! activated validator IDs and session keys are taken from storage and passed to the
61+
//! `SessionHandler`. The validator set supplied by `SessionManager::new_session` and the
62+
//! corresponding session keys, which may have been registered via `set_keys` during the previous
63+
//! session, are written to storage where they will wait one session before being passed to the
64+
//! `SessionHandler` themselves.
6065
//!
6166
//! ### Goals
6267
//!
@@ -75,21 +80,22 @@
7580
//! ### Public Functions
7681
//!
7782
//! - `rotate_session` - Change to the next session. Register the new authority set. Queue changes
78-
//! for next session rotation.
83+
//! for next session rotation.
7984
//! - `disable_index` - Disable a validator by index.
8085
//! - `disable` - Disable a validator by Validator ID
8186
//!
8287
//! ## Usage
8388
//!
8489
//! ### Example from the FRAME
8590
//!
86-
//! The [Staking pallet](../pallet_staking/index.html) uses the Session pallet to get the validator set.
91+
//! The [Staking pallet](../pallet_staking/index.html) uses the Session pallet to get the validator
92+
//! set.
8793
//!
8894
//! ```
8995
//! use pallet_session as session;
9096
//!
9197
//! fn validators<T: pallet_session::Config>() -> Vec<<T as pallet_session::Config>::ValidatorId> {
92-
//! <pallet_session::Module<T>>::validators()
98+
//! <pallet_session::Module<T>>::validators()
9399
//! }
94100
//! # fn main(){}
95101
//! ```
@@ -166,18 +172,18 @@ impl<
166172
period.saturating_sub(block_after_last_session)
167173
)
168174
} else {
169-
Zero::zero()
175+
now
170176
}
171177
} else {
172178
offset
173179
})
174180
}
175181

176182
fn weight(_now: BlockNumber) -> Weight {
177-
// Weight note: `estimate_next_session_rotation` has no storage reads and trivial computational overhead.
178-
// There should be no risk to the chain having this weight value be zero for now.
179-
// However, this value of zero was not properly calculated, and so it would be reasonable
180-
// to come back here and properly calculate the weight of this function.
183+
// Weight note: `estimate_next_session_rotation` has no storage reads and trivial
184+
// computational overhead. There should be no risk to the chain having this weight value be
185+
// zero for now. However, this value of zero was not properly calculated, and so it would be
186+
// reasonable to come back here and properly calculate the weight of this function.
181187
0
182188
}
183189
}
@@ -186,17 +192,17 @@ impl<
186192
pub trait SessionManager<ValidatorId> {
187193
/// Plan a new session, and optionally provide the new validator set.
188194
///
189-
/// Even if the validator-set is the same as before, if any underlying economic
190-
/// conditions have changed (i.e. stake-weights), the new validator set must be returned.
191-
/// This is necessary for consensus engines making use of the session module to
192-
/// issue a validator-set change so misbehavior can be provably associated with the new
193-
/// economic conditions as opposed to the old.
194-
/// The returned validator set, if any, will not be applied until `new_index`.
195-
/// `new_index` is strictly greater than from previous call.
195+
/// Even if the validator-set is the same as before, if any underlying economic conditions have
196+
/// changed (i.e. stake-weights), the new validator set must be returned. This is necessary for
197+
/// consensus engines making use of the session module to issue a validator-set change so
198+
/// misbehavior can be provably associated with the new economic conditions as opposed to the
199+
/// old. The returned validator set, if any, will not be applied until `new_index`. `new_index`
200+
/// is strictly greater than from previous call.
196201
///
197202
/// The first session start at index 0.
198203
///
199-
/// `new_session(session)` is guaranteed to be called before `end_session(session-1)`.
204+
/// `new_session(session)` is guaranteed to be called before `end_session(session-1)`. In other
205+
/// words, a new session must always be planned before an ongoing one can be finished.
200206
fn new_session(new_index: SessionIndex) -> Option<Vec<ValidatorId>>;
201207
/// End the session.
202208
///
@@ -205,7 +211,7 @@ pub trait SessionManager<ValidatorId> {
205211
fn end_session(end_index: SessionIndex);
206212
/// Start the session.
207213
///
208-
/// The session start to be used for validation
214+
/// The session start to be used for validation.
209215
fn start_session(start_index: SessionIndex);
210216
}
211217

@@ -242,7 +248,7 @@ pub trait SessionHandler<ValidatorId> {
242248

243249
/// A notification for end of the session.
244250
///
245-
/// Note it is triggered before any `SessionManager::end_session` handlers,
251+
/// Note it is triggered before any [`SessionManager::end_session`] handlers,
246252
/// so we can still affect the validator set.
247253
fn on_before_session_ending() {}
248254

src/tests.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,31 +252,32 @@ fn session_changed_flag_works() {
252252

253253
#[test]
254254
fn periodic_session_works() {
255-
struct Period;
256-
struct Offset;
257255

258-
impl Get<u64> for Period {
259-
fn get() -> u64 { 10 }
256+
frame_support::parameter_types! {
257+
const Period: u64 = 10;
258+
const Offset: u64 = 3;
260259
}
261260

262-
impl Get<u64> for Offset {
263-
fn get() -> u64 { 3 }
264-
}
265-
266-
267261
type P = PeriodicSessions<Period, Offset>;
268262

269-
for i in 0..3 {
263+
for i in 0u64..3 {
270264
assert!(!P::should_end_session(i));
265+
assert_eq!(P::estimate_next_session_rotation(i).unwrap(), 3);
271266
}
272267

273-
assert!(P::should_end_session(3));
268+
assert!(P::should_end_session(3u64));
269+
assert_eq!(P::estimate_next_session_rotation(3u64).unwrap(), 3);
274270

275-
for i in (1..10).map(|i| 3 + i) {
271+
for i in (1u64..10).map(|i| 3 + i) {
276272
assert!(!P::should_end_session(i));
273+
assert_eq!(P::estimate_next_session_rotation(i).unwrap(), 13);
277274
}
278275

279-
assert!(P::should_end_session(13));
276+
assert!(P::should_end_session(13u64));
277+
assert_eq!(P::estimate_next_session_rotation(13u64).unwrap(), 13);
278+
279+
assert!(!P::should_end_session(14u64));
280+
assert_eq!(P::estimate_next_session_rotation(14u64).unwrap(), 23);
280281
}
281282

282283
#[test]

0 commit comments

Comments
 (0)