p2panda-spaces 0.8.2

Data encryption for multi-device groups
Documentation
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
// SPDX-License-Identifier: MIT OR Apache-2.0

//! High-level API for managing spaces, groups and member keys.
use std::borrow::Borrow;
use std::fmt::Debug;
use std::sync::Arc;

use p2panda_auth::Access;
use p2panda_auth::traits::{Conditions, Operation};
use p2panda_core::traits::{Digest, Provenance, ShortFormat};
use p2panda_core::{Hash, SigningKey, VerifyingKey};
use p2panda_encryption::key_bundle::LongTermKeyBundle;
use p2panda_encryption::key_registry::{KeyRegistry, KeyRegistryError};
use p2panda_encryption::traits::PreKeyRegistry;
use p2panda_encryption::{Rng, RngError};
use p2panda_store::Transaction;
use p2panda_store::groups::GroupsStore;
use p2panda_store::key_registry::KeyRegistryStore;
use p2panda_store::key_secrets::PreKeyBundlesStore;
use p2panda_store::spaces::{SpacesMessageStore, SpacesStore};
use thiserror::Error;
use tracing::debug;

use crate::event::Event;
use crate::forge::Forge;
use crate::group::{Group, GroupError, GroupOutput};
use crate::identity::{IdentityError, IdentityManager, MemberStatus};
use crate::member::AuthenticatedMember;
use crate::message::{AuthMessage, SpaceMembershipMessage, SpacesArgs, SpacesMessage};
use crate::space::RepairOutput;
use crate::space::{Space, SpaceError, SpaceOutput};
use crate::store::{
    SpaceState, SpacesStoreState, StateChanges, StateError, StateReader, StateTransaction,
};
use crate::utils::{is_member, is_secret_member, removed_space_members};
use crate::waiters::EventWaiters;
use crate::{
    ActorId, Config, Credentials, GroupId, MemberId, PendingGroup, PendingMember, PendingSpace,
    SpaceId,
};

/// API for creating and managing groups and spaces.
///
/// There should be only one manager instance per application. Any messages received from other
/// instances must be processed on the manager. All methods which mutate state locally return M
/// message(s) which must be replicated to and processed by other instances. Any action which
/// mutates state (both local method calls and processed messages) will emit events which can be
/// sent to any higher levels to inform of any state changes.
///
/// In order to add a member to a space, we first need to have a key bundle generated from a
/// not-expired pre-key. The manager offers an API for checking our latest key-bundle is valid and
/// issuing new key bundles to be replicated with other instances.
///
/// All methods are idempotent; messages can be processed multiple times without causing any
/// additional state changes.
///
/// p2panda-spaces is agnostic to the concrete message type used to send control and application
/// messages, providing the trait requirements are met.
///
/// ## Requirements
///
/// All messages must be ordered according to their causal relationship _before_ being processed on
/// the manager. All messages created within p2panda-spaces express their dependencies; these should
/// be used to perform partial ordering of all incoming messages.
#[derive(Debug)]
pub struct Manager<S, F, C> {
    actor_id: ActorId,
    inner: Arc<ManagerInner<S, F, C>>,
}

impl<S, F, C> Clone for Manager<S, F, C> {
    // Deriving clone on Manager will enforce generics to also impl Clone even though we are
    // wrapping them in an Arc. Related: <https://stackoverflow.com/questions/72150623>.
    fn clone(&self) -> Self {
        Self {
            actor_id: self.actor_id,
            inner: self.inner.clone(),
        }
    }
}

#[derive(Debug)]
struct ManagerInner<S, F, C> {
    pub config: Config,
    pub forge: F,
    pub store: S,
    pub credentials: Credentials,
    pub rng: Rng,
    pub waiters: EventWaiters<C>,
}

