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

feat(client_cli): Integrating CLI API to set and retrieve parameters. #4961

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
196 changes: 194 additions & 2 deletions client_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Iroha client CLI

use std::{
fs::{self, read as read_file},
io::{stdin, stdout},
Expand Down Expand Up @@ -101,6 +100,9 @@ enum Subcommand {
/// The subcommand related to p2p networking
#[clap(subcommand)]
Peer(peer::Args),
/// The subcommand related to adding config parameters.
#[clap(subcommand)]
Parameters(param::Args),
/// The subcommand related to event streaming
#[clap(subcommand)]
Events(events::Args),
Expand Down Expand Up @@ -165,7 +167,7 @@ macro_rules! match_all {
impl RunArgs for Subcommand {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
use Subcommand::*;
match_all!((self, context), { Domain, Account, Asset, Peer, Events, Wasm, Blocks, Json })
match_all!((self, context), { Domain, Account, Asset, Peer, Parameters, Events, Wasm, Blocks, Json })
}
}

Expand Down Expand Up @@ -1090,6 +1092,196 @@ mod peer {
}
}

mod param {
use super::*;
use clap::Subcommand;
use core::num::NonZeroU64;
use iroha::client::{self, *};
use iroha_data_model::isi::SetParameter;
use iroha_data_model::parameter::{self, *};

// #[derive(Parser, Debug)]
// #[command(author, version, about, long_about = None)]
#[derive(Subcommand, Debug)]
pub enum Args {
// #[command(subcommand)]
// Parameters(ParamCmd),
#[command(subcommand)]
Get(Box<Get>),
divyaranjan1905 marked this conversation as resolved.
Show resolved Hide resolved
#[command(subcommand)]
Set(Box<Set>),
}

impl RunArgs for Args {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all!((self, context), { Args::Get, Args::Set, })
}
}

#[derive(Subcommand, Debug)]
pub enum Get {
All,
#[command(flatten)]
Inner(ParamCmd),
}

/// Get values of parameters
impl RunArgs for Get {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
let client = context.client_from_config();
let vec = match self {
Self::All => client
.request(client::parameter::all())
.wrap_err("Failed to get all the parameters."),
Self::Inner(param) => client
.build_query(client::parameter::all())
.with_filter(ParamCmd::Sumeragi)
.execute()
.wrap_err("Failed to get the parameter value"),
}?;
context.print_data(&vec.collect::<QueryResult<Vec<_>>>()?)?;
Ok(())
}
}
Comment on lines +1129 to +1145
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the first thing is here, I tried to replicate this from another commit, but I'm stuck on how to get the specific parameters that are in ParamCmd. I'd like some suggestion on that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client::parameter::all() returns Parameters, which has all the parameters as fields. No need to filter here. You need to map ParamCmd to the correct field in Parameters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also need to make a version of ParamCmd and its nested types without the value field for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also need to make a version of ParamCmd and its nested types without the value field for this.

@nxsaken I would need to duplicate it again? That seems weird. Maybe ParamCmd could be refactored for the better?


/// Set values for parameters
#[derive(clap::Subcommand, Debug)]
pub enum Set {
All,
#[command(flatten)]
Inner(ParamCmd),
}

impl RunArgs for Set {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
let set_param_value = SetParameter::new(0);
// Not sure what to do.
Ok(())
}
}
Comment on lines +1155 to +1161
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And similarly, how do we exactly take the parameters from the hierarchy we have and actually set them? I believe we need to use SetParameter from data_model::parameter, but don't know how to go further on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetParameter is an Instruction, which can be submitted to an Iroha Client. You need to turn self into a SetParameter here, and then call submit (defined in this file).


#[derive(Subcommand, Debug)]
pub enum ParamCmd {
#[command(subcommand, about = "Setting Sumeragi Parameters.")]
Sumeragi(SumeragiCmd),
#[command(subcommand, about = "Setting Block Parameters.")]
Block(BlockCmd),
#[command(subcommand, about = "Setting Transaction Parameters.")]
Transaction(TransactionCmd),
#[command(subcommand, about = "Setting Smart Contract Parameters.")]
SmartContract(SmartContractCmd),
#[command(subcommand, about = "Setting Custom Parameters.")]
Custom(CustomCmd),
}

impl RunArgs for ParamCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all!((self, context), { ParamCmd::Sumeragi, ParamCmd::Block, ParamCmd::Transaction, ParamCmd::SmartContract, ParamCmd::Custom, })
}
}

#[derive(Subcommand, Debug)]
enum SumeragiCmd {
#[command(about = "Set block time in milliseconds.")]
BlockTime {
#[arg(long)]
value: u64,
},

#[command(about = "Set commit time in milliseconds.")]
CommitTime {
#[arg(long)]
value: u64,
},
}

impl RunArgs for SumeragiCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all! ((self, context), { SumeragiCmd::BlockTime, SumeragiCmd::CommitTime, })
}
}

#[derive(Subcommand, Debug)]
pub enum BlockCmd {
#[command(about = "Set parameter for maximum transactions.")]
MaxTransanctions {
#[arg(long)]
value: NonZeroU64,
},
}

impl RunArgs for BlockCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all! ((self, context), { BlockCmd::MaxTransanctions, })
}
}

#[derive(Subcommand, Debug)]
pub enum TransactionCmd {
#[command(about = "Set parameter for maximum instrutions.")]
MaxInstructions {
#[arg(long)]
value: NonZeroU64,
},

#[command(about = "Set parameter for smart contract size.")]
SmartContractSize {
#[arg(long)]
value: NonZeroU64,
},
}

impl RunArgs for TransactionCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all! ((self, context), { TransactionCmd::MaxInstructions, TransactionCmd::SmartContractSize, })
}
}

#[derive(Subcommand, Debug)]
pub enum SmartContractCmd {
#[command(
about = "Setting parameter for maximum amount of fuel that a smart contract can use."
)]
Fuel {
#[arg(long)]
value: NonZeroU64,
},

#[command(about = "Setting the maximum amount of memory for each smart contract.")]
Memory {
#[arg(long)]
value: NonZeroU64,
},
}

impl RunArgs for SmartContractCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all! ((self, context), { SmartContractCmd::Fuel, SmartContractCmd::Memory, })
}
}

#[derive(Subcommand, Debug)]
pub enum CustomCmd {
#[command(about = "Set the unique id of the custom parameter.")]
Id {
#[arg(long)]
value: String,
},

#[command(about = "Set the JSON-encoded payload.")]
PayLoad {
#[arg(long)]
value: JsonString,
},
}

impl RunArgs for CustomCmd {
fn run(self, context: &mut dyn RunContext) -> Result<()> {
match_all! ((self, context), { CustomCmd::Id, CustomCmd::PayLoad, })
}
}
}

mod wasm {
use std::{io::Read, path::PathBuf};

Expand Down
Loading