Skip to content

Commit

Permalink
add more errors and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Aug 16, 2024
1 parent b78568d commit ee25eac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 10 additions & 1 deletion xmtp_mls/src/groups/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@ impl MlsGroup {
&self,
provider: &XmtpOpenMlsProvider,
) -> Result<Vec<GroupMember>, GroupError> {
log::debug!("top of members provider");
let openmls_group = self.load_mls_group(provider)?;
log::debug!("after load mls group");
let group_membership = extract_group_membership(openmls_group.extensions())?;
log::debug!("after extract group membership");
let requests: Vec<_> = group_membership
.members
.into_iter()
.map(|(inbox_id, sequence_id)| (inbox_id, sequence_id as i64))
.filter(|(_, sequence_id)| *sequence_id != 0) // Skip the initial state
.collect();
log::debug!("after requests");

let conn = provider.conn_ref();
let association_states =
StoredAssociationState::batch_read_from_cache(conn, requests.clone())?;
log::debug!("get from the cache");

let mutable_metadata = self.mutable_metadata()?;
if association_states.len() != requests.len() {
Expand All @@ -58,6 +63,8 @@ impl MlsGroup {
}
return Err(GroupError::InvalidGroupMembership);
}
log::debug!("after the mutable metadata");


// Estimate vector capacity based on the number of association states
let mut members: Vec<GroupMember> = Vec::with_capacity(association_states.len());
Expand All @@ -84,7 +91,9 @@ impl MlsGroup {
permission_level,
}
})
.collect();
.collect();
log::debug!("at the end");


Ok(members)
}
Expand Down
19 changes: 16 additions & 3 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,25 @@ impl EncryptedMessageStore {
pool.state().connections
);

let mut conn = pool.get()?;
let mut conn = pool.get().map_err(|e| {
log::error!("Failed to get connection from pool: {}", e);
StorageError::ConnectionPoolError(format!("Failed to get connection: {}", e))
})?;

if let Some(ref key) = self.enc_key {
conn.batch_execute(&format!("PRAGMA key = \"x'{}'\";", hex::encode(key)))?;
log::info!("Applying encryption key to database connection");
conn.batch_execute(&format!("PRAGMA key = \"x'{}'\";", hex::encode(key)))
.map_err(|e| {
log::error!("Failed to apply encryption key: {}", e);
StorageError::SqlCipherError(format!("Failed to apply encryption key: {}", e))
})?;
}

conn.batch_execute("PRAGMA busy_timeout = 5000;")?;
log::info!("Setting busy_timeout to 5000ms");
conn.batch_execute("PRAGMA busy_timeout = 5000;").map_err(|e| {
log::error!("Failed to set busy_timeout: {}", e);
StorageError::DatabaseConfigError(format!("Failed to set busy_timeout: {}", e))
})?;

Ok(conn)
}
Expand Down
6 changes: 6 additions & 0 deletions xmtp_mls/src/storage/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub enum StorageError {
Lock(String),
#[error("Pool needs to reconnect before use")]
PoolNeedsConnection,
#[error("Connection pool error")]
ConnectionPoolError(String),
#[error("Database is encrypted but key is incorrect")]
SqlCipherError(String),
#[error("Database config error")]
DatabaseConfigError(String),
#[error("Conflict")]
Conflict(String),
#[error(transparent)]
Expand Down

0 comments on commit ee25eac

Please sign in to comment.