Skip to content

Commit

Permalink
common crate for wasm shims
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Dec 11, 2024
1 parent 481fad7 commit 659acd1
Show file tree
Hide file tree
Showing 81 changed files with 1,199 additions and 970 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-webassembly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
- "rust-toolchain"
env:
CARGO_TERM_COLOR: always
WASM_BINDGEN_TEST_TIMEOUT: 120
WASM_BINDGEN_TEST_TIMEOUT: 180
WASM_BINDGEN_TEST_ONLY_WEB: 1
WASM_BINDGEN_SPLIT_LINKED_MODULES: 1
jobs:
Expand All @@ -35,7 +35,7 @@ jobs:
- name: Start Docker containers
run: dev/up
- name: Build WebAssembly Packages
run: cargo build --tests --release --target wasm32-unknown-unknown -p xmtp_id -p xmtp_mls -p xmtp_api_http -p xmtp_cryptography
run: cargo build --tests --release --target wasm32-unknown-unknown -p xmtp_id -p xmtp_mls -p xmtp_api_http -p xmtp_cryptography -p xmtp_common
- name: test with chrome
run: |
cargo test --release --target wasm32-unknown-unknown -p xmtp_mls -p xmtp_id -p xmtp_api_http -p xmtp_cryptography -- \
Expand Down
50 changes: 32 additions & 18 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"bindings_ffi",
"xtask",
"xmtp_debug",
"common"
]

# Make the feature resolver explicit.
Expand Down Expand Up @@ -102,6 +103,7 @@ xmtp_cryptography = { path = "xmtp_cryptography" }
xmtp_id = { path = "xmtp_id" }
xmtp_mls = { path = "xmtp_mls" }
xmtp_proto = { path = "xmtp_proto" }
xmtp_common = { path = "common" }

[profile.dev]
# Disabling debug info speeds up builds a bunch,
Expand Down
1 change: 1 addition & 0 deletions bindings_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ xmtp_mls = { path = "../xmtp_mls" }
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }
xmtp_user_preferences = { path = "../xmtp_user_preferences" }
xmtp_v2 = { path = "../xmtp_v2" }
xmtp_common.workspace = true

[target.'cfg(target_os = "android")'.dependencies]
paranoid-android = "0.2"
Expand Down
23 changes: 8 additions & 15 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{FfiSubscribeError, GenericError};
use std::{collections::HashMap, convert::TryInto, sync::Arc};
use tokio::sync::Mutex;
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_common::retry::Retry;
use xmtp_id::associations::verify_signed_with_public_context;
use xmtp_id::scw_verifier::RemoteSignatureVerifier;
use xmtp_id::{
Expand Down Expand Up @@ -37,7 +38,6 @@ use xmtp_mls::{
GroupMetadataOptions, MlsGroup, PreconfiguredPolicies, UpdateAdminListType,
},
identity::IdentityStrategy,
retry::Retry,
storage::{
consent_record::{ConsentState, ConsentType, StoredConsentRecord},
group::GroupQueryArgs,
Expand Down Expand Up @@ -1827,23 +1827,22 @@ mod tests {
FfiPermissionPolicySet, FfiPermissionUpdateType, FfiSubscribeError,
};
use ethers::utils::hex;
use rand::distributions::{Alphanumeric, DistString};
use std::{
env,
sync::{
atomic::{AtomicU32, Ordering},
Arc, Mutex,
},
time::{Duration, Instant},
};
use tokio::{sync::Notify, time::error::Elapsed};
use xmtp_common::tmp_path;
use xmtp_common::{wait_for_eq, wait_for_ok};
use xmtp_cryptography::{signature::RecoverableSignature, utils::rng};
use xmtp_id::associations::{
generate_inbox_id,
unverified::{UnverifiedRecoverableEcdsaSignature, UnverifiedSignature},
};
use xmtp_mls::{
api::test_utils::{wait_for_eq, wait_for_ok},
groups::{scoped_client::LocalScopedGroupClient, GroupError},
storage::EncryptionKey,
InboxOwner,
Expand Down Expand Up @@ -1974,15 +1973,6 @@ mod tests {
}
}

pub fn rand_string() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 24)
}

pub fn tmp_path() -> String {
let db_name = rand_string();
format!("{}/{}.db3", env::temp_dir().to_str().unwrap(), db_name)
}

fn static_enc_key() -> EncryptionKey {
[2u8; 32]
}
Expand Down Expand Up @@ -4160,7 +4150,9 @@ mod tests {
let _ = wait_for_ok(|| async { alix_a.inner_client.get_sync_group(&alix_a_conn) }).await;

let alix_b = new_test_client_with_wallet_and_history(wallet).await;
wait_for_eq(|| async { alix_b.inner_client.identity().is_ready() }, true).await;
wait_for_eq(|| async { alix_b.inner_client.identity().is_ready() }, true)
.await
.unwrap();

let bo = new_test_client_with_history().await;

Expand All @@ -4179,7 +4171,8 @@ mod tests {
},
2,
)
.await;
.await
.unwrap();

