Skip to content

Commit

Permalink
Revert "use subxt-signer to reduce the number of deps (#720)" (#743)
Browse files Browse the repository at this point in the history
* Revert "use `subxt-signer` to reduce the number of deps (#720)"

This reverts commit eb767d0.

* revert README changes
  • Loading branch information
niklasad1 authored Dec 21, 2023
1 parent 0c92ebe commit 2beb197
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 121 deletions.
51 changes: 5 additions & 46 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
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
28 changes: 0 additions & 28 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use serde::Deserialize;
use std::{
future::Future,
pin::Pin,
str::FromStr,
task::{Context, Poll},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -153,30 +152,3 @@ pub async fn storage_at(
api.storage().at_latest().await.map_err(Into::into)
}
}

pub fn signer_from_seed_or_path(seed_or_path: &str) -> Result<Signer, Error> {
let seed_or_path = seed_or_path.trim();

let unchecked_secret = match std::fs::read(seed_or_path) {
Ok(s) => String::from_utf8(s).map_err(|e| Error::Other(e.to_string()))?,
Err(_) => seed_or_path.to_string(),
};

let secret = subxt_signer::SecretUri::from_str(&unchecked_secret)?;
Signer::from_uri(&secret).map_err(Into::into)
}

#[cfg(test)]
#[test]
fn signer_parsing_works() {
assert!(signer_from_seed_or_path("//Alice").is_ok());
assert!(signer_from_seed_or_path(
"0x1122334455667788112233445566778811223344556677881122334455667788"
)
.is_ok());
assert!(signer_from_seed_or_path(
"1122334455667788112233445566778811223344556677881122334455667788"
)
.is_err());
assert!(signer_from_seed_or_path("0x0").is_err());
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pub mod helpers;
pub mod opt;
pub mod prelude;
pub mod prometheus;
pub mod signer;
pub mod static_types;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod helpers;
mod opt;
mod prelude;
mod prometheus;
mod signer;
mod static_types;

use clap::Parser;
Expand Down
3 changes: 2 additions & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use crate::error::Error;

use clap::*;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use sp_npos_elections::BalancingConfig;
use sp_runtime::DeserializeOwned;
use std::{collections::HashMap, fmt, str::FromStr};
use subxt::backend::legacy::rpc_methods as subxt_rpc;

Expand Down
16 changes: 10 additions & 6 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@
// re-exports.
pub use pallet_election_provider_multi_phase::{Miner, MinerConfig};
pub use subxt::ext::sp_core;
/// The account id type.
pub type AccountId = subxt::utils::AccountId32;
pub type AccountId = sp_runtime::AccountId32;
/// The header type. We re-export it here, but we can easily get it from block as well.
pub type Header =
subxt::config::substrate::SubstrateHeader<u32, subxt::config::substrate::BlakeTwo256>;
/// The header type. We re-export it here, but we can easily get it from block as well.
pub type Hash = subxt::utils::H256;
pub type Hash = sp_core::H256;
/// Balance type
pub type Balance = u128;
/// Signer type
/// The key pair type being used. We "strongly" assume sr25519 for simplicity.
pub type Signer = subxt_signer::sr25519::Keypair;
pub use subxt::ext::sp_runtime::traits::{Block as BlockT, Header as HeaderT};

/// Default URI to connect to.
///
Expand All @@ -43,12 +42,17 @@ pub const DEFAULT_URI: &str = "ws://127.0.0.1:9944";
pub const DEFAULT_PROMETHEUS_PORT: u16 = 9999;
/// The logging target.
pub const LOG_TARGET: &str = "polkadot-staking-miner";

/// The key pair type being used. We "strongly" assume sr25519 for simplicity.
pub type Pair = sp_core::sr25519::Pair;

/// The accuracy that we use for election computation.
pub type Accuracy = sp_runtime::Perbill;
/// Rpc client.

pub type RpcClient = subxt::backend::legacy::LegacyRpcMethods<subxt::PolkadotConfig>;
/// Subxt client used by the staking miner on all chains.
pub type ChainClient = subxt::OnlineClient<subxt::PolkadotConfig>;

/// Config used by the staking-miner
pub type Config = subxt::PolkadotConfig;

Expand Down
Loading

0 comments on commit 2beb197

Please sign in to comment.