Skip to content

Commit

Permalink
[Boost]: Reduce dependency on Boost library (#3751)
Browse files Browse the repository at this point in the history
* [Misc]: Do not use boost/algorithm/string.hpp

* [Misc]: Add string.cpp tests

* [Misc]: Replace <boost/archive/iterators>

* TODO make sure if the changed test covers all cases

* [Boost]: Replace <boost/uuid> with a Rust FFI

* [Boost]: Minimize the use of <boost/lexical_cast>

* [Misc]: Refactor `wallet_core_rs` crate

* Remove `tw_proto` test FFIs

* [Boost]: Refactor includes

* [Boost]: Refactor includes

* [Boost]: Minor changes
  • Loading branch information
satoshiotomakan authored Mar 21, 2024
1 parent adb20c0 commit 2c877a3
Show file tree
Hide file tree
Showing 44 changed files with 181 additions and 332 deletions.
13 changes: 10 additions & 3 deletions rust/Cargo.lock

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

2 changes: 0 additions & 2 deletions rust/tw_proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ fuzz = ["arbitrary"]
# Enable in fuzz tests only!
arbitrary = { version = "1", features = ["derive"], optional = true }
quick-protobuf = "0.8.1"
tw_encoding = { path = "../tw_encoding" }
tw_memory = { path = "../tw_memory" }

[build-dependencies]
pb-rs = "0.10.0"
100 changes: 0 additions & 100 deletions rust/tw_proto/src/ffi.rs

This file was deleted.

2 changes: 0 additions & 2 deletions rust/tw_proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ pub use quick_protobuf::{
Error as ProtoError, MessageRead, MessageWrite, Result as ProtoResult,
};

pub mod ffi;

/// Serializes a Protobuf message without the length prefix.
/// Please note that [`quick_protobuf::serialize_into_vec`] appends a `varint32` length prefix.
pub fn serialize<T: MessageWrite>(message: &T) -> ProtoResult<Vec<u8>> {
Expand Down
24 changes: 0 additions & 24 deletions rust/tw_proto/tests/proto_ffi_tests.rs

This file was deleted.

52 changes: 34 additions & 18 deletions rust/wallet_core_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,46 @@ name = "wallet_core_rs"
crate-type = ["staticlib", "rlib"] # Creates static lib

[features]
default = ["bitcoin-legacy", "ethereum-abi", "ethereum-rlp", "solana-address", "solana-transaction"]
bitcoin-legacy = []
ethereum-abi = []
ethereum-rlp = []
solana-address = []
solana-transaction = []
default = [
"any-coin",
"bitcoin",
"ethereum",
"keypair",
"solana",
"utils",
]
any-coin = ["tw_any_coin"]
bitcoin = ["tw_bitcoin"]
ethereum = ["tw_ethereum", "tw_coin_registry"]
keypair = ["tw_keypair"]
solana = ["tw_solana"]
utils = [
"tw_encoding",
"tw_hash",
"tw_memory",
"tw_number",
"tw_proto",
"uuid"
]

[dependencies]
tw_any_coin = { path = "../tw_any_coin" }
tw_aptos = { path = "../tw_aptos" }
tw_bitcoin = { path = "../tw_bitcoin" }
tw_coin_entry = { path = "../tw_coin_entry", features = ["test-utils"] }
tw_coin_registry = { path = "../tw_coin_registry" }
tw_encoding = { path = "../tw_encoding" }
tw_ethereum = { path = "../tw_ethereum" }
tw_hash = { path = "../tw_hash" }
tw_keypair = { path = "../tw_keypair" }
tw_memory = { path = "../tw_memory" }
tw_any_coin = { path = "../tw_any_coin", optional = true }
tw_bitcoin = { path = "../tw_bitcoin", optional = true }
tw_coin_registry = { path = "../tw_coin_registry", optional = true }
tw_encoding = { path = "../tw_encoding", optional = true }
tw_ethereum = { path = "../tw_ethereum", optional = true }
tw_hash = { path = "../tw_hash", optional = true }
tw_keypair = { path = "../tw_keypair", optional = true }
tw_memory = { path = "../tw_memory", optional = true }
tw_number = { path = "../tw_number", optional = true }
tw_misc = { path = "../tw_misc" }
tw_proto = { path = "../tw_proto" }
tw_solana = { path = "../chains/tw_solana" }
tw_proto = { path = "../tw_proto", optional = true }
tw_solana = { path = "../chains/tw_solana", optional = true }
uuid = { version = "1.7", features = ["v4"], optional = true }

[dev-dependencies]
serde_json = "1.0"
tw_any_coin = { path = "../tw_any_coin", features = ["test-utils"] }
tw_coin_entry = { path = "../tw_coin_entry", features = ["test-utils"] }
tw_memory = { path = "../tw_memory", features = ["test-utils"] }
tw_number = { path = "../tw_number", features = ["helpers"] }
10 changes: 0 additions & 10 deletions rust/wallet_core_rs/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,15 @@ namespaces = ["TW", "Rust"]
parse_deps = true
extra_bindings = [
"tw_any_coin",
"tw_bitcoin",
"tw_coin_registry",
"tw_encoding",
"tw_ethereum",
"tw_hash",
"tw_keypair",
"tw_memory",
"tw_move_parser",
"tw_proto"
]
include = [
"tw_any_coin",
"tw_bitcoin",
"tw_coin_registry",
"tw_encoding",
"tw_ethereum",
"tw_hash",
"tw_keypair",
"tw_memory",
"tw_move_parser",
"tw_proto"
]
1 change: 0 additions & 1 deletion rust/wallet_core_rs/src/ffi/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
//
// Copyright © 2017 Trust Wallet.

#[cfg(feature = "bitcoin-legacy")]
pub mod legacy;
2 changes: 0 additions & 2 deletions rust/wallet_core_rs/src/ffi/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
//
// Copyright © 2017 Trust Wallet.

#[cfg(feature = "ethereum-abi")]
pub mod abi;
#[cfg(feature = "ethereum-rlp")]
pub mod rlp;
5 changes: 5 additions & 0 deletions rust/wallet_core_rs/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//
// Copyright © 2017 Trust Wallet.

#[cfg(feature = "bitcoin")]
pub mod bitcoin;
#[cfg(feature = "ethereum")]
pub mod ethereum;
#[cfg(feature = "solana")]
pub mod solana;
#[cfg(feature = "utils")]
pub mod utils;
2 changes: 0 additions & 2 deletions rust/wallet_core_rs/src/ffi/solana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
//
// Copyright © 2017 Trust Wallet.

#[cfg(feature = "solana-address")]
pub mod address;
#[cfg(feature = "solana-transaction")]
pub mod transaction;
5 changes: 5 additions & 0 deletions rust/wallet_core_rs/src/ffi/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

pub mod uuid_ffi;
16 changes: 16 additions & 0 deletions rust/wallet_core_rs/src/ffi/utils/uuid_ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#![allow(clippy::missing_safety_doc)]

use std::ffi::{c_char, CString};

/// Creates a random UUID.
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
/// as the source of random numbers.
#[no_mangle]
pub unsafe extern "C" fn tw_uuid_random() -> *const c_char {
let res = uuid::Uuid::new_v4();
CString::new(res.to_string()).unwrap().into_raw()
}
10 changes: 6 additions & 4 deletions rust/wallet_core_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
//
// Copyright © 2017 Trust Wallet.

#[cfg(feature = "any-coin")]
pub extern crate tw_any_coin;
pub extern crate tw_aptos;
#[cfg(feature = "bitcoin")]
pub extern crate tw_bitcoin;
pub extern crate tw_coin_registry;
#[cfg(feature = "utils")]
pub extern crate tw_encoding;
pub extern crate tw_ethereum;
#[cfg(feature = "utils")]
pub extern crate tw_hash;
#[cfg(feature = "keypair")]
pub extern crate tw_keypair;
#[cfg(feature = "utils")]
pub extern crate tw_memory;
pub extern crate tw_proto;

pub mod ffi;
40 changes: 40 additions & 0 deletions rust/wallet_core_rs/tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use std::collections::HashSet;
use std::ffi::CStr;
use wallet_core_rs::ffi::utils::uuid_ffi::tw_uuid_random;

/// Example of the valid UUID: 3cbbcce1-db89-4ea2-be24-88a686be461c
#[test]
fn test_tw_uuid_random_is_valid() {
let uuid = unsafe { CStr::from_ptr(tw_uuid_random()) }
.to_str()
.unwrap();

let tokens: Vec<_> = uuid.split("-").collect();
assert_eq!(tokens.len(), 5);
assert_eq!(tokens[0].len(), 8);
assert_eq!(tokens[1].len(), 4);
assert_eq!(tokens[2].len(), 4);
assert_eq!(tokens[3].len(), 4);
assert_eq!(tokens[4].len(), 12);
}

#[test]
fn test_tw_uuid_random_do_not_repeat() {
const ITERATIONS: usize = 10000;

// Use `Vec` instead of `HashSet` here to make each iteration as fast as possible.
let mut uuids = Vec::with_capacity(ITERATIONS);
for _ in 0..ITERATIONS {
let uuid = unsafe { CStr::from_ptr(tw_uuid_random()) }
.to_str()
.unwrap();
uuids.push(uuid);
}

let unique_uuids: HashSet<&str> = uuids.into_iter().collect();
assert_eq!(unique_uuids.len(), ITERATIONS);
}
Loading

0 comments on commit 2c877a3

Please sign in to comment.