12 releases (breaking)

0.10.0 Oct 5, 2023
0.9.1 Feb 15, 2023
0.8.0 Sep 12, 2022
0.7.0 Apr 28, 2022
0.2.0 Jun 21, 2021

#11 in #ruma

Download history 48/week @ 2023-12-15 56/week @ 2023-12-22 38/week @ 2023-12-29 43/week @ 2024-01-05 81/week @ 2024-01-12 54/week @ 2024-01-19 54/week @ 2024-01-26 14/week @ 2024-02-02 64/week @ 2024-02-09 81/week @ 2024-02-16 100/week @ 2024-02-23 193/week @ 2024-03-01 169/week @ 2024-03-08 160/week @ 2024-03-15 132/week @ 2024-03-22 148/week @ 2024-03-29

690 downloads per month
Used in 12 crates (via ruma)

MIT license

1.5MB
25K SLoC

Matrix State Resolution in Rust!

/// Abstraction of a PDU so users can have their own PDU types.
pub trait Event {
    /// The `EventId` of this event.
    fn event_id(&self) -> &EventId;
    /// The `RoomId` of this event.
    fn room_id(&self) -> &RoomId;
    /// The `UserId` of this event.
    fn sender(&self) -> &UserId;
    // and so on...
}

/// A mapping of event type and state_key to some value `T`, usually an `EventId`.
pub type StateMap<T> = BTreeMap<(StateEventType, Option<String>), T>;

/// A mapping of `EventId` to `T`, usually a `OriginalStateEvent`.
pub type EventMap<T> = BTreeMap<OwnedEventId, T>;

struct StateResolution {
    // For now the StateResolution struct is empty. If "caching" `event_map`
    // between `resolve` calls ends up being more efficient (probably not, as this would eat memory)
    // it may have an `event_map` field. The `event_map` is all the events
    // `StateResolution` has to know about to resolve state.
}

impl StateResolution {
    /// The point of this all, resolve the possibly conflicting sets of events.
    pub fn resolve<E: Event>(
        room_id: &RoomId,
        room_version: &RoomVersionId,
        state_sets: &[StateMap<OwnedEventId>],
        auth_events: Vec<Vec<OwnedEventId>>,
        event_map: &mut EventMap<Arc<E>>,
    ) -> Result<StateMap<OwnedEventId>> {;
}

The StateStore trait is an abstraction around what ever database your server (or maybe even client) uses to store Persistent Data Units.

We use rumas types when deserializing any PDU or it's contents which helps avoid a lot of type checking logic synapse must do while authenticating event chains.

Dependencies

~8–22MB
~311K SLoC