impl<S, F, C> Manager<S, F, C>
where
    S: Clone
        + SpacesStore<SpacesStoreState<C>>
        + SpacesMessageStore<F::Message>
        + GroupsStore<AuthMessage<C>, C>
        + KeyRegistryStore
        + PreKeyBundlesStore
        + Transaction,
    F: Forge<C>,
    C: Conditions,
{
    /// Instantiate a new manager.
    #[allow(clippy::result_large_err)]
    pub fn new(
        store: S,
        forge: F,
        credentials: Credentials,
        rng: Rng,
    ) -> Result<Self, ManagerError<F, C>> {
        Self::new_with_config(store, forge, credentials, Config::default(), rng)
    }

    /// Instantiate a new manager with custom configuration.
    #[allow(clippy::result_large_err)]
    pub fn new_with_config(
        store: S,
        forge: F,
        credentials: Credentials,
        config: Config,
        rng: Rng,
    ) -> Result<Self, ManagerError<F, C>> {
        let actor_id: ActorId = credentials.verifying_key();
        let inner = ManagerInner {
            config,
            store,
            forge,
            credentials,
            rng,
            waiters: EventWaiters::new(),
        };
        Ok(Self {
            actor_id,
            inner: Arc::new(inner),
        })
    }

    /// Begin a transaction to change the space- or group state within an atomic read -> modify ->
    /// write cycle.
    ///
    /// All state needs to be read within this transaction and changed state needs to be committed
    /// after all modifications have been done. Dropping it without commit will roll back the
    /// transaction.
    pub(crate) async fn begin_write(&self) -> Result<StateTransaction<'_, S, F, C>, StoreError> {
        StateTransaction::begin(
            &self.inner.store,
            &self.inner.forge,
            &self.inner.credentials,
            &self.inner.rng,
        )
        .await
    }

    /// Read-only access to the latest committed state, without acquiring a transaction for
    /// performance reasons.
    pub(crate) fn read(&self) -> StateReader<'_, S, C> {
        StateReader::new(&self.inner.store, &self.inner.credentials)
    }

    fn identity(&self) -> IdentityManager<F, C> {
        IdentityManager::new(self.inner.config.clone())
    }

    pub(crate) fn waiters(&self) -> &EventWaiters<C> {
        &self.inner.waiters
    }

    /// Create a new group containing initial members with associated access levels.
    ///
    /// It is possible to create a group where the creator is not an initial member or is a member
    /// without manager rights. If this is done then after creation no further change of the group
    /// membership would be possible.
    ///
    /// Persists resulting state, returns group instance and forged message.
    pub async fn create_group(
        &self,
        initial_members: &[(ActorId, Access<C>)],
    ) -> Result<(Group<S, F, C>, GroupOutput<C, F::Message>), ManagerError<F, C>> {
        let tx = self.begin_write().await?;

        // Read.

        let global_groups_y = tx.global_groups_state().await?;

        // Generate random group id.
        let group_id: GroupId = {
            let signing_key = SigningKey::from_bytes(&tx.ctx.rng.random_array()?);
            signing_key.verifying_key()
        };

        // Modify.

        let (global_groups_y, output) = Group::create(
            &tx.ctx,
            global_groups_y,
            group_id,
            initial_members.to_owned(),
        )
        .await
        .map_err(ManagerError::Group)?;

        // Write.

        tx.commit(StateChanges::GlobalGroups { global_groups_y })
            .await?;

        self.waiters().notify_events([&output.event]);

        Ok((Group::new(self.clone(), group_id), output))
    }

    /// Get a group by id.
    ///
    /// A group instance provides an API for adding and removing members from a group and querying
    /// the current group members.
    ///
    /// Returns a `PendingGroup` handle which accounts for situations where we want to join a group
    /// but need to still wait for the regarding messages bringing us in. Use `has_group` and
    /// `has_joined_group` to check these conditions without waiting.
    pub fn group(&self, id: impl Into<GroupId>) -> PendingGroup<S, F, C> {
        PendingGroup::new(self.clone(), id.into())
    }

    /// Returns `true` if the group is known.
    pub async fn has_group(&self, id: impl Into<GroupId>) -> Result<bool, ManagerError<F, C>> {
        let global_groups_y = self.read().global_groups_state().await?;
        Ok(global_groups_y.has_group(id.into()))
    }

    /// Returns `true` if we are currently a member of the group.
    pub async fn has_joined_group(
        &self,
        id: impl Into<GroupId>,
    ) -> Result<bool, ManagerError<F, C>> {
        let global_groups_y = self.read().global_groups_state().await?;
        let members = global_groups_y.members(id.into());
        Ok(is_member(&members, self.id()))
    }

    /// Create a new space containing initial members and access levels.
    ///
    /// If not already included, then the local actor (creator of this space) will be added to the
    /// initial members and given manage access level.
    ///
    /// Persists resulting state, returns space instance and forged messages.
    pub async fn create_space(
        &self,
        id: impl Into<SpaceId>,
        initial_members: &[(ActorId, Access<C>)],
    ) -> Result<(Space<S, F, C>, SpaceOutput<C, F::Message>), ManagerError<F, C>> {
        let id = id.into();

        let tx = self.begin_write().await?;

        // Read.

        let global_groups_y = tx.global_groups_state().await?;
        let key_manager_y = tx.key_manager_state().await?;
        let key_registry_y = tx.key_registry_state().await?;

        // Modify.

        let (global_groups_y, space_y, output) = Space::create(
            &tx.ctx,
            global_groups_y,
            key_manager_y,
            key_registry_y,
            id,
            initial_members.to_owned(),
        )
        .await
        .map_err(ManagerError::Space)?;

        // Write.

        tx.commit(StateChanges::Space {
            global_groups_y: Some(global_groups_y),
            space_y,
        })
        .await?;

        self.waiters().notify_events(output.events());

        Ok((Space::new(self.clone(), id), output))
    }

    /// Get a space by id.
    ///
    /// A space instance provides an API for adding and removing members from the space and querying
    /// the current space members.
    ///
    /// Returns a `PendingSpace` handle which accounts for situations where we want to join a space
    /// but need to still wait for the regarding messages bringing us in. Use `has_space` and
    /// `has_joined_space` to check these conditions without waiting.
    pub fn space(&self, id: impl Into<SpaceId>) -> PendingSpace<S, F, C> {
        PendingSpace::new(self.clone(), id.into())
    }

    /// Returns all members who were removed from, and not re-admitted to, this space.
    ///
    /// Returns an empty list if the space is not known yet.
    pub async fn removed_space_members(
        &self,
        id: impl Into<SpaceId>,
    ) -> Result<Vec<MemberId>, ManagerError<F, C>> {
        let Some(space_y) = self.read().space_state(&id.into()).await? else {
            return Ok(vec![]);
        };

        Ok(removed_space_members(&space_y))
    }

    /// Returns `true` if we are currently a member of the space with more than "pull" access.
    pub async fn has_joined_space(
        &self,
        id: impl Into<SpaceId>,
    ) -> Result<bool, ManagerError<F, C>> {
        let id = id.into();

        let Some(space_y) = self.read().space_state(&id).await? else {
            return Ok(false);
        };

        let members = space_y.groups_y.members(space_y.group_id);
        Ok(is_secret_member(&members, self.id()))
    }

    /// Returns `true` if the space is known.
    pub async fn has_space(&self, id: impl Into<SpaceId>) -> Result<bool, ManagerError<F, C>> {
        let id = id.into();

        let has_space = self
            .inner
            .store
            .has_space(&id)
            .await
            .map_err(|err| StoreError::SpacesStore(err.to_string()))?;

        Ok(has_space)
    }

    /// Process a spaces message.
    ///
    /// We expect messages to be signature-checked, dependency-checked & partially ordered.
    ///
    /// Persists resulting state and returns events which inform users of any state changes which
    /// occurred.
    pub async fn process<M>(&self, message: &M) -> Result<Vec<Event<C>>, ManagerError<F, C>>
    where
        M: Provenance<VerifyingKey> + Digest<Hash> + Borrow<SpacesArgs<C>>,
    {
        let args = message.borrow();
        let span = tracing::debug_span!("spaces", node_id = self.id().fmt_short());
        let _guard = span.enter();

        debug!(
            message_id = message.hash().fmt_short(),
            author = message.author().fmt_short(),
            variant = args.variant_str(),
            "process message"
        );

        let tx = self.begin_write().await?;

        // Route message to the regarding member-, group- or space processor. Read and modify.
        let (changes, events) = match args {
            // Received key bundle from a member.
            SpacesArgs::Member(member) => {
                let key_registry_y = tx.key_registry_state().await?;

                let (key_registry_y, event) = self
                    .identity()
                    .process(key_registry_y, member)
                    .map_err(ManagerError::IdentityManager)?;

                (
                    StateChanges::PrekeyBundles {
                        key_manager_y: None,
                        key_registry_y: Some(key_registry_y),
                    },
                    vec![event],
                )
            }

            SpacesArgs::Group { .. } => {
                let global_groups_y = tx.global_groups_state().await?;

                let (global_groups_y, event) =
                    Group::<S, F, C>::process(global_groups_y, &SpacesMessage::auth(message))
                        .map_err(ManagerError::Group)?;

                let changes = match global_groups_y {
                    Some(global_groups_y) => StateChanges::GlobalGroups { global_groups_y },
                    None => StateChanges::Nothing,
                };

                (changes, vec![event])
            }

            // Received control message related to a space.
            SpacesArgs::KeyAgreement { space_id, .. } => {
                let (space_y, events) = self
                    .handle_space_membership_message(
                        &tx,
                        *space_id,
                        &SpacesMessage::space_membership(message),
                    )
                    .await?;

                let changes = match space_y {
                    Some(space_y) => StateChanges::Space {
                        global_groups_y: None,
                        space_y,
                    },
                    None => StateChanges::Nothing,
                };

                (changes, events)
            }

            SpacesArgs::SpaceUpdate { .. } => unimplemented!(),

            // Received encrypted application data for a space.
            SpacesArgs::Application { space_id, .. } => {
                let Some(space_y) = tx.space_state(space_id).await? else {
                    return Err(ManagerError::UnexpectedMessage(message.hash()));
                };

                let (space_y, events) = Space::<S, F, C>::handle_application_message(
                    space_y,
                    &SpacesMessage::application(message),
                )
                .map_err(ManagerError::Space)?;

                (
                    StateChanges::Space {
                        global_groups_y: None,
                        space_y,
                    },
                    events,
                )
            }
        };

        // Write.

        tx.commit(changes).await?;

        self.waiters().notify_events(&events);

        Ok(events)
    }

    /// The public key of the local actor.
    pub fn id(&self) -> ActorId {
        self.actor_id
    }

    /// Get a member by id.
    ///
    /// Returns a `PendingMember` handle which can be awaited until a valid key bundle of the member
    /// was registered. Use `member_key_bundle` to check this without waiting.
    pub fn member(&self, id: impl Into<MemberId>) -> PendingMember<S, F, C> {
        PendingMember::new(self.clone(), id.into())
    }

    /// Returns the status of a key bundle of a member. It can be expired, valid or not available.
    pub async fn member_key_bundle(
        &self,
        id: impl Into<MemberId>,
    ) -> Result<MemberStatus<LongTermKeyBundle>, ManagerError<F, C>> {
        let key_registry_y = self.read().key_registry_state().await?;

        match <KeyRegistry<MemberId> as PreKeyRegistry<MemberId, LongTermKeyBundle>>::key_bundle(
            key_registry_y,
            &id.into(),
        ) {
            Ok((_, Some(key_bundle))) => Ok(MemberStatus::Healthy(key_bundle)),
            Ok((_, None)) => Ok(MemberStatus::NotAvailable),
            Err(KeyRegistryError::KeyBundlesExpired) => Ok(MemberStatus::Expired),
            Err(err) => Err(ManagerError::IdentityManager(err.into())),
        }
    }

    /// The local actor id and the status of their latest long-term key bundle.
    ///
    /// This never rotates the key bundle, use `rotate_key_bundle` for that.
    pub async fn me(&self) -> Result<MemberStatus<AuthenticatedMember>, ManagerError<F, C>> {
        let identity = self.identity();
        let reader = self.read();

        let key_manager_y = reader.key_manager_state().await?;

        let into_member = |key_bundle: LongTermKeyBundle| {
            AuthenticatedMember::sign(&self.inner.rng, &self.inner.credentials, key_bundle)
                .map_err(|err| ManagerError::IdentityManager(err.into()))
        };

        let status = match identity.key_bundle_status(&key_manager_y)? {
            MemberStatus::Healthy(key_bundle) => MemberStatus::Healthy(into_member(key_bundle)?),
            MemberStatus::RotationDue(key_bundle) => {
                MemberStatus::RotationDue(into_member(key_bundle)?)
            }
            MemberStatus::NotAvailable => MemberStatus::NotAvailable,
            MemberStatus::Expired => MemberStatus::Expired,
        };

        Ok(status)
    }

    /// Register a member with long-term key bundle material which was provided through another
    /// channel (QR code scan etc.).
    pub async fn register_member(
        &self,
        member: &AuthenticatedMember,
    ) -> Result<(), ManagerError<F, C>> {
        let tx = self.begin_write().await?;

        // Read.

        let key_registry_y = tx.key_registry_state().await?;

        // Modify.

        let (key_registry_y, event) = self
            .identity()
            .process(key_registry_y, member)
            .map_err(ManagerError::IdentityManager)?;

        // Write.

        tx.commit(StateChanges::PrekeyBundles {
            key_manager_y: None,
            key_registry_y: Some(key_registry_y),
        })
        .await?;

        self.waiters().notify_events([&event]);

        Ok(())
    }

    /// Check if my latest key bundle is missing, has expired or is about to expire.
    ///
    /// If `true` then users should rotate their pre-key and generate a new bundle message (which
    /// should then be published) by calling `rotate_key_bundle`.
    pub async fn is_key_bundle_rotation_due(&self) -> Result<bool, ManagerError<F, C>> {
        let key_manager_y = self.read().key_manager_state().await?;
        let status = self.identity().key_bundle_status(&key_manager_y)?;
        Ok(status.is_rotation_due())
    }

    /// Rotate our pre-key and forge a key bundle message containing the new key bundle.
    ///
    /// Rotation takes place if no key bundle exists yet, the latest has expired or is reaching
    /// it's configured expiry date. Returns `None` if no rotation is due and no new message is
    /// required.
    ///
    /// The returned message is persisted and should be published to other peers.
    pub async fn rotate_key_bundle(&self) -> Result<Option<F::Message>, ManagerError<F, C>> {
        let identity = self.identity();

        let tx = self.begin_write().await?;

        // Read.

        let key_manager_y = tx.key_manager_state().await?;
        let key_registry_y = tx.key_registry_state().await?;

        // Don't forge a new message if no key rotation is required.
        let is_rotation_due = identity
            .key_bundle_status(&key_manager_y)?
            .is_rotation_due();

        if !is_rotation_due {
            tx.rollback().await?;
            return Ok(None);
        }

        // Modify.

        let (key_manager_y, key_registry_y, member) = identity.rotate_key_bundle(
            key_manager_y,
            key_registry_y,
            tx.ctx.credentials,
            tx.ctx.rng,
        )?;

        let message = tx.ctx.forge(SpacesArgs::Member(member.clone())).await?;

        // Write.

        tx.commit(StateChanges::PrekeyBundles {
            key_manager_y: Some(key_manager_y),
            key_registry_y: Some(key_registry_y),
        })
        .await?;

        // Our own (rotated) key bundle was registered.
        self.waiters().members.notify(&member);

        Ok(Some(message))
    }

    /// Return `true` if the space is missing operations from the passed groups.
    pub async fn space_repair_required(
        &self,
        space_id: SpaceId,
        groups: &[GroupId],
    ) -> Result<bool, ManagerError<F, C>> {
        let reader = self.read();

        let Some(space_y) = reader.space_state(&space_id).await? else {
            return Err(ManagerError::SpaceNotFound(space_id));
        };
        let global_groups_y = reader.global_groups_state().await?;

        let space_heads = space_y.groups_y.inner.heads(groups);
        let global_heads = global_groups_y.inner.heads(groups);

        Ok(space_heads != global_heads)
    }

    /// Publish a reference to any auth messages missing from a space.
    ///
    /// Each space holds a copy of the shared auth state by publishing a reference to each auth
    /// control message it witnesses. A space can get out-of-sync with this shared state if auth
    /// messages were published without the local peer knowing about a space, either because they
    /// are not a member or because they were yet to learn about it.
    ///
    /// ## Out-of-sync Space
    ///
    /// ```text
    /// Shared Auth State     Space State
    ///
    ///       [x]
    ///       [x] <-------------- [z]
    ///       [x] <-------------- [z]
    ///       [x] <-------------- [z]
    /// ```
    ///
    /// On identifying that a space needs "repairing" by calling space_repair_required(), _any_
    /// current space member can publish a message into the space referencing the missing auth
    /// message.
    ///
    /// It is recommended that repair does not occur after every call to process() as this would
    /// cause peers to publish redundant pointers into the spaces graph. Although these duplicates do not
    /// introduce any buggy or unexpected behavior, repairing after every processed message would
    /// introduce an undesirable level of redundancy.
    ///
    /// ## Redundant pointers
    ///
    /// ```text
    /// Shared Auth State     Space State
    ///
    ///       [x] <-----------[z1][z2][z3]
    ///       [x] <-------------- [z]
    ///       [x] <-------------- [z]
    ///       [x] <-------------- [z]
    /// ```
    ///
    /// A sensible approach to detecting and repairing spaces will involve processing messages in
    /// logical batches and only detecting and repairing any out-of-sync spaces after a batch has
    /// been processed. Alternatively some scheduling or throttling logic could be employed.
    ///
    /// Persists resulting state and returns forged messages.
    pub async fn repair_space(
        &self,
        space_id: SpaceId,
        groups: &[GroupId],
    ) -> Result<RepairOutput<C, F::Message>, ManagerError<F, C>> {
        let tx = self.begin_write().await?;

        // Read.

        let Some(space_y) = tx.space_state(&space_id).await? else {
            return Err(ManagerError::SpaceNotFound(space_id));
        };

        // Only members with Read or greater access can repair spaces.
        let can_repair = space_y
            .groups_y
            .members(space_y.group_id)
            .iter()
            .any(|(id, access)| *id == self.id() && *access >= Access::<C>::read());

        if !can_repair {
            tx.rollback().await?;
            return Ok(RepairOutput::from(vec![]));
        }

        let global_groups_y = tx.global_groups_state().await?;

        // Modify.

        let (space_y, output) = Space::apply_repair(&tx.ctx, &global_groups_y, space_y, groups)
            .await
            .map_err(ManagerError::Space)?;

        // Write.

        if let Some(space_y) = space_y {
            tx.commit(StateChanges::Space {
                global_groups_y: None,
                space_y,
            })
            .await?;
        } else {
            tx.rollback().await?;
        }

        self.waiters().notify_events(output.events());

        Ok(output)
    }

    /// Returns resulting space state (`None` if nothing changed) and events.
    #[allow(clippy::type_complexity)]
    async fn handle_space_membership_message(
        &self,
        tx: &StateTransaction<'_, S, F, C>,
        space_id: SpaceId,
        message: &SpaceMembershipMessage,
    ) -> Result<(Option<SpaceState<C>>, Vec<Event<C>>), ManagerError<F, C>> {
        // Get auth message.
        let auth_message = {
            let auth_message_id = message.auth_message_id;
            let Some(message) = tx
                .ctx
                .store
                .get_spaces_message_tx(&auth_message_id)
                .await
                .map_err(|err| StoreError::SpacesStore(err.to_string()))?
            else {
                return Err(ManagerError::MissingAuthMessage(
                    message.id,
                    auth_message_id,
                ));
            };

            match message.borrow() {
                SpacesArgs::Group { .. } => SpacesMessage::auth(&message),
                _ => {
                    return Err(ManagerError::IncorrectMessageVariant(auth_message_id));
                }
            }
        };

        let space_y = match tx.space_state(&space_id).await? {
            Some(space_y) => space_y,
            None => {
                if !auth_message.action().is_create() {
                    // If this is not a "create" message we should have learned about the space
                    // before. This can be either a faulty message or a problem with the message
                    // orderer.
                    return Err(ManagerError::UnexpectedMessage(message.id));
                }

                // Initialise a new space from the "create" message.
                SpaceState::init(
                    self.id(),
                    space_id,
                    message.group_id,
                    tx.key_manager_state().await?,
                    tx.key_registry_state().await?,
                )
            }
        };

        Space::handle_membership_message(&tx.ctx, space_y, message, &auth_message)
            .map_err(ManagerError::Space)
    }
}

