Skip to content

Commit

Permalink
feat: ckb init allow set ba-data, also verify that the input is valid…
Browse files Browse the repository at this point in the history
… hex
  • Loading branch information
driftluo committed Jun 16, 2019
1 parent bd8a964 commit 2bd67a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
format!(
"[block_assembler]\n\
code_hash = \"{}\"\n\
args = [ \"{}\" ]",
args = [ \"{}\" ]\n\
data = \"{}\"",
hash,
args.block_assembler_args.join("\", \"")
args.block_assembler_args.join("\", \""),
args.block_assembler_data.unwrap_or_default()
)
}
None => {
Expand Down
1 change: 1 addition & 0 deletions util/app-config/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pub struct InitArgs {
pub force: bool,
pub block_assembler_code_hash: Option<String>,
pub block_assembler_args: Vec<String>,
pub block_assembler_data: Option<String>,
}
23 changes: 23 additions & 0 deletions util/app-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const ARG_LOG_TO: &str = "log-to";
pub const ARG_BUNDLED: &str = "bundled";
pub const ARG_BA_CODE_HASH: &str = "ba-code-hash";
pub const ARG_BA_ARG: &str = "ba-arg";
pub const ARG_BA_DATA: &str = "ba-data";

pub fn get_matches(version: &Version) -> ArgMatches<'static> {
App::new("ckb")
Expand Down Expand Up @@ -238,6 +239,7 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_CODE_HASH)
.long(ARG_BA_CODE_HASH)
.value_name("code_hash")
.validator(|code_hash| is_hex(code_hash.as_str()))
.takes_value(true)
.help(
"Sets code_hash in [block_assembler] \
Expand All @@ -248,10 +250,18 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_ARG)
.long(ARG_BA_ARG)
.value_name("arg")
.validator(|arg| is_hex(arg.as_str()))
.multiple(true)
.number_of_values(1)
.help("Sets args in [block_assembler]"),
)
.arg(
Arg::with_name(ARG_BA_DATA)
.long(ARG_BA_DATA)
.value_name("data")
.validator(|data| is_hex(data.as_str()))
.help("Sets data in [block_assembler]"),
)
.arg(
Arg::with_name("export-specs")
.long("export-specs")
Expand All @@ -266,3 +276,16 @@ fn init() -> App<'static, 'static> {
.hidden(true),
)
}

fn is_hex(hex: &str) -> Result<(), String> {
let tmp = hex.as_bytes();
if tmp.len() < 2 {
Err("Must be a hexadecimal string".to_string())
} else if tmp.len() & 1 != 0 {
Err("Hexadecimal strings must be of even length".to_string())
} else if tmp[..2] == b"0x"[..] {
Ok(())
} else {
Err("Must hex string".to_string())
}
}
2 changes: 2 additions & 0 deletions util/app-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl Setup {
.unwrap_or_default()
.map(str::to_string)
.collect();
let block_assembler_data = matches.value_of(cli::ARG_BA_DATA).map(str::to_string);

Ok(InitArgs {
root_dir,
Expand All @@ -197,6 +198,7 @@ impl Setup {
log_to_stdout,
block_assembler_code_hash,
block_assembler_args,
block_assembler_data,
})
}

Expand Down

0 comments on commit 2bd67a9

Please sign in to comment.