Skip to content

Commit f6f9af9

Browse files
authored
speed up sync init (#1363)
1 parent 85dd6d3 commit f6f9af9

File tree

22 files changed

+302
-115
lines changed

22 files changed

+302
-115
lines changed

bindings_ffi/src/mls.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl FfiXmtpClient {
285285
}
286286

287287
pub fn installation_id(&self) -> Vec<u8> {
288-
self.inner_client.installation_public_key()
288+
self.inner_client.installation_public_key().to_vec()
289289
}
290290

291291
pub fn release_db_connection(&self) -> Result<(), GenericError> {
@@ -380,7 +380,7 @@ impl FfiXmtpClient {
380380
signature_bytes: Vec<u8>,
381381
) -> Result<(), GenericError> {
382382
let inner = self.inner_client.as_ref();
383-
let public_key = inner.installation_public_key();
383+
let public_key = inner.installation_public_key().to_vec();
384384

385385
self.verify_signed_with_public_key(signature_text, signature_bytes, public_key)
386386
}
@@ -454,12 +454,8 @@ impl FfiXmtpClient {
454454
return Ok(());
455455
}
456456

457-
let provider = self
458-
.inner_client
459-
.mls_provider()
460-
.map_err(GenericError::from_error)?;
461457
self.inner_client
462-
.start_sync_worker(&provider)
458+
.start_sync_worker()
463459
.await
464460
.map_err(GenericError::from_error)?;
465461

@@ -537,7 +533,7 @@ impl FfiXmtpClient {
537533
let other_installation_ids = inbox_state
538534
.installation_ids()
539535
.into_iter()
540-
.filter(|id| id != &installation_id)
536+
.filter(|id| id != installation_id)
541537
.collect();
542538

543539
let signature_request = self
@@ -911,7 +907,8 @@ impl FfiConversations {
911907

912908
pub fn get_sync_group(&self) -> Result<FfiConversation, GenericError> {
913909
let inner = self.inner_client.as_ref();
914-
let sync_group = inner.get_sync_group()?;
910+
let conn = inner.store().conn()?;
911+
let sync_group = inner.get_sync_group(&conn)?;
915912
Ok(sync_group.into())
916913
}
917914

@@ -2096,7 +2093,7 @@ mod tests {
20962093
.unwrap();
20972094
register_client(&ffi_inbox_owner, &client_a).await;
20982095

2099-
let installation_pub_key = client_a.inner_client.installation_public_key();
2096+
let installation_pub_key = client_a.inner_client.installation_public_key().to_vec();
21002097
drop(client_a);
21012098

21022099
let client_b = create_client(
@@ -2114,7 +2111,7 @@ mod tests {
21142111
.await
21152112
.unwrap();
21162113

2117-
let other_installation_pub_key = client_b.inner_client.installation_public_key();
2114+
let other_installation_pub_key = client_b.inner_client.installation_public_key().to_vec();
21182115
drop(client_b);
21192116

21202117
assert!(

bindings_node/src/signatures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Client {
9797
let other_installation_ids = inbox_state
9898
.installation_ids()
9999
.into_iter()
100-
.filter(|id| id != &installation_id)
100+
.filter(|id| id != installation_id)
101101
.collect();
102102
let signature_request = self
103103
.inner_client()

bindings_wasm/src/signatures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Client {
101101
let other_installation_ids = inbox_state
102102
.installation_ids()
103103
.into_iter()
104-
.filter(|id| id != &installation_id)
104+
.filter(|id| id != installation_id)
105105
.collect();
106106
let signature_request = self
107107
.inner_client()

examples/cli/cli-client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
443443
let conn = client.store().conn().unwrap();
444444
let provider = client.mls_provider().unwrap();
445445
client.sync_welcomes(&conn).await.unwrap();
446-
client.start_sync_worker(&provider).await.unwrap();
446+
client.start_sync_worker().await.unwrap();
447447
client
448448
.send_sync_request(&provider, DeviceSyncKind::MessageHistory)
449449
.await
@@ -453,7 +453,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
453453
Commands::ListHistorySyncMessages {} => {
454454
let conn = client.store().conn()?;
455455
client.sync_welcomes(&conn).await?;
456-
let group = client.get_sync_group()?;
456+
let group = client.get_sync_group(&conn)?;
457457
let group_id_str = hex::encode(group.group_id.clone());
458458
group.sync().await?;
459459
let messages = group

examples/cli/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub async fn debug_welcome_messages(
7171
) -> Result<(), String> {
7272
let api_client = client.api();
7373
let envelopes = api_client
74-
.query_welcome_messages(installation_id, None)
74+
.query_welcome_messages(&installation_id, None)
7575
.await
7676
.unwrap();
7777
for envelope in envelopes {

xmtp_mls/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ tracing-subscriber = { workspace = true, features = [
9595
"env-filter",
9696
"fmt",
9797
"ansi",
98+
"json",
99+
"registry"
98100
], optional = true }
99101

100102

@@ -151,6 +153,7 @@ tracing-subscriber = { workspace = true, features = [
151153
"env-filter",
152154
"fmt",
153155
"ansi",
156+
"json",
154157
] }
155158
xmtp_api_grpc = { path = "../xmtp_api_grpc", features = ["test-utils"] }
156159
xmtp_api_http = { path = "../xmtp_api_http", features = ["test-utils"] }
@@ -163,7 +166,7 @@ diesel-wasm-sqlite = { workspace = true, features = [
163166
] }
164167
ethers = { workspace = true, features = ["rustls"] }
165168
openmls = { workspace = true, features = ["js"] }
166-
tracing-subscriber = { workspace = true, features = ["env-filter"] }
169+
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
167170
tracing-wasm = { version = "0.2" }
168171
wasm-bindgen-test.workspace = true
169172
xmtp_api_http = { path = "../xmtp_api_http", features = ["test-utils"] }

xmtp_mls/benches/sync.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ fn start_sync_worker(c: &mut Criterion) {
2929
|| {
3030
bench_async_setup(|| async {
3131
let client = clients::new_client(true).await;
32-
let provider = client.mls_provider().unwrap();
3332
// set history sync URL
34-
(client, provider, span.clone())
33+
(client, span.clone())
3534
})
3635
},
37-
|(client, provider, span)| async move {
36+
|(client, span)| async move {
3837
client
39-
.start_sync_worker(&provider)
38+
.start_sync_worker()
4039
.instrument(span)
4140
.await
4241
.unwrap()

xmtp_mls/src/api/mls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ where
117117
#[tracing::instrument(level = "trace", skip_all)]
118118
pub async fn query_welcome_messages(
119119
&self,
120-
installation_id: Vec<u8>,
120+
installation_id: &[u8],
121121
id_cursor: Option<u64>,
122122
) -> Result<Vec<WelcomeMessage>, ApiError> {
123123
tracing::debug!(
124-
installation_id = hex::encode(&installation_id),
124+
installation_id = hex::encode(installation_id),
125125
cursor = id_cursor,
126126
inbox_id = self.inbox_id,
127127
"query welcomes"
@@ -135,7 +135,7 @@ where
135135
(async {
136136
self.api_client
137137
.query_welcome_messages(QueryWelcomeMessagesRequest {
138-
installation_key: installation_id.clone(),
138+
installation_key: installation_id.to_vec(),
139139
paging_info: Some(PagingInfo {
140140
id_cursor: id_cursor.unwrap_or(0),
141141
limit: page_size,

xmtp_mls/src/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ pub(crate) mod tests {
709709
register_client(&client_a, wallet).await;
710710
assert!(client_a.identity().is_ready());
711711

712-
let keybytes_a = client_a.installation_public_key();
712+
let keybytes_a = client_a.installation_public_key().to_vec();
713713
drop(client_a);
714714

715715
// Reload the existing store and wallet
@@ -729,7 +729,7 @@ pub(crate) mod tests {
729729
.build_with_verifier()
730730
.await
731731
.unwrap();
732-
let keybytes_b = client_b.installation_public_key();
732+
let keybytes_b = client_b.installation_public_key().to_vec();
733733
drop(client_b);
734734

735735
// Ensure the persistence was used to store the generated keys
@@ -762,7 +762,7 @@ pub(crate) mod tests {
762762
.build_with_verifier()
763763
.await
764764
.unwrap();
765-
assert_eq!(client_d.installation_public_key(), keybytes_a);
765+
assert_eq!(client_d.installation_public_key().to_vec(), keybytes_a);
766766
}
767767

768768
/// anvil cannot be used in WebAssembly

xmtp_mls/src/client.rs

+35-21
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ pub struct XmtpMlsLocalContext {
172172

173173
impl XmtpMlsLocalContext {
174174
/// The installation public key is the primary identifier for an installation
175-
pub fn installation_public_key(&self) -> Vec<u8> {
176-
self.identity.installation_keys.public_slice().to_vec()
175+
pub fn installation_public_key(&self) -> &[u8; 32] {
176+
self.identity.installation_keys.public_bytes()
177177
}
178178

179179
/// Get the account address of the blockchain account associated with this client
@@ -259,7 +259,7 @@ where
259259
V: SmartContractSignatureVerifier,
260260
{
261261
/// Retrieves the client's installation public key, sometimes also called `installation_id`
262-
pub fn installation_public_key(&self) -> Vec<u8> {
262+
pub fn installation_public_key(&self) -> &[u8; 32] {
263263
self.context.installation_public_key()
264264
}
265265
/// Retrieves the client's inbox ID
@@ -560,20 +560,25 @@ where
560560
Ok(group)
561561
}
562562

563-
pub(crate) fn create_sync_group(&self) -> Result<MlsGroup<Self>, ClientError> {
563+
pub(crate) fn create_sync_group(
564+
&self,
565+
provider: &XmtpOpenMlsProvider,
566+
) -> Result<MlsGroup<Self>, ClientError> {
564567
tracing::info!("creating sync group");
565-
let sync_group = MlsGroup::create_and_insert_sync_group(Arc::new(self.clone()))?;
568+
let sync_group = MlsGroup::create_and_insert_sync_group(Arc::new(self.clone()), provider)?;
566569

567570
Ok(sync_group)
568571
}
569572

570-
/**
571-
* Look up a group by its ID
572-
*
573-
* Returns a [`MlsGroup`] if the group exists, or an error if it does not
574-
*/
575-
pub fn group(&self, group_id: Vec<u8>) -> Result<MlsGroup<Self>, ClientError> {
576-
let conn = &mut self.store().conn()?;
573+
/// Look up a group by its ID
574+
///
575+
/// Returns a [`MlsGroup`] if the group exists, or an error if it does not
576+
///
577+
pub fn group_with_conn(
578+
&self,
579+
conn: &DbConnection,
580+
group_id: Vec<u8>,
581+
) -> Result<MlsGroup<Self>, ClientError> {
577582
let stored_group: Option<StoredGroup> = conn.fetch(&group_id)?;
578583
match stored_group {
579584
Some(group) => Ok(MlsGroup::new(self.clone(), group.id, group.created_at_ns)),
@@ -584,6 +589,15 @@ where
584589
}
585590
}
586591

592+
/// Look up a group by its ID
593+
///
594+
/// Returns a [`MlsGroup`] if the group exists, or an error if it does not
595+
///
596+
pub fn group(&self, group_id: Vec<u8>) -> Result<MlsGroup<Self>, ClientError> {
597+
let conn = &mut self.store().conn()?;
598+
self.group_with_conn(conn, group_id)
599+
}
600+
587601
/**
588602
* Look up a DM group by the target's inbox_id.
589603
*
@@ -697,7 +711,7 @@ where
697711
conn: &DbConnection,
698712
) -> Result<Vec<WelcomeMessage>, ClientError> {
699713
let installation_id = self.installation_public_key();
700-
let id_cursor = conn.get_last_cursor_for_id(&installation_id, EntityKind::Welcome)?;
714+
let id_cursor = conn.get_last_cursor_for_id(installation_id, EntityKind::Welcome)?;
701715

702716
let welcomes = self
703717
.api_client
@@ -747,7 +761,7 @@ where
747761
(async {
748762
let welcome_v1 = &welcome_v1;
749763
self.intents.process_for_id(
750-
&id,
764+
id,
751765
EntityKind::Welcome,
752766
welcome_v1.id,
753767
|provider| async move {
@@ -1021,7 +1035,7 @@ pub(crate) mod tests {
10211035

10221036
// Get original KeyPackage.
10231037
let kp1 = client
1024-
.get_key_packages_for_installation_ids(vec![client.installation_public_key()])
1038+
.get_key_packages_for_installation_ids(vec![client.installation_public_key().to_vec()])
10251039
.await
10261040
.unwrap();
10271041
assert_eq!(kp1.len(), 1);
@@ -1031,7 +1045,7 @@ pub(crate) mod tests {
10311045
client.rotate_key_package().await.unwrap();
10321046

10331047
let kp2 = client
1034-
.get_key_packages_for_installation_ids(vec![client.installation_public_key()])
1048+
.get_key_packages_for_installation_ids(vec![client.installation_public_key().to_vec()])
10351049
.await
10361050
.unwrap();
10371051
assert_eq!(kp2.len(), 1);
@@ -1299,9 +1313,9 @@ pub(crate) mod tests {
12991313
let bo_store = bo.store();
13001314

13011315
let alix_original_init_key =
1302-
get_key_package_init_key(&alix, &alix.installation_public_key()).await;
1316+
get_key_package_init_key(&alix, alix.installation_public_key()).await;
13031317
let bo_original_init_key =
1304-
get_key_package_init_key(&bo, &bo.installation_public_key()).await;
1318+
get_key_package_init_key(&bo, bo.installation_public_key()).await;
13051319

13061320
// Bo's original key should be deleted
13071321
let bo_original_from_db = bo_store
@@ -1320,19 +1334,19 @@ pub(crate) mod tests {
13201334

13211335
bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();
13221336

1323-
let bo_new_key = get_key_package_init_key(&bo, &bo.installation_public_key()).await;
1337+
let bo_new_key = get_key_package_init_key(&bo, bo.installation_public_key()).await;
13241338
// Bo's key should have changed
13251339
assert_ne!(bo_original_init_key, bo_new_key);
13261340

13271341
bo.sync_welcomes(&bo.store().conn().unwrap()).await.unwrap();
1328-
let bo_new_key_2 = get_key_package_init_key(&bo, &bo.installation_public_key()).await;
1342+
let bo_new_key_2 = get_key_package_init_key(&bo, bo.installation_public_key()).await;
13291343
// Bo's key should not have changed syncing the second time.
13301344
assert_eq!(bo_new_key, bo_new_key_2);
13311345

13321346
alix.sync_welcomes(&alix.store().conn().unwrap())
13331347
.await
13341348
.unwrap();
1335-
let alix_key_2 = get_key_package_init_key(&alix, &alix.installation_public_key()).await;
1349+
let alix_key_2 = get_key_package_init_key(&alix, alix.installation_public_key()).await;
13361350
// Alix's key should not have changed at all
13371351
assert_eq!(alix_original_init_key, alix_key_2);
13381352

0 commit comments

Comments
 (0)