Skip to content

Commit

Permalink
[uniffi] Fix compilation under mls_build_async
Browse files Browse the repository at this point in the history
UniFFI added support for async traits in mozilla/uniffi-rs#1981, but
this is not yet released. This commit temporarily introduces a Git
dependency on UniFFI to let us test the async functionality which was
removed in awslabs#86.

An unrelated UniFFI change (mozilla/uniffi-rs#1840) made our existing
test fail.
  • Loading branch information
mgeisler committed Mar 22, 2024
1 parent 98cf9fe commit 07c8bd2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
4 changes: 2 additions & 2 deletions mls-rs-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ mls-rs = { version = "0.39.0", path = "../mls-rs" }
mls-rs-core = { version = "0.18.0", path = "../mls-rs-core" }
mls-rs-crypto-openssl = { version = "0.9.0", path = "../mls-rs-crypto-openssl" }
thiserror = "1.0.57"
uniffi = "0.26.0"
uniffi = { git = "https://github.com/mozilla/uniffi-rs/", rev = "6b09f11", version = "0.26.0" }

[target.'cfg(mls_build_async)'.dependencies]
tokio = { version = "1.36.0", features = ["sync"] }

[dev-dependencies]
uniffi_bindgen = "0.26.0"
uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs/", rev = "6b09f11", version = "0.26.0" }
20 changes: 11 additions & 9 deletions mls-rs-uniffi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl mls_rs_core::group::GroupStateStorage for ClientGroupStorage {
type Error = Error;

async fn state(&self, group_id: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
self.0.state(group_id.to_vec())
self.0.state(group_id.to_vec()).await
}

async fn epoch(&self, group_id: &[u8], epoch_id: u64) -> Result<Option<Vec<u8>>, Self::Error> {
self.0.epoch(group_id.to_vec(), epoch_id)
self.0.epoch(group_id.to_vec(), epoch_id).await
}

async fn write(
Expand All @@ -41,16 +41,18 @@ impl mls_rs_core::group::GroupStateStorage for ClientGroupStorage {
inserts: Vec<mls_rs_core::group::EpochRecord>,
updates: Vec<mls_rs_core::group::EpochRecord>,
) -> Result<(), Self::Error> {
self.0.write(
state.id,
state.data,
inserts.into_iter().map(Into::into).collect(),
updates.into_iter().map(Into::into).collect(),
)
self.0
.write(
state.id,
state.data,
inserts.into_iter().map(Into::into).collect(),
updates.into_iter().map(Into::into).collect(),
)
.await
}

async fn max_epoch_id(&self, group_id: &[u8]) -> Result<Option<u64>, Self::Error> {
self.0.max_epoch_id(group_id.to_vec())
self.0.max_epoch_id(group_id.to_vec()).await
}
}

Expand Down
19 changes: 18 additions & 1 deletion mls-rs-uniffi/src/config/group_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use mls_rs::error::IntoAnyError;
use std::fmt::Debug;
#[cfg(not(mls_build_async))]
use std::sync::Mutex;
#[cfg(mls_build_async)]
use tokio::sync::Mutex;

use crate::Error;

Expand Down Expand Up @@ -28,9 +31,9 @@ impl From<EpochRecord> for mls_rs_core::group::EpochRecord {
}
}

#[uniffi::export(with_foreign)]
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
#[uniffi::export(with_foreign)]
pub trait GroupStateStorage: Send + Sync + Debug {
async fn state(&self, group_id: Vec<u8>) -> Result<Option<Vec<u8>>, Error>;
async fn epoch(&self, group_id: Vec<u8>, epoch_id: u64) -> Result<Option<Vec<u8>>, Error>;
Expand Down Expand Up @@ -59,9 +62,15 @@ impl<S> GroupStateStorageAdapter<S> {
Self(Mutex::new(group_state_storage))
}

#[cfg(not(mls_build_async))]
fn inner(&self) -> std::sync::MutexGuard<'_, S> {
self.0.lock().unwrap()
}

#[cfg(mls_build_async)]
async fn inner(&self) -> tokio::sync::MutexGuard<'_, S> {
self.0.lock().await
}
}

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
Expand All @@ -73,13 +82,17 @@ where
{
async fn state(&self, group_id: Vec<u8>) -> Result<Option<Vec<u8>>, Error> {
self.inner()
.await
.state(&group_id)
.await
.map_err(|err| err.into_any_error().into())
}

async fn epoch(&self, group_id: Vec<u8>, epoch_id: u64) -> Result<Option<Vec<u8>>, Error> {
self.inner()
.await
.epoch(&group_id, epoch_id)
.await
.map_err(|err| err.into_any_error().into())
}

Expand All @@ -91,17 +104,21 @@ where
epoch_updates: Vec<EpochRecord>,
) -> Result<(), Error> {
self.inner()
.await
.write(
mls_rs_core::group::GroupState { id, data },
epoch_inserts.into_iter().map(Into::into).collect(),
epoch_updates.into_iter().map(Into::into).collect(),
)
.await
.map_err(|err| err.into_any_error().into())
}

