Skip to content

Commit

Permalink
[Misc]: Add WalletCore binary binary tools (#3865)
Browse files Browse the repository at this point in the history
* [Misc]: Add wallet_core_bin crate with binary tools

* Add `registry-stats` command

* [Misc]: Add a helper function to `tools/registry`

* [Misc]: Fix build and test scripts

* [Misc]: Fix linux-ci-rust CI

* [Misc]: Use a script in linux-ci-rust.yml

* [Misc]: Fix linux-ci-rust.yml
  • Loading branch information
satoshiotomakan authored May 31, 2024
1 parent a3d763b commit 55ca3e5
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/linux-ci-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
9 changes: 9 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"tw_number",
"tw_proto",
"tw_utxo",
"wallet_core_bin",
"wallet_core_rs",
]

Expand Down
2 changes: 1 addition & 1 deletion rust/tw_coin_registry/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tw_keypair::tw::PublicKeyType;
type RegistryMap = HashMap<CoinType, CoinItem>;

/// cbindgen:ignore
const REGISTRY_JSON: &str =
pub const REGISTRY_JSON: &str =
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../registry.json"));

lazy_static! {
Expand Down
9 changes: 9 additions & 0 deletions rust/wallet_core_bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
39 changes: 39 additions & 0 deletions rust/wallet_core_bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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)
},
}
}
72 changes: 72 additions & 0 deletions rust/wallet_core_bin/src/registry_stats.rs
Original file line number Diff line number Diff line change
@@ -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<BlockchainStats, Error> {
let chains: Vec<Json> =
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(())
}
22 changes: 21 additions & 1 deletion tools/registry
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import argparse
import json
import sys
import requests
import subprocess
import sys

PATH_TO_REGISTRY_JSON = "registry.json"

Expand Down Expand Up @@ -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()
Expand All @@ -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)
8 changes: 4 additions & 4 deletions tools/rust-bindgen
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tools/rust-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/rust-test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55ca3e5

Please sign in to comment.