.. Copyright 2018-2019 New Vector Ltd .. .. Licensed under the Apache License, Version 2.0 (the "License"); .. you may not use this file except in compliance with the License. .. You may obtain a copy of the License at .. .. http://www.apache.org/licenses/LICENSE-2.0 .. .. Unless required by applicable law or agreed to in writing, software .. distributed under the License is distributed on an "AS IS" BASIS, .. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. .. See the License for the specific language governing permissions and .. limitations under the License. Room Version 2 ============== This room version builds off of `version 1 `_ with an improved state resolution algorithm. .. contents:: Table of Contents .. sectnum:: Server implementation components -------------------------------- .. WARNING:: The information contained in this section is strictly for server implementors. Applications which use the Client-Server API are generally unaffected by the details contained here, and can safely ignore their presence. Room version 2 uses the base components of `room version 1 `_, changing only the state resolution algorithm. State resolution ~~~~~~~~~~~~~~~~ The room state :math:`S'(E)` after an event :math:`E` is defined in terms of the room state :math:`S(E)` before :math:`E`, and depends on whether :math:`E` is a state event or a message event: * If :math:`E` is a message event, then :math:`S'(E) = S(E)`. * If :math:`E` is a state event, then :math:`S'(E)` is :math:`S(E)`, except that its entry corresponding to :math:`E`'s ``event_type`` and ``state_key`` is replaced by :math:`E`'s ``event_id``. The room state :math:`S(E)` before :math:`E` is the *resolution* of the set of states :math:`\{ S'(E_1), S'(E_2), … \}` consisting of the states after each of :math:`E`'s ``prev_event``\s :math:`\{ E_1, E_2, … \}`, where the resolution of a set of states is given in the algorithm below. Definitions +++++++++++ The state resolution algorithm for version 2 rooms uses the following definitions, given the set of room states :math:`\{ S_1, S_2, \ldots \}`: Power events A *power event* is a state event with type ``m.room.power_levels`` or ``m.room.join_rules``, or a state event with type ``m.room.member`` where the ``membership`` is ``leave`` or ``ban`` and the ``sender`` does not match the ``state_key``. The idea behind this is that power events are events that might remove someone's ability to do something in the room. Unconflicted state map and conflicted state set The *unconflicted state map* is the state where the value of each key exists and is the same in each state :math:`S_i`. The *conflicted state set* is the set of all other state events. Note that the unconflicted state map only has one event per ``(event_type, state_key)``, whereas the conflicted state set may have multiple events. Auth difference The *auth difference* is calculated by first calculating the full auth chain for each state :math:`S_i`, that is the union of the auth chains for each event in :math:`S_i`, and then taking every event that doesn't appear in every auth chain. If :math:`C_i` is the full auth chain of :math:`S_i`, then the auth difference is :math:`\cup C_i - \cap C_i`. Full conflicted set The *full conflicted set* is the union of the conflicted state set and the auth difference. Reverse topological power ordering The *reverse topological power ordering* of a set of events is the lexicographically smallest topological ordering based on the DAG formed by auth events. The reverse topological power ordering is ordered from earliest event to latest. For comparing two topological orderings to determine which is the lexicographically smallest, the following comparison relation on events is used: for events :math:`x` and :math:`y`, :math:`x