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

Staking Miner #3141

Merged
merged 39 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
36502dc
Companion for Decouple Staking and Election - Part 3: Signed Phase
coriolinus Apr 1, 2021
1e509e9
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus May 26, 2021
9638fcf
Merge remote-tracking branch 'origin/master' into prgn-companion-for-…
coriolinus May 27, 2021
59646c2
Merge branch 'prgn-companion-for-7910' of github.com:paritytech/polka…
kianenigma May 29, 2021
271d351
Template added
kianenigma May 29, 2021
7e21f24
Add stuff
kianenigma May 29, 2021
61cf070
First working version
kianenigma May 31, 2021
95b6ea0
revamp rpc stuff
kianenigma May 31, 2021
1709494
remove all the stale js stuff
kianenigma Jun 1, 2021
0c10c66
Major updates, closer to completion.
kianenigma Jun 3, 2021
a4aa730
removing my generic WIP for tidyness.
kianenigma Jun 3, 2021
72db361
add --at
kianenigma Jun 3, 2021
dc1a623
ass measure snapshot
kianenigma Jun 13, 2021
5fa5a3f
some new little debugging thigs
kianenigma Jun 17, 2021
138e8b9
pre-merge
kianenigma Jun 25, 2021
c3cfe94
Merge branch 'master' of github.com:paritytech/polkadot into kiz-stak…
kianenigma Jun 25, 2021
863ea3a
Master.into()
kianenigma Jun 28, 2021
3980e85
Fix
kianenigma Jun 28, 2021
d1ba0cb
revert dep update
kianenigma Jun 30, 2021
fe76cee
update to master
kianenigma Jun 30, 2021
3777a8a
revert all the toml shit
kianenigma Jun 30, 2021
64c22d9
Apply suggestions from code review
kianenigma Jun 30, 2021
bcd394d
cleanup
kianenigma Jun 30, 2021
e74218e
Fix more
kianenigma Jun 30, 2021
3ce287f
Fix era
kianenigma Jul 1, 2021
7dd7e64
Apply suggestions from code review
kianenigma Jul 1, 2021
ab87195
Few fixes
kianenigma Jul 1, 2021
c77bf51
Better reward scheme
kianenigma Jul 1, 2021
b25ee40
y
kianenigma Jul 1, 2021
10508ae
Update utils/staking-miner/src/main.rs
kianenigma Jul 1, 2021
f83ddb6
Update utils/staking-miner/src/dry_run.rs
kianenigma Jul 1, 2021
913e75a
Update utils/staking-miner/src/dry_run.rs
kianenigma Jul 1, 2021
89e0a38
Update utils/staking-miner/src/monitor.rs
kianenigma Jul 1, 2021
dcbe582
More chnages
kianenigma Jul 1, 2021
f10f00b
Merge branch 'kiz-staking-miner' of github.com:paritytech/polkadot in…
kianenigma Jul 1, 2021
df8725b
Fix
kianenigma Jul 1, 2021
1e4f6be
Master.into()
kianenigma Jul 1, 2021
01e92c4
Make it restart automatically.
kianenigma Jul 1, 2021
979cfcc
Final touches
kianenigma Jul 1, 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
44 changes: 44 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ members = [
"parachain/test-parachains",
"parachain/test-parachains/adder",
"parachain/test-parachains/adder/collator",
"utils/staking-miner",
]

# We want to be able to build the bridge relayer without pulling it (and all of its
Expand Down
1 change: 1 addition & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub use pallet_staking::StakerStatus;
pub use sp_runtime::BuildStorage;
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_election_provider_multi_phase::Call as EPMCall;

/// Constant values used within the runtime.
pub mod constants;
Expand Down
1 change: 1 addition & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub use pallet_staking::StakerStatus;
pub use sp_runtime::BuildStorage;
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_election_provider_multi_phase::Call as EPMCall;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved

/// Constant values used within the runtime.
pub mod constants;
Expand Down
1 change: 1 addition & 0 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub use pallet_staking::StakerStatus;
pub use sp_runtime::BuildStorage;
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_election_provider_multi_phase::Call as EPMCall;
kianenigma marked this conversation as resolved.
Show resolved Hide resolved

/// Constant values used within the runtime.
pub mod constants;
Expand Down
2 changes: 2 additions & 0 deletions utils/staking-miner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.key
*.bin
47 changes: 47 additions & 0 deletions utils/staking-miner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "staking-miner"
version = "0.9.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0" }
tokio = { version = "0.2", features = ["macros"] }
log = "0.4.11"
env_logger = "0.8.3"
structopt = "0.3.0"
jsonrpsee-ws-client = { version = "0.2.0", default-features = false, features = ["tokio02"] }
jsonrpsee-types = { version = "0.2.0" }
jsonrpsee = "=0.2.0-alpha.6"
serde_json = "1.0"
serde = "1.0.0"
hex = "0.4.0"
lazy_static = "1.4.0"
paste = "1.0.5"
thiserror = "1.0.0"

remote-externalities = { git = "https://github.com/paritytech/substrate", branch = "master" }

