From 194f1dc677d33ae3bfb25aaa434de05fcb94141d Mon Sep 17 00:00:00 2001 From: mohanson Date: Thu, 20 Feb 2025 18:14:30 +0800 Subject: [PATCH] Add a convenience method for specifing script --- ckb-debugger/src/main.rs | 65 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/ckb-debugger/src/main.rs b/ckb-debugger/src/main.rs index c280002..e5d434d 100644 --- a/ckb-debugger/src/main.rs +++ b/ckb-debugger/src/main.rs @@ -113,12 +113,18 @@ fn main() -> Result<(), Box> { .help("Read content from local file or stdin. Then feed the content to syscall in scripts") .takes_value(true), ) + .arg( + Arg::with_name("script") + .long("script") + .default_value("input.0.lock") + .help("A convenience method for setting cell-type, cell-index and script-group-type at the same time") + .takes_value(true), + ) .arg( Arg::with_name("script-group-type") .long("script-group-type") .short("s") .possible_values(&["lock", "type"]) - .default_value("lock") .help("Script group type") .takes_value(true), ) @@ -153,6 +159,7 @@ fn main() -> Result<(), Box> { let matches_pprof = matches.value_of("pprof"); let matches_prompt = matches.is_present("prompt"); let matches_read_file_name = matches.value_of("read-file"); + let matches_script = matches.value_of("script"); let matches_script_group_type = matches.value_of("script-group-type"); let matches_script_hash = matches.value_of("script-hash"); let matches_script_version = matches.value_of("script-version").unwrap(); @@ -277,41 +284,33 @@ fn main() -> Result<(), Box> { MockTransaction { mock_info: mock_info, tx: tx.data() } } }; - let verifier_script_group_type = { - let script_group_type = if matches_tx_file.is_none() { "lock" } else { matches_script_group_type.unwrap() }; - serde_plain::from_str(script_group_type)? + let verifier_cell_type = match matches_cell_type { + Some(data) => data, + None => matches_script.unwrap().split(".").collect::>()[0], }; - let verifier_script_hash = if matches_tx_file.is_none() { - verifier_mock_tx.mock_info.inputs[0].output.calc_lock_hash() - } else if let Some(hex_script_hash) = matches_script_hash { - if hex_script_hash.len() != 66 || (!hex_script_hash.starts_with("0x")) { - panic!("Invalid script hash format!"); + let verifier_cell_index: usize = match matches_cell_index { + Some(data) => data, + None => matches_script.unwrap().split(".").collect::>()[1], + } + .parse()?; + let verifier_script_group_type: ScriptGroupType = match matches_script_group_type { + Some(data) => serde_plain::from_str(data)?, + None => serde_plain::from_str(matches_script.unwrap().split(".").collect::>()[2])?, + }; + let verifier_script_hash = || -> Result> { + if matches_tx_file.is_none() { + return Ok(verifier_mock_tx.mock_info.inputs[0].output.calc_lock_hash()); } - let b = hex::decode(&hex_script_hash.as_bytes()[2..])?; - Byte32::from_slice(b.as_slice())? - } else { - let mut cell_type = matches_cell_type; - let mut cell_index = matches_cell_index; - match verifier_script_group_type { - ScriptGroupType::Lock => { - if cell_type.is_none() { - cell_type = Some("input"); - } - if cell_index.is_none() { - cell_index = Some("0"); - println!("The cell_index is not specified. Assume --cell-index = 0") - } - } - ScriptGroupType::Type => { - if cell_type.is_none() || cell_index.is_none() { - panic!("You must provide either script hash, or cell type + cell index"); - } - } + if let Some(hex_script_hash) = matches_script_hash { + return Ok(Byte32::from_slice(hex::decode(&hex_script_hash.as_bytes()[2..])?.as_slice())?); } - let cell_type = cell_type.unwrap(); - let cell_index: usize = cell_index.unwrap().parse()?; - get_script_hash_by_index(&verifier_mock_tx, &verifier_script_group_type, cell_type, cell_index) - }; + Ok(get_script_hash_by_index( + &verifier_mock_tx, + &verifier_script_group_type, + verifier_cell_type, + verifier_cell_index, + )) + }()?; let verifier_script_version = match matches_script_version { "0" => ScriptVersion::V0, "1" => ScriptVersion::V1,