diff --git a/.github/workflows/linux-ci-rust.yml b/.github/workflows/linux-ci-rust.yml index cd6302e6aa0..768af167d88 100644 --- a/.github/workflows/linux-ci-rust.yml +++ b/.github/workflows/linux-ci-rust.yml @@ -53,8 +53,7 @@ jobs: - name: Run tests run: | - cargo llvm-cov test --no-fail-fast --lcov --output-path coverage.info - working-directory: rust + tools/rust-coverage - name: Gather and check Rust code coverage run: | diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 3d7c0c29603..ed95ae6b7a0 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2131,6 +2131,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "wallet_core_bin" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "tw_coin_registry", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 183f29e20a6..125780d2be9 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -27,6 +27,7 @@ members = [ "tw_number", "tw_proto", "tw_utxo", + "wallet_core_bin", "wallet_core_rs", ] diff --git a/rust/tw_coin_registry/src/registry.rs b/rust/tw_coin_registry/src/registry.rs index 595ed152f28..37db97f44f1 100644 --- a/rust/tw_coin_registry/src/registry.rs +++ b/rust/tw_coin_registry/src/registry.rs @@ -14,7 +14,7 @@ use tw_keypair::tw::PublicKeyType; type RegistryMap = HashMap; /// cbindgen:ignore -const REGISTRY_JSON: &str = +pub const REGISTRY_JSON: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../registry.json")); lazy_static! { diff --git a/rust/wallet_core_bin/Cargo.toml b/rust/wallet_core_bin/Cargo.toml new file mode 100644 index 00000000000..49896eb72b8 --- /dev/null +++ b/rust/wallet_core_bin/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "wallet_core_bin" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = "1.0" +serde_json = "1.0" +tw_coin_registry = { path = "../tw_coin_registry" } diff --git a/rust/wallet_core_bin/src/main.rs b/rust/wallet_core_bin/src/main.rs new file mode 100644 index 00000000000..cf9e7c554ac --- /dev/null +++ b/rust/wallet_core_bin/src/main.rs @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +mod registry_stats; + +#[derive(Debug)] +enum Error { + MissingArguments, + UnknownCommand, + InvalidRegistry, + InvalidRegistryParam { + #[allow(dead_code)] + param: &'static str, + }, +} + +fn help() { + println!("WalletCore binary tools:"); + println!(); + println!("\tregistry-stats Print registry statistic (e.g Rust transition progress)"); +} + +fn main() -> Result<(), Error> { + let args: Vec = std::env::args().collect(); + + if args.len() < 2 { + help(); + return Err(Error::MissingArguments); + } + + match args[1].as_str() { + "registry-stats" => registry_stats::registry_stats(), + _ => { + help(); + Err(Error::UnknownCommand) + }, + } +} diff --git a/rust/wallet_core_bin/src/registry_stats.rs b/rust/wallet_core_bin/src/registry_stats.rs new file mode 100644 index 00000000000..8ebd6b6dda7 --- /dev/null +++ b/rust/wallet_core_bin/src/registry_stats.rs @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: Apache-2.0 +// +// Copyright © 2017 Trust Wallet. + +use crate::Error; +use serde_json::Value as Json; +use std::collections::HashSet; +use tw_coin_registry::blockchain_type::BlockchainType; +use tw_coin_registry::registry; + +struct BlockchainStats { + total: usize, + supported: usize, +} + +fn blockchain_stats() -> Result { + let chains: Vec = + serde_json::from_str(registry::REGISTRY_JSON).map_err(|_| Error::InvalidRegistry)?; + + fn extract_blockchain(chain: &Json) -> Option<(&str, BlockchainType)> { + let blockchain_val = chain.get("blockchain")?; + let blockchain_str = chain["blockchain"].as_str()?; + let blockchain_type: BlockchainType = + serde_json::from_value(blockchain_val.clone()).ok()?; + Some((blockchain_str, blockchain_type)) + } + + let mut supported = HashSet::new(); + let mut total = HashSet::new(); + + for chain in chains.iter() { + let (blockchain_str, blockchain_type) = + extract_blockchain(chain).ok_or(Error::InvalidRegistryParam { + param: "blockchain", + })?; + + total.insert(blockchain_str); + match blockchain_type { + BlockchainType::Unsupported => (), + _ => { + supported.insert(blockchain_str); + }, + } + } + + Ok(BlockchainStats { + total: total.len(), + supported: supported.len(), + }) +} + +pub fn registry_stats() -> Result<(), Error> { + let total_chains = registry::registry_iter().count(); + let chains_in_rust = registry::supported_coin_items().count(); + let rust_chains_progress = (chains_in_rust * 100) as f64 / total_chains as f64; + + let blockchain_stats = blockchain_stats()?; + let blockchain_progress = + (blockchain_stats.supported * 100) as f64 / blockchain_stats.total as f64; + + println!(); + println!("total chains: {total_chains}"); + println!("supported chains: {chains_in_rust}"); + println!("chains transition progress: {rust_chains_progress:.1}%"); + + println!(); + println!("total blockchain impls: {}", blockchain_stats.total); + println!("supported blockchain impls: {}", blockchain_stats.supported); + println!("blockchains transition progress: {blockchain_progress:.1}%"); + + Ok(()) +} diff --git a/tools/registry b/tools/registry index c044a37394c..2f322890910 100755 --- a/tools/registry +++ b/tools/registry @@ -2,8 +2,9 @@ import argparse import json -import sys import requests +import subprocess +import sys PATH_TO_REGISTRY_JSON = "registry.json" @@ -34,6 +35,21 @@ def ping_explorers(_args): print("\tError: ", exception) +def registry_stats(_args): + """Print registry stats""" + subprocess.run([ + "cargo", + "run", + "--manifest-path", + "rust/Cargo.toml", + "-p", + "wallet_core_bin", + "--", + "registry-stats" + ], + shell=False) + + if __name__ == '__main__': parser = argparse.ArgumentParser(description="Operations over registry.json") subparsers = parser.add_subparsers() @@ -42,5 +58,9 @@ if __name__ == '__main__': help="Ping blockchain explorers inside 'registry.json'") ping_explorers_parser.set_defaults(func=ping_explorers) + registry_stats_parser = subparsers.add_parser('registry-stats', + help="Print registry statistic (e.g Rust transition progress)") + registry_stats_parser.set_defaults(func=registry_stats) + args = parser.parse_args() args.func(args) diff --git a/tools/rust-bindgen b/tools/rust-bindgen index 1b0afa3a6a5..0ca640b7766 100755 --- a/tools/rust-bindgen +++ b/tools/rust-bindgen @@ -49,14 +49,14 @@ fi if [[ "$NATIVE" == "true" ]]; then echo "Generating Native target" - cargo build --release + cargo build --release --lib fi export RUSTFLAGS="-Zlocation-detail=none" if [[ "$WASM" == "true" ]]; then echo "Generating WASM target" - cargo build -Z build-std=std,panic_abort --target wasm32-unknown-emscripten --release + cargo build -Z build-std=std,panic_abort --target wasm32-unknown-emscripten --release --lib fi if [[ "$ANDROID" == "true" ]]; then @@ -69,12 +69,12 @@ if [[ "$ANDROID" == "true" ]]; then export CC_armv7_linux_androideabi="$NDK_BIN_PATH/armv7a-linux-androideabi$NDK_API_LEVEL-clang" echo "Generating Android targets" - cargo build -Z build-std=std,panic_abort --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --release + cargo build -Z build-std=std,panic_abort --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --release --lib fi if [[ "$IOS" == "true" ]]; then echo "Generating iOS targets" - cargo build -Z build-std=std,panic_abort --target aarch64-apple-ios --target aarch64-apple-ios-sim --target x86_64-apple-ios --target aarch64-apple-darwin --target x86_64-apple-darwin --target aarch64-apple-ios-macabi --target x86_64-apple-ios-macabi --release & + cargo build -Z build-std=std,panic_abort --target aarch64-apple-ios --target aarch64-apple-ios-sim --target x86_64-apple-ios --target aarch64-apple-darwin --target x86_64-apple-darwin --target aarch64-apple-ios-macabi --target x86_64-apple-ios-macabi --release --lib & wait lipo $BUILD_FOLDER/x86_64-apple-ios/release/$TARGET_NAME $BUILD_FOLDER/aarch64-apple-ios-sim/release/$TARGET_NAME -create -output $BUILD_FOLDER/$TARGET_NAME mkdir -p $BUILD_FOLDER/darwin_universal diff --git a/tools/rust-coverage b/tools/rust-coverage index f89753f935f..835b13997b3 100755 --- a/tools/rust-coverage +++ b/tools/rust-coverage @@ -18,10 +18,10 @@ pushd rust # Generate HTML report if requested if [[ "$1" == "html" ]]; then - cargo llvm-cov test --html + cargo llvm-cov test --workspace --exclude wallet_core_bin --html cargo llvm-cov report --lcov --output-path coverage.info else - cargo llvm-cov test --lcov --output-path coverage.info + cargo llvm-cov test --workspace --exclude wallet_core_bin --lcov --output-path coverage.info fi popd diff --git a/tools/rust-test b/tools/rust-test index 1b20938f589..ad72918e65b 100755 --- a/tools/rust-test +++ b/tools/rust-test @@ -18,7 +18,7 @@ if [[ "$1" == "wasm" ]]; then source ../emsdk/emsdk_env.sh export CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node - cargo test --target wasm32-unknown-emscripten --profile wasm-test + cargo test --target wasm32-unknown-emscripten --profile wasm-test --workspace --exclude wallet_core_bin else cargo test --all fi