Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

846 linhas
27 KiB

  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import heapq
  15. import itertools
  16. import logging
  17. from typing import (
  18. Any,
  19. Awaitable,
  20. Callable,
  21. Dict,
  22. Generator,
  23. Iterable,
  24. List,
  25. Mapping,
  26. Optional,
  27. Sequence,
  28. Set,
  29. Tuple,
  30. overload,
  31. )
  32. from typing_extensions import Literal, Protocol
  33. from synapse import event_auth
  34. from synapse.api.constants import EventTypes
  35. from synapse.api.errors import AuthError
  36. from synapse.api.room_versions import RoomVersion
  37. from synapse.events import EventBase
  38. from synapse.types import MutableStateMap, StateMap, StrCollection
  39. logger = logging.getLogger(__name__)
  40. class Clock(Protocol):
  41. # This is usually synapse.util.Clock, but it's replaced with a FakeClock in tests.
  42. # We only ever sleep(0) though, so that other async functions can make forward
  43. # progress without waiting for stateres to complete.
  44. def sleep(self, duration_ms: float) -> Awaitable[None]:
  45. ...
  46. class StateResolutionStore(Protocol):
  47. # This is usually synapse.state.StateResolutionStore, but it's replaced with a
  48. # TestStateResolutionStore in tests.
  49. def get_events(
  50. self, event_ids: StrCollection, allow_rejected: bool = False
  51. ) -> Awaitable[Dict[str, EventBase]]:
  52. ...
  53. def get_auth_chain_difference(
  54. self, room_id: str, state_sets: List[Set[str]]
  55. ) -> Awaitable[Set[str]]:
  56. ...
  57. # We want to await to the reactor occasionally during state res when dealing
  58. # with large data sets, so that we don't exhaust the reactor. This is done by
  59. # awaiting to reactor during loops every N iterations.
  60. _AWAIT_AFTER_ITERATIONS = 100
  61. __all__ = [
  62. "resolve_events_with_store",
  63. ]
  64. async def resolve_events_with_store(
  65. clock: Clock,
  66. room_id: str,
  67. room_version: RoomVersion,
  68. state_sets: Sequence[StateMap[str]],
  69. event_map: Optional[Dict[str, EventBase]],
  70. state_res_store: StateResolutionStore,
  71. ) -> StateMap[str]:
  72. """Resolves the state using the v2 state resolution algorithm
  73. Args:
  74. clock
  75. room_id: the room we are working in
  76. room_version: The room version
  77. state_sets: List of dicts of (type, state_key) -> event_id,
  78. which are the different state groups to resolve.
  79. event_map:
  80. a dict from event_id to event, for any events that we happen to
  81. have in flight (eg, those currently being persisted). This will be
  82. used as a starting point for finding the state we need; any missing
  83. events will be requested via state_res_store.
  84. If None, all events will be fetched via state_res_store.
  85. state_res_store:
  86. Returns:
  87. A map from (type, state_key) to event_id.
  88. """
  89. logger.debug("Computing conflicted state")
  90. # We use event_map as a cache, so if its None we need to initialize it
  91. if event_map is None:
  92. event_map = {}
  93. # First split up the un/conflicted state
  94. unconflicted_state, conflicted_state = _seperate(state_sets)
  95. if not conflicted_state:
  96. return unconflicted_state
  97. logger.debug("%d conflicted state entries", len(conflicted_state))
  98. logger.debug("Calculating auth chain difference")
  99. # Also fetch all auth events that appear in only some of the state sets'
  100. # auth chains.
  101. auth_diff = await _get_auth_chain_difference(
  102. room_id, state_sets, event_map, state_res_store
  103. )
  104. full_conflicted_set = set(
  105. itertools.chain(
  106. itertools.chain.from_iterable(conflicted_state.values()), auth_diff
  107. )
  108. )
  109. events = await state_res_store.get_events(
  110. [eid for eid in full_conflicted_set if eid not in event_map],
  111. allow_rejected=True,
  112. )
  113. event_map.update(events)
  114. # everything in the event map should be in the right room
  115. for event in event_map.values():
  116. if event.room_id != room_id:
  117. raise Exception(
  118. "Attempting to state-resolve for room %s with event %s which is in %s"
  119. % (
  120. room_id,
  121. event.event_id,
  122. event.room_id,
  123. )
  124. )
  125. full_conflicted_set = {eid for eid in full_conflicted_set if eid in event_map}
  126. logger.debug("%d full_conflicted_set entries", len(full_conflicted_set))
  127. # Get and sort all the power events (kicks/bans/etc)
  128. power_events = (
  129. eid for eid in full_conflicted_set if _is_power_event(event_map[eid])
  130. )
  131. sorted_power_events = await _reverse_topological_power_sort(
  132. clock, room_id, power_events, event_map, state_res_store, full_conflicted_set
  133. )
  134. logger.debug("sorted %d power events", len(sorted_power_events))
  135. # Now sequentially auth each one
  136. resolved_state = await _iterative_auth_checks(
  137. clock,
  138. room_id,
  139. room_version,
  140. sorted_power_events,
  141. unconflicted_state,
  142. event_map,
  143. state_res_store,
  144. )
  145. logger.debug("resolved power events")
  146. # OK, so we've now resolved the power events. Now sort the remaining
  147. # events using the mainline of the resolved power level.
  148. set_power_events = set(sorted_power_events)
  149. leftover_events = [
  150. ev_id for ev_id in full_conflicted_set if ev_id not in set_power_events
  151. ]
  152. logger.debug("sorting %d remaining events", len(leftover_events))
  153. pl = resolved_state.get((EventTypes.PowerLevels, ""), None)
  154. leftover_events = await _mainline_sort(
  155. clock, room_id, leftover_events, pl, event_map, state_res_store
  156. )
  157. logger.debug("resolving remaining events")
  158. resolved_state = await _iterative_auth_checks(
  159. clock,
  160. room_id,
  161. room_version,
  162. leftover_events,
  163. resolved_state,
  164. event_map,
  165. state_res_store,
  166. )
  167. logger.debug("resolved")
  168. # We make sure that unconflicted state always still applies.
  169. resolved_state.update(unconflicted_state)
  170. logger.debug("done")
  171. return resolved_state
  172. async def _get_power_level_for_sender(
  173. room_id: str,
  174. event_id: str,
  175. event_map: Dict[str, EventBase],
  176. state_res_store: StateResolutionStore,
  177. ) -> int:
  178. """Return the power level of the sender of the given event according to
  179. their auth events.
  180. Args:
  181. room_id
  182. event_id
  183. event_map
  184. state_res_store
  185. Returns:
  186. The power level.
  187. """
  188. event = await _get_event(room_id, event_id, event_map, state_res_store)
  189. pl = None
  190. for aid in event.auth_event_ids():
  191. aev = await _get_event(
  192. room_id, aid, event_map, state_res_store, allow_none=True
  193. )
  194. if aev and (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
  195. pl = aev
  196. break
  197. if pl is None:
  198. # Couldn't find power level. Check if they're the creator of the room
  199. for aid in event.auth_event_ids():
  200. aev = await _get_event(
  201. room_id, aid, event_map, state_res_store, allow_none=True
  202. )
  203. if aev and (aev.type, aev.state_key) == (EventTypes.Create, ""):
  204. if aev.content.get("creator") == event.sender:
  205. return 100
  206. break
  207. return 0
  208. level = pl.content.get("users", {}).get(event.sender)
  209. if level is None:
  210. level = pl.content.get("users_default", 0)
  211. if level is None:
  212. return 0
  213. else:
  214. return int(level)
  215. async def _get_auth_chain_difference(
  216. room_id: str,
  217. state_sets: Sequence[Mapping[Any, str]],
  218. unpersisted_events: Dict[str, EventBase],
  219. state_res_store: StateResolutionStore,
  220. ) -> Set[str]:
  221. """Compare the auth chains of each state set and return the set of events
  222. that only appear in some, but not all of the auth chains.
  223. Args:
  224. state_sets: The input state sets we are trying to resolve across.
  225. unpersisted_events: A map from event ID to EventBase containing all unpersisted
  226. events involved in this resolution.
  227. state_res_store:
  228. Returns:
  229. The auth difference of the given state sets, as a set of event IDs.
  230. """
  231. # The `StateResolutionStore.get_auth_chain_difference` function assumes that
  232. # all events passed to it (and their auth chains) have been persisted
  233. # previously. We need to manually handle any other events that are yet to be
  234. # persisted.
  235. #
  236. # We do this in three steps:
  237. # 1. Compute the set of unpersisted events belonging to the auth difference.
  238. # 2. Replacing any unpersisted events in the state_sets with their auth events,
  239. # recursively, until the state_sets contain only persisted events.
  240. # Then we call `store.get_auth_chain_difference` as normal, which computes
  241. # the set of persisted events belonging to the auth difference.
  242. # 3. Adding the results of 1 and 2 together.
  243. # Map from event ID in `unpersisted_events` to their auth event IDs, and their auth
  244. # event IDs if they appear in the `unpersisted_events`. This is the intersection of
  245. # the event's auth chain with the events in `unpersisted_events` *plus* their
  246. # auth event IDs.
  247. events_to_auth_chain: Dict[str, Set[str]] = {}
  248. for event in unpersisted_events.values():
  249. chain = {event.event_id}
  250. events_to_auth_chain[event.event_id] = chain
  251. to_search = [event]
  252. while to_search:
  253. for auth_id in to_search.pop().auth_event_ids():
  254. chain.add(auth_id)
  255. auth_event = unpersisted_events.get(auth_id)
  256. if auth_event:
  257. to_search.append(auth_event)
  258. # We now 1) calculate the auth chain difference for the unpersisted events
  259. # and 2) work out the state sets to pass to the store.
  260. #
  261. # Note: If there are no `unpersisted_events` (which is the common case), we can do a
  262. # much simpler calculation.
  263. if unpersisted_events:
  264. # The list of state sets to pass to the store, where each state set is a set
  265. # of the event ids making up the state. This is similar to `state_sets`,
  266. # except that (a) we only have event ids, not the complete
  267. # ((type, state_key)->event_id) mappings; and (b) we have stripped out
  268. # unpersisted events and replaced them with the persisted events in
  269. # their auth chain.
  270. state_sets_ids: List[Set[str]] = []
  271. # For each state set, the unpersisted event IDs reachable (by their auth
  272. # chain) from the events in that set.
  273. unpersisted_set_ids: List[Set[str]] = []
  274. for state_set in state_sets:
  275. set_ids: Set[str] = set()
  276. state_sets_ids.append(set_ids)
  277. unpersisted_ids: Set[str] = set()
  278. unpersisted_set_ids.append(unpersisted_ids)
  279. for event_id in state_set.values():
  280. event_chain = events_to_auth_chain.get(event_id)
  281. if event_chain is not None:
  282. # We have an unpersisted event. We add all the auth
  283. # events that it references which are also unpersisted.
  284. set_ids.update(
  285. e for e in event_chain if e not in unpersisted_events
  286. )
  287. # We also add the full chain of unpersisted event IDs
  288. # referenced by this state set, so that we can work out the
  289. # auth chain difference of the unpersisted events.
  290. unpersisted_ids.update(
  291. e for e in event_chain if e in unpersisted_events
  292. )
  293. else:
  294. set_ids.add(event_id)
  295. # The auth chain difference of the unpersisted events of the state sets
  296. # is calculated by taking the difference between the union and
  297. # intersections.
  298. union = unpersisted_set_ids[0].union(*unpersisted_set_ids[1:])
  299. intersection = unpersisted_set_ids[0].intersection(*unpersisted_set_ids[1:])
  300. auth_difference_unpersisted_part: StrCollection = union - intersection
  301. else:
  302. auth_difference_unpersisted_part = ()
  303. state_sets_ids = [set(state_set.values()) for state_set in state_sets]
  304. difference = await state_res_store.get_auth_chain_difference(
  305. room_id, state_sets_ids
  306. )
  307. difference.update(auth_difference_unpersisted_part)
  308. return difference
  309. def _seperate(
  310. state_sets: Iterable[StateMap[str]],
  311. ) -> Tuple[StateMap[str], StateMap[Set[str]]]:
  312. """Return the unconflicted and conflicted state. This is different than in
  313. the original algorithm, as this defines a key to be conflicted if one of
  314. the state sets doesn't have that key.
  315. Args:
  316. state_sets
  317. Returns:
  318. A tuple of unconflicted and conflicted state. The conflicted state dict
  319. is a map from type/state_key to set of event IDs
  320. """
  321. unconflicted_state = {}
  322. conflicted_state = {}
  323. for key in set(itertools.chain.from_iterable(state_sets)):
  324. event_ids = {state_set.get(key) for state_set in state_sets}
  325. if len(event_ids) == 1:
  326. unconflicted_state[key] = event_ids.pop()
  327. else:
  328. event_ids.discard(None)
  329. conflicted_state[key] = event_ids
  330. # mypy doesn't understand that discarding None above means that conflicted
  331. # state is StateMap[Set[str]], not StateMap[Set[Optional[Str]]].
  332. return unconflicted_state, conflicted_state # type: ignore
  333. def _is_power_event(event: EventBase) -> bool:
  334. """Return whether or not the event is a "power event", as defined by the
  335. v2 state resolution algorithm
  336. Args:
  337. event
  338. Returns:
  339. True if the event is a power event.
  340. """
  341. if (event.type, event.state_key) in (
  342. (EventTypes.PowerLevels, ""),
  343. (EventTypes.JoinRules, ""),
  344. (EventTypes.Create, ""),
  345. ):
  346. return True
  347. if event.type == EventTypes.Member:
  348. if event.membership in ("leave", "ban"):
  349. return event.sender != event.state_key
  350. return False
  351. async def _add_event_and_auth_chain_to_graph(
  352. graph: Dict[str, Set[str]],
  353. room_id: str,
  354. event_id: str,
  355. event_map: Dict[str, EventBase],
  356. state_res_store: StateResolutionStore,
  357. full_conflicted_set: Set[str],
  358. ) -> None:
  359. """Helper function for _reverse_topological_power_sort that add the event
  360. and its auth chain (that is in the auth diff) to the graph
  361. Args:
  362. graph: A map from event ID to the events auth event IDs
  363. room_id: the room we are working in
  364. event_id: Event to add to the graph
  365. event_map
  366. state_res_store
  367. full_conflicted_set: Set of event IDs that are in the full conflicted set.
  368. """
  369. state = [event_id]
  370. while state:
  371. eid = state.pop()
  372. graph.setdefault(eid, set())
  373. event = await _get_event(room_id, eid, event_map, state_res_store)
  374. for aid in event.auth_event_ids():
  375. if aid in full_conflicted_set:
  376. if aid not in graph:
  377. state.append(aid)
  378. graph.setdefault(eid, set()).add(aid)
  379. async def _reverse_topological_power_sort(
  380. clock: Clock,
  381. room_id: str,
  382. event_ids: Iterable[str],
  383. event_map: Dict[str, EventBase],
  384. state_res_store: StateResolutionStore,
  385. full_conflicted_set: Set[str],
  386. ) -> List[str]:
  387. """Returns a list of the event_ids sorted by reverse topological ordering,
  388. and then by power level and origin_server_ts
  389. Args:
  390. clock
  391. room_id: the room we are working in
  392. event_ids: The events to sort
  393. event_map
  394. state_res_store
  395. full_conflicted_set: Set of event IDs that are in the full conflicted set.
  396. Returns:
  397. The sorted list
  398. """
  399. graph: Dict[str, Set[str]] = {}
  400. for idx, event_id in enumerate(event_ids, start=1):
  401. await _add_event_and_auth_chain_to_graph(
  402. graph, room_id, event_id, event_map, state_res_store, full_conflicted_set
  403. )
  404. # We await occasionally when we're working with large data sets to
  405. # ensure that we don't block the reactor loop for too long.
  406. if idx % _AWAIT_AFTER_ITERATIONS == 0:
  407. await clock.sleep(0)
  408. event_to_pl = {}
  409. for idx, event_id in enumerate(graph, start=1):
  410. pl = await _get_power_level_for_sender(
  411. room_id, event_id, event_map, state_res_store
  412. )
  413. event_to_pl[event_id] = pl
  414. # We await occasionally when we're working with large data sets to
  415. # ensure that we don't block the reactor loop for too long.
  416. if idx % _AWAIT_AFTER_ITERATIONS == 0:
  417. await clock.sleep(0)
  418. def _get_power_order(event_id: str) -> Tuple[int, int, str]:
  419. ev = event_map[event_id]
  420. pl = event_to_pl[event_id]
  421. return -pl, ev.origin_server_ts, event_id
  422. # Note: graph is modified during the sort
  423. it = lexicographical_topological_sort(graph, key=_get_power_order)
  424. sorted_events = list(it)
  425. return sorted_events
  426. async def _iterative_auth_checks(
  427. clock: Clock,
  428. room_id: str,
  429. room_version: RoomVersion,
  430. event_ids: List[str],
  431. base_state: StateMap[str],
  432. event_map: Dict[str, EventBase],
  433. state_res_store: StateResolutionStore,
  434. ) -> MutableStateMap[str]:
  435. """Sequentially apply auth checks to each event in given list, updating the
  436. state as it goes along.
  437. Args:
  438. clock
  439. room_id
  440. room_version
  441. event_ids: Ordered list of events to apply auth checks to
  442. base_state: The set of state to start with
  443. event_map
  444. state_res_store
  445. Returns:
  446. Returns the final updated state
  447. """
  448. resolved_state = dict(base_state)
  449. for idx, event_id in enumerate(event_ids, start=1):
  450. event = event_map[event_id]
  451. auth_events = {}
  452. for aid in event.auth_event_ids():
  453. ev = await _get_event(
  454. room_id, aid, event_map, state_res_store, allow_none=True
  455. )
  456. if not ev:
  457. logger.warning(
  458. "auth_event id %s for event %s is missing", aid, event_id
  459. )
  460. else:
  461. if ev.rejected_reason is None:
  462. auth_events[(ev.type, ev.state_key)] = ev
  463. for key in event_auth.auth_types_for_event(room_version, event):
  464. if key in resolved_state:
  465. ev_id = resolved_state[key]
  466. ev = await _get_event(room_id, ev_id, event_map, state_res_store)
  467. if ev.rejected_reason is None:
  468. auth_events[key] = event_map[ev_id]
  469. if event.rejected_reason is not None:
  470. # Do not admit previously rejected events into state.
  471. # TODO: This isn't spec compliant. Events that were previously rejected due
  472. # to failing auth checks at their state, but pass auth checks during
  473. # state resolution should be accepted. Synapse does not handle the
  474. # change of rejection status well, so we preserve the previous
  475. # rejection status for now.
  476. #
  477. # Note that events rejected for non-state reasons, such as having the
  478. # wrong auth events, should remain rejected.
  479. #
  480. # https://spec.matrix.org/v1.2/rooms/v9/#rejected-events
  481. # https://github.com/matrix-org/synapse/issues/13797
  482. continue
  483. try:
  484. event_auth.check_state_dependent_auth_rules(
  485. event,
  486. auth_events.values(),
  487. )
  488. resolved_state[(event.type, event.state_key)] = event_id
  489. except AuthError:
  490. pass
  491. # We await occasionally when we're working with large data sets to
  492. # ensure that we don't block the reactor loop for too long.
  493. if idx % _AWAIT_AFTER_ITERATIONS == 0:
  494. await clock.sleep(0)
  495. return resolved_state
  496. async def _mainline_sort(
  497. clock: Clock,
  498. room_id: str,
  499. event_ids: List[str],
  500. resolved_power_event_id: Optional[str],
  501. event_map: Dict[str, EventBase],
  502. state_res_store: StateResolutionStore,
  503. ) -> List[str]:
  504. """Returns a sorted list of event_ids sorted by mainline ordering based on
  505. the given event resolved_power_event_id
  506. Args:
  507. clock
  508. room_id: room we're working in
  509. event_ids: Events to sort
  510. resolved_power_event_id: The final resolved power level event ID
  511. event_map
  512. state_res_store
  513. Returns:
  514. The sorted list
  515. """
  516. if not event_ids:
  517. # It's possible for there to be no event IDs here to sort, so we can
  518. # skip calculating the mainline in that case.
  519. return []
  520. mainline = []
  521. pl = resolved_power_event_id
  522. idx = 0
  523. while pl:
  524. mainline.append(pl)
  525. pl_ev = await _get_event(room_id, pl, event_map, state_res_store)
  526. auth_events = pl_ev.auth_event_ids()
  527. pl = None
  528. for aid in auth_events:
  529. ev = await _get_event(
  530. room_id, aid, event_map, state_res_store, allow_none=True
  531. )
  532. if ev and (ev.type, ev.state_key) == (EventTypes.PowerLevels, ""):
  533. pl = aid
  534. break
  535. # We await occasionally when we're working with large data sets to
  536. # ensure that we don't block the reactor loop for too long.
  537. if idx != 0 and idx % _AWAIT_AFTER_ITERATIONS == 0:
  538. await clock.sleep(0)
  539. idx += 1
  540. mainline_map = {ev_id: i + 1 for i, ev_id in enumerate(reversed(mainline))}
  541. event_ids = list(event_ids)
  542. order_map = {}
  543. for idx, ev_id in enumerate(event_ids, start=1):
  544. depth = await _get_mainline_depth_for_event(
  545. clock, event_map[ev_id], mainline_map, event_map, state_res_store
  546. )
  547. order_map[ev_id] = (depth, event_map[ev_id].origin_server_ts, ev_id)
  548. # We await occasionally when we're working with large data sets to
  549. # ensure that we don't block the reactor loop for too long.
  550. if idx % _AWAIT_AFTER_ITERATIONS == 0:
  551. await clock.sleep(0)
  552. event_ids.sort(key=lambda ev_id: order_map[ev_id])
  553. return event_ids
  554. async def _get_mainline_depth_for_event(
  555. clock: Clock,
  556. event: EventBase,
  557. mainline_map: Dict[str, int],
  558. event_map: Dict[str, EventBase],
  559. state_res_store: StateResolutionStore,
  560. ) -> int:
  561. """Get the mainline depths for the given event based on the mainline map
  562. Args:
  563. event
  564. mainline_map: Map from event_id to mainline depth for events in the mainline.
  565. event_map
  566. state_res_store
  567. Returns:
  568. The mainline depth
  569. """
  570. room_id = event.room_id
  571. tmp_event: Optional[EventBase] = event
  572. # We do an iterative search, replacing `event with the power level in its
  573. # auth events (if any)
  574. idx = 0
  575. while tmp_event:
  576. depth = mainline_map.get(tmp_event.event_id)
  577. if depth is not None:
  578. return depth
  579. auth_events = tmp_event.auth_event_ids()
  580. tmp_event = None
  581. for aid in auth_events:
  582. aev = await _get_event(
  583. room_id, aid, event_map, state_res_store, allow_none=True
  584. )
  585. if aev and (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
  586. tmp_event = aev
  587. break
  588. idx += 1
  589. if idx % _AWAIT_AFTER_ITERATIONS == 0:
  590. await clock.sleep(0)
  591. # Didn't find a power level auth event, so we just return 0
  592. return 0
  593. @overload
  594. async def _get_event(
  595. room_id: str,
  596. event_id: str,
  597. event_map: Dict[str, EventBase],
  598. state_res_store: StateResolutionStore,
  599. allow_none: Literal[False] = False,
  600. ) -> EventBase:
  601. ...
  602. @overload
  603. async def _get_event(
  604. room_id: str,
  605. event_id: str,
  606. event_map: Dict[str, EventBase],
  607. state_res_store: StateResolutionStore,
  608. allow_none: Literal[True],
  609. ) -> Optional[EventBase]:
  610. ...
  611. async def _get_event(
  612. room_id: str,
  613. event_id: str,
  614. event_map: Dict[str, EventBase],
  615. state_res_store: StateResolutionStore,
  616. allow_none: bool = False,
  617. ) -> Optional[EventBase]:
  618. """Helper function to look up event in event_map, falling back to looking
  619. it up in the store
  620. Args:
  621. room_id
  622. event_id
  623. event_map
  624. state_res_store
  625. allow_none: if the event is not found, return None rather than raising
  626. an exception
  627. Returns:
  628. The event, or none if the event does not exist (and allow_none is True).
  629. """
  630. if event_id not in event_map:
  631. events = await state_res_store.get_events([event_id], allow_rejected=True)
  632. event_map.update(events)
  633. event = event_map.get(event_id)
  634. if event is None:
  635. if allow_none:
  636. return None
  637. raise Exception("Unknown event %s" % (event_id,))
  638. if event.room_id != room_id:
  639. raise Exception(
  640. "In state res for room %s, event %s is in %s"
  641. % (room_id, event_id, event.room_id)
  642. )
  643. return event
  644. def lexicographical_topological_sort(
  645. graph: Dict[str, Set[str]], key: Callable[[str], Any]
  646. ) -> Generator[str, None, None]:
  647. """Performs a lexicographic reverse topological sort on the graph.
  648. This returns a reverse topological sort (i.e. if node A references B then B
  649. appears before A in the sort), with ties broken lexicographically based on
  650. return value of the `key` function.
  651. NOTE: `graph` is modified during the sort.
  652. Args:
  653. graph: A representation of the graph where each node is a key in the
  654. dict and its value are the nodes edges.
  655. key: A function that takes a node and returns a value that is comparable
  656. and used to order nodes
  657. Yields:
  658. The next node in the topological sort
  659. """
  660. # Note, this is basically Kahn's algorithm except we look at nodes with no
  661. # outgoing edges, c.f.
  662. # https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
  663. outdegree_map = graph
  664. reverse_graph: Dict[str, Set[str]] = {}
  665. # Lists of nodes with zero out degree. Is actually a tuple of
  666. # `(key(node), node)` so that sorting does the right thing
  667. zero_outdegree = []
  668. for node, edges in graph.items():
  669. if len(edges) == 0:
  670. zero_outdegree.append((key(node), node))
  671. reverse_graph.setdefault(node, set())
  672. for edge in edges:
  673. reverse_graph.setdefault(edge, set()).add(node)
  674. # heapq is a built in implementation of a sorted queue.
  675. heapq.heapify(zero_outdegree)
  676. while zero_outdegree:
  677. _, node = heapq.heappop(zero_outdegree)
  678. for parent in reverse_graph[node]:
  679. out = outdegree_map[parent]
  680. out.discard(node)
  681. if len(out) == 0:
  682. heapq.heappush(zero_outdegree, (key(parent), parent))
  683. yield node