Skip to content

Commit

Permalink
Mesh 2149/staking update part2 (#1678)
Browse files Browse the repository at this point in the history
* Update staking to match monthly-2023-03 release + cargo fmt

* Set new associated types to Staking; Add multi-phase election to the runtime

* Add IdentityConfig bound to Staking; Fetch did for bounded event

* Add most polymesh specific changes

* Fix test compilation

* Add cargo lock

* Add inflation logic; Add polymesh specific changes to test builder

* Run all tests

* Add polymesh changes to benchmarks

* Fix compilation warnings; Add EraPayout to runtime

* Add slashing check; Add scheduler

* Add associated types; Fix tests

* Add missing benchmarks; Add missing tests; Add unbond_balance impl

* Change polymesh version

* Remove old staking function

* Remove multi_phase benchmark run

* Add commission cap; Add validate unsigned to multiphase election

* Add validator_comission_cap

* Add new reap_stash logic; Add max_validators check

* Add migrations

* Add missing benchmarks; Add unbond test
  • Loading branch information
HenriqueNogara authored Aug 16, 2024
1 parent e568106 commit e2a4739
Show file tree
Hide file tree
Showing 44 changed files with 11,687 additions and 9,825 deletions.
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pallet-babe = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", bra
pallet-balances = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-contracts = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-contracts-primitives = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-election-provider-multi-phase = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-grandpa = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-im-online = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
pallet-indices = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
Expand Down Expand Up @@ -102,6 +103,7 @@ substrate-prometheus-endpoint = { git = "https://github.com/PolymeshAssociation/
substrate-build-script-utils = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
substrate-wasm-builder = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
substrate-test-client = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }
substrate-test-utils = { git = "https://github.com/PolymeshAssociation/polkadot-sdk", branch = "polymesh-v6-monthly-2023-03" }

[workspace]
members = [
Expand Down Expand Up @@ -223,6 +225,7 @@ pallet-authorship = { version = "4.0.0-dev", default-features = false }
pallet-babe = { version = "4.0.0-dev", default-features = false }
pallet-contracts = { version = "4.0.0-dev", default-features = false }
pallet-contracts-primitives = { version = "7.0.0", default-features = false}
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false }
pallet-executive = { package = "frame-executive", version = "4.0.0-dev", default-features = false }
pallet-grandpa = { version = "4.0.0-dev", default-features = false }
pallet-im-online = { version = "4.0.0-dev", default-features = false }
Expand Down
13 changes: 10 additions & 3 deletions pallets/runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ smallvec = "1.4.0"

# Substrate
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "5.0.0", default-features = false }
sp-runtime = { version = "7.0.0", default-features = false }
frame-election-provider-support = { version = "4.0.0-dev", default-features = false }
frame-support = { version = "4.0.0-dev", default-features = false }
frame-system = { version = "4.0.0-dev", default-features = false }

pallet-authorship = { version = "4.0.0-dev", default-features = false }
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false }
sp-io = { version = "7.0.0", default-features = false }
sp-std = { version = "5.0.0", default-features = false }
sp-runtime = { version = "7.0.0", default-features = false }

[features]
equalize = []
Expand All @@ -48,6 +50,9 @@ std = [
"polymesh-primitives/std",
"sp-runtime/std",
"sp-std/std",
"frame-election-provider-support/std",
"pallet-election-provider-multi-phase/std",
"sp-io/std"
]
runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
Expand All @@ -61,4 +66,6 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
]
59 changes: 57 additions & 2 deletions pallets/runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
//! Some configurable implementations as associated type for the substrate runtime.
//! Auxillary struct/enums
use crate::NegativeImbalance;
use frame_election_provider_support::BalancingConfig;
use frame_support::traits::{Currency, OnUnbalanced};
use frame_system as system;
use sp_runtime::traits::Convert;

use pallet_authorship as authorship;
use pallet_balances as balances;
use polymesh_primitives::Balance;
use sp_runtime::traits::Convert;

use crate::NegativeImbalance;

pub struct Author<R>(sp_std::marker::PhantomData<R>);

Expand Down Expand Up @@ -87,3 +90,55 @@ where
x * Self::factor()
}
}

pub struct EstimateCallFeeMax;

impl<Call, Balance: From<u32>> frame_support::traits::EstimateCallFee<Call, Balance>
for EstimateCallFeeMax
{
fn estimate_call_fee(_: &Call, _: frame_support::dispatch::PostDispatchInfo) -> Balance {
u32::MAX.into()
}
}

/// A source of random balance for NposSolver, which is meant to be run by the OCW election miner.
pub struct OffchainRandomBalancing;

impl frame_support::pallet_prelude::Get<Option<BalancingConfig>> for OffchainRandomBalancing {
fn get() -> Option<BalancingConfig> {
use codec::Decode;
use sp_runtime::traits::TrailingZeroInput;

let iterations = match crate::MINER_MAX_ITERATIONS {
0 => 0,
max => {
let seed = sp_io::offchain::random_seed();
let random = <u32>::decode(&mut TrailingZeroInput::new(&seed))
.expect("input is padded with zeroes; qed")
% max.saturating_add(1);
random as usize
}
};

let config = BalancingConfig {
iterations,
tolerance: 0,
};
Some(config)
}
}

