Skip to content

Commit cd9778e

Browse files
Initial commit for parameters cli module.
Signed-off-by: Divya Ranjan <[email protected]>
1 parent b9f2844 commit cd9778e

File tree

1 file changed

+194
-2
lines changed

1 file changed

+194
-2
lines changed

client_cli/src/main.rs

+194-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Iroha client CLI
2-
32
use std::{
43
fs::{self, read as read_file},
54
io::{stdin, stdout},
@@ -101,6 +100,9 @@ enum Subcommand {
101100
/// The subcommand related to p2p networking
102101
#[clap(subcommand)]
103102
Peer(peer::Args),
103+
/// The subcommand related to adding config parameters.
104+
#[clap(subcommand)]
105+
Parameters(param::Args),
104106
/// The subcommand related to event streaming
105107
#[clap(subcommand)]
106108
Events(events::Args),
@@ -165,7 +167,7 @@ macro_rules! match_all {
165167
impl RunArgs for Subcommand {
166168
fn run(self, context: &mut dyn RunContext) -> Result<()> {
167169
use Subcommand::*;
168-
match_all!((self, context), { Domain, Account, Asset, Peer, Events, Wasm, Blocks, Json })
170+
match_all!((self, context), { Domain, Account, Asset, Peer, Parameters, Events, Wasm, Blocks, Json })
169171
}
170172
}
171173

@@ -1090,6 +1092,196 @@ mod peer {
10901092
}
10911093
}
10921094

1095+
mod param {
1096+
use super::*;
1097+
use clap::Subcommand;
1098+
use core::num::NonZeroU64;
1099+
use iroha::client::{self, *};
1100+
use iroha_data_model::isi::SetParameter;
1101+
use iroha_data_model::parameter::{self, *};
1102+
1103+
// #[derive(Parser, Debug)]
1104+
// #[command(author, version, about, long_about = None)]
1105+
#[derive(Subcommand, Debug)]
1106+
pub enum Args {
1107+
// #[command(subcommand)]
1108+
// Parameters(ParamCmd),
1109+
#[command(subcommand)]
1110+
Get(Box<Get>),
1111+
#[command(subcommand)]
1112+
Set(Box<Set>),
1113+
}
1114+
1115+
impl RunArgs for Args {
1116+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1117+
match_all!((self, context), { Args::Get, Args::Set, })
1118+
}
1119+
}
1120+
1121+
#[derive(Subcommand, Debug)]
1122+
pub enum Get {
1123+
All,
1124+
#[command(flatten)]
1125+
Inner(ParamCmd),
1126+
}
1127+
1128+
/// Get values of parameters
1129+
impl RunArgs for Get {
1130+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1131+
let client = context.client_from_config();
1132+
let vec = match self {
1133+
Self::All => client
1134+
.request(client::parameter::all())
1135+
.wrap_err("Failed to get all the parameters."),
1136+
Self::Inner(param) => client
1137+
.build_query(client::parameter::all())
1138+
.with_filter(ParamCmd::Sumeragi)
1139+
.execute()
1140+
.wrap_err("Failed to get the parameter value"),
1141+
}?;
1142+
context.print_data(&vec.collect::<QueryResult<Vec<_>>>()?)?;
1143+
Ok(())
1144+
}
1145+
}
1146+
1147+
/// Set values for parameters
1148+
#[derive(clap::Subcommand, Debug)]
1149+
pub enum Set {
1150+
All,
1151+
#[command(flatten)]
1152+
Inner(ParamCmd),
1153+
}
1154+
1155+
impl RunArgs for Set {
1156+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1157+
let set_param_value = SetParameter::new(0);
1158+
// Not sure what to do.
1159+
Ok(())
1160+
}
1161+
}
1162+
1163+
#[derive(Subcommand, Debug)]
1164+
pub enum ParamCmd {
1165+
#[command(subcommand, about = "Setting Sumeragi Parameters.")]
1166+
Sumeragi(SumeragiCmd),
1167+
#[command(subcommand, about = "Setting Block Parameters.")]
1168+
Block(BlockCmd),
1169+
#[command(subcommand, about = "Setting Transaction Parameters.")]
1170+
Transaction(TransactionCmd),
1171+
#[command(subcommand, about = "Setting Smart Contract Parameters.")]
1172+
SmartContract(SmartContractCmd),
1173+
#[command(subcommand, about = "Setting Custom Parameters.")]
1174+
Custom(CustomCmd),
1175+
}
1176+
1177+
impl RunArgs for ParamCmd {
1178+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1179+
match_all!((self, context), { ParamCmd::Sumeragi, ParamCmd::Block, ParamCmd::Transaction, ParamCmd::SmartContract, ParamCmd::Custom, })
1180+
}
1181+
}
1182+
1183+
#[derive(Subcommand, Debug)]
1184+
enum SumeragiCmd {
1185+
#[command(about = "Set block time in milliseconds.")]
1186+
BlockTime {
1187+
#[arg(long)]
1188+
value: u64,
1189+
},
1190+
1191+
#[command(about = "Set commit time in milliseconds.")]
1192+
CommitTime {
1193+
#[arg(long)]
1194+
value: u64,
1195+
},
1196+
}
1197+
1198+
impl RunArgs for SumeragiCmd {
1199+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1200+
match_all! ((self, context), { SumeragiCmd::BlockTime, SumeragiCmd::CommitTime, })
1201+
}
1202+
}
1203+
1204+
#[derive(Subcommand, Debug)]
1205+
pub enum BlockCmd {
1206+
#[command(about = "Set parameter for maximum transactions.")]
1207+
MaxTransanctions {
1208+
#[arg(long)]
1209+
value: NonZeroU64,
1210+
},
1211+
}
1212+
1213+
impl RunArgs for BlockCmd {
1214+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1215+
match_all! ((self, context), { BlockCmd::MaxTransanctions, })
1216+
}
1217+
}
1218+
1219+
#[derive(Subcommand, Debug)]
1220+
pub enum TransactionCmd {
1221+
#[command(about = "Set parameter for maximum instrutions.")]
1222+
MaxInstructions {
1223+
#[arg(long)]
1224+
value: NonZeroU64,
1225+
},
1226+
1227+
#[command(about = "Set parameter for smart contract size.")]
1228+
SmartContractSize {
1229+
#[arg(long)]
1230+
value: NonZeroU64,
1231+
},
1232+
}
1233+
1234+
impl RunArgs for TransactionCmd {
1235+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1236+
match_all! ((self, context), { TransactionCmd::MaxInstructions, TransactionCmd::SmartContractSize, })
1237+
}
1238+
}
1239+
1240+
#[derive(Subcommand, Debug)]
1241+
pub enum SmartContractCmd {
1242+
#[command(
1243+
about = "Setting parameter for maximum amount of fuel that a smart contract can use."
1244+
)]
1245+
Fuel {
1246+
#[arg(long)]
1247+
value: NonZeroU64,
1248+
},
1249+
1250+
#[command(about = "Setting the maximum amount of memory for each smart contract.")]
1251+
Memory {
1252+
#[arg(long)]
1253+
value: NonZeroU64,
1254+
},
1255+
}
1256+
1257+
impl RunArgs for SmartContractCmd {
1258+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1259+
match_all! ((self, context), { SmartContractCmd::Fuel, SmartContractCmd::Memory, })
1260+
}
1261+
}
1262+
1263+
#[derive(Subcommand, Debug)]
1264+
pub enum CustomCmd {
1265+
#[command(about = "Set the unique id of the custom parameter.")]
1266+
Id {
1267+
#[arg(long)]
1268+
value: String,
1269+
},
1270+
1271+
#[command(about = "Set the JSON-encoded payload.")]
1272+
PayLoad {
1273+
#[arg(long)]
1274+
value: JsonString,
1275+
},
1276+
}
1277+
1278+
impl RunArgs for CustomCmd {
1279+
fn run(self, context: &mut dyn RunContext) -> Result<()> {
1280+
match_all! ((self, context), { CustomCmd::Id, CustomCmd::PayLoad, })
1281+
}
1282+
}
1283+
}
1284+
10931285
mod wasm {
10941286
use std::{io::Read, path::PathBuf};
10951287

0 commit comments

Comments
 (0)