// check that they have the same sync group
let sync_group_a = wait_for_ok(|| async { alix_a.conversations().get_sync_group() })
Expand Down
1 change: 1 addition & 0 deletions bindings_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ xmtp_cryptography = { path = "../xmtp_cryptography" }
xmtp_id = { path = "../xmtp_id" }
xmtp_mls = { path = "../xmtp_mls" }
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }
xmtp_common.workspace = true

[build-dependencies]
napi-build = "2.0.1"
Expand Down
2 changes: 1 addition & 1 deletion bindings_node/src/inbox_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use napi::bindgen_prelude::Uint8Array;
use napi_derive::napi;
use std::sync::Arc;
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_common::retry::Retry;
use xmtp_id::associations::generate_inbox_id as xmtp_id_generate_inbox_id;
use xmtp_id::associations::MemberIdentifier;
use xmtp_mls::api::ApiClientWrapper;
use xmtp_mls::retry::Retry;

#[napi]
pub async fn get_inbox_id_for_address(
Expand Down
1 change: 1 addition & 0 deletions bindings_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xmtp_api_http = { path = "../xmtp_api_http" }
xmtp_cryptography = { path = "../xmtp_cryptography" }
xmtp_id = { path = "../xmtp_id" }
xmtp_mls = { path = "../xmtp_mls", features = ["test-utils", "http-api"] }
xmtp_common.workspace = true
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }
tracing-web = "0.1"
tracing.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion bindings_wasm/src/inbox_id.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use wasm_bindgen::prelude::{wasm_bindgen, JsError};
use xmtp_api_http::XmtpHttpApiClient;
use xmtp_common::retry::Retry;
use xmtp_id::associations::generate_inbox_id as xmtp_id_generate_inbox_id;
use xmtp_mls::api::ApiClientWrapper;
use xmtp_mls::retry::Retry;

#[wasm_bindgen(js_name = getInboxIdForAddress)]
pub async fn get_inbox_id_for_address(
Expand Down
38 changes: 38 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "xmtp_common"
edition = "2021"
version.workspace = true
license.workspace = true

[dependencies]
web-time.workspace = true
tracing.workspace = true
tokio = { workspace = true, features = ["time"] }
rand = "0.8"
futures.workspace = true
xmtp_cryptography.workspace = true

parking_lot = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, features = ["fmt", "env-filter", "ansi", "json"], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { workspace = true, features = ["js"] }
gloo-timers = { workspace = true, features = ["futures"] }
tracing-wasm = { version = "0.2", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }
js-sys.workspace = true
web-sys = { workspace = true, features = ["Window"] }
wasm-bindgen-futures.workspace = true

[dev-dependencies]
thiserror.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
tokio = { workspace = true, features = ["time", "macros", "rt", "sync"]}
wasm-bindgen-test.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { workspace = true, features = ["time", "macros", "rt-multi-thread", "sync"]}

[features]
test-utils = ["dep:parking_lot", "dep:tracing-subscriber", "dep:tracing-wasm", "dep:console_error_panic_hook"]
47 changes: 47 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Common types shared among all XMTP Crates
mod macros;

#[cfg(feature = "test-utils")]
mod test;
#[cfg(feature = "test-utils")]
pub use test::*;

pub mod retry;
pub use retry::*;

/// Global Marker trait for WebAssembly
#[cfg(target_arch = "wasm32")]
pub trait Wasm {}
#[cfg(target_arch = "wasm32")]
impl<T> Wasm for T {}

pub mod time;

use rand::{
distributions::{Alphanumeric, DistString},
RngCore,
};
use xmtp_cryptography::utils as crypto_utils;

pub fn rand_string<const N: usize>() -> String {
Alphanumeric.sample_string(&mut crypto_utils::rng(), N)
}

pub fn rand_array<const N: usize>() -> [u8; N] {
let mut buffer = [0u8; N];
crypto_utils::rng().fill_bytes(&mut buffer);
buffer
}

/// Yield back control to the async runtime
#[cfg(not(target_arch = "wasm32"))]
pub async fn yield_() {
tokio::task::yield_now().await
}

/// Yield back control to the async runtime
#[cfg(target_arch = "wasm32")]
pub async fn yield_() {
sleep(crate::time::Duration::from_millis(1)).await;
}
24 changes: 24 additions & 0 deletions common/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// Turn the `Result<T, E>` into an `Option<T>`, logging the error with `tracing::error` and
/// returning `None` if the value matches on Result::Err().
/// Optionally pass a message as the second argument.
#[macro_export]
macro_rules! optify {
( $e: expr ) => {
match $e {
Ok(v) => Some(v),
Err(e) => {
tracing::error!("{}", e);
None
}
}
};
( $e: expr, $msg: tt ) => {
match $e {
Ok(v) => Some(v),
Err(e) => {
tracing::error!("{}: {:?}", $msg, e);
None
}
}
};
}
Loading

0 comments on commit 659acd1

Please sign in to comment.