synapse.state module

class synapse.state.KeyStateTuple(context, type, state_key)

Bases: tuple

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

static __new__(_cls, context, type, state_key)

Create new instance of KeyStateTuple(context, type, state_key)

__repr__()

Return a nicely formatted representation string

context

Alias for field number 0

state_key

Alias for field number 2

type

Alias for field number 1

class synapse.state.StateHandler(hs)

Bases: object

Fetches bits of state from the stores, and does state resolution where necessary

compute_event_context(event, old_state=None)

Build an EventContext structure for the event.

This works out what the current state should be for the event, and generates a new state group if necessary.

Parameters:
  • event (synapse.events.EventBase) –
  • old_state (dict|None) – The state at the event if it can’t be calculated from existing events. This is normally only specified when receiving an event from federation where we don’t have the prev events for, e.g. when backfilling.
Returns:

Return type:

synapse.events.snapshot.EventContext

get_current_hosts_in_room(room_id, latest_event_ids=None)
get_current_state(room_id, event_type=None, state_key='', latest_event_ids=None)

Retrieves the current state for the room. This is done by calling get_latest_events_in_room to get the leading edges of the event graph and then resolving any of the state conflicts.

This is equivalent to getting the state of an event that were to send next before receiving any new events.

If event_type is specified, then the method returns only the one event (or None) with that event_type and state_key.

Returns:map from (type, state_key) to event
get_current_state_ids(room_id, latest_event_ids=None)

Get the current state, or the state at a set of events, for a room

Parameters:
  • room_id (str) –
  • latest_event_ids (iterable[str]|None) – if given, the forward extremities to resolve. If None, we look them up from the database (via a cache)
Returns:

the state dict, mapping from

(event_type, state_key) -> event_id

Return type:

Deferred[dict[(str, str), str)]]

get_current_user_in_room(room_id, latest_event_ids=None)
resolve_events(room_version, state_sets, event)
resolve_state_groups_for_events(room_id, event_ids)

Given a list of event_ids this method fetches the state at each event, resolves conflicts between them and returns them.

Parameters:
  • room_id (str) –
  • event_ids (list[str]) –
  • explicit_room_version (str|None) – If set uses the the given room version to choose the resolution algorithm. If None, then checks the database for room version.
Returns:

resolved state

Return type:

Deferred[_StateCacheEntry]

class synapse.state.StateResolutionHandler(hs)

Bases: object

Responsible for doing state conflict resolution.

Note that the storage layer depends on this handler, so all functions must be storage-independent.

resolve_state_groups(room_id, room_version, state_groups_ids, event_map, state_res_store)

Resolves conflicts between a set of state groups

Always generates a new state group (unless we hit the cache), so should not be called for a single state group

Parameters:
  • room_id (str) – room we are resolving for (used for logging)
  • room_version (str) – version of the room
  • state_groups_ids (dict[int, dict[(str, str), str]]) – map from state group id to the state in that state group (where ‘state’ is a map from state key to event id)
  • event_map (dict[str,FrozenEvent]|None) –

    a dict from event_id to event, for any events that we happen to have in flight (eg, those currently being persisted). This will be used as a starting point fof finding the state we need; any missing events will be requested via state_res_store.

    If None, all events will be fetched via state_res_store.

  • state_res_store (StateResolutionStore) –
Returns:

resolved state

Return type:

Deferred[_StateCacheEntry]

class synapse.state.StateResolutionStore(store)

Bases: object

Interface that allows state resolution algorithms to access the database in well defined way.

Parameters:store (DataStore) –
get_auth_chain(event_ids)

Gets the full auth chain for a set of events (including rejected events).

Includes the given event IDs in the result.

Note that:
  1. All events must be state events.
  2. For v1 rooms this may not have the full auth chain in the presence of rejected events
Parameters:event_ids (list) – The event IDs of the events to fetch the auth chain for. Must be state events.
Returns:List of event IDs of the auth chain.
Return type:Deferred[list[str]]
get_events(event_ids, allow_rejected=False)

Get events from the database

Parameters:
  • event_ids (list) – The event_ids of the events to fetch
  • allow_rejected (bool) – If True return rejected events.
Returns:

Dict from event_id to event.

Return type:

Deferred[dict[str, FrozenEvent]]

synapse.state.resolve_events_with_store(room_version, state_sets, event_map, state_res_store)
Parameters:
  • room_version (str) – Version of the room
  • state_sets (list) – List of dicts of (type, state_key) -> event_id, which are the different state groups to resolve.
  • event_map (dict[str,FrozenEvent]|None) –

    a dict from event_id to event, for any events that we happen to have in flight (eg, those currently being persisted). This will be used as a starting point fof finding the state we need; any missing events will be requested via state_map_factory.

    If None, all events will be fetched via state_map_factory.

  • state_res_store (StateResolutionStore) –
Returns
Deferred[dict[(str, str), str]]:
a map from (type, state_key) to event_id.