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

Allow staking miner to use different election algorithms #3752

Merged
28 commits merged into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e0232a6
WIP
emostov Aug 31, 2021
3bfb3f9
Merge remote-tracking branch 'origin' into zeke-npos-solver-trait-com…
emostov Aug 31, 2021
f735d09
Dry run cmd working
emostov Aug 31, 2021
8935429
Monitor cmd works
emostov Aug 31, 2021
d876c43
Configure balance with parameter type
emostov Aug 31, 2021
a27ef8c
Comments
emostov Aug 31, 2021
2487ad0
cleannnn
emostov Aug 31, 2021
148bcab
Merge branch 'master' of https://github.com/paritytech/polkadot into …
emostov Aug 31, 2021
9f8e64e
Add balancing to PhragMMS
emostov Aug 31, 2021
7bd3494
Move OffchainRanomBalancing to common
emostov Sep 1, 2021
6ff4e40
DRY mine_unchecked over config.solver
emostov Sep 1, 2021
44b9bb3
FMT
emostov Sep 1, 2021
bfe5ef2
Apply suggestions from code review
emostov Sep 1, 2021
6b6e879
Improve docs for any_runtime_unit!
emostov Sep 1, 2021
3ea5d5e
Merge branch 'zeke-npos-solver-trait-companion' of https://github.com…
emostov Sep 1, 2021
8a07f86
Some cleanup
emostov Sep 1, 2021
0590a58
fmt
emostov Sep 1, 2021
4155002
Correct capitilaztion
emostov Sep 1, 2021
55f7993
Merge branch 'master' into zeke-npos-solver-trait-companion
emostov Sep 4, 2021
3e7b833
Merge branch 'master' of https://github.com/paritytech/polkadot into …
emostov Sep 5, 2021
5757040
Improve version mismatch log
emostov Sep 6, 2021
55f13e5
Revert "Improve version mismatch log"
emostov Sep 6, 2021
3f9e959
Merge remote-tracking branch 'origin' into zeke-npos-solver-trait-com…
emostov Sep 7, 2021
af0b729
Merge remote-tracking branch 'origin' into zeke-npos-solver-trait-com…
emostov Sep 8, 2021
c15130c
Apply suggestions from code review
emostov Sep 9, 2021
c68af04
Remove Balancing struct and use Balancing Parameter type instead
emostov Sep 9, 2021
ed66703
Merge remote-tracking branch 'origin/master' into zeke-npos-solver-tr…
Sep 9, 2021
a8e5d05
update Substrate
Sep 9, 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
334 changes: 164 additions & 170 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
sp-session = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-staking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-npos-elections = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -90,6 +91,7 @@ std = [
"libsecp256k1/std",
"runtime-parachains/std",
"xcm/std",
"sp-npos-elections/std",
]
runtime-benchmarks = [
"libsecp256k1/hmac",
Expand Down
27 changes: 27 additions & 0 deletions runtime/common/src/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@ impl pallet_election_provider_multi_phase::BenchmarkingConfig for BenchmarkConfi
const MINER_MAXIMUM_VOTERS: u32 = 15_000;
const MAXIMUM_TARGETS: u32 = 2000;
}

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

/// A source of random balance for the NPoS Solver, which is meant to be run by the OCW election
/// miner.
pub struct OffchainRandomBalancing;
impl frame_support::pallet_prelude::Get<Option<(usize, sp_npos_elections::ExtendedBalance)>>
for OffchainRandomBalancing
{
fn get() -> Option<(usize, sp_npos_elections::ExtendedBalance)> {
use sp_runtime::{codec::Decode, traits::TrailingZeroInput};
let iters = match 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
},
};

Some((iters, 0))
}
}
7 changes: 5 additions & 2 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ parameter_types! {
pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

// miner configs
pub const MinerMaxIterations: u32 = 10;
pub OffchainRepeat: BlockNumber = 5;
}

Expand Down Expand Up @@ -384,7 +383,6 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type RewardHandler = (); // nothing to do upon rewards
type SignedPhase = SignedPhase;
type SolutionImprovementThreshold = SolutionImprovementThreshold;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerMaxLength = OffchainSolutionLengthLimit;
type OffchainRepeat = OffchainRepeat;
Expand All @@ -393,6 +391,11 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type Solution = NposCompactSolution24;
type OnChainAccuracy = Perbill;
type Fallback = Fallback;
type Solver = frame_election_provider_support::SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Runtime>,
runtime_common::elections::OffchainRandomBalancing,
>;
type BenchmarkingConfig = runtime_common::elections::BenchmarkConfig;
type ForceOrigin = EnsureOneOf<
AccountId,
Expand Down
7 changes: 5 additions & 2 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ parameter_types! {
pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

// miner configs
pub const MinerMaxIterations: u32 = 10;
pub OffchainRepeat: BlockNumber = 5;
}

Expand Down Expand Up @@ -390,7 +389,6 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type SlashHandler = (); // burn slashes
type RewardHandler = (); // nothing to do upon rewards
type SolutionImprovementThreshold = SolutionImprovementThreshold;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerMaxLength = OffchainSolutionLengthLimit;
type OffchainRepeat = OffchainRepeat;
Expand All @@ -399,6 +397,11 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type OnChainAccuracy = Perbill;
type Solution = NposCompactSolution16;
type Fallback = Fallback;
type Solver = frame_election_provider_support::SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Runtime>,
runtime_common::elections::OffchainRandomBalancing,
>;
type BenchmarkingConfig = runtime_common::elections::BenchmarkConfig;
type ForceOrigin = EnsureOneOf<
AccountId,
Expand Down
7 changes: 5 additions & 2 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ parameter_types! {
pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

// miner configs
pub const MinerMaxIterations: u32 = 10;
pub OffchainRepeat: BlockNumber = 5;
}

