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: ckb init allow set ba-data #1035

Merged
merged 2 commits into from
Jun 18, 2019
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
7 changes: 5 additions & 2 deletions ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ 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_else(|| "0x".to_string())
)
}
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 @@ -55,4 +55,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>,
}
32 changes: 32 additions & 0 deletions util/app-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 const ARG_FROM: &str = "from";
pub const ARG_TO: &str = "to";

Expand Down Expand Up @@ -263,6 +264,7 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_CODE_HASH)
.long(ARG_BA_CODE_HASH)
.value_name("code_hash")
.validator(is_hex)
.takes_value(true)
.help(
"Sets code_hash in [block_assembler] \
Expand All @@ -273,10 +275,18 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_ARG)
.long(ARG_BA_ARG)
.value_name("arg")
.validator(is_hex)
.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(is_hex)
.help("Sets data in [block_assembler]"),
)
.arg(
Arg::with_name("export-specs")
.long("export-specs")
Expand All @@ -291,3 +301,25 @@ fn init() -> App<'static, 'static> {
.hidden(true),
)
}

fn is_hex(hex: String) -> Result<(), String> {
let tmp = hex.as_bytes();
if tmp.len() < 2 {
Err("Must be a 0x-prefixed 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"[..] {
for byte in &tmp[2..] {
match byte {
b'A'...b'F' | b'a'...b'f' | b'0'...b'9' => continue,
invalid_char => {
return Err(format!("Hex has invalid char: {}", invalid_char));
}
}
}

Ok(())
} else {
Err("Must 0x-prefixed hexadecimal 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 @@ -213,6 +213,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 @@ -225,6 +226,7 @@ impl Setup {
log_to_stdout,
block_assembler_code_hash,
block_assembler_args,
block_assembler_data,
})
}

Expand Down