Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

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

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

/// Maximum number of iterations for balancing that will be executed in the embedded miner of
/// the pallet.
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 OffchainRandomBalance;
impl frame_support::pallet_prelude::Get<Option<(usize, sp_npos_elections::ExtendedBalance)>>
for OffchainRandomBalance
{
fn get() -> Option<(usize, sp_npos_elections::ExtendedBalance)> {
use sp_runtime::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))
}
}

sp_npos_elections::generate_solution_type!(
#[compact]
pub struct NposCompactSolution24::<
Expand All @@ -384,7 +410,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 +418,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>,
OffchainRandomBalance,
>;
type BenchmarkingConfig = runtime_common::elections::BenchmarkConfig;
type ForceOrigin = EnsureOneOf<
AccountId,
Expand Down
34 changes: 32 additions & 2 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,36 @@ parameter_types! {
pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

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

/// Maximum number of iteration of balancing that will be executed in the embedded miner of
/// the pallet.
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 OffchainRandomBalance;
impl frame_support::pallet_prelude::Get<Option<(usize, sp_npos_elections::ExtendedBalance)>>
for OffchainRandomBalance
{
fn get() -> Option<(usize, sp_npos_elections::ExtendedBalance)> {
use sp_runtime::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))
}
}

sp_npos_elections::generate_solution_type!(
#[compact]
pub struct NposCompactSolution16::<
Expand All @@ -390,7 +416,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 +424,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>,
OffchainRandomBalance,
>;
type BenchmarkingConfig = runtime_common::elections::BenchmarkConfig;
type ForceOrigin = EnsureOneOf<
AccountId,
Expand Down
34 changes: 32 additions & 2 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,36 @@ parameter_types! {
pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

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

/// Maximum number of iterations for balancing that will be executed in the embedded miner of
/// the pallet.
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 OffchainRandomBalance;
impl frame_support::pallet_prelude::Get<Option<(usize, sp_npos_elections::ExtendedBalance)>>
for OffchainRandomBalance
{
fn get() -> Option<(usize, sp_npos_elections::ExtendedBalance)> {
use sp_runtime::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))
}
}

sp_npos_elections::generate_solution_type!(
#[compact]
pub struct NposCompactSolution16::<
Expand All @@ -372,7 +398,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 @@ -381,6 +406,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>,
OffchainRandomBalance,
>;
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
26 changes: 21 additions & 5 deletions utils/staking-miner/src/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
//! The dry-run command.

use crate::{
params, prelude::*, rpc_helpers::*, signer::Signer, DryRunConfig, Error, SharedConfig, WsClient,
params, prelude::*, rpc_helpers::*, signer::Signer, BalanceIterations, Balancing, DryRunConfig,
Error, SharedConfig, Solvers, WsClient,
};
use codec::Encode;
use frame_election_provider_support::{PhragMMS, SequentialPhragmen};
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 +114,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 +123,19 @@ 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) = match config.solver {
Solvers::SeqPhragmen { iterations } => {
BalanceIterations::set(iterations);
type Solver = SequentialPhragmen<AccountId, sp_runtime::Perbill, Balancing>;
crate::mine_unchecked::<Runtime, Solver>(&mut ext, false)?
},
Solvers::PhragMMS { iterations } => {
BalanceIterations::set(iterations);
type Solver = SequentialPhragmen<AccountId, sp_runtime::Perbill, Balancing>;
crate::mine_unchecked::<Runtime, Solver>(&mut ext, false)?
}
};

let nonce = crate::get_account_info::<Runtime>(client, &signer.account, config.at)
.await?
.map(|i| i.nonce)
Expand All @@ -148,7 +162,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