Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPERIMENTAL DO NOT MERGE: build: replace async-lock with tokio locks in core-crypto #766

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sha2 = "0.10"
strum = { version = "0.26", features = ["derive"] }
thiserror = "1.0"
tls_codec = "0.4.1"
tokio = { version = "1.41.1", features = ["sync"] }
uniffi = "0.28"
url = "2.5"
uuid = "1.11"
Expand All @@ -72,7 +73,3 @@ opt-level = "s"
# ! This will cause the FFI to stop working because UniFFI stores the Rust <-> cdylib mapping
# ! in the `.strtab` section of the executable. Stripping this causes everything to stop functioning.
strip = false
# panic = "abort"

[profile.dev.package.backtrace]
opt-level = 3
2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ serde.workspace = true
serde_json.workspace = true
url.workspace = true
async-trait.workspace = true
async-lock.workspace = true
schnellru.workspace = true
zeroize.workspace = true
wire-e2e-identity.workspace = true
Expand All @@ -55,6 +54,7 @@ base64.workspace = true
log.workspace = true
proteus-wasm = { workspace = true, features = ["hazmat"], optional = true }
proteus-traits = { workspace = true, optional = true }
tokio.workspace = true

[target.'cfg(not(target_family = "wasm"))'.dependencies]
sysinfo = { version = "0.32", default-features = false, features = ["apple-app-store", "system"] }
Expand Down
12 changes: 6 additions & 6 deletions crypto/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
prelude::{Client, MlsConversation},
CoreCrypto, CoreCryptoCallbacks, CryptoError, CryptoResult,
};
use async_lock::{Mutex, RwLock, RwLockReadGuardArc, RwLockWriteGuardArc};
use core_crypto_keystore::connection::FetchFromDatabase;
use core_crypto_keystore::entities::ConsumerData;
use core_crypto_keystore::CryptoKeystoreError;
use mls_crypto_provider::{CryptoKeystore, MlsCryptoProvider};
use std::{ops::Deref, sync::Arc};
use tokio::sync::{Mutex, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock};

Check warning on line 17 in crypto/src/context.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `Mutex`

Check warning on line 17 in crypto/src/context.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `Mutex`

/// This struct provides transactional support for Core Crypto.
///
Expand Down Expand Up @@ -90,9 +90,9 @@

pub(crate) async fn callbacks(
&self,
) -> CryptoResult<RwLockReadGuardArc<Option<Arc<dyn CoreCryptoCallbacks + 'static>>>> {
) -> CryptoResult<OwnedRwLockReadGuard<Option<Arc<dyn CoreCryptoCallbacks + 'static>>>> {
match self.state.read().await.deref() {
ContextState::Valid { callbacks, .. } => Ok(callbacks.read_arc().await),
ContextState::Valid { callbacks, .. } => Ok(callbacks.clone().read_owned().await),
ContextState::Invalid => Err(CryptoError::InvalidContext),
}
}
Expand All @@ -104,7 +104,7 @@
) -> CryptoResult<()> {
match self.state.read().await.deref() {
ContextState::Valid { callbacks: cbs, .. } => {
*cbs.write_arc().await = callbacks;
*cbs.clone().write_owned().await = callbacks;
Ok(())
}
ContextState::Invalid => Err(CryptoError::InvalidContext),
Expand All @@ -126,9 +126,9 @@
}
}

pub(crate) async fn mls_groups(&self) -> CryptoResult<RwLockWriteGuardArc<GroupStore<MlsConversation>>> {
pub(crate) async fn mls_groups(&self) -> CryptoResult<OwnedRwLockWriteGuard<GroupStore<MlsConversation>>> {
match self.state.read().await.deref() {
ContextState::Valid { mls_groups, .. } => Ok(mls_groups.write_arc().await),
ContextState::Valid { mls_groups, .. } => Ok(mls_groups.clone().write_owned().await),
ContextState::Invalid => Err(CryptoError::InvalidContext),
}
}
Expand Down
10 changes: 5 additions & 5 deletions crypto/src/group_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
}
}

pub(crate) type GroupStoreValue<V> = std::sync::Arc<async_lock::RwLock<V>>;
pub(crate) type GroupStoreValue<V> = std::sync::Arc<tokio::sync::RwLock<V>>;

