check runtime version in staking miner (#3628)

* check runtime version in staking miner

* fmt

* add short alias for things

* fix fee

* print length as well

* fix build

* review comments
This commit is contained in:
Kian Paimani
2021-08-16 17:49:03 +02:00
committed by GitHub
parent 5b54c8cae9
commit 961b9621ec
8 changed files with 155 additions and 40 deletions
+56 -23
View File
@@ -38,6 +38,7 @@ mod signer;
pub(crate) use prelude::*;
pub(crate) use signer::get_account_info;
use frame_support::traits::Get;
use jsonrpsee_ws_client::{WsClient, WsClientBuilder};
use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::traits::Block as BlockT;
@@ -93,8 +94,9 @@ macro_rules! construct_runtime_prelude {
let address = <Runtime as frame_system::Config>::Lookup::unlookup(account.clone());
let extrinsic = UncheckedExtrinsic::new_signed(call, address, signature.into(), extra);
log::debug!(
target: crate::LOG_TARGET, "constructed extrinsic {}",
sp_core::hexdisplay::HexDisplay::from(&extrinsic.encode())
target: crate::LOG_TARGET, "constructed extrinsic {} with length {}",
sp_core::hexdisplay::HexDisplay::from(&extrinsic.encode()),
extrinsic.encode().len(),
);
extrinsic
}
@@ -172,14 +174,17 @@ macro_rules! any_runtime {
unsafe {
match $crate::RUNTIME {
$crate::AnyRuntime::Polkadot => {
#[allow(unused)]
use $crate::polkadot_runtime_exports::*;
$($code)*
},
$crate::AnyRuntime::Kusama => {
#[allow(unused)]
use $crate::kusama_runtime_exports::*;
$($code)*
},
$crate::AnyRuntime::Westend => {
#[allow(unused)]
use $crate::westend_runtime_exports::*;
$($code)*
}
@@ -201,6 +206,7 @@ enum Error {
AccountDoesNotExists,
IncorrectPhase,
AlreadySubmitted,
VersionMismatch,
}
impl From<sp_core::crypto::SecretStringError> for Error {
@@ -270,14 +276,14 @@ struct DryRunConfig {
#[derive(Debug, Clone, StructOpt)]
struct SharedConfig {
/// The `ws` node to connect to.
#[structopt(long, default_value = DEFAULT_URI)]
#[structopt(long, short, default_value = DEFAULT_URI)]
uri: String,
/// The file from which we read the account seed.
///
/// 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)]
#[structopt(long, short)]
account_seed: std::path::PathBuf,
}
@@ -291,34 +297,25 @@ struct Opt {
command: Command,
}
/// Build the `Ext` at `hash` with all the data of `ElectionProviderMultiPhase` and `Staking`
/// stored.
/// Build the Ext at hash with all the data of `ElectionProviderMultiPhase` and any additional
/// pallets.
async fn create_election_ext<T: EPM::Config, B: BlockT>(
uri: String,
at: Option<B::Hash>,
with_staking: bool,
additional: Vec<String>,
) -> Result<Ext, Error> {
use frame_support::{storage::generator::StorageMap, traits::PalletInfo};
use sp_core::hashing::twox_128;
let mut modules = vec![<T as frame_system::Config>::PalletInfo::name::<EPM::Pallet<T>>()
.expect("Pallet always has name; qed.")
.to_string()];
modules.extend(additional);
Builder::<B>::new()
.mode(Mode::Online(OnlineConfig {
transport: uri.into(),
at,
modules: if with_staking {
vec![
<T as frame_system::Config>::PalletInfo::name::<EPM::Pallet<T>>()
.expect("Pallet always has name; qed.")
.to_string(),
<T as frame_system::Config>::PalletInfo::name::<pallet_staking::Pallet<T>>()
.expect("Pallet always has name; qed.")
.to_string(),
]
} else {
vec![<T as frame_system::Config>::PalletInfo::name::<EPM::Pallet<T>>()
.expect("Pallet always has name; qed.")
.to_string()]
},
modules,
..Default::default()
}))
.inject_hashed_prefix(&<frame_system::BlockHash<T>>::prefix_hash())
@@ -386,6 +383,34 @@ fn mine_dpos<T: EPM::Config>(ext: &mut Ext) -> Result<(), Error> {
})
}
pub(crate) async fn check_versions<T: frame_system::Config>(
client: &WsClient,
print: bool,
) -> Result<(), Error> {
let linked_version = T::Version::get();
let on_chain_version = rpc_helpers::rpc::<sp_version::RuntimeVersion>(
client,
"state_getRuntimeVersion",
params! {},
)
.await
.expect("runtime version RPC should always work; qed");
if print {
log::info!(target: LOG_TARGET, "linked version {:?}", linked_version);
log::info!(target: LOG_TARGET, "on-chain version {:?}", on_chain_version);
}
if linked_version != on_chain_version {
log::error!(
target: LOG_TARGET,
"VERSION MISMATCH: any transaction will fail with bad-proof"
);
Err(Error::VersionMismatch)
} else {
Ok(())
}
}
#[tokio::main]
async fn main() {
env_logger::Builder::from_default_env()
@@ -422,6 +447,8 @@ async fn main() {
sp_core::crypto::set_default_ss58_version(
sp_core::crypto::Ss58AddressFormat::PolkadotAccount,
);
sub_tokens::dynamic::set_name("DOT");
sub_tokens::dynamic::set_decimal_points(10_000_000_000);
// safety: this program will always be single threaded, thus accessing global static is
// safe.
unsafe {
@@ -432,6 +459,8 @@ async fn main() {
sp_core::crypto::set_default_ss58_version(
sp_core::crypto::Ss58AddressFormat::KusamaAccount,
);
sub_tokens::dynamic::set_name("KSM");
sub_tokens::dynamic::set_decimal_points(1_000_000_000_000);
// safety: this program will always be single threaded, thus accessing global static is
// safe.
unsafe {
@@ -442,6 +471,8 @@ async fn main() {
sp_core::crypto::set_default_ss58_version(
sp_core::crypto::Ss58AddressFormat::PolkadotAccount,
);
sub_tokens::dynamic::set_name("WND");
sub_tokens::dynamic::set_decimal_points(1_000_000_000_000);
// safety: this program will always be single threaded, thus accessing global static is
// safe.
unsafe {
@@ -455,6 +486,10 @@ async fn main() {
}
log::info!(target: LOG_TARGET, "connected to chain {:?}", chain);
let _ = any_runtime! {
check_versions::<Runtime>(&client, true).await
};
let signer_account = any_runtime! {
signer::read_signer_uri::<_, Runtime>(&shared.account_seed, &client)
.await
@@ -464,7 +499,6 @@ async fn main() {
let outcome = any_runtime! {
match command.clone() {
Command::Monitor(c) => monitor_cmd(&client, shared, c, signer_account).await,
// --------------------^^ comes from the macro prelude, needs no generic.
Command::DryRun(c) => dry_run_cmd(&client, shared, c, signer_account).await,
Command::EmergencySolution => emergency_solution_cmd(shared.clone()).await,
}
@@ -477,7 +511,6 @@ mod tests {
use super::*;
fn get_version<T: frame_system::Config>() -> sp_version::RuntimeVersion {
use frame_support::traits::Get;
T::Version::get()
}