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

Commit

Permalink
Enable two phase election provider in runtimes in 'DryRun' mode (#2476)
Browse files Browse the repository at this point in the history
* Apply some changes

* Make the runtimes build

* Master.into()

* Undo branch updates.

* Undo unwatned changes

* Fix deps one last time

* Fix election lookahead

* Revert deps

* Fix :P

Co-authored-by: Bastian Köcher <info@kchr.de>
  • Loading branch information
kianenigma and bkchr authored Feb 23, 2021
1 parent 302d658 commit aaea3a2
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 144 deletions.
320 changes: 179 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substra
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-election-providers = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
8 changes: 8 additions & 0 deletions runtime/common/src/paras_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ mod tests {
pub const StakingUnsignedPriority: u64 = u64::max_value() / 2;
}

impl sp_election_providers::onchain::Config for Test {
type AccountId = <Self as frame_system::Config>::AccountId;
type BlockNumber = <Self as frame_system::Config>::BlockNumber;
type Accuracy = sp_runtime::Perbill;
type DataProvider = pallet_staking::Module<Test>;
}

impl pallet_staking::Config for Test {
type RewardRemainder = ();
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
Expand All @@ -421,6 +428,7 @@ mod tests {
type MaxIterations = ();
type MinSolutionScoreBump = ();
type OffchainSolutionWeightLimit = ();
type ElectionProvider = sp_election_providers::onchain::OnChainSequentialPhragmen<Self>;
type WeightInfo = ();
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate",
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-democracy = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-elections-phragmen = { package = "pallet-elections-phragmen", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-executive = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-identity = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -114,6 +115,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-collective/std",
"pallet-elections-phragmen/std",
"pallet-election-provider-multi-phase/std",
"pallet-democracy/std",
"frame-executive/std",
"pallet-grandpa/std",
Expand Down
39 changes: 39 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,41 @@ impl pallet_session::historical::Config for Runtime {
type FullIdentificationOf = pallet_staking::ExposureOf<Runtime>;
}

parameter_types! {
// no signed phase for now, just unsigned.
pub const SignedPhase: u32 = 0;
// NOTE: length of unsigned phase is, for now, different than `ElectionLookahead` to make sure
// that we won't run OCW threads at the same time with staking.
pub const UnsignedPhase: u32 = ElectionLookahead::get() / 2;

// fallback: no need to do on-chain phragmen while we re on a dry-run.
pub const Fallback: pallet_election_provider_multi_phase::FallbackStrategy =
pallet_election_provider_multi_phase::FallbackStrategy::Nothing;

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

// miner configs
pub MultiPhaseUnsignedPriority: TransactionPriority = StakingUnsignedPriority::get() - 1u64;
pub const MinerMaxIterations: u32 = 10;
}

impl pallet_election_provider_multi_phase::Config for Runtime {
type Event = Event;
type Currency = Balances;
type SignedPhase = SignedPhase;
type UnsignedPhase = UnsignedPhase;
type SolutionImprovementThreshold = MinSolutionScoreBump;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerTxPriority = MultiPhaseUnsignedPriority;
type DataProvider = Staking;
type OnChainAccuracy = Perbill;
type CompactSolution = pallet_staking::CompactAssignments;
type Fallback = Fallback;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = ();
}

// TODO #6469: This shouldn't be static, but a lazily cached value, not built unless needed, and
// re-built in case input parameters have changed. The `ideal_stake` should be determined by the
// amount of parachain slots being bid on: this should be around `(75 - 25.min(slots / 4))%`.
Expand Down Expand Up @@ -361,6 +396,7 @@ impl pallet_staking::Config for Runtime {
// The unsigned solution weight targeted by the OCW. We set it to the maximum possible value of
// a single extrinsic.
type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit;
type ElectionProvider = ElectionProviderMultiPhase;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
}

Expand Down Expand Up @@ -994,6 +1030,9 @@ construct_runtime! {

// Tips module.
Tips: pallet_tips::{Module, Call, Storage, Event<T>} = 36,

// Election pallet. Only works with staking, but placed here to maintain indices.
ElectionProviderMultiPhase: pallet_election_provider_multi_phase::{Module, Call, Storage, Event<T>, ValidateUnsigned} = 37,
}
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/parityt
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-democracy = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-executive = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-identity = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -112,6 +113,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-collective/std",
"pallet-elections-phragmen/std",
"pallet-election-provider-multi-phase/std",
"pallet-democracy/std",
"frame-executive/std",
"pallet-grandpa/std",
Expand Down
41 changes: 40 additions & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Filter<Call> for BaseFilter {
Call::AuthorityDiscovery(_) |
Call::Utility(_) | Call::Claims(_) | Call::Vesting(_) |
Call::Identity(_) | Call::Proxy(_) | Call::Multisig(_) |
Call::Bounties(_) | Call::Tips(_)
Call::Bounties(_) | Call::Tips(_) | Call::ElectionProviderMultiPhase(_)
=> true,
}
}
Expand Down Expand Up @@ -308,6 +308,41 @@ impl pallet_session::historical::Config for Runtime {
type FullIdentificationOf = pallet_staking::ExposureOf<Runtime>;
}

parameter_types! {
// no signed phase for now, just unsigned.
pub const SignedPhase: u32 = 0;
// NOTE: length of unsigned phase is, for now, different than `ElectionLookahead` to make sure
// that we won't run OCW threads at the same time with staking.
pub const UnsignedPhase: u32 = ElectionLookahead::get() / 2;

// fallback: no need to do on-chain phragmen while we re on a dry-run.
pub const Fallback: pallet_election_provider_multi_phase::FallbackStrategy =
pallet_election_provider_multi_phase::FallbackStrategy::Nothing;

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

// miner configs
pub MultiPhaseUnsignedPriority: TransactionPriority = StakingUnsignedPriority::get() - 1u64;
pub const MinerMaxIterations: u32 = 10;
}

impl pallet_election_provider_multi_phase::Config for Runtime {
type Event = Event;
type Currency = Balances;
type SignedPhase = SignedPhase;
type UnsignedPhase = UnsignedPhase;
type SolutionImprovementThreshold = MinSolutionScoreBump;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerTxPriority = MultiPhaseUnsignedPriority;
type DataProvider = Staking;
type OnChainAccuracy = Perbill;
type CompactSolution = pallet_staking::CompactAssignments;
type Fallback = Fallback;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = ();
}

// TODO #6469: This shouldn't be static, but a lazily cached value, not built unless needed, and
// re-built in case input parameters have changed. The `ideal_stake` should be determined by the
// amount of parachain slots being bid on: this should be around `(75 - 25.min(slots / 4))%`.
Expand Down Expand Up @@ -369,6 +404,7 @@ impl pallet_staking::Config for Runtime {
// The unsigned solution weight targeted by the OCW. We set it to the maximum possible value of
// a single extrinsic.
type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit;
type ElectionProvider = ElectionProviderMultiPhase;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
}

Expand Down Expand Up @@ -989,6 +1025,9 @@ construct_runtime! {
// Tips module.
Tips: pallet_tips::{Module, Call, Storage, Event<T>} = 35,

// Election pallet. Only works with staking, but placed here to maintain indices.
ElectionProviderMultiPhase: pallet_election_provider_multi_phase::{Module, Call, Storage, Event<T>, ValidateUnsigned} = 36,

}
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/test-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sp-staking = { git = "https://github.com/paritytech/substrate", branch = "master
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-session = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-election-providers = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
tx-pool-api = { package = "sp-transaction-pool", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
block-builder-api = { package = "sp-block-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

Expand Down Expand Up @@ -114,4 +115,5 @@ std = [
"sp-session/std",
"pallet-randomness-collective-flip/std",
"runtime-common/std",
"sp-election-providers/std",
]
8 changes: 8 additions & 0 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ parameter_types! {
pub MinSolutionScoreBump: Perbill = Perbill::from_rational_approximation(5u32, 10_000);
}

impl sp_election_providers::onchain::Config for Runtime {
type AccountId = <Self as frame_system::Config>::AccountId;
type BlockNumber = <Self as frame_system::Config>::BlockNumber;
type Accuracy = sp_runtime::Perbill;
type DataProvider = pallet_staking::Module<Self>;
}

impl pallet_staking::Config for Runtime {
type Currency = Balances;
type UnixTime = Timestamp;
Expand All @@ -333,6 +340,7 @@ impl pallet_staking::Config for Runtime {
type MaxIterations = MaxIterations;
type OffchainSolutionWeightLimit = ();
type MinSolutionScoreBump = MinSolutionScoreBump;
type ElectionProvider = sp_election_providers::onchain::OnChainSequentialPhragmen<Self>;
type WeightInfo = ();

}
Expand Down
4 changes: 3 additions & 1 deletion runtime/westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ static_assertions = "1.1.0"

authority-discovery-primitives = { package = "sp-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
inherents = { package = "sp-inherents", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
offchain-primitives = { package = "sp-offchain", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -39,6 +39,7 @@ pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/parityt
pallet-collective = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-democracy = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-elections-phragmen = { package = "pallet-elections-phragmen", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-executive = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-identity = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -113,6 +114,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-collective/std",
"pallet-elections-phragmen/std",
"pallet-election-provider-multi-phase/std",
"pallet-democracy/std",
"frame-executive/std",
"pallet-grandpa/std",
Expand Down
39 changes: 39 additions & 0 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,41 @@ impl pallet_session::historical::Config for Runtime {
type FullIdentificationOf = pallet_staking::ExposureOf<Runtime>;
}

parameter_types! {
// no signed phase for now, just unsigned.
pub const SignedPhase: u32 = 0;
// NOTE: length of unsigned phase is, for now, different than `ElectionLookahead` to make sure
// that we won't run OCW threads at the same time with staking.
pub const UnsignedPhase: u32 = ElectionLookahead::get() / 2;

// fallback: no need to do on-chain phragmen while we re on a dry-run.
pub const Fallback: pallet_election_provider_multi_phase::FallbackStrategy =
pallet_election_provider_multi_phase::FallbackStrategy::Nothing;

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

// miner configs
pub const MultiPhaseUnsignedPriority: TransactionPriority = StakingUnsignedPriority::get() - 1u64;
pub const MinerMaxIterations: u32 = 10;
}

impl pallet_election_provider_multi_phase::Config for Runtime {
type Event = Event;
type Currency = Balances;
type SignedPhase = SignedPhase;
type UnsignedPhase = UnsignedPhase;
type SolutionImprovementThreshold = MinSolutionScoreBump;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerTxPriority = MultiPhaseUnsignedPriority;
type DataProvider = Staking;
type OnChainAccuracy = Perbill;
type CompactSolution = pallet_staking::CompactAssignments;
type Fallback = Fallback;
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = ();
}

pallet_staking_reward_curve::build! {
const REWARD_CURVE: PiecewiseLinear<'static> = curve!(
min_inflation: 0_025_000,
Expand Down Expand Up @@ -339,6 +374,7 @@ impl pallet_staking::Config for Runtime {
type MaxIterations = MaxIterations;
type MinSolutionScoreBump = MinSolutionScoreBump;
type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit;
type ElectionProvider = ElectionProviderMultiPhase;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
}

Expand Down Expand Up @@ -701,6 +737,9 @@ construct_runtime! {

// Multisig module. Late addition.
Multisig: pallet_multisig::{Module, Call, Storage, Event<T>} = 23,

// Election pallet. Only works with staking, but placed here to maintain indices.
ElectionProviderMultiPhase: pallet_election_provider_multi_phase::{Module, Call, Storage, Event<T>, ValidateUnsigned} = 24,
}
}

Expand Down

0 comments on commit aaea3a2

Please sign in to comment.