Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
247 changes: 166 additions & 81 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ members = [
"substrate/frame/core-fellowship",
"substrate/frame/delegated-staking",
"substrate/frame/democracy",
"substrate/frame/election-provider-multi-block",
"substrate/frame/election-provider-multi-phase",
"substrate/frame/election-provider-multi-block/integration-tests",
"substrate/frame/election-provider-multi-phase/test-staking-e2e",
"substrate/frame/election-provider-support",
"substrate/frame/election-provider-support/benchmarking",
Expand Down Expand Up @@ -925,6 +927,7 @@ pallet-default-config-example = { path = "substrate/frame/examples/default-confi
pallet-delegated-staking = { path = "substrate/frame/delegated-staking", default-features = false }
pallet-democracy = { path = "substrate/frame/democracy", default-features = false }
pallet-dev-mode = { path = "substrate/frame/examples/dev-mode", default-features = false }
pallet-election-provider-multi-block = { path = "substrate/frame/election-provider-multi-block", default-features = false }
pallet-election-provider-multi-phase = { path = "substrate/frame/election-provider-multi-phase", default-features = false }
pallet-election-provider-support-benchmarking = { path = "substrate/frame/election-provider-support/benchmarking", default-features = false }
pallet-elections-phragmen = { path = "substrate/frame/elections-phragmen", default-features = false }
Expand Down
25 changes: 0 additions & 25 deletions prdoc/pr_6034.prdoc

This file was deleted.

11 changes: 11 additions & 0 deletions prdoc/pr_6213.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Adds multi-block election pallet

doc:
- audience: Runtime Dev
description: |
This PR adds the `election-provider-multi-block` (EPM-MB) pallet, which is the multi-block
variant of the `election-provider-multi-phase` (EPM) pallet.

crates:
- name: pallet-election-provider-multi-block
bump: major
72 changes: 72 additions & 0 deletions substrate/frame/election-provider-multi-block/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[package]
name = "pallet-election-provider-multi-block"
version = "1.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository.workspace = true
description = "FRAME pallet election provider multi-block"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.10.0", default-features = false, features = [
"derive",
] }
log = { version = "0.4.17", default-features = false }

frame-support = { path = "../support", default-features = false }
frame-system = { path = "../system", default-features = false }
frame-benchmarking = { path = "../benchmarking", default-features = false }

sp-io = { path = "../../primitives/io", default-features = false }
sp-std = { path = "../../primitives/std", default-features = false }
sp-core = { path = "../../primitives/core", default-features = false }
sp-runtime = { path = "../../primitives/runtime", default-features = false }

frame-election-provider-support = { default-features = false, path = "../election-provider-support" }
sp-npos-elections = { default-features = false, path = "../../primitives/npos-elections" }

[dev-dependencies]
sp-tracing = { path = "../../primitives/tracing" }
pallet-balances = { path = "../balances", default-features = false }
parking_lot = "0.12.1"

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-election-provider-support/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-npos-elections/std",
"sp-runtime/std",
"sp-std/std",
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]

try-runtime = [
"frame-election-provider-support/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"sp-runtime/try-runtime",
]
1 change: 1 addition & 0 deletions substrate/frame/election-provider-multi-block/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Election Provider multi-block
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pallet-epm-mb-integrity-tests"
version = "1.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage.workspace = true
repository.workspace = true
description = "FRAME election provider multi block pallet tests with staking pallet, bags-list and session pallets"
publish = false

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dev-dependencies]
parking_lot = { workspace = true, default-features = true }
codec = { features = ["derive"], workspace = true, default-features = true }
scale-info = { features = ["derive"], workspace = true, default-features = true }
log = { workspace = true }

sp-runtime = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-std = { workspace = true, default-features = true }
sp-staking = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-npos-elections = { workspace = true }
sp-tracing = { workspace = true, default-features = true }