pub(crate) type LruMap<V> = schnellru::LruMap<Vec<u8>, GroupStoreValue<V>, HybridMemoryLimiter>;

Expand Down Expand Up @@ -206,7 +206,7 @@
// Not in store, fetch the thing in the keystore
let mut value = V::fetch_from_id(k, identity, keystore).await?;
if let Some(value) = value.take() {
let value_to_insert = std::sync::Arc::new(async_lock::RwLock::new(value));
let value_to_insert = std::sync::Arc::new(tokio::sync::RwLock::new(value));
self.insert_prepped(k.to_vec(), value_to_insert.clone());

Ok(Some(value_to_insert))
Expand Down Expand Up @@ -235,7 +235,7 @@
.into_iter()
.map(|g| {
let id = g.id().to_vec();
let to_insert = std::sync::Arc::new(async_lock::RwLock::new(g));
let to_insert = std::sync::Arc::new(tokio::sync::RwLock::new(g));
self.insert_prepped(id, to_insert.clone());
to_insert
})
Expand All @@ -248,12 +248,12 @@
}

pub(crate) fn insert(&mut self, k: Vec<u8>, entity: V) {
let value_to_insert = std::sync::Arc::new(async_lock::RwLock::new(entity));
let value_to_insert = std::sync::Arc::new(tokio::sync::RwLock::new(entity));
self.insert_prepped(k, value_to_insert)
}

pub(crate) fn try_insert(&mut self, k: Vec<u8>, entity: V) -> Result<(), V> {

Check warning on line 255 in crypto/src/group_store.rs

View workflow job for this annotation

GitHub Actions / hack

methods `try_insert` and `get` are never used

Check warning on line 255 in crypto/src/group_store.rs

View workflow job for this annotation

GitHub Actions / hack

methods `try_insert` and `get` are never used
let value_to_insert = std::sync::Arc::new(async_lock::RwLock::new(entity));
let value_to_insert = std::sync::Arc::new(tokio::sync::RwLock::new(entity));

if self.0.insert(k, value_to_insert.clone()) {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

//! Core Crypto is a wrapper on top of OpenMLS aimed to provide an ergonomic API for usage in web

Check warning on line 17 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `!important`

Check warning on line 17 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `!note`

Check warning on line 17 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `!warning`

Check warning on line 17 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / doc

could not parse code block as Rust code
//! through Web Assembly and in mobile devices through FFI.
//!
//! The goal is provide a easier and less verbose API to create, manage and interact with MLS
Expand All @@ -23,10 +23,10 @@
#![deny(missing_docs)]
#![allow(clippy::single_component_path_imports)]

use async_lock::Mutex;
#[cfg(test)]
pub use core_crypto_attributes::{dispotent, durable, idempotent};
use std::sync::Arc;

Check warning on line 28 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `std::sync::Arc`

Check warning on line 28 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `std::sync::Arc`

Check warning on line 28 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `std::sync::Arc`
use tokio::sync::Mutex;

Check warning on line 29 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `tokio::sync::Mutex`

Check warning on line 29 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `tokio::sync::Mutex`

Check warning on line 29 in crypto/src/lib.rs

View workflow job for this annotation

GitHub Actions / hack

unused import: `tokio::sync::Mutex`

pub use self::error::*;

Expand Down
2 changes: 1 addition & 1 deletion crypto/src/mls/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::{
CryptoError, CryptoResult, MlsCiphersuite, MlsCredentialType, MlsError,
},
};
use async_lock::RwLock;
use core_crypto_keystore::{connection::FetchFromDatabase, Connection, CryptoKeystoreError};
use log::debug;
use openmls::prelude::{Credential, CredentialType};
Expand All @@ -37,6 +36,7 @@ use std::collections::HashSet;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use tls_codec::{Deserialize, Serialize};
use tokio::sync::RwLock;

use core_crypto_keystore::entities::{EntityFindParams, MlsCredential, MlsSignatureKeyPair};
use identities::ClientIdentities;
Expand Down
2 changes: 1 addition & 1 deletion crypto/src/mls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use async_lock::RwLock;
use log::trace;
use tokio::sync::RwLock;

use crate::prelude::{
identifier::ClientIdentifier, key_package::INITIAL_KEYING_MATERIAL_COUNT, Client, ClientId, ConversationId,
Expand Down
Loading