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

Backport Staking Fixes for Polkadot v0.8.31 #9135

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
898d78a
[pallet-staking] Refund unused weight for `payout_stakers` (#8458)
emostov Mar 28, 2021
13fd30f
Bring back the on_finalize weight of staking. (#8463)
kianenigma Mar 29, 2021
1e9cc0c
helper macro to create storage types on the fly (#8456)
kianenigma Mar 30, 2021
600c1f2
Remove Offence delay (#8414)
Lohann May 3, 2021
0168dcb
Don't inlucde nominaotrs that back no one in the snapshot. (#9017)
kianenigma Jun 7, 2021
87f920c
Emit `Bonded` event when rebonding (#9040)
shawntabrizi Jun 8, 2021
64c0634
Migrate pallet-staking to pallet attribute macro (#9083)
shaunxw Jun 12, 2021
4f94bc4
fix ordering of staking weight arguments (#9063)
coriolinus Jun 14, 2021
2bb009a
staking/election: prolonged era and emergency mode for governance sub…
gui1117 Jun 14, 2021
c7ca5cb
improve variable name (#9108)
gui1117 Jun 14, 2021
c22d612
Add Control to Growth of the Staking Pallet (#8920)
shawntabrizi Jun 16, 2021
83c1cf9
Make backwards compatible with CountedMap (#9126)
shawntabrizi Jun 16, 2021
d84ccaa
backport saturating helpers
shawntabrizi Jun 17, 2021
30e2ca8
Update Cargo.lock
shawntabrizi Jun 17, 2021
b1f644a
make pub
shawntabrizi Jun 17, 2021
b07cb42
fix compiler warning
shawntabrizi Jun 17, 2021
9019ec0
update environmental (#8789)
NikVolf May 12, 2021
4b7f898
another compiler warning
shawntabrizi Jun 17, 2021
765da16
Fix Compiler Warnings (new rustc) (#8907)
shawntabrizi May 27, 2021
2530e2a
fix more compiler warnings
shawntabrizi Jun 17, 2021
e3c9e36
update ui test
shawntabrizi Jun 17, 2021
7011a9a
fix another ui test
shawntabrizi Jun 17, 2021
6523dfd
fix more ui tests
shawntabrizi Jun 17, 2021
87fbc35
fix build (#9136)
niklasad1 Jun 17, 2021
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
536 changes: 473 additions & 63 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../.
frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" }
frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" }
frame-system-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true }
frame-election-provider-support = { version = "3.0.0", default-features = false, path = "../../../frame/election-provider-support" }
frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
frame-try-runtime = { version = "0.9.0", default-features = false, path = "../../../frame/try-runtime", optional = true }
pallet-assets = { version = "3.0.0", default-features = false, path = "../../../frame/assets" }
Expand Down
11 changes: 4 additions & 7 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ parameter_types! {
pub const MaxNominatorRewardedPerValidator: u32 = 256;
}

use frame_election_provider_support::onchain;
impl pallet_staking::Config for Runtime {
const MAX_NOMINATIONS: u32 =
<NposCompactSolution16 as sp_npos_elections::CompactSolution>::LIMIT as u32;
Expand All @@ -494,6 +495,8 @@ impl pallet_staking::Config for Runtime {
type NextNewSession = Session;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider =
onchain::OnChainSequentialPhragmen<pallet_election_provider_multi_phase::OnChainConfig<Self>>;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
}

Expand All @@ -504,7 +507,7 @@ parameter_types! {

// fallback: no need to do on-chain phragmen initially.
pub const Fallback: pallet_election_provider_multi_phase::FallbackStrategy =
pallet_election_provider_multi_phase::FallbackStrategy::OnChain;
pallet_election_provider_multi_phase::FallbackStrategy::Nothing;

pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(1u32, 10_000);

Expand Down Expand Up @@ -878,16 +881,10 @@ impl pallet_im_online::Config for Runtime {
type WeightInfo = pallet_im_online::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) *
RuntimeBlockWeights::get().max_block;
}

impl pallet_offences::Config for Runtime {
type Event = Event;
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = Staking;
type WeightSoftLimit = OffencesWeightSoftLimit;
}

impl pallet_authority_discovery::Config for Runtime {}
Expand Down
10 changes: 2 additions & 8 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use sp_runtime::{
use frame_system::InitKind;
use frame_support::{
parameter_types,
traits::{KeyOwnerProofSystem, OnInitialize},
weights::Weight,
traits::{KeyOwnerProofSystem, OnInitialize, GenesisBuild},
};
use sp_io;
use sp_core::{H256, U256, crypto::{IsWrappedBy, KeyTypeId, Pair}};
Expand Down Expand Up @@ -210,19 +209,14 @@ impl pallet_staking::Config for Test {
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type NextNewSession = Session;
type ElectionProvider = onchain::OnChainSequentialPhragmen<Self>;
type GenesisElectionProvider = Self::ElectionProvider;
type WeightInfo = ();
}

parameter_types! {
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60)
* BlockWeights::get().max_block;
}

impl pallet_offences::Config for Test {
type Event = Event;
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = Staking;
type WeightSoftLimit = OffencesWeightSoftLimit;
}

parameter_types! {
Expand Down
18 changes: 9 additions & 9 deletions frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub type CompactAccuracyOf<T> = <CompactOf<T> as CompactSolution>::Accuracy;
pub type OnChainAccuracyOf<T> = <T as Config>::OnChainAccuracy;

/// Wrapper type that implements the configurations needed for the on-chain backup.
struct OnChainConfig<T: Config>(sp_std::marker::PhantomData<T>);
pub struct OnChainConfig<T: Config>(sp_std::marker::PhantomData<T>);
impl<T: Config> onchain::Config for OnChainConfig<T> {
type AccountId = T::AccountId;
type BlockNumber = T::BlockNumber;
Expand Down Expand Up @@ -381,11 +381,11 @@ impl Default for ElectionCompute {
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct RawSolution<C> {
/// Compact election edges.
compact: C,
pub compact: C,
/// The _claimed_ score of the solution.
score: ElectionScore,
pub score: ElectionScore,
/// The round at which this solution should be submitted.
round: u32,
pub round: u32,
}

impl<C: Default> Default for RawSolution<C> {
Expand All @@ -402,13 +402,13 @@ pub struct ReadySolution<A> {
///
/// This is target-major vector, storing each winners, total backing, and each individual
/// backer.
supports: Supports<A>,
pub supports: Supports<A>,
/// The score of the solution.
///
/// This is needed to potentially challenge the solution.
score: ElectionScore,
pub score: ElectionScore,
/// How this election was computed.
compute: ElectionCompute,
pub compute: ElectionCompute,
}

/// A snapshot of all the data that is needed for en entire round. They are provided by
Expand All @@ -432,10 +432,10 @@ pub struct RoundSnapshot<A> {
pub struct SolutionOrSnapshotSize {
/// The length of voters.
#[codec(compact)]
voters: u32,
pub voters: u32,
/// The length of targets.
#[codec(compact)]
targets: u32,
pub targets: u32,
}

/// Internal errors of the pallet.
Expand Down
16 changes: 12 additions & 4 deletions frame/election-provider-multi-phase/src/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,16 @@ impl<T: Config> Pallet<T> {
size,
T::MinerMaxWeight::get(),
);

log!(
debug,
"miner: current compact solution voters = {}, maximum_allowed = {}",
"initial solution voters = {}, snapshot = {:?}, maximum_allowed(capped) = {}",
compact.voter_count(),
size,
maximum_allowed_voters,
);

// trim weight.
let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?;

// re-calc score.
Expand Down Expand Up @@ -252,10 +256,12 @@ impl<T: Config> Pallet<T> {
}
}

log!(debug, "removed {} voter to meet the max weight limit.", to_remove);
Ok(compact)
}
_ => {
// nada, return as-is
log!(debug, "didn't remove any voter for weight limits.");
Ok(compact)
}
}
Expand Down Expand Up @@ -298,6 +304,7 @@ impl<T: Config> Pallet<T> {
// First binary-search the right amount of voters
let mut step = voters / 2;
let mut current_weight = weight_with(voters);

while step > 0 {
match next_voters(current_weight, voters, step) {
// proceed with the binary search
Expand All @@ -324,13 +331,14 @@ impl<T: Config> Pallet<T> {
voters -= 1;
}

let final_decision = voters.min(size.voters);
debug_assert!(
weight_with(voters.min(size.voters)) <= max_weight,
weight_with(final_decision) <= max_weight,
"weight_with({}) <= {}",
voters.min(size.voters),
final_decision,
max_weight,
);
voters.min(size.voters)
final_decision
}

/// Checks if an execution of the offchain worker is permitted at the given block number, or
Expand Down
1 change: 0 additions & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,6 @@ mod tests {
}

fn has_lock(who: &u64) -> u64 {
dbg!(Balances::locks(who));
Balances::locks(who)
.get(0)
.cloned()
Expand Down
48 changes: 15 additions & 33 deletions frame/elections-phragmen/src/migrations_3_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use codec::{Encode, Decode, FullCodec};
use sp_std::prelude::*;
use frame_support::{
RuntimeDebug, weights::Weight, Twox64Concat,
storage::types::{StorageMap, StorageValue},
traits::{GetPalletVersion, PalletVersion},
};

Expand Down Expand Up @@ -51,38 +50,21 @@ pub trait V2ToV3 {
type Balance: 'static + FullCodec + Copy;
}

struct __Candidates;
impl frame_support::traits::StorageInstance for __Candidates {
fn pallet_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Candidates";
}

#[allow(type_alias_bounds)]
type Candidates<T: V2ToV3> = StorageValue<__Candidates, Vec<(T::AccountId, T::Balance)>>;

struct __Members;
impl frame_support::traits::StorageInstance for __Members {
fn pallet_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Members";
}
#[allow(type_alias_bounds)]
type Members<T: V2ToV3> = StorageValue<__Members, Vec<SeatHolder<T::AccountId, T::Balance>>>;

struct __RunnersUp;
impl frame_support::traits::StorageInstance for __RunnersUp {
fn pallet_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "RunnersUp";
}
#[allow(type_alias_bounds)]
type RunnersUp<T: V2ToV3> = StorageValue<__RunnersUp, Vec<SeatHolder<T::AccountId, T::Balance>>>;

struct __Voting;
impl frame_support::traits::StorageInstance for __Voting {
fn pallet_prefix() -> &'static str { "PhragmenElection" }
const STORAGE_PREFIX: &'static str = "Voting";
}
#[allow(type_alias_bounds)]
type Voting<T: V2ToV3> = StorageMap<__Voting, Twox64Concat, T::AccountId, Voter<T::AccountId, T::Balance>>;
frame_support::generate_storage_alias!(
PhragmenElection, Candidates<T: V2ToV3> => Value<Vec<(T::AccountId, T::Balance)>>
);
frame_support::generate_storage_alias!(
PhragmenElection, Members<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
);
frame_support::generate_storage_alias!(
PhragmenElection, RunnersUp<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
);
frame_support::generate_storage_alias!(
PhragmenElection, Voting<T: V2ToV3> => Map<
(Twox64Concat, T::AccountId),
Voter<T::AccountId, T::Balance>
>
);

/// Apply all of the migrations from 2_0_0 to 3_0_0.
///
Expand Down
9 changes: 2 additions & 7 deletions frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use ::grandpa as finality_grandpa;
use codec::Encode;
use frame_support::{
parameter_types,
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize},
weights::Weight,
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize, GenesisBuild},
};
use pallet_staking::EraIndex;
use sp_core::{crypto::KeyTypeId, H256};
Expand Down Expand Up @@ -216,18 +215,14 @@ impl pallet_staking::Config for Test {
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type NextNewSession = Session;
type ElectionProvider = onchain::OnChainSequentialPhragmen<Self>;
type GenesisElectionProvider = Self::ElectionProvider;
type WeightInfo = ();
}

parameter_types! {
pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * BlockWeights::get().max_block;
}

impl pallet_offences::Config for Test {
type Event = Event;
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = Staking;
type WeightSoftLimit = OffencesWeightSoftLimit;
}

parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ decl_storage! {
decl_module! {
/// A public part of the pallet.
pub struct Module<T: Config<I>, I: Instance = DefaultInstance> for enum Call where origin: T::Origin {
fn on_initialize(n: T::BlockNumber) -> Weight {
fn on_initialize(_n: T::BlockNumber) -> Weight {
use primitives::LeafDataProvider;
let leaves = Self::mmr_leaves();
let peaks_before = mmr::utils::NodesUtils::new(leaves).number_of_peaks();
Expand Down
Loading