From 2768918cd2d56efb1fbb5510cec2702d95e13d13 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Wed, 15 Nov 2023 12:19:38 -0500 Subject: [PATCH] merge with latest changes --- xmtp_mls/src/client.rs | 85 +++++++++++++++++--------------------- xmtp_mls/src/groups/mod.rs | 3 +- xmtp_mls/src/identity.rs | 3 +- 3 files changed, 41 insertions(+), 50 deletions(-) diff --git a/xmtp_mls/src/client.rs b/xmtp_mls/src/client.rs index 565f9b908..4d68bae22 100644 --- a/xmtp_mls/src/client.rs +++ b/xmtp_mls/src/client.rs @@ -88,25 +88,6 @@ where } } - /// Build this struct - /// # Arguments - /// * `strat`: the [`IdentityStrategy`] for this client - /// - /// # Example - /// - /// TODO: Fix this example - /// ```no_run - /// Client::builder() - /// .api_client(api_client) - /// .network(Network::Dev) - /// .build() - /// ``` - pub fn builder( - strat: IdentityStrategy, - ) -> ClientBuilder { - ClientBuilder::new(strat) - } - // TODO: Remove this and figure out the correct lifetimes to allow long lived provider pub fn mls_provider(&self, conn: &'a mut DbConnection) -> XmtpOpenMlsProvider<'a> { XmtpOpenMlsProvider::new(conn) @@ -141,8 +122,9 @@ where pub async fn register_identity(&self) -> Result<(), ClientError> { // TODO: Mark key package as last_resort in creation let mut connection = self.store.conn()?; - let mls_provider = XmtpOpenMlsProvider::new(&mut connection); - let last_resort_kp = self.identity.new_key_package(&mls_provider)?; + let last_resort_kp = self + .identity + .new_key_package(&self.mls_provider(&mut connection))?; let last_resort_kp_bytes = last_resort_kp.tls_serialize_detached()?; self.api_client @@ -221,7 +203,6 @@ where pub async fn sync_welcomes(&self) -> Result>, ClientError> { let welcome_topic = get_welcome_topic(&self.installation_public_key()); let mut conn = self.store.conn()?; - let provider = self.mls_provider(); // TODO: Use the last_message_timestamp_ns field on the TopicRefreshState to only fetch new messages // Waiting for more atomic update methods let envelopes = self.api_client.read_topic(&welcome_topic, 0).await?; @@ -229,24 +210,32 @@ where let groups: Vec> = envelopes .into_iter() .filter_map(|envelope| { - // TODO: Wrap in a transaction - let welcome = match extract_welcome(&envelope.message) { - Ok(welcome) => welcome, - Err(err) => { - log::error!("failed to extract welcome: {}", err); - return None; - } - }; - - // TODO: Update last_message_timestamp_ns on success or non-retryable error - // TODO: Abort if error is retryable - match MlsGroup::create_from_welcome(self, &mut conn, &provider, welcome) { - Ok(mls_group) => Some(mls_group), - Err(err) => { - log::error!("failed to create group from welcome: {}", err); - None + XmtpOpenMlsProvider::transaction(&mut conn, |provider| { + let welcome = match extract_welcome(&envelope.message) { + Ok(welcome) => welcome, + Err(err) => { + log::error!("failed to extract welcome: {}", err); + return Ok::<_, ClientError>(None); + } + }; + + // TODO: Update last_message_timestamp_ns on success or non-retryable error + // TODO: Abort if error is retryable + match MlsGroup::create_from_welcome( + self, + *provider.conn().borrow_mut(), + &provider, + welcome, + ) { + Ok(mls_group) => Ok(Some(mls_group)), + Err(err) => { + log::error!("failed to create group from welcome: {}", err); + Ok(None) + } } - } + }) + .ok() + .flatten() }) .collect(); @@ -277,7 +266,7 @@ fn extract_welcome(welcome_bytes: &Vec) -> Result { mod tests { use xmtp_cryptography::utils::generate_local_wallet; - use crate::{builder::ClientBuilder, InboxOwner}; + use crate::{builder::ClientBuilder, storage::EncryptedMessageStore, InboxOwner}; #[tokio::test] async fn test_mls_error() { @@ -331,16 +320,16 @@ mod tests { // Manually mark as committed // TODO: Replace with working synchronization once we can add members end to end - let intents = alice - .store - .find_group_intents(conn, alice_bob_group.group_id.clone(), None, None) - .unwrap(); + let intents = EncryptedMessageStore::find_group_intents( + conn, + alice_bob_group.group_id.clone(), + None, + None, + ) + .unwrap(); let intent = intents.first().unwrap(); // Set the intent to committed manually - alice - .store - .set_group_intent_committed(conn, intent.id) - .unwrap(); + EncryptedMessageStore::set_group_intent_committed(conn, intent.id).unwrap(); alice_bob_group.post_commit(conn).await.unwrap(); diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index a3a62a6aa..52d0dc6cd 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -254,7 +254,8 @@ where } pub fn process_messages(&self, envelopes: Vec) -> Result<(), GroupError> { - let provider = self.client.mls_provider(); + let mut conn = self.client.store.conn()?; + let provider = self.client.mls_provider(&mut conn); let mut openmls_group = self.load_mls_group(&provider)?; let receive_errors: Vec = envelopes .into_iter() diff --git a/xmtp_mls/src/identity.rs b/xmtp_mls/src/identity.rs index b33ebbc25..97ace2196 100644 --- a/xmtp_mls/src/identity.rs +++ b/xmtp_mls/src/identity.rs @@ -160,7 +160,8 @@ mod tests { #[test] fn test_key_package_extensions() { let store = EncryptedMessageStore::new_test(); - let provider = XmtpOpenMlsProvider::new(&store); + let mut conn = store.conn().unwrap(); + let provider = XmtpOpenMlsProvider::new(&mut conn); let identity = Identity::new(&store, &provider, &generate_local_wallet()).unwrap(); let new_key_package = identity.new_key_package(&provider).unwrap();