#[cfg(any(test, feature = "test_utils"))]
impl<S, F, C> Manager<S, F, C>
where
    S: Clone
        + SpacesStore<SpacesStoreState<C>>
        + SpacesMessageStore<F::Message>
        + GroupsStore<AuthMessage<C>, C>
        + KeyRegistryStore
        + PreKeyBundlesStore
        + Transaction,
    F: Forge<C>,
    C: Conditions,
{
    /// Get the latest committed global auth state.
    pub async fn get_global_groups_state(
        &self,
    ) -> Result<crate::types::AuthGroupState<C>, ManagerError<F, C>> {
        Ok(self.read().global_groups_state().await?)
    }

    /// Get the latest committed space state.
    pub async fn get_space_state(
        &self,
        id: &SpaceId,
    ) -> Result<Option<SpaceState<C>>, ManagerError<F, C>> {
        Ok(self.read().space_state(id).await?)
    }

    /// Repair all given spaces, each in their own transaction.
    pub async fn repair_spaces(
        &self,
        space_ids: &[SpaceId],
    ) -> Result<Vec<RepairOutput<C, F::Message>>, ManagerError<F, C>> {
        let mut results = vec![];

        for id in space_ids {
            let Some(space_y) = self.get_space_state(id).await? else {
                continue;
            };

            let result = self.repair_space(*id, &[space_y.group_id]).await?;
            results.push(result);
        }

        Ok(results)
    }
}

