Skip to content
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
5 changes: 3 additions & 2 deletions cli/src/base_cli/commands/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
command_utils::{get_accountid_from_str, get_chain_api},
Cli,
Cli, CliResult, CliResultOk,
};
use substrate_api_client::GetAccountInformation;

Expand All @@ -28,11 +28,12 @@ pub struct BalanceCommand {
}

impl BalanceCommand {
pub(crate) fn run(&self, cli: &Cli) {
pub(crate) fn run(&self, cli: &Cli) -> CliResult {
let api = get_chain_api(cli);
let accountid = get_accountid_from_str(&self.account);
let balance =
if let Some(data) = api.get_account_data(&accountid).unwrap() { data.free } else { 0 };
println!("{}", balance);
Ok(CliResultOk::Balance { balance })
}
}
6 changes: 4 additions & 2 deletions cli/src/base_cli/commands/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
command_utils::{get_accountid_from_str, get_chain_api},
Cli,
Cli, CliResult, CliResultOk,
};
use itp_node_api::api_client::ParentchainExtrinsicSigner;
use my_node_runtime::{BalancesCall, RuntimeCall};
Expand All @@ -36,7 +36,7 @@ pub struct FaucetCommand {
}

impl FaucetCommand {
pub(crate) fn run(&self, cli: &Cli) {
pub(crate) fn run(&self, cli: &Cli) -> CliResult {
let mut api = get_chain_api(cli);
api.set_signer(ParentchainExtrinsicSigner::new(AccountKeyring::Alice.pair()));
let mut nonce = api.get_nonce().unwrap();
Expand All @@ -56,5 +56,7 @@ impl FaucetCommand {
let _blockh = api.submit_extrinsic(xt).unwrap();
nonce += 1;
}

Ok(CliResultOk::None)
}
}
8 changes: 4 additions & 4 deletions cli/src/base_cli/commands/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

*/

use crate::{command_utils::get_chain_api, Cli};
use crate::{command_utils::get_chain_api, Cli, CliResult, CliResultOk};
use base58::ToBase58;
use codec::Encode;
use log::*;
Expand All @@ -34,7 +34,7 @@ pub struct ListenCommand {
}

impl ListenCommand {
pub(crate) fn run(&self, cli: &Cli) {
pub(crate) fn run(&self, cli: &Cli) -> CliResult {
println!("{:?} {:?}", self.events, self.blocks);
let api = get_chain_api(cli);
info!("Subscribing to events");
Expand All @@ -44,12 +44,12 @@ impl ListenCommand {
loop {
if let Some(e) = self.events {
if count >= e {
return
return Ok(CliResultOk::None)
}
};
if let Some(b) = self.blocks {
if blocks >= b {
return
return Ok(CliResultOk::None)
}
};

Expand Down
6 changes: 4 additions & 2 deletions cli/src/base_cli/commands/shield_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
command_utils::{get_accountid_from_str, get_chain_api, *},
Cli,
Cli, CliResult, CliResultOk,
};
use base58::FromBase58;
use codec::{Decode, Encode};
Expand All @@ -42,7 +42,7 @@ pub struct ShieldFundsCommand {
}

impl ShieldFundsCommand {
pub(crate) fn run(&self, cli: &Cli) {
pub(crate) fn run(&self, cli: &Cli) -> CliResult {
let mut chain_api = get_chain_api(cli);

let shard_opt = match self.shard.from_base58() {
Expand Down Expand Up @@ -77,5 +77,7 @@ impl ShieldFundsCommand {

let tx_hash = chain_api.submit_and_watch_extrinsic_until(xt, XtStatus::Finalized).unwrap();
println!("[+] TrustedOperation got finalized. Hash: {:?}\n", tx_hash);

Ok(CliResultOk::None)
}
}
9 changes: 6 additions & 3 deletions cli/src/base_cli/commands/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
command_utils::{get_accountid_from_str, get_chain_api, *},
Cli,
Cli, CliResult, CliResultOk,
};
use itp_node_api::api_client::{Address, ParentchainExtrinsicSigner};
use log::*;
Expand All @@ -40,7 +40,7 @@ pub struct TransferCommand {
}

impl TransferCommand {
pub(crate) fn run(&self, cli: &Cli) {
pub(crate) fn run(&self, cli: &Cli) -> CliResult {
let from_account = get_pair_from_str(&self.from);
let to_account = get_accountid_from_str(&self.to);
info!("from ss58 is {}", from_account.public().to_ss58check());
Expand All @@ -54,6 +54,9 @@ impl TransferCommand {
.extrinsic_hash;
println!("[+] TrustedOperation got finalized. Hash: {:?}\n", tx_hash);
let result = api.get_account_data(&to_account).unwrap().unwrap();
println!("balance for {} is now {}", to_account, result.free);
let balance = result.free;
println!("balance for {} is now {}", to_account, balance);

Ok(CliResultOk::Balance { balance })
}
}
48 changes: 37 additions & 11 deletions cli/src/base_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
shield_funds::ShieldFundsCommand, transfer::TransferCommand,
},
command_utils::*,
Cli,
Cli, CliResult, CliResultOk,
};
use base58::ToBase58;
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -73,7 +73,7 @@ pub enum BaseCommand {
}

impl BaseCommand {
pub fn run(&self, cli: &Cli) {
pub fn run(&self, cli: &Cli) -> CliResult {
match self {
BaseCommand::Balance(cmd) => cmd.run(cli),
BaseCommand::NewAccount => new_account(),
Expand All @@ -89,42 +89,63 @@ impl BaseCommand {
}
}

fn new_account() {
fn new_account() -> CliResult {
let store = LocalKeystore::open(PathBuf::from(&KEYSTORE_PATH), None).unwrap();
let key: sr25519::AppPair = store.generate().unwrap();
let key_base58 = key.public().to_ss58check();
drop(store);
println!("{}", key.public().to_ss58check());
println!("{}", key_base58);
Ok(CliResultOk::PubKeysBase58 {
pubkeys_sr25519: Some(vec![key_base58]),
pubkeys_ed25519: None,
})
}

fn list_accounts() {
fn list_accounts() -> CliResult {
let store = LocalKeystore::open(PathBuf::from(&KEYSTORE_PATH), None).unwrap();
println!("sr25519 keys:");
let mut keys_sr25519 = vec![];
for pubkey in store.public_keys::<sr25519::AppPublic>().unwrap().into_iter() {
println!("{}", pubkey.to_ss58check());
let key_ss58 = pubkey.to_ss58check();
println!("{}", key_ss58);
keys_sr25519.push(key_ss58);
}
println!("ed25519 keys:");
let mut keys_ed25519 = vec![];
for pubkey in store.public_keys::<ed25519::AppPublic>().unwrap().into_iter() {
println!("{}", pubkey.to_ss58check());
let key_ss58 = pubkey.to_ss58check();
println!("{}", key_ss58);
keys_ed25519.push(key_ss58);
}
drop(store);

Ok(CliResultOk::PubKeysBase58 {
pubkeys_sr25519: Some(keys_sr25519),
pubkeys_ed25519: Some(keys_ed25519),
})
}

fn print_metadata(cli: &Cli) {
fn print_metadata(cli: &Cli) -> CliResult {
let api = get_chain_api(cli);
let meta = api.metadata();
println!("Metadata:\n {}", Metadata::pretty_format(&meta.runtime_metadata()).unwrap());
Ok(CliResultOk::Metadata { metadata: meta.clone() })
}

fn print_sgx_metadata(cli: &Cli) {
fn print_sgx_metadata(cli: &Cli) -> CliResult {
let worker_api_direct = get_worker_api_direct(cli);
let metadata = worker_api_direct.get_state_metadata().unwrap();
println!("Metadata:\n {}", Metadata::pretty_format(metadata.runtime_metadata()).unwrap());
Ok(CliResultOk::Metadata { metadata })
}

fn list_workers(cli: &Cli) {
fn list_workers(cli: &Cli) -> CliResult {
let api = get_chain_api(cli);
let wcount = api.enclave_count(None).unwrap();
println!("number of workers registered: {}", wcount);

let mut mr_enclaves = Vec::with_capacity(wcount as usize);

for w in 1..=wcount {
let enclave = api.enclave(w, None).unwrap();
if enclave.is_none() {
Expand All @@ -134,10 +155,15 @@ fn list_workers(cli: &Cli) {
let enclave = enclave.unwrap();
let timestamp =
DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_millis(enclave.timestamp));
let mr_enclave = enclave.mr_enclave.to_base58();
println!("Enclave {}", w);
println!(" AccountId: {}", enclave.pubkey.to_ss58check());
println!(" MRENCLAVE: {}", enclave.mr_enclave.to_base58());
println!(" MRENCLAVE: {}", mr_enclave);
println!(" RA timestamp: {}", timestamp);
println!(" URL: {}", enclave.url);

mr_enclaves.push(mr_enclave);
}

Ok(CliResultOk::MrEnclaveBase58 { mr_enclaves })
}
12 changes: 7 additions & 5 deletions cli/src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
decode_balance, get_identifiers, get_keystore_path, get_pair_from_str,
},
trusted_operation::{get_json_request, get_state, perform_trusted_operation, wait_until},
Cli,
Cli, CliResult, CliResultOk,
};
use codec::Decode;
use hdrhistogram::Histogram;
Expand Down Expand Up @@ -113,7 +113,7 @@ struct BenchmarkTransaction {
}

impl BenchmarkCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
let random_wait_before_transaction_ms: (u32, u32) = (
self.random_wait_before_transaction_min_ms,
self.random_wait_before_transaction_max_ms,
Expand Down Expand Up @@ -247,7 +247,9 @@ impl BenchmarkCommand {
overall_start.elapsed().as_millis()
);

print_benchmark_statistic(outputs, self.wait_for_confirmation)
print_benchmark_statistic(outputs, self.wait_for_confirmation);

Ok(CliResultOk::None)
}
}

Expand All @@ -262,7 +264,7 @@ fn get_balance(
);

let getter_start_timer = Instant::now();
let getter_result = get_state(direct_client, shard, &getter);
let getter_result = get_state(direct_client, shard, &getter).unwrap_or_default();
let getter_execution_time = getter_start_timer.elapsed().as_millis();

let balance = decode_balance(getter_result);
Expand All @@ -282,7 +284,7 @@ fn get_nonce(
);

let getter_start_timer = Instant::now();
let getter_result = get_state(direct_client, shard, &getter);
let getter_result = get_state(direct_client, shard, &getter).unwrap_or_default();
let getter_execution_time = getter_start_timer.elapsed().as_millis();

let nonce = match getter_result {
Expand Down
16 changes: 11 additions & 5 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

extern crate chrono;
use crate::{base_cli::BaseCommand, trusted_cli::TrustedCli, Cli};
use crate::{base_cli::BaseCommand, trusted_cli::TrustedCli, Cli, CliResult, CliResultOk};
use clap::Subcommand;

#[cfg(feature = "teeracle")]
Expand All @@ -43,12 +43,18 @@ pub enum Commands {
Attesteer(AttesteerCommand),
}

pub fn match_command(cli: &Cli) {
pub fn match_command(cli: &Cli) -> CliResult {
match &cli.command {
Commands::Base(cmd) => cmd.run(cli),
Commands::Trusted(trusted_cli) => trusted_cli.run(cli),
#[cfg(feature = "teeracle")]
Commands::Oracle(cmd) => cmd.run(cli),
Commands::Attesteer(cmd) => cmd.run(cli),
};
Commands::Oracle(cmd) => {
cmd.run(cli);
Ok(CliResultOk::None)
},
Commands::Attesteer(cmd) => {
cmd.run(cli);
Ok(CliResultOk::None)
},
}
}
7 changes: 4 additions & 3 deletions cli/src/evm/commands/evm_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
Cli,
Cli, CliResult, CliResultOk,
};
use codec::Decode;
use ita_stf::{Index, TrustedCall, TrustedGetter, TrustedOperation};
Expand All @@ -43,7 +43,7 @@ pub struct EvmCallCommands {
}

impl EvmCallCommands {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
let sender = get_pair_from_str(trusted_args, &self.from);
let sender_acc: AccountId = sender.public().into();

Expand Down Expand Up @@ -80,6 +80,7 @@ impl EvmCallCommands {
)
.sign(&KeyPair::Sr25519(Box::new(sender)), nonce, &mrenclave, &shard)
.into_trusted_operation(trusted_args.direct);
let _ = perform_trusted_operation(cli, trusted_args, &function_call);
Ok(perform_trusted_operation(cli, trusted_args, &function_call)
.map(|_| CliResultOk::None)?)
}
}
13 changes: 4 additions & 9 deletions cli/src/evm/commands/evm_command_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ macro_rules! get_layer_two_evm_nonce {
let top: TrustedOperation = TrustedGetter::evm_nonce($signer_pair.public().into())
.sign(&KeyPair::Sr25519(Box::new($signer_pair.clone())))
.into();
let res = perform_trusted_operation($cli, $trusted_args, &top);
let nonce: Index = if let Some(n) = res {
if let Ok(nonce) = Index::decode(&mut n.as_slice()) {
nonce
} else {
0
}
} else {
0
let res = perform_trusted_operation($cli, $trusted_args, &top).unwrap_or_default();
let nonce = match res {
Some(n) => Index::decode(&mut n.as_slice()).unwrap_or(0),
None => 0,
};
debug!("got evm nonce: {:?}", nonce);
nonce
Expand Down
Loading