frame-system = { workspace = true, default-features = true }
frame-support = { workspace = true, default-features = true }
frame-election-provider-support = { workspace = true, default-features = true }

pallet-election-provider-multi-block = { workspace = true, default-features = true }
pallet-staking = { workspace = true, default-features = true }
pallet-nomination-pools = { workspace = true, default-features = true }
pallet-bags-list = { workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }
pallet-session = { workspace = true, default-features = true }

[features]
try-runtime = [
"frame-election-provider-support/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-bags-list/try-runtime",
"pallet-balances/try-runtime",
#"pallet-election-provider-multi-block/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg(test)]
mod mock;

pub(crate) const LOG_TARGET: &str = "integration-tests::epm-staking";

use mock::*;

use frame_election_provider_support::{bounds::ElectionBoundsBuilder, ElectionDataProvider};

use frame_support::{assert_ok, traits::UnixTime};

// syntactic sugar for logging.
#[macro_export]
macro_rules! log {
($level:tt, $patter:expr $(, $values:expr)* $(,)?) => {
log::$level!(
target: crate::LOG_TARGET,
concat!("🛠️ ", $patter) $(, $values)*
)
};
}

fn log_current_time() {
log!(
info,
"block: {:?}, session: {:?}, era: {:?}, EPM phase: {:?} ts: {:?}",
System::block_number(),
Session::current_index(),
Staking::current_era(),
ElectionProvider::current_phase(),
Timestamp::now()
);
}

#[test]
fn block_progression_works() {
let (mut ext, _pool_state, _) = ExtBuilder::default().build_offchainify();
ext.execute_with(|| {})
}

#[test]
fn verify_snapshot() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(Pages::get(), 3);

// manually get targets and voters from staking to see the inspect the issue with the
// DataProvider.
let bounds = ElectionBoundsBuilder::default()
.targets_count((TargetSnapshotPerBlock::get() as u32).into())
.voters_count((VoterSnapshotPerBlock::get() as u32).into())
.build();

assert_ok!(<Staking as ElectionDataProvider>::electable_targets(bounds.targets, 2));
assert_ok!(<Staking as ElectionDataProvider>::electing_voters(bounds.voters, 2));
})
}

mod staking_integration {
use super::*;
use pallet_election_provider_multi_block::Phase;

#[test]
fn call_elect_multi_block() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(Pages::get(), 3);
assert_eq!(ElectionProvider::current_round(), 0);
assert_eq!(Staking::current_era(), Some(0));

let export_starts_at = election_prediction() - Pages::get();

assert!(Staking::election_data_lock().is_none());

// check that the election data provider lock is set during the snapshot phase and
// released afterwards.
roll_to_phase(Phase::Snapshot(Pages::get() - 1), false);
assert!(Staking::election_data_lock().is_some());

roll_one(None, false);
assert!(Staking::election_data_lock().is_some());
roll_one(None, false);
assert!(Staking::election_data_lock().is_some());
// snapshot phase done, election data lock was released.
roll_one(None, false);
assert_eq!(ElectionProvider::current_phase(), Phase::Signed);
assert!(Staking::election_data_lock().is_none());

// last block where phase is waiting for unsignned submissions.
roll_to(election_prediction() - 4, false);
assert_eq!(ElectionProvider::current_phase(), Phase::Unsigned(17));

// staking prepares first page of exposures.
roll_to(export_starts_at, false);
assert_eq!(ElectionProvider::current_phase(), Phase::Export(export_starts_at));

// staking prepares second page of exposures.
roll_to(election_prediction() - 2, false);
assert_eq!(ElectionProvider::current_phase(), Phase::Export(export_starts_at));

// staking prepares third page of exposures.
roll_to(election_prediction() - 1, false);

// election successfully, round & era progressed.
assert_eq!(ElectionProvider::current_phase(), Phase::Off);
assert_eq!(ElectionProvider::current_round(), 1);
assert_eq!(Staking::current_era(), Some(1));
})
}
}
Loading