core_crypto/mls/client/epoch_observer.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use std::sync::Arc;
use async_trait::async_trait;
use crate::{CoreCrypto, RecursiveError, mls::HasClientAndProvider as _, prelude::ConversationId};
use super::{Client, Error, Result};
/// An `EpochObserver` is notified whenever a conversation's epoch changes.
#[async_trait]
pub trait EpochObserver: Send + Sync {
/// This function will be called every time a conversation's epoch changes.
///
/// The `epoch` parameter is the new epoch.
///
/// <div class="warning">
/// This function must not block! Foreign implementors of this inteface can
/// spawn a task indirecting the notification, or (unblocking) send the notification
/// on some kind of channel, or anything else, as long as the operation completes
/// quickly.
/// </div>
async fn epoch_changed(&self, conversation_id: ConversationId, epoch: u64);
}
impl Client {
/// Add an epoch observer to this client.
///
/// This function should be called 0 or 1 times in a client's lifetime. If called
/// when an epoch observer already exists, this will return an error.
pub(crate) async fn register_epoch_observer(&self, epoch_observer: Arc<dyn EpochObserver>) -> Result<()> {
let mut guard = self.state.write().await;
let inner = guard.as_mut().ok_or(Error::MlsNotInitialized)?;
if inner.epoch_observer.is_some() {
return Err(Error::EpochObserverAlreadyExists);
}
inner.epoch_observer = Some(epoch_observer);
Ok(())
}
/// Notify the observer that the epoch has changed, if one is present.
pub(crate) async fn notify_epoch_changed(&self, conversation_id: ConversationId, epoch: u64) {
let guard = self.state.read().await;
if let Some(inner) = guard.as_ref() {
if let Some(observer) = inner.epoch_observer.as_ref() {
observer.epoch_changed(conversation_id, epoch).await;
}
}
}
}
impl CoreCrypto {
/// Add an epoch observer to this client.
///
/// This function should be called 0 or 1 times in a client's lifetime.
/// If called when an epoch observer already exists, this will return an error.
pub async fn register_epoch_observer(&self, epoch_observer: Arc<dyn EpochObserver>) -> Result<()> {
let client = self.client().await.map_err(RecursiveError::mls("getting mls client"))?;
client.register_epoch_observer(epoch_observer).await
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use rstest_reuse::apply;
use wasm_bindgen_test::*;
use crate::test_utils::{TestCase, TestEpochObserver, all_cred_cipher, conversation_id, run_test_with_client_ids};
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn observe_local_epoch_change(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice"], move |[client]| {
Box::pin(async move {
let id = conversation_id();
client
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
let observer = TestEpochObserver::new();
client
.client()
.await
.register_epoch_observer(observer.clone())
.await
.unwrap();
// trigger an epoch
client
.context
.conversation(&id)
.await
.unwrap()
.update_key_material()
.await
.unwrap();
// ensure we have observed the epoch change
let observed_epochs = observer.observed_epochs().await;
assert_eq!(
observed_epochs.len(),
1,
"we triggered exactly one epoch change and so should observe one epoch change"
);
assert_eq!(
observed_epochs[0].0, id,
"conversation id of observed epoch change must match"
);
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn observe_remote_epoch_change(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice, bob]| {
Box::pin(async move {
let id = conversation_id();
alice
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
alice.invite_all(&case, &id, [&bob]).await.unwrap();
// bob has the observer
let observer = TestEpochObserver::new();
bob.client()
.await
.register_epoch_observer(observer.clone())
.await
.unwrap();
// alice triggers an epoch
alice
.context
.conversation(&id)
.await
.unwrap()
.update_key_material()
.await
.unwrap();
// communicate that to bob
let commit = alice.mls_transport.latest_commit().await;
bob.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(commit.to_bytes().unwrap())
.await
.unwrap();
// ensure we have observed the epoch change
let observed_epochs = observer.observed_epochs().await;
assert_eq!(
observed_epochs.len(),
1,
"we triggered exactly one epoch change and so should observe one epoch change"
);
assert_eq!(
observed_epochs[0].0, id,
"conversation id of observed epoch change must match"
);
})
})
.await
}
}