/// The numbers configured here could always be more than the the maximum limits of staking pallet
/// to ensure election snapshot will not run out of memory. For now, we set them to smaller values
/// since the staking is bounded and the weight pipeline takes hours for this single pallet.
pub struct ElectionProviderBenchmarkConfig;

impl pallet_election_provider_multi_phase::BenchmarkingConfig for ElectionProviderBenchmarkConfig {
const VOTERS: [u32; 2] = [1000, 2000];
const TARGETS: [u32; 2] = [500, 1000];
const ACTIVE_VOTERS: [u32; 2] = [500, 800];
const DESIRED_TARGETS: [u32; 2] = [200, 400];
const SNAPSHOT_MAXIMUM_VOTERS: u32 = 1000;
const MINER_MAXIMUM_VOTERS: u32 = 1000;
const MAXIMUM_TARGETS: u32 = 300;
}
62 changes: 58 additions & 4 deletions pallets/runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub mod fee_details;
pub mod impls;
pub mod runtime;

pub use cdd_check::CddChecker;
pub use sp_runtime::{Perbill, Permill};

pub use frame_support::{
dispatch::{DispatchClass, GetDispatchInfo, Weight},
parameter_types,
Expand All @@ -38,11 +35,15 @@ pub use frame_support::{
},
};
use frame_system::limits::{BlockLength, BlockWeights};
use smallvec::smallvec;
pub use sp_runtime::transaction_validity::TransactionPriority;
pub use sp_runtime::{Perbill, Permill};

use pallet_balances as balances;
use polymesh_common_utilities::constants::currency::*;
use polymesh_primitives::{Balance, BlockNumber, IdentityId, Moment};
use smallvec::smallvec;

pub use cdd_check::CddChecker;
pub use impls::{Author, CurrencyToVoteHandler};

pub type NegativeImbalance<T> =
Expand All @@ -62,6 +63,10 @@ const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2)).set_proof_size(u64::MAX);

/// Maximum number of iterations for balancing that will be executed in the embedded OCW
/// miner of election provider multi phase.
const MINER_MAX_ITERATIONS: u32 = 10;

// TODO (miguel) Remove unused constants.
parameter_types! {
pub const BlockHashCount: BlockNumber = 4096;
Expand Down Expand Up @@ -137,10 +142,59 @@ parameter_types! {
.get(DispatchClass::Normal)
.max_extrinsic.expect("Normal extrinsics have a weight limit configured; qed")
.saturating_sub(BlockExecutionWeight::get());

// Staking constants
pub MaxNominations: u32 = 16;
pub HistoryDepth:u32 = 84;
pub MaxUnlockingChunks: u32 = 32;
pub MaxValidatorPerIdentity: Permill = Permill::from_percent(33);

// Multi-phase election parameters
// Signed phase
pub const SignedPhase: u32 = 0;
pub const SignedPhaseBench: u32 = 1_200;
pub const SignedMaxSubmissions: u32 = 0;
pub const SignedMaxWeight: Weight = Weight::zero();
pub const SignedMaxRefunds: u32 = 0;
pub const SignedRewardBase: Balance = 0;
pub const SignedDepositBase: Balance = 0;
pub const SignedDepositByte: Balance = 0;
// Unsigned phase
pub const UnsignedPhase: u32 = 1_200;
pub BetterUnsignedThreshold: Perbill = Perbill::from_rational(1u32, 10_000);
pub const MultiPhaseUnsignedPriority: TransactionPriority = TransactionPriority::max_value() / 2 - 1u64;
// Fallback parameters
pub MaxOnChainElectingVoters: u32 = 5000;
pub MaxOnChainElectableTargets: u16 = 1250;
// Other config parameters
pub OffChainRepeat: BlockNumber = 5;
pub MaxElectingVoters: u32 = 40_000;
pub MaxElectableTargets: u16 = 10_000;
pub MaxActiveValidators: u32 = 1_000;
// Miner Config parameters
pub MinerMaxLength: u32 = Perbill::from_rational(9u32, 10) *
*RuntimeBlockLength::get()
.max
.get(DispatchClass::Normal);
pub MinerMaxWeight: Weight = RuntimeBlockWeights::get()
.get(DispatchClass::Normal)
.max_extrinsic.expect("Normal extrinsics have a weight limit configured; qed")
.saturating_sub(BlockExecutionWeight::get());
}

frame_election_provider_support::generate_solution_type!(
#[compact]
pub struct NposSolution16::<
VoterIndex = u32,
TargetIndex = u16,
Accuracy = sp_runtime::PerU16,
MaxVoters = MaxElectingVoters,
>(16)
);

/// Converts Weight to Fee
pub struct WeightToFee;

impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
/// We want a 0.03 POLYX fee per ExtrinsicBaseWeight.
Expand Down
Loading

0 comments on commit e2a4739

Please sign in to comment.