2018-09-03 10:13:17 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-09-03 10:13:17 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import heapq
|
|
|
|
import itertools
|
|
|
|
import logging
|
2020-08-24 14:25:27 -04:00
|
|
|
from typing import (
|
|
|
|
Any,
|
2022-06-09 04:48:04 -04:00
|
|
|
Awaitable,
|
2020-08-24 14:25:27 -04:00
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Generator,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Set,
|
|
|
|
Tuple,
|
|
|
|
overload,
|
|
|
|
)
|
|
|
|
|
2022-06-09 04:48:04 -04:00
|
|
|
from typing_extensions import Literal, Protocol
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
from synapse import event_auth
|
|
|
|
from synapse.api.constants import EventTypes
|
|
|
|
from synapse.api.errors import AuthError
|
2021-07-26 12:17:00 -04:00
|
|
|
from synapse.api.room_versions import RoomVersion
|
2019-12-13 07:55:32 -05:00
|
|
|
from synapse.events import EventBase
|
2023-09-13 07:57:19 -04:00
|
|
|
from synapse.types import MutableStateMap, StateMap, StrCollection
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-06-09 04:48:04 -04:00
|
|
|
class Clock(Protocol):
|
|
|
|
# This is usually synapse.util.Clock, but it's replaced with a FakeClock in tests.
|
|
|
|
# We only ever sleep(0) though, so that other async functions can make forward
|
|
|
|
# progress without waiting for stateres to complete.
|
|
|
|
def sleep(self, duration_ms: float) -> Awaitable[None]:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class StateResolutionStore(Protocol):
|
|
|
|
# This is usually synapse.state.StateResolutionStore, but it's replaced with a
|
|
|
|
# TestStateResolutionStore in tests.
|
|
|
|
def get_events(
|
2023-09-13 07:57:19 -04:00
|
|
|
self, event_ids: StrCollection, allow_rejected: bool = False
|
2022-06-09 04:48:04 -04:00
|
|
|
) -> Awaitable[Dict[str, EventBase]]:
|
|
|
|
...
|
|
|
|
|
|
|
|
def get_auth_chain_difference(
|
|
|
|
self, room_id: str, state_sets: List[Set[str]]
|
|
|
|
) -> Awaitable[Set[str]]:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We want to await to the reactor occasionally during state res when dealing
|
2020-06-24 13:48:18 -04:00
|
|
|
# with large data sets, so that we don't exhaust the reactor. This is done by
|
2020-07-24 10:59:51 -04:00
|
|
|
# awaiting to reactor during loops every N iterations.
|
|
|
|
_AWAIT_AFTER_ITERATIONS = 100
|
2020-06-24 13:48:18 -04:00
|
|
|
|
|
|
|
|
2022-06-09 04:48:04 -04:00
|
|
|
__all__ = [
|
|
|
|
"resolve_events_with_store",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def resolve_events_with_store(
|
2020-06-24 13:48:18 -04:00
|
|
|
clock: Clock,
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id: str,
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
2020-08-24 14:25:27 -04:00
|
|
|
state_sets: Sequence[StateMap[str]],
|
2019-12-13 07:55:32 -05:00
|
|
|
event_map: Optional[Dict[str, EventBase]],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> StateMap[str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Resolves the state using the v2 state resolution algorithm
|
|
|
|
|
|
|
|
Args:
|
2020-06-24 13:48:18 -04:00
|
|
|
clock
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id: the room we are working in
|
|
|
|
room_version: The room version
|
|
|
|
state_sets: List of dicts of (type, state_key) -> event_id,
|
2018-09-03 10:13:17 -04:00
|
|
|
which are the different state groups to resolve.
|
2019-12-13 07:55:32 -05:00
|
|
|
event_map:
|
2018-09-03 10:13:17 -04:00
|
|
|
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
|
2020-10-23 12:38:40 -04:00
|
|
|
used as a starting point for finding the state we need; any missing
|
2018-09-03 10:13:17 -04:00
|
|
|
events will be requested via state_res_store.
|
|
|
|
|
|
|
|
If None, all events will be fetched via state_res_store.
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
state_res_store:
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
A map from (type, state_key) to event_id.
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
logger.debug("Computing conflicted state")
|
|
|
|
|
2018-11-02 06:29:19 -04:00
|
|
|
# We use event_map as a cache, so if its None we need to initialize it
|
|
|
|
if event_map is None:
|
|
|
|
event_map = {}
|
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
# First split up the un/conflicted state
|
|
|
|
unconflicted_state, conflicted_state = _seperate(state_sets)
|
|
|
|
|
|
|
|
if not conflicted_state:
|
2019-07-23 09:00:55 -04:00
|
|
|
return unconflicted_state
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
logger.debug("%d conflicted state entries", len(conflicted_state))
|
|
|
|
logger.debug("Calculating auth chain difference")
|
|
|
|
|
|
|
|
# Also fetch all auth events that appear in only some of the state sets'
|
|
|
|
# auth chains.
|
2020-12-04 10:52:49 -05:00
|
|
|
auth_diff = await _get_auth_chain_difference(
|
|
|
|
room_id, state_sets, event_map, state_res_store
|
|
|
|
)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
full_conflicted_set = set(
|
|
|
|
itertools.chain(
|
2020-06-15 07:03:36 -04:00
|
|
|
itertools.chain.from_iterable(conflicted_state.values()), auth_diff
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
|
|
|
)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
events = await state_res_store.get_events(
|
2019-06-20 05:32:02 -04:00
|
|
|
[eid for eid in full_conflicted_set if eid not in event_map],
|
|
|
|
allow_rejected=True,
|
|
|
|
)
|
2018-09-03 10:13:17 -04:00
|
|
|
event_map.update(events)
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
# everything in the event map should be in the right room
|
|
|
|
for event in event_map.values():
|
|
|
|
if event.room_id != room_id:
|
|
|
|
raise Exception(
|
|
|
|
"Attempting to state-resolve for room %s with event %s which is in %s"
|
2021-02-16 17:32:34 -05:00
|
|
|
% (
|
|
|
|
room_id,
|
|
|
|
event.event_id,
|
|
|
|
event.room_id,
|
|
|
|
)
|
2019-12-13 07:55:32 -05:00
|
|
|
)
|
|
|
|
|
2020-02-21 07:15:07 -05:00
|
|
|
full_conflicted_set = {eid for eid in full_conflicted_set if eid in event_map}
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
logger.debug("%d full_conflicted_set entries", len(full_conflicted_set))
|
|
|
|
|
|
|
|
# Get and sort all the power events (kicks/bans/etc)
|
|
|
|
power_events = (
|
2019-06-20 05:32:02 -04:00
|
|
|
eid for eid in full_conflicted_set if _is_power_event(event_map[eid])
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
sorted_power_events = await _reverse_topological_power_sort(
|
2020-06-24 13:48:18 -04:00
|
|
|
clock, room_id, power_events, event_map, state_res_store, full_conflicted_set
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("sorted %d power events", len(sorted_power_events))
|
|
|
|
|
|
|
|
# Now sequentially auth each one
|
2020-07-24 10:59:51 -04:00
|
|
|
resolved_state = await _iterative_auth_checks(
|
2020-06-26 05:44:52 -04:00
|
|
|
clock,
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id,
|
2019-06-20 05:32:02 -04:00
|
|
|
room_version,
|
|
|
|
sorted_power_events,
|
|
|
|
unconflicted_state,
|
|
|
|
event_map,
|
2018-09-03 10:13:17 -04:00
|
|
|
state_res_store,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("resolved power events")
|
|
|
|
|
|
|
|
# OK, so we've now resolved the power events. Now sort the remaining
|
|
|
|
# events using the mainline of the resolved power level.
|
|
|
|
|
2020-06-19 08:56:35 -04:00
|
|
|
set_power_events = set(sorted_power_events)
|
2018-09-03 10:13:17 -04:00
|
|
|
leftover_events = [
|
2020-06-19 08:56:35 -04:00
|
|
|
ev_id for ev_id in full_conflicted_set if ev_id not in set_power_events
|
2018-09-03 10:13:17 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
logger.debug("sorting %d remaining events", len(leftover_events))
|
|
|
|
|
|
|
|
pl = resolved_state.get((EventTypes.PowerLevels, ""), None)
|
2020-07-24 10:59:51 -04:00
|
|
|
leftover_events = await _mainline_sort(
|
2020-06-24 13:48:18 -04:00
|
|
|
clock, room_id, leftover_events, pl, event_map, state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("resolving remaining events")
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
resolved_state = await _iterative_auth_checks(
|
2020-06-26 05:44:52 -04:00
|
|
|
clock,
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id,
|
|
|
|
room_version,
|
|
|
|
leftover_events,
|
|
|
|
resolved_state,
|
|
|
|
event_map,
|
|
|
|
state_res_store,
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("resolved")
|
|
|
|
|
|
|
|
# We make sure that unconflicted state always still applies.
|
|
|
|
resolved_state.update(unconflicted_state)
|
|
|
|
|
|
|
|
logger.debug("done")
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return resolved_state
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
async def _get_power_level_for_sender(
|
|
|
|
room_id: str,
|
|
|
|
event_id: str,
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> int:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Return the power level of the sender of the given event according to
|
|
|
|
their auth events.
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
room_id
|
|
|
|
event_id
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
The power level.
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
2020-07-24 10:59:51 -04:00
|
|
|
event = await _get_event(room_id, event_id, event_map, state_res_store)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
pl = None
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in event.auth_event_ids():
|
2020-07-24 10:59:51 -04:00
|
|
|
aev = await _get_event(
|
2019-12-31 05:41:44 -05:00
|
|
|
room_id, aid, event_map, state_res_store, allow_none=True
|
|
|
|
)
|
|
|
|
if aev and (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
|
2018-09-03 10:13:17 -04:00
|
|
|
pl = aev
|
|
|
|
break
|
|
|
|
|
|
|
|
if pl is None:
|
|
|
|
# Couldn't find power level. Check if they're the creator of the room
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in event.auth_event_ids():
|
2020-07-24 10:59:51 -04:00
|
|
|
aev = await _get_event(
|
2019-12-31 05:41:44 -05:00
|
|
|
room_id, aid, event_map, state_res_store, allow_none=True
|
|
|
|
)
|
|
|
|
if aev and (aev.type, aev.state_key) == (EventTypes.Create, ""):
|
2018-09-03 10:13:17 -04:00
|
|
|
if aev.content.get("creator") == event.sender:
|
2019-07-23 09:00:55 -04:00
|
|
|
return 100
|
2018-09-03 10:13:17 -04:00
|
|
|
break
|
2019-07-23 09:00:55 -04:00
|
|
|
return 0
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
level = pl.content.get("users", {}).get(event.sender)
|
|
|
|
if level is None:
|
|
|
|
level = pl.content.get("users_default", 0)
|
|
|
|
|
|
|
|
if level is None:
|
2019-07-23 09:00:55 -04:00
|
|
|
return 0
|
2018-09-03 10:13:17 -04:00
|
|
|
else:
|
2019-07-23 09:00:55 -04:00
|
|
|
return int(level)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
async def _get_auth_chain_difference(
|
2020-12-04 10:52:49 -05:00
|
|
|
room_id: str,
|
2023-09-28 07:01:46 -04:00
|
|
|
state_sets: Sequence[StateMap[str]],
|
2022-08-24 16:06:31 -04:00
|
|
|
unpersisted_events: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> Set[str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Compare the auth chains of each state set and return the set of events
|
2022-08-24 16:06:31 -04:00
|
|
|
that only appear in some, but not all of the auth chains.
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Args:
|
2022-08-24 16:06:31 -04:00
|
|
|
state_sets: The input state sets we are trying to resolve across.
|
|
|
|
unpersisted_events: A map from event ID to EventBase containing all unpersisted
|
|
|
|
events involved in this resolution.
|
|
|
|
state_res_store:
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2022-08-24 16:06:31 -04:00
|
|
|
The auth difference of the given state sets, as a set of event IDs.
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
2020-12-02 10:22:37 -05:00
|
|
|
# The `StateResolutionStore.get_auth_chain_difference` function assumes that
|
|
|
|
# all events passed to it (and their auth chains) have been persisted
|
2022-08-24 16:06:31 -04:00
|
|
|
# previously. We need to manually handle any other events that are yet to be
|
|
|
|
# persisted.
|
2020-12-02 10:22:37 -05:00
|
|
|
#
|
2022-08-24 16:06:31 -04:00
|
|
|
# We do this in three steps:
|
|
|
|
# 1. Compute the set of unpersisted events belonging to the auth difference.
|
|
|
|
# 2. Replacing any unpersisted events in the state_sets with their auth events,
|
|
|
|
# recursively, until the state_sets contain only persisted events.
|
|
|
|
# Then we call `store.get_auth_chain_difference` as normal, which computes
|
|
|
|
# the set of persisted events belonging to the auth difference.
|
|
|
|
# 3. Adding the results of 1 and 2 together.
|
|
|
|
|
|
|
|
# Map from event ID in `unpersisted_events` to their auth event IDs, and their auth
|
|
|
|
# event IDs if they appear in the `unpersisted_events`. This is the intersection of
|
|
|
|
# the event's auth chain with the events in `unpersisted_events` *plus* their
|
2020-12-02 10:22:37 -05:00
|
|
|
# auth event IDs.
|
2021-07-15 06:02:43 -04:00
|
|
|
events_to_auth_chain: Dict[str, Set[str]] = {}
|
2022-08-24 16:06:31 -04:00
|
|
|
for event in unpersisted_events.values():
|
2020-12-02 10:22:37 -05:00
|
|
|
chain = {event.event_id}
|
|
|
|
events_to_auth_chain[event.event_id] = chain
|
|
|
|
|
|
|
|
to_search = [event]
|
|
|
|
while to_search:
|
|
|
|
for auth_id in to_search.pop().auth_event_ids():
|
|
|
|
chain.add(auth_id)
|
2022-08-24 16:06:31 -04:00
|
|
|
auth_event = unpersisted_events.get(auth_id)
|
2020-12-02 10:22:37 -05:00
|
|
|
if auth_event:
|
|
|
|
to_search.append(auth_event)
|
|
|
|
|
2022-08-24 16:06:31 -04:00
|
|
|
# We now 1) calculate the auth chain difference for the unpersisted events
|
|
|
|
# and 2) work out the state sets to pass to the store.
|
2020-12-02 10:22:37 -05:00
|
|
|
#
|
2022-08-24 16:06:31 -04:00
|
|
|
# Note: If there are no `unpersisted_events` (which is the common case), we can do a
|
2020-12-02 10:22:37 -05:00
|
|
|
# much simpler calculation.
|
2022-08-24 16:06:31 -04:00
|
|
|
if unpersisted_events:
|
2020-12-02 10:22:37 -05:00
|
|
|
# The list of state sets to pass to the store, where each state set is a set
|
|
|
|
# of the event ids making up the state. This is similar to `state_sets`,
|
|
|
|
# except that (a) we only have event ids, not the complete
|
|
|
|
# ((type, state_key)->event_id) mappings; and (b) we have stripped out
|
|
|
|
# unpersisted events and replaced them with the persisted events in
|
|
|
|
# their auth chain.
|
2021-07-15 06:02:43 -04:00
|
|
|
state_sets_ids: List[Set[str]] = []
|
2020-12-02 10:22:37 -05:00
|
|
|
|
|
|
|
# For each state set, the unpersisted event IDs reachable (by their auth
|
|
|
|
# chain) from the events in that set.
|
2021-07-15 06:02:43 -04:00
|
|
|
unpersisted_set_ids: List[Set[str]] = []
|
2020-12-02 10:22:37 -05:00
|
|
|
|
|
|
|
for state_set in state_sets:
|
2021-07-15 06:02:43 -04:00
|
|
|
set_ids: Set[str] = set()
|
2020-12-02 10:22:37 -05:00
|
|
|
state_sets_ids.append(set_ids)
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
unpersisted_ids: Set[str] = set()
|
2020-12-02 10:22:37 -05:00
|
|
|
unpersisted_set_ids.append(unpersisted_ids)
|
|
|
|
|
|
|
|
for event_id in state_set.values():
|
|
|
|
event_chain = events_to_auth_chain.get(event_id)
|
|
|
|
if event_chain is not None:
|
2022-08-24 16:06:31 -04:00
|
|
|
# We have an unpersisted event. We add all the auth
|
|
|
|
# events that it references which are also unpersisted.
|
|
|
|
set_ids.update(
|
|
|
|
e for e in event_chain if e not in unpersisted_events
|
|
|
|
)
|
2020-12-02 10:22:37 -05:00
|
|
|
|
|
|
|
# We also add the full chain of unpersisted event IDs
|
|
|
|
# referenced by this state set, so that we can work out the
|
|
|
|
# auth chain difference of the unpersisted events.
|
2022-08-24 16:06:31 -04:00
|
|
|
unpersisted_ids.update(
|
|
|
|
e for e in event_chain if e in unpersisted_events
|
|
|
|
)
|
2020-12-02 10:22:37 -05:00
|
|
|
else:
|
|
|
|
set_ids.add(event_id)
|
|
|
|
|
|
|
|
# The auth chain difference of the unpersisted events of the state sets
|
|
|
|
# is calculated by taking the difference between the union and
|
|
|
|
# intersections.
|
|
|
|
union = unpersisted_set_ids[0].union(*unpersisted_set_ids[1:])
|
|
|
|
intersection = unpersisted_set_ids[0].intersection(*unpersisted_set_ids[1:])
|
|
|
|
|
2023-09-13 07:57:19 -04:00
|
|
|
auth_difference_unpersisted_part: StrCollection = union - intersection
|
2020-12-02 10:22:37 -05:00
|
|
|
else:
|
2022-08-24 16:06:31 -04:00
|
|
|
auth_difference_unpersisted_part = ()
|
2020-12-02 10:22:37 -05:00
|
|
|
state_sets_ids = [set(state_set.values()) for state_set in state_sets]
|
|
|
|
|
2020-12-04 10:52:49 -05:00
|
|
|
difference = await state_res_store.get_auth_chain_difference(
|
|
|
|
room_id, state_sets_ids
|
|
|
|
)
|
2022-08-24 16:06:31 -04:00
|
|
|
difference.update(auth_difference_unpersisted_part)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2020-03-18 12:46:41 -04:00
|
|
|
return difference
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _seperate(
|
|
|
|
state_sets: Iterable[StateMap[str]],
|
|
|
|
) -> Tuple[StateMap[str], StateMap[Set[str]]]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Return the unconflicted and conflicted state. This is different than in
|
|
|
|
the original algorithm, as this defines a key to be conflicted if one of
|
|
|
|
the state sets doesn't have that key.
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
state_sets
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
A tuple of unconflicted and conflicted state. The conflicted state dict
|
|
|
|
is a map from type/state_key to set of event IDs
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
unconflicted_state = {}
|
|
|
|
conflicted_state = {}
|
|
|
|
|
|
|
|
for key in set(itertools.chain.from_iterable(state_sets)):
|
2020-02-21 07:15:07 -05:00
|
|
|
event_ids = {state_set.get(key) for state_set in state_sets}
|
2018-09-03 10:13:17 -04:00
|
|
|
if len(event_ids) == 1:
|
|
|
|
unconflicted_state[key] = event_ids.pop()
|
|
|
|
else:
|
|
|
|
event_ids.discard(None)
|
|
|
|
conflicted_state[key] = event_ids
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
# mypy doesn't understand that discarding None above means that conflicted
|
|
|
|
# state is StateMap[Set[str]], not StateMap[Set[Optional[Str]]].
|
2023-09-28 07:01:46 -04:00
|
|
|
return unconflicted_state, conflicted_state # type: ignore[return-value]
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _is_power_event(event: EventBase) -> bool:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Return whether or not the event is a "power event", as defined by the
|
|
|
|
v2 state resolution algorithm
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
event
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
True if the event is a power event.
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
if (event.type, event.state_key) in (
|
|
|
|
(EventTypes.PowerLevels, ""),
|
|
|
|
(EventTypes.JoinRules, ""),
|
|
|
|
(EventTypes.Create, ""),
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if event.type == EventTypes.Member:
|
2019-06-20 05:32:02 -04:00
|
|
|
if event.membership in ("leave", "ban"):
|
2018-09-03 10:13:17 -04:00
|
|
|
return event.sender != event.state_key
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def _add_event_and_auth_chain_to_graph(
|
2020-08-24 14:25:27 -04:00
|
|
|
graph: Dict[str, Set[str]],
|
|
|
|
room_id: str,
|
|
|
|
event_id: str,
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2022-08-08 11:59:56 -04:00
|
|
|
full_conflicted_set: Set[str],
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> None:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Helper function for _reverse_topological_power_sort that add the event
|
|
|
|
and its auth chain (that is in the auth diff) to the graph
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
graph: A map from event ID to the events auth event IDs
|
|
|
|
room_id: the room we are working in
|
|
|
|
event_id: Event to add to the graph
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2022-08-08 11:59:56 -04:00
|
|
|
full_conflicted_set: Set of event IDs that are in the full conflicted set.
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
state = [event_id]
|
|
|
|
while state:
|
|
|
|
eid = state.pop()
|
|
|
|
graph.setdefault(eid, set())
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
event = await _get_event(room_id, eid, event_map, state_res_store)
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in event.auth_event_ids():
|
2022-08-08 11:59:56 -04:00
|
|
|
if aid in full_conflicted_set:
|
2018-09-03 10:13:17 -04:00
|
|
|
if aid not in graph:
|
|
|
|
state.append(aid)
|
|
|
|
|
|
|
|
graph.setdefault(eid, set()).add(aid)
|
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def _reverse_topological_power_sort(
|
2020-08-24 14:25:27 -04:00
|
|
|
clock: Clock,
|
|
|
|
room_id: str,
|
|
|
|
event_ids: Iterable[str],
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2022-08-08 11:59:56 -04:00
|
|
|
full_conflicted_set: Set[str],
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> List[str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Returns a list of the event_ids sorted by reverse topological ordering,
|
|
|
|
and then by power level and origin_server_ts
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
clock
|
|
|
|
room_id: the room we are working in
|
|
|
|
event_ids: The events to sort
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2022-08-08 11:59:56 -04:00
|
|
|
full_conflicted_set: Set of event IDs that are in the full conflicted set.
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
The sorted list
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
graph: Dict[str, Set[str]] = {}
|
2020-06-24 13:48:18 -04:00
|
|
|
for idx, event_id in enumerate(event_ids, start=1):
|
2020-07-24 10:59:51 -04:00
|
|
|
await _add_event_and_auth_chain_to_graph(
|
2022-08-08 11:59:56 -04:00
|
|
|
graph, room_id, event_id, event_map, state_res_store, full_conflicted_set
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We await occasionally when we're working with large data sets to
|
2020-06-24 13:48:18 -04:00
|
|
|
# ensure that we don't block the reactor loop for too long.
|
2020-07-24 10:59:51 -04:00
|
|
|
if idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
2020-06-24 13:48:18 -04:00
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
event_to_pl = {}
|
2020-06-24 13:48:18 -04:00
|
|
|
for idx, event_id in enumerate(graph, start=1):
|
2020-07-24 10:59:51 -04:00
|
|
|
pl = await _get_power_level_for_sender(
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id, event_id, event_map, state_res_store
|
|
|
|
)
|
2018-09-03 10:13:17 -04:00
|
|
|
event_to_pl[event_id] = pl
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We await occasionally when we're working with large data sets to
|
2020-06-24 13:48:18 -04:00
|
|
|
# ensure that we don't block the reactor loop for too long.
|
2020-07-24 10:59:51 -04:00
|
|
|
if idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
2020-06-24 13:48:18 -04:00
|
|
|
|
2021-10-06 13:55:25 -04:00
|
|
|
def _get_power_order(event_id: str) -> Tuple[int, int, str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
ev = event_map[event_id]
|
|
|
|
pl = event_to_pl[event_id]
|
|
|
|
|
|
|
|
return -pl, ev.origin_server_ts, event_id
|
|
|
|
|
|
|
|
# Note: graph is modified during the sort
|
2019-06-20 05:32:02 -04:00
|
|
|
it = lexicographical_topological_sort(graph, key=_get_power_order)
|
2018-09-03 10:13:17 -04:00
|
|
|
sorted_events = list(it)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return sorted_events
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def _iterative_auth_checks(
|
2020-08-24 14:25:27 -04:00
|
|
|
clock: Clock,
|
|
|
|
room_id: str,
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
2020-08-24 14:25:27 -04:00
|
|
|
event_ids: List[str],
|
|
|
|
base_state: StateMap[str],
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-28 07:28:53 -04:00
|
|
|
) -> MutableStateMap[str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Sequentially apply auth checks to each event in given list, updating the
|
|
|
|
state as it goes along.
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
clock
|
|
|
|
room_id
|
|
|
|
room_version
|
|
|
|
event_ids: Ordered list of events to apply auth checks to
|
|
|
|
base_state: The set of state to start with
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
Returns the final updated state
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
2020-08-28 07:28:53 -04:00
|
|
|
resolved_state = dict(base_state)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2020-06-26 05:44:52 -04:00
|
|
|
for idx, event_id in enumerate(event_ids, start=1):
|
2018-09-03 10:13:17 -04:00
|
|
|
event = event_map[event_id]
|
|
|
|
|
|
|
|
auth_events = {}
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in event.auth_event_ids():
|
2020-07-24 10:59:51 -04:00
|
|
|
ev = await _get_event(
|
2019-12-31 05:41:44 -05:00
|
|
|
room_id, aid, event_map, state_res_store, allow_none=True
|
|
|
|
)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2019-12-31 05:41:44 -05:00
|
|
|
if not ev:
|
|
|
|
logger.warning(
|
|
|
|
"auth_event id %s for event %s is missing", aid, event_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if ev.rejected_reason is None:
|
|
|
|
auth_events[(ev.type, ev.state_key)] = ev
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2021-07-26 12:17:00 -04:00
|
|
|
for key in event_auth.auth_types_for_event(room_version, event):
|
2018-09-03 10:13:17 -04:00
|
|
|
if key in resolved_state:
|
|
|
|
ev_id = resolved_state[key]
|
2020-07-24 10:59:51 -04:00
|
|
|
ev = await _get_event(room_id, ev_id, event_map, state_res_store)
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
if ev.rejected_reason is None:
|
|
|
|
auth_events[key] = event_map[ev_id]
|
|
|
|
|
2022-09-16 07:45:04 -04:00
|
|
|
if event.rejected_reason is not None:
|
|
|
|
# Do not admit previously rejected events into state.
|
|
|
|
# TODO: This isn't spec compliant. Events that were previously rejected due
|
|
|
|
# to failing auth checks at their state, but pass auth checks during
|
|
|
|
# state resolution should be accepted. Synapse does not handle the
|
|
|
|
# change of rejection status well, so we preserve the previous
|
|
|
|
# rejection status for now.
|
|
|
|
#
|
|
|
|
# Note that events rejected for non-state reasons, such as having the
|
|
|
|
# wrong auth events, should remain rejected.
|
|
|
|
#
|
|
|
|
# https://spec.matrix.org/v1.2/rooms/v9/#rejected-events
|
|
|
|
# https://github.com/matrix-org/synapse/issues/13797
|
|
|
|
continue
|
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
try:
|
2022-06-15 14:48:22 -04:00
|
|
|
event_auth.check_state_dependent_auth_rules(
|
2019-06-20 05:32:02 -04:00
|
|
|
event,
|
2021-10-18 13:28:30 -04:00
|
|
|
auth_events.values(),
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
resolved_state[(event.type, event.state_key)] = event_id
|
|
|
|
except AuthError:
|
|
|
|
pass
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We await occasionally when we're working with large data sets to
|
2020-06-26 05:44:52 -04:00
|
|
|
# ensure that we don't block the reactor loop for too long.
|
2020-07-24 10:59:51 -04:00
|
|
|
if idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
2020-06-26 05:44:52 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return resolved_state
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def _mainline_sort(
|
2020-08-24 14:25:27 -04:00
|
|
|
clock: Clock,
|
|
|
|
room_id: str,
|
|
|
|
event_ids: List[str],
|
|
|
|
resolved_power_event_id: Optional[str],
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> List[str]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Returns a sorted list of event_ids sorted by mainline ordering based on
|
|
|
|
the given event resolved_power_event_id
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
clock
|
|
|
|
room_id: room we're working in
|
|
|
|
event_ids: Events to sort
|
|
|
|
resolved_power_event_id: The final resolved power level event ID
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
The sorted list
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
2020-06-24 13:48:18 -04:00
|
|
|
if not event_ids:
|
|
|
|
# It's possible for there to be no event IDs here to sort, so we can
|
|
|
|
# skip calculating the mainline in that case.
|
|
|
|
return []
|
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
mainline = []
|
|
|
|
pl = resolved_power_event_id
|
2020-06-24 13:48:18 -04:00
|
|
|
idx = 0
|
2018-09-03 10:13:17 -04:00
|
|
|
while pl:
|
|
|
|
mainline.append(pl)
|
2020-07-24 10:59:51 -04:00
|
|
|
pl_ev = await _get_event(room_id, pl, event_map, state_res_store)
|
2018-11-05 08:35:15 -05:00
|
|
|
auth_events = pl_ev.auth_event_ids()
|
2018-09-03 10:13:17 -04:00
|
|
|
pl = None
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in auth_events:
|
2020-07-24 10:59:51 -04:00
|
|
|
ev = await _get_event(
|
2019-12-31 05:41:44 -05:00
|
|
|
room_id, aid, event_map, state_res_store, allow_none=True
|
|
|
|
)
|
|
|
|
if ev and (ev.type, ev.state_key) == (EventTypes.PowerLevels, ""):
|
2018-09-03 10:13:17 -04:00
|
|
|
pl = aid
|
|
|
|
break
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We await occasionally when we're working with large data sets to
|
2020-06-24 13:48:18 -04:00
|
|
|
# ensure that we don't block the reactor loop for too long.
|
2020-07-24 10:59:51 -04:00
|
|
|
if idx != 0 and idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
2020-06-24 13:48:18 -04:00
|
|
|
|
|
|
|
idx += 1
|
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
mainline_map = {ev_id: i + 1 for i, ev_id in enumerate(reversed(mainline))}
|
|
|
|
|
|
|
|
event_ids = list(event_ids)
|
|
|
|
|
|
|
|
order_map = {}
|
2020-06-24 13:48:18 -04:00
|
|
|
for idx, ev_id in enumerate(event_ids, start=1):
|
2020-07-24 10:59:51 -04:00
|
|
|
depth = await _get_mainline_depth_for_event(
|
2023-07-19 13:00:33 -04:00
|
|
|
clock, event_map[ev_id], mainline_map, event_map, state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
)
|
|
|
|
order_map[ev_id] = (depth, event_map[ev_id].origin_server_ts, ev_id)
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
# We await occasionally when we're working with large data sets to
|
2020-06-24 13:48:18 -04:00
|
|
|
# ensure that we don't block the reactor loop for too long.
|
2020-07-24 10:59:51 -04:00
|
|
|
if idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
2020-06-24 13:48:18 -04:00
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
event_ids.sort(key=lambda ev_id: order_map[ev_id])
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return event_ids
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def _get_mainline_depth_for_event(
|
2023-07-19 13:00:33 -04:00
|
|
|
clock: Clock,
|
2020-08-24 14:25:27 -04:00
|
|
|
event: EventBase,
|
|
|
|
mainline_map: Dict[str, int],
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> int:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Get the mainline depths for the given event based on the mainline map
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
event
|
|
|
|
mainline_map: Map from event_id to mainline depth for events in the mainline.
|
|
|
|
event_map
|
|
|
|
state_res_store
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
The mainline depth
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id = event.room_id
|
2021-07-15 06:02:43 -04:00
|
|
|
tmp_event: Optional[EventBase] = event
|
2019-12-13 07:55:32 -05:00
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
# We do an iterative search, replacing `event with the power level in its
|
|
|
|
# auth events (if any)
|
2023-07-19 13:00:33 -04:00
|
|
|
idx = 0
|
2020-08-24 14:25:27 -04:00
|
|
|
while tmp_event:
|
2020-12-18 10:00:34 -05:00
|
|
|
depth = mainline_map.get(tmp_event.event_id)
|
2018-09-03 10:13:17 -04:00
|
|
|
if depth is not None:
|
2019-07-23 09:00:55 -04:00
|
|
|
return depth
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
auth_events = tmp_event.auth_event_ids()
|
|
|
|
tmp_event = None
|
2018-09-03 10:13:17 -04:00
|
|
|
|
2018-11-05 08:35:15 -05:00
|
|
|
for aid in auth_events:
|
2020-07-24 10:59:51 -04:00
|
|
|
aev = await _get_event(
|
2019-12-31 05:41:44 -05:00
|
|
|
room_id, aid, event_map, state_res_store, allow_none=True
|
|
|
|
)
|
|
|
|
if aev and (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
|
2020-08-24 14:25:27 -04:00
|
|
|
tmp_event = aev
|
2018-09-03 10:13:17 -04:00
|
|
|
break
|
|
|
|
|
2023-07-19 13:00:33 -04:00
|
|
|
idx += 1
|
|
|
|
|
|
|
|
if idx % _AWAIT_AFTER_ITERATIONS == 0:
|
|
|
|
await clock.sleep(0)
|
|
|
|
|
2018-09-03 10:13:17 -04:00
|
|
|
# Didn't find a power level auth event, so we just return 0
|
2019-07-23 09:00:55 -04:00
|
|
|
return 0
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
@overload
|
|
|
|
async def _get_event(
|
|
|
|
room_id: str,
|
|
|
|
event_id: str,
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
allow_none: Literal[False] = False,
|
|
|
|
) -> EventBase:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
async def _get_event(
|
|
|
|
room_id: str,
|
|
|
|
event_id: str,
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
allow_none: Literal[True],
|
|
|
|
) -> Optional[EventBase]:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
async def _get_event(
|
|
|
|
room_id: str,
|
|
|
|
event_id: str,
|
|
|
|
event_map: Dict[str, EventBase],
|
2022-06-09 04:48:04 -04:00
|
|
|
state_res_store: StateResolutionStore,
|
2020-08-24 14:25:27 -04:00
|
|
|
allow_none: bool = False,
|
|
|
|
) -> Optional[EventBase]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Helper function to look up event in event_map, falling back to looking
|
|
|
|
it up in the store
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
room_id
|
|
|
|
event_id
|
|
|
|
event_map
|
|
|
|
state_res_store
|
|
|
|
allow_none: if the event is not found, return None rather than raising
|
2019-12-31 05:41:44 -05:00
|
|
|
an exception
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
The event, or none if the event does not exist (and allow_none is True).
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
if event_id not in event_map:
|
2020-07-24 10:59:51 -04:00
|
|
|
events = await state_res_store.get_events([event_id], allow_rejected=True)
|
2018-09-03 10:13:17 -04:00
|
|
|
event_map.update(events)
|
2019-12-31 05:41:44 -05:00
|
|
|
event = event_map.get(event_id)
|
|
|
|
|
|
|
|
if event is None:
|
|
|
|
if allow_none:
|
|
|
|
return None
|
|
|
|
raise Exception("Unknown event %s" % (event_id,))
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
if event.room_id != room_id:
|
|
|
|
raise Exception(
|
|
|
|
"In state res for room %s, event %s is in %s"
|
|
|
|
% (room_id, event_id, event.room_id)
|
|
|
|
)
|
|
|
|
return event
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def lexicographical_topological_sort(
|
|
|
|
graph: Dict[str, Set[str]], key: Callable[[str], Any]
|
|
|
|
) -> Generator[str, None, None]:
|
2018-09-03 10:13:17 -04:00
|
|
|
"""Performs a lexicographic reverse topological sort on the graph.
|
|
|
|
|
|
|
|
This returns a reverse topological sort (i.e. if node A references B then B
|
|
|
|
appears before A in the sort), with ties broken lexicographically based on
|
|
|
|
return value of the `key` function.
|
|
|
|
|
|
|
|
NOTE: `graph` is modified during the sort.
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
graph: A representation of the graph where each node is a key in the
|
|
|
|
dict and its value are the nodes edges.
|
|
|
|
key: A function that takes a node and returns a value that is comparable
|
|
|
|
and used to order nodes
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
Yields:
|
2020-08-24 14:25:27 -04:00
|
|
|
The next node in the topological sort
|
2018-09-03 10:13:17 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Note, this is basically Kahn's algorithm except we look at nodes with no
|
|
|
|
# outgoing edges, c.f.
|
|
|
|
# https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
|
|
|
|
outdegree_map = graph
|
2021-07-15 06:02:43 -04:00
|
|
|
reverse_graph: Dict[str, Set[str]] = {}
|
2018-09-03 10:13:17 -04:00
|
|
|
|
|
|
|
# Lists of nodes with zero out degree. Is actually a tuple of
|
|
|
|
# `(key(node), node)` so that sorting does the right thing
|
|
|
|
zero_outdegree = []
|
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for node, edges in graph.items():
|
2018-09-03 10:13:17 -04:00
|
|
|
if len(edges) == 0:
|
|
|
|
zero_outdegree.append((key(node), node))
|
|
|
|
|
|
|
|
reverse_graph.setdefault(node, set())
|
|
|
|
for edge in edges:
|
|
|
|
reverse_graph.setdefault(edge, set()).add(node)
|
|
|
|
|
|
|
|
# heapq is a built in implementation of a sorted queue.
|
|
|
|
heapq.heapify(zero_outdegree)
|
|
|
|
|
|
|
|
while zero_outdegree:
|
|
|
|
_, node = heapq.heappop(zero_outdegree)
|
|
|
|
|
|
|
|
for parent in reverse_graph[node]:
|
|
|
|
out = outdegree_map[parent]
|
|
|
|
out.discard(node)
|
|
|
|
if len(out) == 0:
|
|
|
|
heapq.heappush(zero_outdegree, (key(parent), parent))
|
|
|
|
|
|
|
|
yield node
|