From 948e0f1e3e9b307f7f778b0d08f6354d4604e871 Mon Sep 17 00:00:00 2001 From: Chevdor Date: Fri, 27 Aug 2021 11:29:17 +0200 Subject: [PATCH] staking-miner: remove need of a file to pass the seed (#3680) * staking-miner: remove need of a file to pass the seed * cleanup * linting round * fix linting * fix naming and remove unused field --- .gitignore | 1 + utils/staking-miner/src/dry_run.rs | 6 ++++-- utils/staking-miner/src/main.rs | 16 ++++++++-------- utils/staking-miner/src/prelude.rs | 4 ++-- utils/staking-miner/src/rpc_helpers.rs | 4 ++-- utils/staking-miner/src/signer.rs | 24 +++++++++--------------- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index a654e51fd7c8..5ea0458ddfc8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ polkadot.* !.rpm/* .DS_Store .cargo +.env diff --git a/utils/staking-miner/src/dry_run.rs b/utils/staking-miner/src/dry_run.rs index 79190793f9e9..236d63ce4cc1 100644 --- a/utils/staking-miner/src/dry_run.rs +++ b/utils/staking-miner/src/dry_run.rs @@ -61,7 +61,7 @@ async fn print_info( let snapshot_size = >::snapshot_metadata().expect("snapshot must exist by now; qed."); - let deposit = EPM::Pallet::::deposit_for(&raw_solution, snapshot_size); + let deposit = EPM::Pallet::::deposit_for(raw_solution, snapshot_size); log::info!( target: LOG_TARGET, "solution score {:?} / deposit {:?} / length {:?}", @@ -80,7 +80,9 @@ async fn print_info( log::info!( target: LOG_TARGET, "payment_queryInfo: (fee = {}) {:?}", - info.as_ref().map(|d| Token::from(d.partial_fee)).unwrap_or(Token::from(0)), + info.as_ref() + .map(|d| Token::from(d.partial_fee)) + .unwrap_or_else(|_| Token::from(0)), info, ); } diff --git a/utils/staking-miner/src/main.rs b/utils/staking-miner/src/main.rs index 2aabe722150d..1769382e62af 100644 --- a/utils/staking-miner/src/main.rs +++ b/utils/staking-miner/src/main.rs @@ -276,15 +276,15 @@ struct DryRunConfig { #[derive(Debug, Clone, StructOpt)] struct SharedConfig { /// The `ws` node to connect to. - #[structopt(long, short, default_value = DEFAULT_URI)] + #[structopt(long, short, default_value = DEFAULT_URI, env = "URI")] uri: String, - /// The file from which we read the account seed. + /// The seed of a funded account in hex. /// - /// WARNING: don't use an account with a large stash for this. Based on how the bot is - /// configured, it might re-try lose funds through transaction fees/deposits. - #[structopt(long, short)] - account_seed: std::path::PathBuf, + /// WARNING: Don't use an account with a large stash for this. Based on how the bot is + /// configured, it might re-try and lose funds through transaction fees/deposits. + #[structopt(long, short, env = "SEED")] + seed: String, } #[derive(Debug, Clone, StructOpt)] @@ -354,7 +354,7 @@ fn mine_dpos(ext: &mut Ext) -> Result<(), Error> { let desired_targets = EPM::DesiredTargets::::get().unwrap(); let mut candidates_and_backing = BTreeMap::::new(); voters.into_iter().for_each(|(who, stake, targets)| { - if targets.len() == 0 { + if targets.is_empty() { println!("target = {:?}", (who, stake, targets)); return } @@ -491,7 +491,7 @@ async fn main() { }; let signer_account = any_runtime! { - signer::read_signer_uri::<_, Runtime>(&shared.account_seed, &client) + signer::signer_uri_from_string::(&shared.seed, &client) .await .expect("Provided account is invalid, terminating.") }; diff --git a/utils/staking-miner/src/prelude.rs b/utils/staking-miner/src/prelude.rs index e3c25887af2d..db9ce4e2c1e2 100644 --- a/utils/staking-miner/src/prelude.rs +++ b/utils/staking-miner/src/prelude.rs @@ -34,9 +34,9 @@ pub type Hash = core_primitives::Hash; pub use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; /// Default URI to connect to. -pub const DEFAULT_URI: &'static str = "wss://rpc.polkadot.io"; +pub const DEFAULT_URI: &str = "wss://rpc.polkadot.io"; /// The logging target. -pub const LOG_TARGET: &'static str = "staking-miner"; +pub const LOG_TARGET: &str = "staking-miner"; /// The election provider pallet. pub use pallet_election_provider_multi_phase as EPM; diff --git a/utils/staking-miner/src/rpc_helpers.rs b/utils/staking-miner/src/rpc_helpers.rs index e759917adee6..cc086b1235b1 100644 --- a/utils/staking-miner/src/rpc_helpers.rs +++ b/utils/staking-miner/src/rpc_helpers.rs @@ -82,7 +82,7 @@ where Hash: serde::Serialize, { let key = >::hashed_key(); - get_storage::(&client, params! { key, maybe_at }).await + get_storage::(client, params! { key, maybe_at }).await } #[allow(unused)] @@ -103,5 +103,5 @@ where Hash: serde::Serialize, { let key = >::hashed_key_for(key); - get_storage::(&client, params! { key, maybe_at }).await + get_storage::(client, params! { key, maybe_at }).await } diff --git a/utils/staking-miner/src/signer.rs b/utils/staking-miner/src/signer.rs index 9ced41a93e72..8dd2696b3309 100644 --- a/utils/staking-miner/src/signer.rs +++ b/utils/staking-miner/src/signer.rs @@ -18,9 +18,8 @@ use crate::{prelude::*, rpc_helpers, AccountId, Error, Index, Pair, WsClient, LOG_TARGET}; use sp_core::crypto::Pair as _; -use std::path::Path; -pub(crate) const SIGNER_ACCOUNT_WILL_EXIST: &'static str = +pub(crate) const SIGNER_ACCOUNT_WILL_EXIST: &str = "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."; @@ -30,10 +29,9 @@ pub(crate) const SIGNER_ACCOUNT_WILL_EXIST: &'static str = pub(crate) struct Signer { /// The account id. pub(crate) account: AccountId, + /// The full crypto key-pair. pub(crate) pair: Pair, - /// The raw URI read from file. - pub(crate) uri: String, } pub(crate) async fn get_account_info( @@ -51,26 +49,22 @@ pub(crate) async fn get_account_info( .await } -/// Read the signer account's URI from the given `path`. -pub(crate) async fn read_signer_uri< - P: AsRef, +/// Read the signer account's URI +pub(crate) async fn signer_uri_from_string< T: frame_system::Config< AccountId = AccountId, Index = Index, AccountData = pallet_balances::AccountData, >, >( - path: P, + seed: &str, client: &WsClient, ) -> Result { - let uri = std::fs::read_to_string(path)?; - - // trim any trailing garbage. - let uri = uri.trim_end(); + let seed = seed.trim(); - let pair = Pair::from_string(&uri, None)?; + let pair = Pair::from_string(seed, None)?; let account = T::AccountId::from(pair.public()); - let _info = get_account_info::(&client, &account, None) + let _info = get_account_info::(client, &account, None) .await? .ok_or(Error::AccountDoesNotExists)?; log::info!( @@ -80,5 +74,5 @@ pub(crate) async fn read_signer_uri< Token::from(_info.data.free), _info ); - Ok(Signer { account, pair, uri: uri.to_string() }) + Ok(Signer { account, pair }) }