Skip to content

Commit 2098911

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 <[email protected]> * Fix all tests with session length 5. * Test for active != current * Better doc * Update frame/staking/src/lib.rs Co-authored-by: Guillaume Thiolliere <[email protected]> * 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 <[email protected]>
1 parent 27b9822 commit 2098911

File tree

3 files changed

+610
-367
lines changed

3 files changed

+610
-367
lines changed

src/lib.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,14 @@ decl_storage! {
952952

953953
/// The active era information, it holds index and start.
954954
///
955-
/// The active era is the era currently rewarded.
956-
/// Validator set of this era must be equal to `SessionInterface::validators`.
955+
/// The active era is the era being currently rewarded. Validator set of this era must be
956+
/// equal to [`SessionInterface::validators`].
957957
pub ActiveEra get(fn active_era): Option<ActiveEraInfo>;
958958

959959
/// The session index at which the era start for the last `HISTORY_DEPTH` eras.
960+
///
961+
/// Note: This tracks the starting session (i.e. session index when era start being active)
962+
/// for the eras in `[CurrentEra - HISTORY_DEPTH, CurrentEra]`.
960963
pub ErasStartSessionIndex get(fn eras_start_session_index):
961964
map hasher(twox_64_concat) EraIndex => Option<SessionIndex>;
962965

@@ -2630,14 +2633,17 @@ impl<T: Config> Module<T> {
26302633
/// Start a session potentially starting an era.
26312634
fn start_session(start_session: SessionIndex) {
26322635
let next_active_era = Self::active_era().map(|e| e.index + 1).unwrap_or(0);
2636+
// This is only `Some` when current era has already progressed to the next era, while the
2637+
// active era is one behind (i.e. in the *last session of the active era*, or *first session
2638+
// of the new current era*, depending on how you look at it).
26332639
if let Some(next_active_era_start_session_index) =
26342640
Self::eras_start_session_index(next_active_era)
26352641
{
26362642
if next_active_era_start_session_index == start_session {
26372643
Self::start_era(start_session);
26382644
} else if next_active_era_start_session_index < start_session {
2639-
// This arm should never happen, but better handle it than to stall the
2640-
// staking pallet.
2645+
// This arm should never happen, but better handle it than to stall the staking
2646+
// pallet.
26412647
frame_support::print("Warning: A session appears to have been skipped.");
26422648
Self::start_era(start_session);
26432649
}
@@ -2893,9 +2899,11 @@ impl<T: Config> Module<T> {
28932899
/// Self votes are added and nominations before the most recent slashing span are ignored.
28942900
///
28952901
/// No storage item is updated.
2896-
pub fn do_phragmen<Accuracy: PerThing>(iterations: usize)
2897-
-> Option<PrimitiveElectionResult<T::AccountId, Accuracy>>
2898-
where ExtendedBalance: From<InnerOf<Accuracy>>
2902+
pub fn do_phragmen<Accuracy: PerThing>(
2903+
iterations: usize,
2904+
) -> Option<PrimitiveElectionResult<T::AccountId, Accuracy>>
2905+
where
2906+
ExtendedBalance: From<InnerOf<Accuracy>>,
28992907
{
29002908
let weight_of = Self::slashable_balance_of_fn();
29012909
let mut all_nominators: Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)> = Vec::new();
@@ -2928,7 +2936,11 @@ impl<T: Config> Module<T> {
29282936

29292937
if all_validators.len() < Self::minimum_validator_count().max(1) as usize {
29302938
// If we don't have enough candidates, nothing to do.
2931-
log!(error, "💸 Chain does not have enough staking candidates to operate. Era {:?}.", Self::current_era());
2939+
log!(
2940+
warn,
2941+
"💸 Chain does not have enough staking candidates to operate. Era {:?}.",
2942+
Self::current_era()
2943+
);
29322944
None
29332945
} else {
29342946
seq_phragmen::<_, Accuracy>(
@@ -3090,12 +3102,30 @@ impl<T: Config> Module<T> {
30903102
/// some session can lag in between the newest session planned and the latest session started.
30913103
impl<T: Config> pallet_session::SessionManager<T::AccountId> for Module<T> {
30923104
fn new_session(new_index: SessionIndex) -> Option<Vec<T::AccountId>> {
3105+
frame_support::debug::native::trace!(
3106+
target: LOG_TARGET,
3107+
"[{}] planning new_session({})",
3108+
<frame_system::Module<T>>::block_number(),
3109+
new_index
3110+
);
30933111
Self::new_session(new_index)
30943112
}
30953113
fn start_session(start_index: SessionIndex) {
3114+
frame_support::debug::native::trace!(
3115+
target: LOG_TARGET,
3116+
"[{}] starting start_session({})",
3117+
<frame_system::Module<T>>::block_number(),
3118+
start_index
3119+
);
30963120
Self::start_session(start_index)
30973121
}
30983122
fn end_session(end_index: SessionIndex) {
3123+
frame_support::debug::native::trace!(
3124+
target: LOG_TARGET,
3125+
"[{}] ending end_session({})",
3126+
<frame_system::Module<T>>::block_number(),
3127+
end_index
3128+
);
30993129
Self::end_session(end_index)
31003130
}
31013131
}

0 commit comments

Comments
 (0)