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

Commit 79809a6

Browse files
authored
Forcing-aware offchain Phragmén. (#5580)
* Make it force-aware * Fix merge issues
1 parent 54b6164 commit 79809a6

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

frame/staking/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ decl_storage! {
10101010
/// solutions to be submitted.
10111011
pub EraElectionStatus get(fn era_election_status): ElectionStatus<T::BlockNumber>;
10121012

1013-
/// True if the current planned session is final.
1013+
/// True if the current **planned** session is final. Note that this does not take era
1014+
/// forcing into account.
10141015
pub IsCurrentSessionFinal get(fn is_current_session_final): bool = false;
10151016

10161017
/// True if network has been upgraded to this version.
@@ -1166,7 +1167,8 @@ decl_module! {
11661167
if
11671168
// if we don't have any ongoing offchain compute.
11681169
Self::era_election_status().is_closed() &&
1169-
Self::is_current_session_final()
1170+
// either current session final based on the plan, or we're forcing.
1171+
(Self::is_current_session_final() || Self::will_era_be_forced())
11701172
{
11711173
if let Some(next_session_change) = T::NextNewSession::estimate_next_new_session(now){
11721174
if let Some(remaining) = next_session_change.checked_sub(&now) {
@@ -2895,6 +2897,13 @@ impl<T: Trait> Module<T> {
28952897
}
28962898
}
28972899

2900+
fn will_era_be_forced() -> bool {
2901+
match ForceEra::get() {
2902+
Forcing::ForceAlways | Forcing::ForceNew => true,
2903+
Forcing::ForceNone | Forcing::NotForcing => false,
2904+
}
2905+
}
2906+
28982907
#[cfg(feature = "runtime-benchmarks")]
28992908
pub fn add_era_stakers(current_era: EraIndex, controller: T::AccountId, exposure: Exposure<T::AccountId, BalanceOf<T>>) {
29002909
<ErasStakers<T>>::insert(&current_era, &controller, &exposure);

frame/staking/src/tests.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,27 @@ mod offchain_phragmen {
28722872
})
28732873
}
28742874

2875+
#[test]
2876+
fn offchain_election_flag_is_triggered_when_forcing() {
2877+
ExtBuilder::default()
2878+
.session_per_era(5)
2879+
.session_length(10)
2880+
.election_lookahead(3)
2881+
.build()
2882+
.execute_with(|| {
2883+
run_to_block(7);
2884+
assert_session_era!(0, 0);
2885+
2886+
run_to_block(12);
2887+
ForceEra::put(Forcing::ForceNew);
2888+
run_to_block(13);
2889+
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
2890+
2891+
run_to_block(17); // instead of 47
2892+
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(17));
2893+
})
2894+
}
2895+
28752896
#[test]
28762897
fn election_on_chain_fallback_works() {
28772898
ExtBuilder::default().build().execute_with(|| {

0 commit comments

Comments
 (0)