sp-core = { 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" }
sp-transaction-pool = { 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-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master" }

core-primitives = { package = "polkadot-core-primitives", path = "../../core-primitives" }

runtime-common = { package = "polkadot-runtime-common", path = "../../runtime/common" }
polkadot-runtime = { path = "../../runtime/polkadot" }
kusama-runtime = { path = "../../runtime/kusama" }
westend-runtime = { path = "../../runtime/westend" }

[dev-dependencies]
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master" }
97 changes: 97 additions & 0 deletions utils/staking-miner/src/dry_run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! The dry-run command.

use crate::{prelude::*, Signer, SharedConfig, DryRunConfig, WsClient, Error, rpc_helpers::*, params};
use codec::Encode;

/// 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> {
ext.execute_with(|| {
if <EPM::Snapshot<T>>::exists() {
log::info!(target: LOG_TARGET, "snapshot already exists.");
Ok(())
} else {
log::info!(target: LOG_TARGET, "creating a fake snapshot now.");
<EPM::Pallet<T>>::create_snapshot().map(|_| ()).map_err(Into::into)
}
})
}

/// Helper method to print the encoded size of the snapshot.
fn measure_snapshot_size<T: EPM::Config>(ext: &mut Ext) {
ext.execute_with(|| {
log::info!(target: LOG_TARGET, "Metadata: {:?}", <EPM::Pallet<T>>::snapshot_metadata());
log::info!(
target: LOG_TARGET,
"Encoded Length: {:?}",
<EPM::Pallet<T>>::snapshot()
.expect("snapshot must exist before calling `measure_snapshot_size`")
.encode()
.len()
);
})
}

/// Fir the stake threshold to in order to have at most `count` voters.
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
#[allow(unused)]
fn find_threshold<T: EPM::Config>(ext: &mut Ext, count: usize) {
ext.execute_with(|| {
let mut voters = <EPM::Pallet<T>>::snapshot()
.expect("snapshot must exist before calling `measure_snapshot_size`")
.voters;
voters.sort_by_key(|v| v.1);
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
println!("smallest allowed voter is {:?}", voters.into_iter().rev().take(count).last());
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
})
}

macro_rules! dry_run_cmd_for { ($runtime:ident) => { paste::paste! {
/// Execute the dry-run command.
pub(crate) async fn [<dry_run_cmd_ $runtime>](
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
client: WsClient,
shared: SharedConfig,
config: DryRunConfig,
signer: Signer,
) -> Result<(), Error> {
use $crate::[<$runtime _runtime_exports>]::*;
let mut ext = crate::create_election_ext::<Runtime, Block>(shared.uri.clone(), config.at, true).await?;
force_create_snapshot::<Runtime>(&mut ext)?;
measure_snapshot_size::<Runtime>(&mut ext);
let (raw_solution, witness) = crate::mine_unchecked::<Runtime>(&mut ext, 100)?;
log::info!(target: LOG_TARGET, "mined solution with {:?}", &raw_solution.score);

let nonce = crate::get_account_info::<Runtime>(&client, &signer.account, config.at)
.await?
.map(|i| i.nonce)
.expect("signer account is checked to exist upon startup; it can only die if it \
transfers funds out of it, or get slashed. If it does not exist at this point, \
it is likely due to a bug, or the signer got slashed. Terminating."
);
let tip = 0 as Balance;
let era = sp_runtime::generic::Era::Immortal;
let extrinsic = ext.execute_with(|| create_uxt(raw_solution, witness, signer.clone(), nonce, tip, era));

let bytes = sp_core::Bytes(extrinsic.encode().to_vec());
let outcome = rpc_decode::<sp_runtime::ApplyExtrinsicResult>(&client, "system_dryRun", params!{ bytes }).await?;
log::info!(target: LOG_TARGET, "dry-run outcome is {:?}", outcome);
Ok(())
}
}}}

dry_run_cmd_for!(polkadot);
dry_run_cmd_for!(kusama);
dry_run_cmd_for!(westend);
53 changes: 53 additions & 0 deletions utils/staking-miner/src/emergency_solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! The emergency-solution command.

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

macro_rules! emergency_solution_cmd_for { ($runtime:ident) => { paste::paste! {
/// Execute the emergency-solution command.
pub(crate) async fn [<emergency_solution_cmd_ $runtime>](
_client: WsClient,
shared: SharedConfig,
) -> Result<(), Error> {
use $crate::[<$runtime _runtime_exports>]::*;
let mut ext = crate::create_election_ext::<Runtime, Block>(shared.uri.clone(), None, false).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)?;
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();
let encoded_support = ready_solution.supports.encode();
let mut solution_file = std::fs::File::create("solution.bin")?;
let mut supports_file = std::fs::File::create("solution.supports.bin")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume this works with no ./ or /full/path/to/file

solution_file.write_all(&encoded_ready)?;
supports_file.write_all(&encoded_support)?;
log::info!(target: LOG_TARGET, "ReadySolution: size {:?} / score = {:?}", encoded_ready.len(), ready_solution.score);
log::trace!(target: LOG_TARGET, "Supports: {}", sp_core::hexdisplay::HexDisplay::from(&encoded_support));
Ok(())
})
}
}}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to change for now, but in the future would nice to have cli options for

  • solution_file
  • supports_file
  • iters (min solution)


emergency_solution_cmd_for!(polkadot);
emergency_solution_cmd_for!(kusama);
emergency_solution_cmd_for!(westend);
Loading