/// Errors which can be returned from stores.
#[derive(Debug, Error)]
pub enum StoreError {
    #[error("spaces store error: {0}")]
    SpacesStore(String),

    #[error("groups store error: {0}")]
    GroupsStore(String),

    #[error("spaces message store error: {0}")]
    MessageStore(String),

    #[error("key registry store error: {0}")]
    KeyRegistryStore(String),

    #[error("key secret store error: {0}")]
    KeySecretStore(String),

    #[error("store transaction error: {0}")]
    Transaction(String),
}

/// Error occurred when creating or managing groups and spaces.
#[derive(Debug, Error)]
#[allow(clippy::large_enum_variant)]
pub enum ManagerError<F, C>
where
    F: Forge<C>,
    C: Conditions,
{
    #[error(transparent)]
    Space(#[from] SpaceError<F, C>),

    #[error(transparent)]
    Group(#[from] GroupError<F, C>),

    #[error(transparent)]
    Store(#[from] StoreError),

    #[error(transparent)]
    IdentityManager(#[from] IdentityError<F, C>),

    #[error("received unexpected message with id {0}, maybe it arrived out-of-order")]
    UnexpectedMessage(Hash),

    #[error(
        "received space message with id {0} before auth message {1}, maybe it arrived out-of-order"
    )]
    MissingAuthMessage(Hash, Hash),

    #[error("space not found {0}")]
    SpaceNotFound(SpaceId),

    #[error("unexpected message variant, expected auth {0}")]
    IncorrectMessageVariant(Hash),

    #[error(transparent)]
    Rng(#[from] RngError),

    #[error(transparent)]
    State(#[from] StateError),
}