From e919605f353897e41a8f48a6499d31f1034e720e Mon Sep 17 00:00:00 2001 From: chanhsu Date: Mon, 21 Mar 2022 22:17:18 +0800 Subject: [PATCH] fix(ckb cli) add h256 validator for --assume-valid-target and ba-code-hash --- util/app-config/src/cli.rs | 12 +++++- util/app-config/src/tests/cli.rs | 71 ++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/util/app-config/src/cli.rs b/util/app-config/src/cli.rs index a147e7259a..e381b1c498 100644 --- a/util/app-config/src/cli.rs +++ b/util/app-config/src/cli.rs @@ -180,7 +180,7 @@ fn run<'help>() -> Command<'help> { Arg::new(ARG_ASSUME_VALID_TARGET) .long(ARG_ASSUME_VALID_TARGET) .takes_value(true) - .validator(is_hex) + .validator(is_h256) .help("This parameter specifies the hash of a block. \ When the height does not reach this block's height, the execution of the script will be disabled, \ that is, skip verifying the script content. \ @@ -417,7 +417,7 @@ fn init<'help>() -> Command<'help> { Arg::new(ARG_BA_CODE_HASH) .long(ARG_BA_CODE_HASH) .value_name("code_hash") - .validator(is_hex) + .validator(is_h256) .takes_value(true) .help( "Sets code_hash in [block_assembler] \ @@ -526,3 +526,11 @@ fn is_hex(hex: &str) -> Result<(), String> { Err("Must 0x-prefixed hexadecimal string".to_string()) } } + +fn is_h256(hex: &str) -> Result<(), String> { + if hex.len() != 66 { + Err("Must be 0x-prefixed hexadecimal string and string length is 66".to_owned()) + } else { + is_hex(hex) + } +} diff --git a/util/app-config/src/tests/cli.rs b/util/app-config/src/tests/cli.rs index 411d9e365a..a92e33f86d 100644 --- a/util/app-config/src/tests/cli.rs +++ b/util/app-config/src/tests/cli.rs @@ -37,7 +37,7 @@ fn ba_message_requires_ba_arg_or_ba_code_hash() { "--ba-arg", "0x00", ]); - let ok_ba_code_hash = basic_app().try_get_matches_from(&[ + let ba_code_hash = basic_app().try_get_matches_from(&[ BIN_NAME, "init", "--ba-message", @@ -52,11 +52,7 @@ fn ba_message_requires_ba_arg_or_ba_code_hash() { "--ba-message is ok with --ba-arg, but gets error: {:?}", ok_ba_arg.err() ); - assert!( - ok_ba_code_hash.is_ok(), - "--ba-message is ok with --ba-code-hash, but gets error: {:?}", - ok_ba_code_hash.err() - ); + assert!(ba_code_hash.is_err()); assert!( err.is_err(), "--ba-message requires --ba-arg or --ba-code-hash" @@ -73,7 +69,7 @@ fn ba_message_requires_ba_arg_or_ba_code_hash() { #[test] fn ba_arg_and_ba_code_hash() { - let ok_matches = basic_app().try_get_matches_from(&[ + let matches = basic_app().try_get_matches_from(&[ BIN_NAME, "init", "--ba-code-hash", @@ -81,11 +77,7 @@ fn ba_arg_and_ba_code_hash() { "--ba-arg", "0x00", ]); - assert!( - ok_matches.is_ok(), - "--ba-code-hash is OK with --ba-arg, but gets error: {:?}", - ok_matches.err() - ); + assert!(matches.is_err()); } #[test] @@ -97,3 +89,58 @@ fn ba_advanced() { assert_eq!(1, sub_matches.occurrences_of(ARG_BA_ADVANCED)); } + +#[test] +/// 2 cases in which use h256 validator: +/// ckb init --ba-code-hash +/// ckb run --assume-valid-target +/// not for `ckb init --ba-arg` && `ckb init --ba-message` +fn h256_as_validator() { + let ok_matches = basic_app().try_get_matches_from(&[ + BIN_NAME, + "init", + "--ba-code-hash", + "0x00d1b86f6824d33a91b72ec20e2118cf7788a5ffff656bd1ea1ea638c764cb5f", + "--ba-arg", + "0x00", + ]); + assert!(ok_matches.is_ok()); + + let err_matches = basic_app().try_get_matches_from(&[ + BIN_NAME, + "init", + "--ba-code-hash", + "0xd1b86f6824d33a91b72ec20e2118cf7788a5ffff656bd1ea1ea638c764cb5f", + "--ba-arg", + "0x00", + ]); + let err = err_matches.err().unwrap(); + assert_eq!(clap::ErrorKind::ValueValidation, err.kind()); + + let err_matches = basic_app().try_get_matches_from(&[ + BIN_NAME, + "init", + "--ba-code-hash", + "0x4630c0", + "--ba-arg", + "0x00", + ]); + let err = err_matches.err().unwrap(); + assert_eq!(clap::ErrorKind::ValueValidation, err.kind()); + + let ok_matches = basic_app().try_get_matches_from(&[ + BIN_NAME, + "run", + "--assume-valid-target", + "0x94a4e93601f7295501891764880d37e9fcf886d02bf64b3d06f9137db8fa981e", + ]); + assert!(ok_matches.is_ok()); + let err_matches = basic_app().try_get_matches_from(&[ + BIN_NAME, + "run", + "--assume-valid-target", + "0x94a4e93601f729550", + ]); + let err = err_matches.err().unwrap(); + assert_eq!(clap::ErrorKind::ValueValidation, err.kind()); +}