remove the uselsss weight return type from election provider API (#9569)

* remove the uselsss weight return type from election provider API

* fix everything, should be ready for final benchmark

* simplify on_init a bit furhter

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* remove unwraps

* fmt

* Update lock file

* whitelist block weight

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* fix warning

Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
This commit is contained in:
Kian Paimani
2021-08-19 08:45:54 +01:00
committed by GitHub
parent 354b77fb88
commit 7a32c3504d
22 changed files with 552 additions and 556 deletions
@@ -80,7 +80,6 @@
//! ```rust
//! # use frame_election_provider_support::{*, data_provider};
//! # use sp_npos_elections::{Support, Assignment};
//! # use frame_support::weights::Weight;
//!
//! type AccountId = u64;
//! type Balance = u64;
@@ -101,16 +100,16 @@
//!
//! impl<T: Config> ElectionDataProvider<AccountId, BlockNumber> for Module<T> {
//! const MAXIMUM_VOTES_PER_VOTER: u32 = 1;
//! fn desired_targets() -> data_provider::Result<(u32, Weight)> {
//! Ok((1, 0))
//! fn desired_targets() -> data_provider::Result<u32> {
//! Ok(1)
//! }
//! fn voters(maybe_max_len: Option<usize>)
//! -> data_provider::Result<(Vec<(AccountId, VoteWeight, Vec<AccountId>)>, Weight)>
//! -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>>
//! {
//! Ok((Default::default(), 0))
//! Ok(Default::default())
//! }
//! fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)> {
//! Ok((vec![10, 20, 30], 0))
//! fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
//! Ok(vec![10, 20, 30])
//! }
//! fn next_election_prediction(now: BlockNumber) -> BlockNumber {
//! 0
@@ -132,12 +131,10 @@
//! type Error = &'static str;
//! type DataProvider = T::DataProvider;
//!
//! fn elect() -> Result<(Supports<AccountId>, Weight), Self::Error> {
//! fn elect() -> Result<Supports<AccountId>, Self::Error> {
//! Self::DataProvider::targets(None)
//! .map_err(|_| "failed to elect")
//! .map(|(t, weight)| {
//! (vec![(t[0], Support::default())], weight)
//! })
//! .map(|t| vec![(t[0], Support::default())])
//! }
//! }
//! }
@@ -164,7 +161,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub mod onchain;
use frame_support::weights::Weight;
use sp_std::{fmt::Debug, prelude::*};
/// Re-export some type as they are used in the interface.
@@ -189,9 +185,9 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
/// If `maybe_max_len` is `Some(v)` then the resulting vector MUST NOT be longer than `v` items
/// long.
///
/// It is assumed that this function will only consume a notable amount of weight, when it
/// returns `Ok(_)`.
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)>;
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn targets(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>>;
/// All possible voters for the election.
///
@@ -200,14 +196,17 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
/// If `maybe_max_len` is `Some(v)` then the resulting vector MUST NOT be longer than `v` items
/// long.
///
/// It is assumed that this function will only consume a notable amount of weight, when it
/// returns `Ok(_)`.
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn voters(
maybe_max_len: Option<usize>,
) -> data_provider::Result<(Vec<(AccountId, VoteWeight, Vec<AccountId>)>, Weight)>;
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>>;
/// The number of targets to elect.
fn desired_targets() -> data_provider::Result<(u32, Weight)>;
///
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn desired_targets() -> data_provider::Result<u32>;
/// Provide a best effort prediction about when the next election is about to happen.
///
@@ -249,15 +248,15 @@ pub trait ElectionDataProvider<AccountId, BlockNumber> {
#[cfg(feature = "std")]
impl<AccountId, BlockNumber> ElectionDataProvider<AccountId, BlockNumber> for () {
const MAXIMUM_VOTES_PER_VOTER: u32 = 0;
fn targets(_maybe_max_len: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)> {
fn targets(_maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
Ok(Default::default())
}
fn voters(
_maybe_max_len: Option<usize>,
) -> data_provider::Result<(Vec<(AccountId, VoteWeight, Vec<AccountId>)>, Weight)> {
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
Ok(Default::default())
}
fn desired_targets() -> data_provider::Result<(u32, Weight)> {
fn desired_targets() -> data_provider::Result<u32> {
Ok(Default::default())
}
fn next_election_prediction(now: BlockNumber) -> BlockNumber {
@@ -280,7 +279,10 @@ pub trait ElectionProvider<AccountId, BlockNumber> {
/// Elect a new set of winners.
///
/// The result is returned in a target major format, namely as vector of supports.
fn elect() -> Result<(Supports<AccountId>, Weight), Self::Error>;
///
/// This should be implemented as a self-weighing function. The implementor should register its
/// appropriate weight at the end of execution with the system pallet directly.
fn elect() -> Result<Supports<AccountId>, Self::Error>;
}
#[cfg(feature = "std")]
@@ -288,7 +290,7 @@ impl<AccountId, BlockNumber> ElectionProvider<AccountId, BlockNumber> for () {
type Error = &'static str;
type DataProvider = ();
fn elect() -> Result<(Supports<AccountId>, Weight), Self::Error> {
fn elect() -> Result<Supports<AccountId>, Self::Error> {
Err("<() as ElectionProvider> cannot do anything.")
}
}
@@ -18,7 +18,6 @@
//! An implementation of [`ElectionProvider`] that does an on-chain sequential phragmen.
use crate::{ElectionDataProvider, ElectionProvider};
use frame_support::{traits::Get, weights::Weight};
use sp_npos_elections::*;
use sp_std::{collections::btree_map::BTreeMap, marker::PhantomData, prelude::*};
@@ -55,8 +54,6 @@ pub struct OnChainSequentialPhragmen<T: Config>(PhantomData<T>);
///
/// Note that this is similar to a pallet traits, but [`OnChainSequentialPhragmen`] is not a pallet.
pub trait Config {
/// The block limits.
type BlockWeights: Get<frame_system::limits::BlockWeights>;
/// The account identifier type.
type AccountId: IdentifierT;
/// The block number type.
@@ -71,11 +68,10 @@ impl<T: Config> ElectionProvider<T::AccountId, T::BlockNumber> for OnChainSequen
type Error = Error;
type DataProvider = T::DataProvider;
fn elect() -> Result<(Supports<T::AccountId>, Weight), Self::Error> {
let (voters, _) = Self::DataProvider::voters(None).map_err(Error::DataProvider)?;
let (targets, _) = Self::DataProvider::targets(None).map_err(Error::DataProvider)?;
let (desired_targets, _) =
Self::DataProvider::desired_targets().map_err(Error::DataProvider)?;
fn elect() -> Result<Supports<T::AccountId>, Self::Error> {
let voters = Self::DataProvider::voters(None).map_err(Error::DataProvider)?;
let targets = Self::DataProvider::targets(None).map_err(Error::DataProvider)?;
let desired_targets = Self::DataProvider::desired_targets().map_err(Error::DataProvider)?;
let mut stake_map: BTreeMap<T::AccountId, VoteWeight> = BTreeMap::new();
@@ -93,16 +89,13 @@ impl<T: Config> ElectionProvider<T::AccountId, T::BlockNumber> for OnChainSequen
let staked = assignment_ratio_to_staked_normalized(assignments, &stake_of)?;
let winners = to_without_backing(winners);
to_supports(&winners, &staked)
.map_err(Error::from)
.map(|s| (s, T::BlockWeights::get().max_block))
to_supports(&winners, &staked).map_err(Error::from)
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::weights::Weight;
use sp_npos_elections::Support;
use sp_runtime::Perbill;
@@ -110,7 +103,6 @@ mod tests {
type BlockNumber = u32;
struct Runtime;
impl Config for Runtime {
type BlockWeights = ();
type AccountId = AccountId;
type BlockNumber = BlockNumber;
type Accuracy = Perbill;
@@ -124,21 +116,20 @@ mod tests {
use crate::data_provider;
pub struct DataProvider;
impl ElectionDataProvider<AccountId, BlockNumber> for DataProvider {
const MAXIMUM_VOTES_PER_VOTER: u32 = 2;
fn voters(
_: Option<usize>,
) -> data_provider::Result<(Vec<(AccountId, VoteWeight, Vec<AccountId>)>, Weight)> {
Ok((vec![(1, 10, vec![10, 20]), (2, 20, vec![30, 20]), (3, 30, vec![10, 30])], 0))
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
Ok(vec![(1, 10, vec![10, 20]), (2, 20, vec![30, 20]), (3, 30, vec![10, 30])])
}
fn targets(_: Option<usize>) -> data_provider::Result<(Vec<AccountId>, Weight)> {
Ok((vec![10, 20, 30], 0))
fn targets(_: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
Ok(vec![10, 20, 30])
}
fn desired_targets() -> data_provider::Result<(u32, Weight)> {
Ok((2, 0))
fn desired_targets() -> data_provider::Result<u32> {
Ok(2)
}
fn next_election_prediction(_: BlockNumber) -> BlockNumber {
@@ -150,7 +141,7 @@ mod tests {
#[test]
fn onchain_seq_phragmen_works() {
assert_eq!(
OnChainPhragmen::elect().unwrap().0,
OnChainPhragmen::elect().unwrap(),
vec![
(10, Support { total: 25, voters: vec![(1, 10), (3, 15)] }),
(30, Support { total: 35, voters: vec![(2, 20), (3, 15)] })