Skip to content

Commit 55ca3e5

Browse files
[Misc]: Add WalletCore binary binary tools (#3865)
* [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
1 parent a3d763b commit 55ca3e5

File tree

11 files changed

+160
-11
lines changed

11 files changed

+160
-11
lines changed

.github/workflows/linux-ci-rust.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ jobs:
5353

5454
- name: Run tests
5555
run: |
56-
cargo llvm-cov test --no-fail-fast --lcov --output-path coverage.info
57-
working-directory: rust
56+
tools/rust-coverage
5857
5958
- name: Gather and check Rust code coverage
6059
run: |

rust/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ members = [
2727
"tw_number",
2828
"tw_proto",
2929
"tw_utxo",
30+
"wallet_core_bin",
3031
"wallet_core_rs",
3132
]
3233

rust/tw_coin_registry/src/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tw_keypair::tw::PublicKeyType;
1414
type RegistryMap = HashMap<CoinType, CoinItem>;
1515

1616
/// cbindgen:ignore
17-
const REGISTRY_JSON: &str =
17+
pub const REGISTRY_JSON: &str =
1818
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../registry.json"));
1919

2020
lazy_static! {

rust/wallet_core_bin/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "wallet_core_bin"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = "1.0"
8+
serde_json = "1.0"
9+
tw_coin_registry = { path = "../tw_coin_registry" }

rust/wallet_core_bin/src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright © 2017 Trust Wallet.
4+
5+
mod registry_stats;
6+
7+
#[derive(Debug)]
8+
enum Error {
9+
MissingArguments,
10+
UnknownCommand,
11+
InvalidRegistry,
12+
InvalidRegistryParam {
13+
#[allow(dead_code)]
14+
param: &'static str,
15+
},
16+
}
17+
18+
fn help() {
19+
println!("WalletCore binary tools:");
20+
println!();
21+
println!("\tregistry-stats Print registry statistic (e.g Rust transition progress)");
22+
}
23+
24+
fn main() -> Result<(), Error> {
25+
let args: Vec<String> = std::env::args().collect();
26+
27+
if args.len() < 2 {
28+
help();
29+
return Err(Error::MissingArguments);
30+
}
31+
32+
match args[1].as_str() {
33+
"registry-stats" => registry_stats::registry_stats(),
34+
_ => {
35+
help();
36+
Err(Error::UnknownCommand)
37+
},
38+
}
39+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright © 2017 Trust Wallet.
4+
5+
use crate::Error;
6+
use serde_json::Value as Json;
7+
use std::collections::HashSet;
8+
use tw_coin_registry::blockchain_type::BlockchainType;
9+
use tw_coin_registry::registry;
10+
11+
struct BlockchainStats {
12+
total: usize,
13+
supported: usize,
14+
}
15+
16+
fn blockchain_stats() -> Result<BlockchainStats, Error> {
17+
let chains: Vec<Json> =
18+
serde_json::from_str(registry::REGISTRY_JSON).map_err(|_| Error::InvalidRegistry)?;
19+
20+
fn extract_blockchain(chain: &Json) -> Option<(&str, BlockchainType)> {
21+
let blockchain_val = chain.get("blockchain")?;
22+
let blockchain_str = chain["blockchain"].as_str()?;
23+
let blockchain_type: BlockchainType =
24+
serde_json::from_value(blockchain_val.clone()).ok()?;
25+
Some((blockchain_str, blockchain_type))
26+
}
27+
28+
let mut supported = HashSet::new();
29+
let mut total = HashSet::new();
30+
31+
for chain in chains.iter() {
32+
let (blockchain_str, blockchain_type) =
33+
extract_blockchain(chain).ok_or(Error::InvalidRegistryParam {
34+
param: "blockchain",
35+
})?;
36+
37+
total.insert(blockchain_str);
38+
match blockchain_type {
39+
BlockchainType::Unsupported => (),
40+
_ => {
41+
supported.insert(blockchain_str);
42+
},
43+
}
44+
}
45+
46+
Ok(BlockchainStats {
47+
total: total.len(),
48+
supported: supported.len(),
49+
})
50+
}
51+
52+
pub fn registry_stats() -> Result<(), Error> {
53+
let total_chains = registry::registry_iter().count();
54+
let chains_in_rust = registry::supported_coin_items().count();
55+
let rust_chains_progress = (chains_in_rust * 100) as f64 / total_chains as f64;
56+
57+
let blockchain_stats = blockchain_stats()?;
58+
let blockchain_progress =
59+
(blockchain_stats.supported * 100) as f64 / blockchain_stats.total as f64;
60+
61+
println!();
62+
println!("total chains: {total_chains}");
63+
println!("supported chains: {chains_in_rust}");
64+
println!("chains transition progress: {rust_chains_progress:.1}%");
65+
66+
println!();
67+
println!("total blockchain impls: {}", blockchain_stats.total);
68+
println!("supported blockchain impls: {}", blockchain_stats.supported);
69+
println!("blockchains transition progress: {blockchain_progress:.1}%");
70+
71+
Ok(())
72+
}

tools/registry

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import argparse
44
import json
5-
import sys
65
import requests
6+
import subprocess
7+
import sys
78

89
PATH_TO_REGISTRY_JSON = "registry.json"
910

@@ -34,6 +35,21 @@ def ping_explorers(_args):
3435
print("\tError: ", exception)
3536

3637

38+
def registry_stats(_args):
39+
"""Print registry stats"""
40+
subprocess.run([
41+
"cargo",
42+
"run",
43+
"--manifest-path",
44+
"rust/Cargo.toml",
45+
"-p",
46+
"wallet_core_bin",
47+
"--",
48+
"registry-stats"
49+
],
50+
shell=False)
51+
52+
3753
if __name__ == '__main__':
3854
parser = argparse.ArgumentParser(description="Operations over registry.json")
3955
subparsers = parser.add_subparsers()
@@ -42,5 +58,9 @@ if __name__ == '__main__':
4258
help="Ping blockchain explorers inside 'registry.json'")
4359
ping_explorers_parser.set_defaults(func=ping_explorers)
4460

61+
registry_stats_parser = subparsers.add_parser('registry-stats',
62+
help="Print registry statistic (e.g Rust transition progress)")
63+
registry_stats_parser.set_defaults(func=registry_stats)
64+
4565
args = parser.parse_args()
4666
args.func(args)

tools/rust-bindgen

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ fi
4949

5050
if [[ "$NATIVE" == "true" ]]; then
5151
echo "Generating Native target"
52-
cargo build --release
52+
cargo build --release --lib
5353
fi
5454

5555
export RUSTFLAGS="-Zlocation-detail=none"
5656

5757
if [[ "$WASM" == "true" ]]; then
5858
echo "Generating WASM target"
59-
cargo build -Z build-std=std,panic_abort --target wasm32-unknown-emscripten --release
59+
cargo build -Z build-std=std,panic_abort --target wasm32-unknown-emscripten --release --lib
6060
fi
6161

6262
if [[ "$ANDROID" == "true" ]]; then
@@ -69,12 +69,12 @@ if [[ "$ANDROID" == "true" ]]; then
6969
export CC_armv7_linux_androideabi="$NDK_BIN_PATH/armv7a-linux-androideabi$NDK_API_LEVEL-clang"
7070

7171
echo "Generating Android targets"
72-
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
72+
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
7373
fi
7474

7575
if [[ "$IOS" == "true" ]]; then
7676
echo "Generating iOS targets"
77-
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 &
77+
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 &
7878
wait
7979
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
8080
mkdir -p $BUILD_FOLDER/darwin_universal

tools/rust-coverage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pushd rust
1818

1919
# Generate HTML report if requested
2020
if [[ "$1" == "html" ]]; then
21-
cargo llvm-cov test --html
21+
cargo llvm-cov test --workspace --exclude wallet_core_bin --html
2222
cargo llvm-cov report --lcov --output-path coverage.info
2323
else
24-
cargo llvm-cov test --lcov --output-path coverage.info
24+
cargo llvm-cov test --workspace --exclude wallet_core_bin --lcov --output-path coverage.info
2525
fi
2626

2727
popd

tools/rust-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if [[ "$1" == "wasm" ]]; then
1818
source ../emsdk/emsdk_env.sh
1919
export CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node
2020

21-
cargo test --target wasm32-unknown-emscripten --profile wasm-test
21+
cargo test --target wasm32-unknown-emscripten --profile wasm-test --workspace --exclude wallet_core_bin
2222
else
2323
cargo test --all
2424
fi

0 commit comments

Comments
 (0)