async fn max_epoch_id(&self, group_id: Vec<u8>) -> Result<Option<u64>, Error> {
self.inner()
.await
.max_epoch_id(&group_id)
.await
.map_err(|err| err.into_any_error().into())
}
}
10 changes: 8 additions & 2 deletions mls-rs-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl TryFrom<mls_rs::CipherSuite> for CipherSuite {
/// See [`mls_rs::CipherSuiteProvider::signature_key_generate`]
/// for details.
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
#[uniffi::export]
pub async fn generate_signature_keypair(
cipher_suite: CipherSuite,
Expand Down Expand Up @@ -292,6 +293,7 @@ pub struct Client {
}

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
#[uniffi::export]
impl Client {
/// Create a new client.
Expand Down Expand Up @@ -495,6 +497,7 @@ pub struct Group {
}

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
impl Group {
#[cfg(not(mls_build_async))]
fn inner(&self) -> std::sync::MutexGuard<'_, mls_rs::Group<UniFFIConfig>> {
Expand All @@ -520,6 +523,7 @@ fn index_to_identity(

/// Extract the basic credential identifier from a from a key package.
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
async fn signing_identity_to_identifier(
signing_identity: &identity::SigningIdentity,
) -> Result<Vec<u8>, Error> {
Expand All @@ -531,6 +535,7 @@ async fn signing_identity_to_identifier(
}

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
#[uniffi::export]
impl Group {
/// Write the current state of the group to storage defined by
Expand All @@ -545,8 +550,9 @@ impl Group {
/// This function is used to provide the current group tree to new
/// members when `use_ratchet_tree_extension` is set to false in
/// `ClientConfig`.
pub fn export_tree(&self) -> Result<RatchetTree, Error> {
self.inner().export_tree().try_into()
pub async fn export_tree(&self) -> Result<RatchetTree, Error> {
let group = self.inner().await;
group.export_tree().try_into()
}

/// Perform a commit of received proposals (or an empty commit).
Expand Down
2 changes: 1 addition & 1 deletion mls-rs-uniffi/tests/custom_storage_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def max_epoch_id(self, group_id: bytes):


group_state_storage = PythonGroupStateStorage()
client_config = ClientConfig(group_state_storage,
client_config = ClientConfig(group_state_storage=group_state_storage,
use_ratchet_tree_extension=True)

key = generate_signature_keypair(CipherSuite.CURVE25519_AES128)
Expand Down
4 changes: 0 additions & 4 deletions mls-rs-uniffi/tests/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,5 @@ macro_rules! generate_python_tests {
generate_python_tests!(generate_signature_keypair, None);
generate_python_tests!(client_config_default_sync, None);
generate_python_tests!(custom_storage_sync, None);

// TODO(mulmarta): it'll break if we use async trait which will be
// supported in the next UniFFI release
// TODO(mgeisler): add back simple_scenario_async
generate_python_tests!(simple_scenario_sync, None);
generate_python_tests!(ratchet_tree_sync, None);
2 changes: 1 addition & 1 deletion mls-rs-uniffi/uniffi-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uniffi = { version = "0.26.0", features = ["cli"] }
uniffi = { git = "https://github.com/mozilla/uniffi-rs/", rev = "6b09f11", version = "0.26.0", features = ["cli"] }

0 comments on commit 07c8bd2

Please sign in to comment.