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

staking-miner: remove need of a file to pass the seed #3680

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ polkadot.*
!.rpm/*
.DS_Store
.cargo
.env
6 changes: 4 additions & 2 deletions utils/staking-miner/src/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn print_info<T: EPM::Config>(

let snapshot_size =
<EPM::Pallet<T>>::snapshot_metadata().expect("snapshot must exist by now; qed.");
let deposit = EPM::Pallet::<T>::deposit_for(&raw_solution, snapshot_size);
let deposit = EPM::Pallet::<T>::deposit_for(raw_solution, snapshot_size);
log::info!(
target: LOG_TARGET,
"solution score {:?} / deposit {:?} / length {:?}",
Expand All @@ -80,7 +80,9 @@ async fn print_info<T: EPM::Config>(
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,
);
}
Expand Down
16 changes: 8 additions & 8 deletions utils/staking-miner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
chevdor marked this conversation as resolved.
Show resolved Hide resolved
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")]
chevdor marked this conversation as resolved.
Show resolved Hide resolved
seed: String,
}

#[derive(Debug, Clone, StructOpt)]
Expand Down Expand Up @@ -354,7 +354,7 @@ fn mine_dpos<T: EPM::Config>(ext: &mut Ext) -> Result<(), Error> {
let desired_targets = EPM::DesiredTargets::<T>::get().unwrap();
let mut candidates_and_backing = BTreeMap::<T::AccountId, u128>::new();
voters.into_iter().for_each(|(who, stake, targets)| {
if targets.len() == 0 {
if targets.is_empty() {
println!("target = {:?}", (who, stake, targets));
return
}
Expand Down Expand Up @@ -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::<Runtime>(&shared.seed, &client)
.await
.expect("Provided account is invalid, terminating.")
};
Expand Down
4 changes: 2 additions & 2 deletions utils/staking-miner/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions utils/staking-miner/src/rpc_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ where
Hash: serde::Serialize,
{
let key = <V as StorageValue<T>>::hashed_key();
get_storage::<V::Query>(&client, params! { key, maybe_at }).await
get_storage::<V::Query>(client, params! { key, maybe_at }).await
}

#[allow(unused)]
Expand All @@ -103,5 +103,5 @@ where
Hash: serde::Serialize,
{
let key = <M as StorageMap<K, T>>::hashed_key_for(key);
get_storage::<M::Query>(&client, params! { key, maybe_at }).await
get_storage::<M::Query>(client, params! { key, maybe_at }).await
}
24 changes: 9 additions & 15 deletions utils/staking-miner/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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<T: frame_system::Config>(
Expand All @@ -51,26 +49,22 @@ pub(crate) async fn get_account_info<T: frame_system::Config>(
.await
}

/// Read the signer account's URI from the given `path`.
pub(crate) async fn read_signer_uri<
P: AsRef<Path>,
/// 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<Balance>,
>,
>(
path: P,
seed: &str,
client: &WsClient,
) -> Result<Signer, Error> {
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::<T>(&client, &account, None)
let _info = get_account_info::<T>(client, &account, None)
.await?
.ok_or(Error::AccountDoesNotExists)?;
log::info!(
Expand All @@ -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 })
}