Expand Down Expand Up @@ -370,7 +369,6 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type SlashHandler = (); // burn slashes
type RewardHandler = (); // nothing to do upon rewards
type SolutionImprovementThreshold = SolutionImprovementThreshold;
type MinerMaxIterations = MinerMaxIterations;
type MinerMaxWeight = OffchainSolutionWeightLimit; // For now use the one from staking.
type MinerMaxLength = OffchainSolutionLengthLimit;
type OffchainRepeat = OffchainRepeat;
Expand All @@ -379,6 +377,11 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type OnChainAccuracy = Perbill;
type Solution = NposCompactSolution16;
type Fallback = Fallback;
type Solver = frame_election_provider_support::SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Runtime>,
runtime_common::elections::OffchainRandomBalancing,
>;
type BenchmarkingConfig = runtime_common::elections::BenchmarkConfig;
type ForceOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::pallet_election_provider_multi_phase::WeightInfo<Runtime>;
Expand Down
2 changes: 2 additions & 0 deletions utils/staking-miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-npos-elections = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "master" }


frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-staking = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
11 changes: 7 additions & 4 deletions utils/staking-miner/src/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use codec::Encode;
use frame_support::traits::Currency;

/// Forcefully create the snapshot. This can be used to compute the election at anytime.
fn force_create_snapshot<T: EPM::Config>(ext: &mut Ext) -> Result<(), Error> {
fn force_create_snapshot<T: EPM::Config>(ext: &mut Ext) -> Result<(), Error<T>> {
ext.execute_with(|| {
if <EPM::Snapshot<T>>::exists() {
log::info!(target: LOG_TARGET, "snapshot already exists.");
Expand Down Expand Up @@ -112,7 +112,7 @@ macro_rules! dry_run_cmd_for { ($runtime:ident) => { paste::paste! {
shared: SharedConfig,
config: DryRunConfig,
signer: Signer,
) -> Result<(), Error> {
) -> Result<(), Error<$crate::[<$runtime _runtime_exports>]::Runtime>> {
use $crate::[<$runtime _runtime_exports>]::*;
let mut ext = crate::create_election_ext::<Runtime, Block>(
shared.uri.clone(),
Expand All @@ -121,7 +121,8 @@ macro_rules! dry_run_cmd_for { ($runtime:ident) => { paste::paste! {
).await?;
force_create_snapshot::<Runtime>(&mut ext)?;

let (raw_solution, witness) = crate::mine_unchecked::<Runtime>(&mut ext, config.iterations, false)?;
let (raw_solution, witness) = crate::mine_with::<Runtime>(&config.solver, &mut ext)?;

let nonce = crate::get_account_info::<Runtime>(client, &signer.account, config.at)
.await?
.map(|i| i.nonce)
Expand All @@ -148,7 +149,9 @@ macro_rules! dry_run_cmd_for { ($runtime:ident) => { paste::paste! {
});
log::info!(target: LOG_TARGET, "dispatch result is {:?}", dispatch_result);

let outcome = rpc_decode::<sp_runtime::ApplyExtrinsicResult>(client, "system_dryRun", params!{ bytes }).await?;
let outcome = rpc_decode::<sp_runtime::ApplyExtrinsicResult>(client, "system_dryRun", params!{ bytes })
.await
.map_err::<Error<Runtime>, _>(Into::into)?;
log::info!(target: LOG_TARGET, "dry-run outcome is {:?}", outcome);
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions utils/staking-miner/src/emergency_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@

use crate::{prelude::*, Error, SharedConfig};
use codec::Encode;
use frame_election_provider_support::SequentialPhragmen;
use std::io::Write;

macro_rules! emergency_solution_cmd_for { ($runtime:ident) => { paste::paste! {
/// Execute the emergency-solution command.
pub(crate) async fn [<emergency_solution_cmd_ $runtime>](
shared: SharedConfig,
) -> Result<(), Error> {
) -> Result<(), Error<$crate::[<$runtime _runtime_exports>]::Runtime>> {
use $crate::[<$runtime _runtime_exports>]::*;
let mut ext = crate::create_election_ext::<Runtime, Block>(shared.uri.clone(), None, vec![]).await?;
ext.execute_with(|| {
assert!(EPM::Pallet::<Runtime>::current_phase().is_emergency());
// NOTE: this internally calls feasibility_check, but we just re-do it here as an easy way
// to get a `ReadySolution`.
let (raw_solution, _) = <EPM::Pallet<Runtime>>::mine_solution(50)?;
let (raw_solution, _) =
<EPM::Pallet<Runtime>>::mine_solution::<SequentialPhragmen<AccountId, sp_runtime::Perbill>>()?;
log::info!(target: LOG_TARGET, "mined solution with {:?}", &raw_solution.score);
let ready_solution = EPM::Pallet::<Runtime>::feasibility_check(raw_solution, EPM::ElectionCompute::Signed)?;
let encoded_ready = ready_solution.encode();
Expand Down
Loading