Skip to content
Prev Previous commit
Next Next commit
added cli commands for new rpc methods
  • Loading branch information
brenzi committed Nov 17, 2023
commit 72dca6902746be816f56c71ed4d31582453937f7
2 changes: 2 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub enum CliError {
TrustedOp { msg: String },
#[error("EvmReadCommands error: {:?}", msg)]
EvmRead { msg: String },
#[error("worker rpc api error: {:?}", msg)]
WorkerRpcApi { msg: String },
}

pub type CliResult = Result<CliResultOk, CliError>;
Expand Down
75 changes: 75 additions & 0 deletions cli/src/trusted_base_cli/commands/get_shard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

use crate::{
command_utils::get_worker_api_direct,
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
Cli, CliError, CliResult, CliResultOk,
};
use base58::ToBase58;
use codec::{Decode, Encode};
use ita_stf::{Getter, Index, TrustedCall, TrustedCallSigned};
use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_stf_primitives::{
traits::TrustedCallSigning,
types::{KeyPair, TrustedOperation},
};
use itp_types::DirectRequestStatus;
use itp_utils::FromHexPrefixed;
use log::*;
use my_node_runtime::Balance;
use sp_core::{crypto::Ss58Codec, Pair, H256};
use std::boxed::Box;

#[derive(Parser)]
pub struct GetShardCommand {}

impl GetShardCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
let direct_api = get_worker_api_direct(cli);
let rpc_method = "author_getShard".to_owned();
let jsonrpc_call: String = RpcRequest::compose_jsonrpc_call(rpc_method, vec![]).unwrap();
let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();
// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str)
.map_err(|err| CliError::WorkerRpcApi { msg: err.to_string() })?;
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode RpcReturnValue: {:?}", err);
CliError::WorkerRpcApi { msg: "failed to decode RpcReturnValue".to_string() }
})?;

if rpc_return_value.status == DirectRequestStatus::Error {
println!("[Error] {}", String::decode(&mut rpc_return_value.value.as_slice()).unwrap());
return Err(CliError::WorkerRpcApi { msg: "rpc error".to_string() })
}

let shard = H256::decode(&mut rpc_return_value.value.as_slice())
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode shard: {:?}", err);
CliError::WorkerRpcApi { msg: err.to_string() }
})?;
println!("{}", shard.encode().to_base58());
Ok(CliResultOk::H256 { hash: shard })
}
}
79 changes: 79 additions & 0 deletions cli/src/trusted_base_cli/commands/get_shard_vault.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

use crate::{
command_utils::get_worker_api_direct,
get_layer_two_nonce,
trusted_cli::TrustedCli,
trusted_command_utils::{get_identifiers, get_pair_from_str},
trusted_operation::perform_trusted_operation,
Cli, CliError, CliResult, CliResultOk,
};
use base58::ToBase58;
use codec::{Decode, Encode};
use ita_stf::{Getter, Index, TrustedCall, TrustedCallSigned};
use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_stf_primitives::{
traits::TrustedCallSigning,
types::{KeyPair, TrustedOperation},
};
use itp_types::{AccountId, DirectRequestStatus};
use itp_utils::FromHexPrefixed;
use log::*;
use my_node_runtime::Balance;
use sp_core::{crypto::Ss58Codec, Pair, H256};
use std::boxed::Box;

#[derive(Parser)]
pub struct GetShardVaultCommand {}

impl GetShardVaultCommand {
pub(crate) fn run(&self, cli: &Cli, trusted_args: &TrustedCli) -> CliResult {
let direct_api = get_worker_api_direct(cli);
let rpc_method = "author_getShardVault".to_owned();
let jsonrpc_call: String = RpcRequest::compose_jsonrpc_call(rpc_method, vec![]).unwrap();
let rpc_response_str = direct_api.get(&jsonrpc_call).unwrap();
// Decode RPC response.
let rpc_response: RpcResponse = serde_json::from_str(&rpc_response_str)
.map_err(|err| CliError::WorkerRpcApi { msg: err.to_string() })?;
let rpc_return_value = RpcReturnValue::from_hex(&rpc_response.result)
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode RpcReturnValue: {:?}", err);
CliError::WorkerRpcApi { msg: "failed to decode RpcReturnValue".to_string() }
})?;

if rpc_return_value.status == DirectRequestStatus::Error {
println!("[Error] {}", String::decode(&mut rpc_return_value.value.as_slice()).unwrap());
return Err(CliError::WorkerRpcApi { msg: "rpc error".to_string() })
}

let vault = AccountId::decode(&mut rpc_return_value.value.as_slice())
// Replace with `inspect_err` once it's stable.
.map_err(|err| {
error!("Failed to decode vault account: {:?}", err);
CliError::WorkerRpcApi { msg: err.to_string() }
})?;
let vault_ss58 = vault.to_ss58check();
println!("{}", vault_ss58);
Ok(CliResultOk::PubKeysBase58 {
pubkeys_sr25519: None,
pubkeys_ed25519: Some(vec![vault_ss58]),
})
}
}
2 changes: 2 additions & 0 deletions cli/src/trusted_base_cli/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod balance;
pub mod get_shard;
pub mod get_shard_vault;
pub mod nonce;
pub mod set_balance;
pub mod transfer;
Expand Down
13 changes: 11 additions & 2 deletions cli/src/trusted_base_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

use crate::{
trusted_base_cli::commands::{
balance::BalanceCommand, nonce::NonceCommand, set_balance::SetBalanceCommand,
transfer::TransferCommand, unshield_funds::UnshieldFundsCommand,
balance::BalanceCommand, get_shard::GetShardCommand, get_shard_vault::GetShardVaultCommand,
nonce::NonceCommand, set_balance::SetBalanceCommand, transfer::TransferCommand,
unshield_funds::UnshieldFundsCommand,
},
trusted_cli::TrustedCli,
trusted_command_utils::get_keystore_path,
Expand Down Expand Up @@ -54,6 +55,12 @@ pub enum TrustedBaseCommand {
/// gets the nonce of a given account, taking the pending trusted calls
/// in top pool in consideration
Nonce(NonceCommand),

/// get shard for this worker
GetShard(GetShardCommand),

/// get shard vault for shielding (if defined for this worker)
GetShardVault(GetShardVaultCommand),
}

impl TrustedBaseCommand {
Expand All @@ -66,6 +73,8 @@ impl TrustedBaseCommand {
TrustedBaseCommand::Balance(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::UnshieldFunds(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::Nonce(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetShard(cmd) => cmd.run(cli, trusted_cli),
TrustedBaseCommand::GetShardVault(cmd) => cmd.run(cli, trusted_cli),
}
}
}
Expand Down