Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve remote-ext #1070

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
67 changes: 58 additions & 9 deletions utils/remote-ext/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// TODO #167: fix clippy warnings
#![allow(clippy::all)]

use clap::Parser;
use clap::{Args, Parser, Subcommand};
use codec::Decode;
use frame_remote_externalities::{
Builder, Mode, OfflineConfig, OnlineConfig, RemoteExternalities, SnapshotConfig,
};
use framenode_runtime::AccountId;
use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
use sp_core::H256;
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

use anyhow::Result as AnyResult;
use frame_support::traits::OnRuntimeUpgrade;
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
use sp_runtime::traits::Dispatchable;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -58,9 +60,36 @@
/// Sora snapshot path.
#[clap(long)]
snapshot_path: Option<PathBuf>,
/// Encoded extrinsic
/// Run migrations
#[clap(long)]
xt: String,
run_migrations: bool,
/// Command type
#[clap(subcommand)]
command: Command,
}

#[derive(Debug, Clone, Args)]
#[group(multiple = false, required = true)]
pub struct AccountArg {
#[clap(long)]
account: Option<AccountId>,
#[clap(long)]
root: bool,
#[clap(long)]
unsigned: bool,
}

#[derive(Subcommand, Clone, Debug)]
pub enum Command {
Call {
#[clap(long)]
call: String,
#[clap(flatten)]
account: AccountArg,
},
Xt {
xt: String,
},
}

#[tokio::main]
Expand All @@ -75,12 +104,32 @@
let mut ext =
create_ext::<framenode_runtime::Block>(client.clone(), cli.at, cli.snapshot_path).await?;
let _res: AnyResult<()> = ext.execute_with(|| {
let xt_encoded = hex::decode(&cli.xt).unwrap();
let xt = framenode_runtime::UncheckedExtrinsic::decode(&mut &xt_encoded[..]).unwrap();
if let Some((account, _signature, _extra)) = xt.signature {
xt.function
.dispatch(framenode_runtime::RuntimeOrigin::signed(account))
.unwrap();
if cli.run_migrations {
framenode_runtime::migrations::Migrations::on_runtime_upgrade();
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
}
match cli.command {
Command::Call { call, account } => {
let origin = if account.unsigned {
framenode_runtime::RuntimeOrigin::none()
} else if account.root {
framenode_runtime::RuntimeOrigin::root()
} else {
framenode_runtime::RuntimeOrigin::signed(account.account.unwrap())
};
let call_encoded = hex::decode(call.strip_prefix("0x").unwrap_or(&call)).unwrap();
let call = framenode_runtime::RuntimeCall::decode(&mut &call_encoded[..]).unwrap();
call.dispatch(origin).unwrap();
}
Command::Xt { xt } => {
let xt_encoded = hex::decode(xt.strip_prefix("0x").unwrap_or(&xt)).unwrap();
let xt =
framenode_runtime::UncheckedExtrinsic::decode(&mut &xt_encoded[..]).unwrap();
if let Some((account, _signature, _extra)) = xt.signature {
xt.function
.dispatch(framenode_runtime::RuntimeOrigin::signed(account))
.unwrap();
}
}
}
Ok(())
});
Expand Down
Loading