Skip to content

Commit

Permalink
Address PR feedback on error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Nov 15, 2023
1 parent 3a47451 commit b0f8145
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use intents::SendMessageIntentData;
use log::debug;
use openmls::{
framing::ProtocolMessage,
group::GroupEpoch,
group::{GroupEpoch, MergePendingCommitError},
prelude::{
CredentialWithKey, CryptoConfig, GroupId, LeafNodeIndex, MlsGroup as OpenMlsGroup,
MlsGroupConfig, MlsMessageIn, MlsMessageInBody, PrivateMessageIn, ProcessedMessage,
Expand Down Expand Up @@ -84,8 +84,10 @@ pub enum MessageProcessingError {
MergePendingCommit(#[from] openmls::group::MergePendingCommitError<StorageError>),
#[error("merge staged commit: {0}")]
MergeStagedCommit(#[from] openmls::group::MergeCommitError<StorageError>),
#[error("wrong epoch. expected {group_epoch:?} and got {message_epoch:?}")]
WrongEpoch {
#[error(
"no pending commit to merge. group epoch is {group_epoch:?} and got {message_epoch:?}"
)]
NoPendingCommit {
message_epoch: GroupEpoch,
group_epoch: GroupEpoch,
},
Expand Down Expand Up @@ -242,36 +244,37 @@ where
intent.id,
intent.kind
);
let message_epoch = message.epoch();
let group_epoch = openmls_group.epoch();

// TODO: Figure out if we really need to do this for application messages
// With the max_past_epochs flag this likely isn't necessary
if group_epoch != message_epoch {
log::warn!(
"wrong epoch. expected {}. received {}",
group_epoch,
message_epoch
);
// All other intent kinds create pending commits. They need to be cleared
if intent.kind != IntentKind::SendMessage {
openmls_group.clear_pending_commit();
}
// Revert intent to ToPublish state
self.client
.store
.set_group_intent_to_publish(conn, intent.id)?;

return Err(MessageProcessingError::WrongEpoch {
message_epoch,
group_epoch,
});
}

match intent.kind {
IntentKind::AddMembers | IntentKind::RemoveMembers | IntentKind::KeyUpdate => {
// We don't get errors with merge_pending_commit when there are no commits to merge
if openmls_group.pending_commit().is_none() {
let message_epoch = message.epoch();
let group_epoch = openmls_group.epoch();
debug!(
"no pending commit to merge. Group epoch: {}. Message epoch: {}",
group_epoch, message_epoch
);
self.client
.store
.set_group_intent_to_publish(conn, intent.id)?;

return Err(MessageProcessingError::NoPendingCommit {
message_epoch,
group_epoch,
});
}
debug!("[{}] merging pending commit", self.client.account_address());
openmls_group.merge_pending_commit(provider)?;
match openmls_group.merge_pending_commit(provider) {
Err(MergePendingCommitError::MlsGroupStateError(err)) => {
debug!("error merging commit: {}", err);
openmls_group.clear_pending_commit();
self.client
.store
.set_group_intent_to_publish(conn, intent.id)?;
}
_ => (),
};
// TOOD: Handle writing transcript messages for adding/removing members
}
IntentKind::SendMessage => {
Expand Down Expand Up @@ -338,7 +341,10 @@ where
// intentionally left blank.
}
ProcessedMessageContent::StagedCommitMessage(staged_commit) => {
debug!("[{}] received staged commit", self.client.account_address());
debug!(
"[{}] received staged commit. Merging and clearing any pending commits",
self.client.account_address()
);
openmls_group.merge_staged_commit(provider, *staged_commit)?;
}
}
Expand Down Expand Up @@ -736,7 +742,6 @@ mod tests {
.add_members_by_installation_id(vec![charlie.installation_public_key()])
.await
.expect("failed to add charlie");

bola_group
.add_members_by_installation_id(vec![charlie.installation_public_key()])
.await
Expand Down

0 comments on commit b0f8145

Please sign in to comment.