Skip to content

Commit

Permalink
Revert "use subxt-signer to reduce the number of deps (#720)"
Browse files Browse the repository at this point in the history
This reverts commit eb767d0.
  • Loading branch information
niklasad1 committed Dec 21, 2023
1 parent baf9541 commit 596a622
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 156 deletions.
55 changes: 7 additions & 48 deletions Cargo.lock

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

9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ pin-project-lite = "0.2"

# subxt
scale-value = "0.13"
subxt = "0.33"
subxt-signer = { version = "0.33", features = ["subxt"] }
subxt = { version = "0.33", features = ["substrate-compat"] }

# polkadot-sdk
frame-election-provider-support = "26.0.0"
pallet-election-provider-multi-phase = "25.0.0"
frame-support = "26.0.0"
sp-npos-elections = "24.0.0"
# Both `sp-runtime` and `sp-core` has plenty of dependencies
# and because `pallet-election-provider-multi-phase` is depending
# on them it's not much we can do it about it.
frame-support = "26.0.0"
sp-runtime = "29.0.0"
sp-core = "26.0.0"

# prometheus
prometheus = "0.13"
Expand Down
44 changes: 11 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# Polkadot staking miner
# Staking Miner v2

[![Daily compatibility check against latest polkadot](https://github.com/paritytech/polkadot-staking-miner/actions/workflows/nightly.yml/badge.svg)](https://github.com/paritytech/polkadot-staking-miner/actions/workflows/nightly.yml)
[![Daily compatibility check against latest polkadot](https://github.com/paritytech/staking-miner-v2/actions/workflows/nightly.yml/badge.svg)](https://github.com/paritytech/staking-miner-v2/actions/workflows/nightly.yml)

This is a re-write of the [staking miner](https://github.com/paritytech/polkadot/tree/master/utils/staking-miner) using [subxt](https://github.com/paritytech/subxt) to avoid hard dependency to each runtime version.
This is a re-write of the [polkadot staking miner](https://github.com/paritytech/polkadot/tree/master/utils/staking-miner) using [subxt](https://github.com/paritytech/subxt) to avoid hard dependency to each runtime version.

The binary itself embeds [static metadata](./artifacts/metadata.scale) to
generate a rust codegen at compile-time that [subxt provides](https://github.com/paritytech/subxt).

Runtime upgrades are handled by the polkadot-staking-miner by upgrading storage constants
and that will work unless there is a breaking change in the pallets `pallet-election-provider-multi-phase`
or `frame_system`.
Runtime upgrades are handled by staking-miner-v2 by upgrading storage constants
and that will work unless there is a breaking change in any of pallets used by
the staking-miner (mainly pallet-election-provider and pallet-system are used).

Because detecting breaking changes when connecting to a RPC node when using
Because detecting breaking changes when connection to RPC node when using
`polkadot-staking-miner` is hard, this repo performs daily integration tests
against `polkadot master` and in most cases [updating the metadata](#update-metadata)
against `polkadot master` and in most cases [update the metadata](#update-metadata)
and fixing the compile errors are sufficient.

It's also possible to use the `info` command to check whether the metadata
embedded in the binary is compatible with a remote node, [see the info the command for further information](#info-command)

Each release will specify which runtime version it was tested against but
it's not possible to know in advance which runtimes it will work with.

Thus, it's important to subscribe to releases to this repo or
add some logic that triggers an alert once the polkadot-staking-miner crashes.
add some logic that triggers an alert once the staking-miner-v2 crashes.

## Usage

You can check the help with:

```bash
$ polkadot-staking-miner --help
$ staking-miner --help
```

### Monitor
Expand Down Expand Up @@ -70,25 +67,6 @@ Mine a solution that can be submitted as an emergency solution.
$ cargo run --release -- --uri ws://localhost:9944 emergency-solution --at 0xba86a0ba663df496743eeb077d004ef86bd767716e0d8cb935ab90d3ae174e85 seq-phragmen
```

### Info command

Check if the polkadot-staking-miner's metadata is compatible with a remote node.

```bash
$ cargo run --release -- --uri wss://rpc.polkadot.io info
Remote_node:
{
"spec_name": "polkadot",
"impl_name": "parity-polkadot",
"spec_version": 9431,
"impl_version": 0,
"authoring_version": 0,
"transaction_version": 24,
"state_version": 0
}
Compatible: YES
```

### Prepare your SEED

While you could pass your seed directly to the cli or Docker, this is highly **NOT** recommended. Instead, you should use an ENV variable.
Expand Down Expand Up @@ -136,7 +114,7 @@ To update the metadata you need to connect to a polkadot, kusama or westend node
$ cargo install --locked subxt-cli
# Download the metadata from a local node and replace the current metadata
# See `https://github.com/paritytech/subxt/tree/master/cli` for further documentation of the `subxt-cli` tool.
$ subxt metadata --pallets "ElectionProviderMultiPhase,System" -f bytes > artifacts/metadata.scale
$ subxt metadata -f bytes > artifacts/metadata.scale
# Inspect the generated code
$ subxt codegen --file artifacts/metadata.scale | rustfmt > code.rs
```
Expand Down
25 changes: 9 additions & 16 deletions src/commands/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

//! The dry-run command.
use pallet_election_provider_multi_phase::RawSolution;

use crate::{
client::Client,
epm,
error::Error,
helpers::{signer_from_seed_or_path, storage_at},
opt::Solver,
prelude::*,
static_types,
client::Client, epm, error::Error, helpers::storage_at, opt::Solver, prelude::*,
signer::Signer, static_types,
};
use clap::Parser;
use codec::Encode;
use pallet_election_provider_multi_phase::RawSolution;

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
Expand Down Expand Up @@ -103,22 +99,19 @@ where
// If an account seed or path is provided, then do a dry run to the node. Otherwise,
// we've logged the solution above and we do nothing else.
if let Some(seed_or_path) = &config.seed_or_path {
let signer = signer_from_seed_or_path(seed_or_path)?;
let signer = Signer::new(seed_or_path)?;
let account_info = storage
.fetch(&runtime::storage().system().account(signer.public_key().to_account_id()))
.fetch(&runtime::storage().system().account(signer.account_id()))
.await?
.ok_or(Error::AccountDoesNotExists)?;

log::info!(target: LOG_TARGET, "Loaded account {}, {:?}", signer.public_key().to_account_id(), account_info);
log::info!(target: LOG_TARGET, "Loaded account {}, {:?}", signer, account_info);

let nonce = client
.rpc()
.system_account_next_index(&signer.public_key().to_account_id())
.await?;
let nonce = client.rpc().system_account_next_index(signer.account_id()).await?;
let tx = epm::signed_solution(raw_solution)?;
let xt = client.chain_api().tx().create_signed_with_nonce(
&tx,
&signer,
&*signer,
nonce,
Default::default(),
)?;
Expand Down
25 changes: 12 additions & 13 deletions src/commands/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use crate::{
client::Client,
epm,
error::Error,
helpers::{kill_main_task_if_critical_err, signer_from_seed_or_path, TimedFuture},
helpers::{kill_main_task_if_critical_err, TimedFuture},
opt::Solver,
prelude::*,
prometheus, static_types,
prometheus,
signer::Signer,
static_types,
};
use clap::Parser;
use codec::{Decode, Encode};
Expand Down Expand Up @@ -169,10 +171,10 @@ where
+ 'static,
T::Solution: Send,
{
let signer = signer_from_seed_or_path(&config.seed_or_path)?;
let signer = Signer::new(&config.seed_or_path)?;

let account_info = {
let addr = runtime::storage().system().account(signer.public_key().to_account_id());
let addr = runtime::storage().system().account(signer.account_id());
client
.chain_api()
.storage()
Expand All @@ -183,7 +185,7 @@ where
.ok_or(Error::AccountDoesNotExists)?
};

log::info!(target: LOG_TARGET, "Loaded account {}, {:?}", signer.public_key().to_account_id(), account_info);
log::info!(target: LOG_TARGET, "Loaded account {}, {:?}", signer, account_info);

if config.dry_run {
// if we want to try-run, ensure the node supports it.
Expand Down Expand Up @@ -241,7 +243,7 @@ where
.storage()
.at_latest()
.await?
.fetch(&runtime::storage().system().account(signer.public_key().to_account_id()))
.fetch(&runtime::storage().system().account(signer.account_id()))
.await?
.ok_or(Error::AccountDoesNotExists)?;
// this is lossy but fine for now.
Expand Down Expand Up @@ -270,10 +272,7 @@ where
// NOTE: as we try to send at each block then the nonce is used guard against
// submitting twice. Because once a solution has been accepted on chain
// the "next transaction" at a later block but with the same nonce will be rejected
let nonce = client
.rpc()
.system_account_next_index(&signer.public_key().to_account_id())
.await?;
let nonce = client.rpc().system_account_next_index(signer.account_id()).await?;

ensure_signed_phase(client.chain_api(), block_hash)
.inspect_err(|e| {
Expand Down Expand Up @@ -302,7 +301,7 @@ where
ensure_no_previous_solution::<T::Solution>(
client.chain_api(),
block_hash,
&signer.public_key().0.into(),
&signer.account_id().0.into(),
)
.inspect_err(|e| {
log::debug!(
Expand Down Expand Up @@ -384,7 +383,7 @@ where
ensure_no_previous_solution::<T::Solution>(
client.chain_api(),
best_head,
&signer.public_key().0.into(),
&signer.account_id().0.into(),
)
.inspect_err(|e| {
log::debug!(
Expand Down Expand Up @@ -544,7 +543,7 @@ async fn submit_and_watch_solution<T: MinerConfig + Send + Sync + 'static>(
client
.chain_api()
.tx()
.create_signed_with_nonce(&tx, &signer, nonce as u64, xt_cfg)?;
.create_signed_with_nonce(&tx, &*signer, nonce as u64, xt_cfg)?;

if dry_run {
let dry_run_bytes = client.rpc().dry_run(xt.encoded(), None).await?;
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use crate::prelude::*;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to parse log directive: `{0}´")]
Expand All @@ -24,10 +26,8 @@ pub enum Error {
RpcError(#[from] jsonrpsee::core::Error),
#[error("subxt error: `{0}`")]
Subxt(#[from] subxt::Error),
#[error("SecretUri error: `{0}`")]
SecretUri(#[from] subxt_signer::SecretUriError),
#[error("Keypair error: `{0}`")]
Keypair(#[from] subxt_signer::sr25519::Error),
#[error("Crypto error: `{0:?}`")]
Crypto(sp_core::crypto::SecretStringError),
#[error("Codec error: `{0}`")]
Codec(#[from] codec::Error),
#[error("Incorrect phase")]
Expand Down
Loading

0 comments on commit 596a622

Please sign in to comment.