From a5dc0835af5e2b82c7c5c9e1ee267db307d370bd Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 4 Jan 2024 17:20:22 +0800 Subject: [PATCH 1/5] Add cached dependency --- Cargo.lock | 151 ++++++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 1 + 2 files changed, 138 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7dc0762..24ae7ba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "0.7.20" @@ -69,6 +81,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -322,6 +340,39 @@ dependencies = [ "serde", ] +[[package]] +name = "cached" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b0116662497bc24e4b177c90eaf8870e39e2714c3fcfa296327a93f593fc21" +dependencies = [ + "ahash 0.8.7", + "cached_proc_macro", + "cached_proc_macro_types", + "hashbrown 0.14.3", + "instant", + "once_cell", + "thiserror", +] + +[[package]] +name = "cached_proc_macro" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c878c71c2821aa2058722038a59a67583a4240524687c6028571c9b395ded61f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "cached_proc_macro_types" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663" + [[package]] name = "cassowary" version = "0.3.0" @@ -416,6 +467,7 @@ dependencies = [ "atty", "bitcoin", "byteorder", + "cached", "chrono", "ckb-build-info", "ckb-chain-spec", @@ -1060,6 +1112,41 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + [[package]] name = "dashmap" version = "5.4.0" @@ -1067,7 +1154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc" dependencies = [ "cfg-if 1.0.0", - "hashbrown", + "hashbrown 0.12.3", "lock_api", "once_cell", "parking_lot_core 0.9.8", @@ -1385,7 +1472,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", ] [[package]] @@ -1529,7 +1616,17 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ - "ahash", + "ahash 0.7.7", +] + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +dependencies = [ + "ahash 0.8.7", + "allocator-api2", ] [[package]] @@ -1684,6 +1781,12 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.1.5" @@ -1733,7 +1836,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg 1.1.0", - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -1974,7 +2077,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e999beba7b6e8345721bd280141ed958096a2e4abdf74f67ff4ce49b4b54e47a" dependencies = [ - "hashbrown", + "hashbrown 0.12.3", ] [[package]] @@ -2209,7 +2312,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", ] [[package]] @@ -2457,9 +2560,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.60" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" +checksum = "907a61bd0f64c2f29cd1cf1dc34d05176426a3f504a78010f08416ddb7b13708" dependencies = [ "unicode-ident", ] @@ -2993,7 +3096,7 @@ checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", ] [[package]] @@ -3157,9 +3260,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.18" +version = "2.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" dependencies = [ "proc-macro2", "quote", @@ -3227,7 +3330,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", ] [[package]] @@ -3554,7 +3657,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", "wasm-bindgen-shared", ] @@ -3588,7 +3691,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.43", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3825,3 +3928,23 @@ checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" dependencies = [ "linked-hash-map", ] + +[[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.43", +] diff --git a/Cargo.toml b/Cargo.toml index 00d95748..0b316a89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ futures = "0.3" # remove this line when ckb-vm stick the version of ckb-vm-definitions ckb-vm-definitions = "=0.21.3" +cached = "0.47.0" [target.'cfg(unix)'.dependencies] tui = "0.6.0" From 5d793dcf431d1f5b998de4caef0345760a28b9b2 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 4 Jan 2024 17:24:23 +0800 Subject: [PATCH 2/5] Support fifo file type like `<(cat /tmp/x1)` --- src/utils/arg_parser.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/utils/arg_parser.rs b/src/utils/arg_parser.rs index 76f12d37..7abdec09 100644 --- a/src/utils/arg_parser.rs +++ b/src/utils/arg_parser.rs @@ -7,6 +7,9 @@ use std::path::PathBuf; use std::str::FromStr; use std::time::Duration; +#[cfg(unix)] +use std::os::unix::fs::FileTypeExt; + use clap::ArgMatches; use faster_hex::hex_decode; use url::Url; @@ -232,6 +235,15 @@ impl ArgParser for FilePathParser { fn parse(&self, input: &str) -> Result { let path = self.path_parser.parse(input)?; if path.exists() && !path.is_file() { + #[cfg(unix)] + if path + .metadata() + .map(|m| m.file_type().is_fifo()) + .unwrap_or_default() + { + return Ok(path); + } + Err(format!("path <{}> is not file", input)) } else { Ok(path) From ab5df1f62f39ff97dfdb304835539fc25bd84407 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 4 Jan 2024 17:24:53 +0800 Subject: [PATCH 3/5] Cache file content, since some path maybe fifo file --- src/utils/arg_parser.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/arg_parser.rs b/src/utils/arg_parser.rs index 7abdec09..60dc89b9 100644 --- a/src/utils/arg_parser.rs +++ b/src/utils/arg_parser.rs @@ -1,3 +1,4 @@ +use cached::proc_macro::cached; use std::fmt::Display; use std::fs; use std::io::Read; @@ -305,13 +306,21 @@ impl ArgParser> for HexFilePathParser { pub struct PrivkeyPathParser; +#[cached] +fn cache_read_privkey_file(path: PathBuf) -> Result { + let mut content = String::new(); + let mut file = fs::File::open(path).map_err(|err| err.to_string())?; + file.read_to_string(&mut content) + .map_err(|err| err.to_string())?; + Ok(content) +} + impl ArgParser for PrivkeyPathParser { fn parse(&self, input: &str) -> Result { let path: PathBuf = FilePathParser::new(true).parse(input)?; - let mut content = String::new(); - let mut file = fs::File::open(path).map_err(|err| err.to_string())?; - file.read_to_string(&mut content) - .map_err(|err| err.to_string())?; + + let content = cache_read_privkey_file(path)?; + let privkey_string: String = content .split_whitespace() .next() @@ -329,10 +338,9 @@ pub struct ExtendedPrivkeyPathParser; impl ArgParser for ExtendedPrivkeyPathParser { fn parse(&self, input: &str) -> Result { let path: PathBuf = FilePathParser::new(true).parse(input)?; - let mut content = String::new(); - let mut file = fs::File::open(path).map_err(|err| err.to_string())?; - file.read_to_string(&mut content) - .map_err(|err| err.to_string())?; + + let content = cache_read_privkey_file(path)?; + let lines = content .split_whitespace() .map(ToOwned::to_owned) From 9f49c415d4ffec1090298b400a3fbb2f26673a9b Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Wed, 10 Jan 2024 14:02:25 +0800 Subject: [PATCH 4/5] Add a test for account import process substitution support --- test/cli_test.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 test/cli_test.sh diff --git a/test/cli_test.sh b/test/cli_test.sh new file mode 100755 index 00000000..fcc8e1d3 --- /dev/null +++ b/test/cli_test.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -evxo pipefail + +# assert must contains one argument +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +CKB_CLI_BIN=$1 + +function account_import_process_substitution { + + HOME1=$(mktemp -d) + export CKB_CLI_HOME=${HOME1} + printf "abc123\nabc123" | ("${CKB_CLI_BIN}" account new) + + LOCK_ARG=$("${CKB_CLI_BIN}" account list --output-format json | jq '.[0].lock_arg' | sed 's/"//g') + + PRIV_PATH=$(mktemp -d)/privkey.text + printf "abc123" | ("${CKB_CLI_BIN}" account export --lock-arg ${LOCK_ARG} --extended-privkey-path ${PRIV_PATH}) + + HOME1=$(mktemp -d) + export CKB_CLI_HOME=${HOME1} + printf "ABC123" | "${CKB_CLI_BIN}" account import --privkey-path <(cat "${PRIV_PATH}") + +} + +account_import_process_substitution From 8302460a01e3d8b644efb01131b895929c4a97b6 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Wed, 10 Jan 2024 14:04:19 +0800 Subject: [PATCH 5/5] Add cli test to make integration --- devtools/ci/integration.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/devtools/ci/integration.sh b/devtools/ci/integration.sh index 50b78a0d..0b26eabf 100755 --- a/devtools/ci/integration.sh +++ b/devtools/ci/integration.sh @@ -51,6 +51,8 @@ cd plugin-protocol && cargo build --example keystore_no_password && cd .. rm -rf test/target && ln -snf "${CKB_CLI_DIR}/target" test/target export RUST_LOG=ckb_cli=info,cli_test=info +./test/cli_test.sh "${CKB_CLI_DIR}/target/release/ckb-cli" + cd test && cargo run -- \ --ckb-bin "${CKB_BIN}" \ --cli-bin "${CKB_CLI_DIR}/target/release/ckb-cli" \