Skip to content

Commit 7bce12c

Browse files
authored
Merge pull request #3320 from matrix-org/andybalaam/fast-backup-reset-in-memorystore2
crypto: MemoryStore uses backup versions to track which sessions are backed up
2 parents 5df53d7 + 2652b77 commit 7bce12c

File tree

4 files changed

+443
-38
lines changed

4 files changed

+443
-38
lines changed

crates/matrix-sdk-crypto/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Breaking changes:
2929

3030
Additions:
3131

32+
- Expose new method `BackupMachine::backup_version`.
33+
([#3320](https://github.com/matrix-org/matrix-rust-sdk/pull/3320))
34+
3235
- Add data types to parse the QR code data for the QR code login defined in
3336
[MSC4108](https://github.com/matrix-org/matrix-spec-proposals/pull/4108)
3437

crates/matrix-sdk-crypto/src/backups/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ impl BackupMachine {
419419
Ok(())
420420
}
421421

422+
/// Provide the `backup_version` of the current `backup_key`, or None if
423+
/// there is no current key, or the key is not used with any backup
424+
/// version.
425+
pub async fn backup_version(&self) -> Option<String> {
426+
self.backup_key.read().await.as_ref().and_then(|k| k.backup_version())
427+
}
428+
422429
/// Store the backup decryption key in the crypto store.
423430
///
424431
/// This is useful if the client wants to support gossiping of the backup

crates/matrix-sdk-crypto/src/store/integration_tests.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -375,38 +375,42 @@ macro_rules! cryptostore_integration_tests {
375375

376376
#[async_test]
377377
async fn reset_inbound_group_session_for_backup() {
378+
// Given a store exists where all sessions are backed up to backup_1
378379
let (account, store) =
379380
get_loaded_store("reset_inbound_group_session_for_backup").await;
380-
assert_eq!(store.inbound_group_session_counts(None).await.unwrap().total, 0);
381-
382381
let room_id = &room_id!("!test:localhost");
383-
let (_, session) = account.create_group_session_pair_with_defaults(room_id).await;
384-
385-
let changes =
386-
Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() };
387-
382+
let mut sessions: Vec<InboundGroupSession> = Vec::with_capacity(10);
383+
for _ in 0..10 {
384+
sessions.push(account.create_group_session_pair_with_defaults(room_id).await.1);
385+
}
386+
let changes = Changes { inbound_group_sessions: sessions.clone(), ..Default::default() };
388387
store.save_changes(changes).await.expect("Can't save group session");
388+
assert_eq!(store.inbound_group_sessions_for_backup("backup_1", 100).await.unwrap().len(), 10);
389+
store.mark_inbound_group_sessions_as_backed_up(
390+
"backup_1",
391+
&(0..10).map(|i| session_info(&sessions[i])).collect::<Vec<_>>(),
392+
).await.expect("Failed to mark sessions as backed up");
389393

390-
// Given we have backed up our session
391-
store
392-
.mark_inbound_group_sessions_as_backed_up("bkpver1", &[session_info(&session)])
393-
.await
394-
.expect("Failed to mark_inbound_group_sessions_as_backed_up.");
395-
396-
assert_eq!(store.inbound_group_session_counts(Some("bkpver1")).await.unwrap().total, 1);
397-
assert_eq!(store.inbound_group_session_counts(Some("bkpver1")).await.unwrap().backed_up, 1);
394+
// Sanity: none need backing up to the same backup
395+
{
396+
let to_back_up_old = store.inbound_group_sessions_for_backup("backup_1", 10).await.unwrap();
397+
assert_eq!(to_back_up_old.len(), 0);
398+
}
398399

399-
// Sanity: before resetting, we have nothing to back up
400-
let to_back_up = store.inbound_group_sessions_for_backup("bkpver1", 1).await.unwrap();
401-
assert_eq!(to_back_up, vec![]);
400+
// Some stores ignore backup_version and just reset when you tell them to. Tell
401+
// them here.
402+
store.reset_backup_state().await.expect("reset failed");
402403

403-
// When we reset the backup
404-
store.reset_backup_state().await.unwrap();
404+
// When we ask what needs backing up to a different backup version
405+
let to_back_up = store.inbound_group_sessions_for_backup("backup_02", 10).await.unwrap();
405406

406-
// Then after resetting, even if we supply the same backup version number, we need
407-
// to back up the session
408-
let to_back_up = store.inbound_group_sessions_for_backup("bkpver1", 1).await.unwrap();
409-
assert_eq!(to_back_up, vec![session]);
407+
// Then the answer is everything
408+
let needs_backing_up = |i: usize| to_back_up.iter().any(|s| s.session_id() == sessions[i].session_id());
409+
assert!(needs_backing_up(0));
410+
assert!(needs_backing_up(1));
411+
assert!(needs_backing_up(8));
412+
assert!(needs_backing_up(9));
413+
assert_eq!(to_back_up.len(), 10);
410414
}
411415

412416
#[async_test]

0 commit comments

Comments
 (0)