test_state.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. # Copyright 2014-2016 OpenMarket 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. from typing import Collection, Dict, List, Optional, cast
  15. from unittest.mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.auth import Auth
  18. from synapse.api.constants import EventTypes, Membership
  19. from synapse.api.room_versions import RoomVersions
  20. from synapse.events import make_event_from_dict
  21. from synapse.events.snapshot import EventContext
  22. from synapse.state import StateHandler, StateResolutionHandler
  23. from synapse.util import Clock
  24. from synapse.util.macaroons import MacaroonGenerator
  25. from tests import unittest
  26. from .utils import MockClock, default_config
  27. _next_event_id = 1000
  28. def create_event(
  29. name=None,
  30. type=None,
  31. state_key=None,
  32. depth=2,
  33. event_id=None,
  34. prev_events: Optional[List[str]] = None,
  35. **kwargs,
  36. ):
  37. global _next_event_id
  38. if not event_id:
  39. _next_event_id += 1
  40. event_id = "$%s:test" % (_next_event_id,)
  41. if not name:
  42. if state_key is not None:
  43. name = "<%s-%s, %s>" % (type, state_key, event_id)
  44. else:
  45. name = "<%s, %s>" % (type, event_id)
  46. d = {
  47. "event_id": event_id,
  48. "type": type,
  49. "sender": "@user_id:example.com",
  50. "room_id": "!room_id:example.com",
  51. "depth": depth,
  52. "prev_events": prev_events or [],
  53. }
  54. if state_key is not None:
  55. d["state_key"] = state_key
  56. d.update(kwargs)
  57. event = make_event_from_dict(d)
  58. return event
  59. class _DummyStore:
  60. def __init__(self):
  61. self._event_to_state_group = {}
  62. self._group_to_state = {}
  63. self._event_id_to_event = {}
  64. self._next_group = 1
  65. async def get_state_groups_ids(self, room_id, event_ids):
  66. groups = {}
  67. for event_id in event_ids:
  68. group = self._event_to_state_group.get(event_id)
  69. if group:
  70. groups[group] = self._group_to_state[group]
  71. return groups
  72. async def get_state_ids_for_group(self, state_group, state_filter=None):
  73. return self._group_to_state[state_group]
  74. async def store_state_group(
  75. self, event_id, room_id, prev_group, delta_ids, current_state_ids
  76. ):
  77. state_group = self._next_group
  78. self._next_group += 1
  79. self._group_to_state[state_group] = dict(current_state_ids)
  80. return state_group
  81. async def get_events(self, event_ids, **kwargs):
  82. return {
  83. e_id: self._event_id_to_event[e_id]
  84. for e_id in event_ids
  85. if e_id in self._event_id_to_event
  86. }
  87. async def get_partial_state_events(
  88. self, event_ids: Collection[str]
  89. ) -> Dict[str, bool]:
  90. return {e: False for e in event_ids}
  91. async def get_state_group_delta(self, name):
  92. return None, None
  93. def register_events(self, events):
  94. for e in events:
  95. self._event_id_to_event[e.event_id] = e
  96. def register_event_context(self, event, context):
  97. self._event_to_state_group[event.event_id] = context.state_group
  98. def register_event_id_state_group(self, event_id, state_group):
  99. self._event_to_state_group[event_id] = state_group
  100. async def get_room_version_id(self, room_id):
  101. return RoomVersions.V1.identifier
  102. async def get_state_group_for_events(self, event_ids):
  103. res = {}
  104. for event in event_ids:
  105. res[event] = self._event_to_state_group[event]
  106. return res
  107. async def get_state_for_groups(self, groups):
  108. res = {}
  109. for group in groups:
  110. state = self._group_to_state[group]
  111. res[group] = state
  112. return res
  113. class DictObj(dict):
  114. def __init__(self, **kwargs):
  115. super().__init__(kwargs)
  116. self.__dict__ = self
  117. class Graph:
  118. def __init__(self, nodes, edges):
  119. events = {}
  120. clobbered = set(events.keys())
  121. for event_id, fields in nodes.items():
  122. refs = edges.get(event_id)
  123. if refs:
  124. clobbered.difference_update(refs)
  125. prev_events = [(r, {}) for r in refs]
  126. else:
  127. prev_events = []
  128. events[event_id] = create_event(
  129. event_id=event_id, prev_events=prev_events, **fields
  130. )
  131. self._leaves = clobbered
  132. self._events = sorted(events.values(), key=lambda e: e.depth)
  133. def walk(self):
  134. return iter(self._events)
  135. def get_leaves(self):
  136. return (self._events[i] for i in self._leaves)
  137. class StateTestCase(unittest.TestCase):
  138. def setUp(self):
  139. self.dummy_store = _DummyStore()
  140. storage_controllers = Mock(main=self.dummy_store, state=self.dummy_store)
  141. hs = Mock(
  142. spec_set=[
  143. "config",
  144. "get_datastores",
  145. "get_storage_controllers",
  146. "get_auth",
  147. "get_state_handler",
  148. "get_clock",
  149. "get_state_resolution_handler",
  150. "get_account_validity_handler",
  151. "get_macaroon_generator",
  152. "hostname",
  153. ]
  154. )
  155. clock = cast(Clock, MockClock())
  156. hs.config = default_config("tesths", True)
  157. hs.get_datastores.return_value = Mock(main=self.dummy_store)
  158. hs.get_state_handler.return_value = None
  159. hs.get_clock.return_value = clock
  160. hs.get_macaroon_generator.return_value = MacaroonGenerator(
  161. clock, "tesths", b"verysecret"
  162. )
  163. hs.get_auth.return_value = Auth(hs)
  164. hs.get_state_resolution_handler = lambda: StateResolutionHandler(hs)
  165. hs.get_storage_controllers.return_value = storage_controllers
  166. self.state = StateHandler(hs)
  167. self.event_id = 0
  168. @defer.inlineCallbacks
  169. def test_branch_no_conflict(self):
  170. graph = Graph(
  171. nodes={
  172. "START": DictObj(
  173. type=EventTypes.Create, state_key="", content={}, depth=1
  174. ),
  175. "A": DictObj(type=EventTypes.Message, depth=2),
  176. "B": DictObj(type=EventTypes.Message, depth=3),
  177. "C": DictObj(type=EventTypes.Name, state_key="", depth=3),
  178. "D": DictObj(type=EventTypes.Message, depth=4),
  179. },
  180. edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
  181. )
  182. self.dummy_store.register_events(graph.walk())
  183. context_store: dict[str, EventContext] = {}
  184. for event in graph.walk():
  185. context = yield defer.ensureDeferred(
  186. self.state.compute_event_context(event)
  187. )
  188. self.dummy_store.register_event_context(event, context)
  189. context_store[event.event_id] = context
  190. ctx_c = context_store["C"]
  191. ctx_d = context_store["D"]
  192. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  193. self.assertEqual(2, len(prev_state_ids))
  194. self.assertEqual(ctx_c.state_group, ctx_d.state_group_before_event)
  195. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  196. @defer.inlineCallbacks
  197. def test_branch_basic_conflict(self):
  198. graph = Graph(
  199. nodes={
  200. "START": DictObj(
  201. type=EventTypes.Create,
  202. state_key="",
  203. content={"creator": "@user_id:example.com"},
  204. depth=1,
  205. ),
  206. "A": DictObj(
  207. type=EventTypes.Member,
  208. state_key="@user_id:example.com",
  209. content={"membership": Membership.JOIN},
  210. membership=Membership.JOIN,
  211. depth=2,
  212. ),
  213. "B": DictObj(type=EventTypes.Name, state_key="", depth=3),
  214. "C": DictObj(type=EventTypes.Name, state_key="", depth=4),
  215. "D": DictObj(type=EventTypes.Message, depth=5),
  216. },
  217. edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
  218. )
  219. self.dummy_store.register_events(graph.walk())
  220. context_store = {}
  221. for event in graph.walk():
  222. context = yield defer.ensureDeferred(
  223. self.state.compute_event_context(event)
  224. )
  225. self.dummy_store.register_event_context(event, context)
  226. context_store[event.event_id] = context
  227. # C ends up winning the resolution between B and C
  228. ctx_c = context_store["C"]
  229. ctx_d = context_store["D"]
  230. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  231. self.assertSetEqual({"START", "A", "C"}, set(prev_state_ids.values()))
  232. self.assertEqual(ctx_c.state_group, ctx_d.state_group_before_event)
  233. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  234. @defer.inlineCallbacks
  235. def test_branch_have_banned_conflict(self):
  236. graph = Graph(
  237. nodes={
  238. "START": DictObj(
  239. type=EventTypes.Create,
  240. state_key="",
  241. content={"creator": "@user_id:example.com"},
  242. depth=1,
  243. ),
  244. "A": DictObj(
  245. type=EventTypes.Member,
  246. state_key="@user_id:example.com",
  247. content={"membership": Membership.JOIN},
  248. membership=Membership.JOIN,
  249. depth=2,
  250. ),
  251. "B": DictObj(type=EventTypes.Name, state_key="", depth=3),
  252. "C": DictObj(
  253. type=EventTypes.Member,
  254. state_key="@user_id_2:example.com",
  255. content={"membership": Membership.BAN},
  256. membership=Membership.BAN,
  257. depth=4,
  258. ),
  259. "D": DictObj(
  260. type=EventTypes.Name,
  261. state_key="",
  262. depth=4,
  263. sender="@user_id_2:example.com",
  264. ),
  265. "E": DictObj(type=EventTypes.Message, depth=5),
  266. },
  267. edges={"A": ["START"], "B": ["A"], "C": ["B"], "D": ["B"], "E": ["C", "D"]},
  268. )
  269. self.dummy_store.register_events(graph.walk())
  270. context_store = {}
  271. for event in graph.walk():
  272. context = yield defer.ensureDeferred(
  273. self.state.compute_event_context(event)
  274. )
  275. self.dummy_store.register_event_context(event, context)
  276. context_store[event.event_id] = context
  277. # C ends up winning the resolution between C and D because bans win over other
  278. # changes
  279. ctx_c = context_store["C"]
  280. ctx_e = context_store["E"]
  281. prev_state_ids = yield defer.ensureDeferred(ctx_e.get_prev_state_ids())
  282. self.assertSetEqual({"START", "A", "B", "C"}, set(prev_state_ids.values()))
  283. self.assertEqual(ctx_c.state_group, ctx_e.state_group_before_event)
  284. self.assertEqual(ctx_e.state_group_before_event, ctx_e.state_group)
  285. @defer.inlineCallbacks
  286. def test_branch_have_perms_conflict(self):
  287. userid1 = "@user_id:example.com"
  288. userid2 = "@user_id2:example.com"
  289. nodes = {
  290. "A1": DictObj(
  291. type=EventTypes.Create,
  292. state_key="",
  293. content={"creator": userid1},
  294. depth=1,
  295. ),
  296. "A2": DictObj(
  297. type=EventTypes.Member,
  298. state_key=userid1,
  299. content={"membership": Membership.JOIN},
  300. membership=Membership.JOIN,
  301. ),
  302. "A3": DictObj(
  303. type=EventTypes.Member,
  304. state_key=userid2,
  305. content={"membership": Membership.JOIN},
  306. membership=Membership.JOIN,
  307. ),
  308. "A4": DictObj(
  309. type=EventTypes.PowerLevels,
  310. state_key="",
  311. content={
  312. "events": {"m.room.name": 50},
  313. "users": {userid1: 100, userid2: 60},
  314. },
  315. ),
  316. "A5": DictObj(type=EventTypes.Name, state_key=""),
  317. "B": DictObj(
  318. type=EventTypes.PowerLevels,
  319. state_key="",
  320. content={"events": {"m.room.name": 50}, "users": {userid2: 30}},
  321. ),
  322. "C": DictObj(type=EventTypes.Name, state_key="", sender=userid2),
  323. "D": DictObj(type=EventTypes.Message),
  324. }
  325. edges = {
  326. "A2": ["A1"],
  327. "A3": ["A2"],
  328. "A4": ["A3"],
  329. "A5": ["A4"],
  330. "B": ["A5"],
  331. "C": ["A5"],
  332. "D": ["B", "C"],
  333. }
  334. self._add_depths(nodes, edges)
  335. graph = Graph(nodes, edges)
  336. self.dummy_store.register_events(graph.walk())
  337. context_store = {}
  338. for event in graph.walk():
  339. context = yield defer.ensureDeferred(
  340. self.state.compute_event_context(event)
  341. )
  342. self.dummy_store.register_event_context(event, context)
  343. context_store[event.event_id] = context
  344. # B ends up winning the resolution between B and C because power levels
  345. # win over other changes.
  346. ctx_b = context_store["B"]
  347. ctx_d = context_store["D"]
  348. prev_state_ids = yield defer.ensureDeferred(ctx_d.get_prev_state_ids())
  349. self.assertSetEqual({"A1", "A2", "A3", "A5", "B"}, set(prev_state_ids.values()))
  350. self.assertEqual(ctx_b.state_group, ctx_d.state_group_before_event)
  351. self.assertEqual(ctx_d.state_group_before_event, ctx_d.state_group)
  352. def _add_depths(self, nodes, edges):
  353. def _get_depth(ev):
  354. node = nodes[ev]
  355. if "depth" not in node:
  356. prevs = edges[ev]
  357. depth = max(_get_depth(prev) for prev in prevs) + 1
  358. node["depth"] = depth
  359. return node["depth"]
  360. for n in nodes:
  361. _get_depth(n)
  362. @defer.inlineCallbacks
  363. def test_annotate_with_old_message(self):
  364. event = create_event(type="test_message", name="event")
  365. old_state = [
  366. create_event(type="test1", state_key="1"),
  367. create_event(type="test1", state_key="2"),
  368. create_event(type="test2", state_key=""),
  369. ]
  370. context = yield defer.ensureDeferred(
  371. self.state.compute_event_context(
  372. event,
  373. state_ids_before_event={
  374. (e.type, e.state_key): e.event_id for e in old_state
  375. },
  376. )
  377. )
  378. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  379. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  380. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  381. self.assertCountEqual(
  382. (e.event_id for e in old_state), current_state_ids.values()
  383. )
  384. self.assertIsNotNone(context.state_group_before_event)
  385. self.assertEqual(context.state_group_before_event, context.state_group)
  386. @defer.inlineCallbacks
  387. def test_annotate_with_old_state(self):
  388. event = create_event(type="state", state_key="", name="event")
  389. old_state = [
  390. create_event(type="test1", state_key="1"),
  391. create_event(type="test1", state_key="2"),
  392. create_event(type="test2", state_key=""),
  393. ]
  394. context = yield defer.ensureDeferred(
  395. self.state.compute_event_context(
  396. event,
  397. state_ids_before_event={
  398. (e.type, e.state_key): e.event_id for e in old_state
  399. },
  400. )
  401. )
  402. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  403. self.assertCountEqual((e.event_id for e in old_state), prev_state_ids.values())
  404. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  405. self.assertCountEqual(
  406. (e.event_id for e in old_state + [event]), current_state_ids.values()
  407. )
  408. self.assertIsNotNone(context.state_group_before_event)
  409. self.assertNotEqual(context.state_group_before_event, context.state_group)
  410. self.assertEqual(context.state_group_before_event, context.prev_group)
  411. self.assertEqual({("state", ""): event.event_id}, context.delta_ids)
  412. @defer.inlineCallbacks
  413. def test_trivial_annotate_message(self):
  414. prev_event_id = "prev_event_id"
  415. event = create_event(
  416. type="test_message", name="event2", prev_events=[(prev_event_id, {})]
  417. )
  418. old_state = [
  419. create_event(type="test1", state_key="1"),
  420. create_event(type="test1", state_key="2"),
  421. create_event(type="test2", state_key=""),
  422. ]
  423. group_name = yield defer.ensureDeferred(
  424. self.dummy_store.store_state_group(
  425. prev_event_id,
  426. event.room_id,
  427. None,
  428. None,
  429. {(e.type, e.state_key): e.event_id for e in old_state},
  430. )
  431. )
  432. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  433. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  434. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  435. self.assertEqual(
  436. {e.event_id for e in old_state}, set(current_state_ids.values())
  437. )
  438. self.assertEqual(group_name, context.state_group)
  439. @defer.inlineCallbacks
  440. def test_trivial_annotate_state(self):
  441. prev_event_id = "prev_event_id"
  442. event = create_event(
  443. type="state", state_key="", name="event2", prev_events=[(prev_event_id, {})]
  444. )
  445. old_state = [
  446. create_event(type="test1", state_key="1"),
  447. create_event(type="test1", state_key="2"),
  448. create_event(type="test2", state_key=""),
  449. ]
  450. group_name = yield defer.ensureDeferred(
  451. self.dummy_store.store_state_group(
  452. prev_event_id,
  453. event.room_id,
  454. None,
  455. None,
  456. {(e.type, e.state_key): e.event_id for e in old_state},
  457. )
  458. )
  459. self.dummy_store.register_event_id_state_group(prev_event_id, group_name)
  460. context = yield defer.ensureDeferred(self.state.compute_event_context(event))
  461. prev_state_ids = yield defer.ensureDeferred(context.get_prev_state_ids())
  462. self.assertEqual({e.event_id for e in old_state}, set(prev_state_ids.values()))
  463. self.assertIsNotNone(context.state_group)
  464. @defer.inlineCallbacks
  465. def test_resolve_message_conflict(self):
  466. prev_event_id1 = "event_id1"
  467. prev_event_id2 = "event_id2"
  468. event = create_event(
  469. type="test_message",
  470. name="event3",
  471. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  472. )
  473. creation = create_event(type=EventTypes.Create, state_key="")
  474. old_state_1 = [
  475. creation,
  476. create_event(type="test1", state_key="1"),
  477. create_event(type="test1", state_key="2"),
  478. create_event(type="test2", state_key=""),
  479. ]
  480. old_state_2 = [
  481. creation,
  482. create_event(type="test1", state_key="1"),
  483. create_event(type="test3", state_key="2"),
  484. create_event(type="test4", state_key=""),
  485. ]
  486. self.dummy_store.register_events(old_state_1)
  487. self.dummy_store.register_events(old_state_2)
  488. context = yield self._get_context(
  489. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  490. )
  491. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  492. self.assertEqual(len(current_state_ids), 6)
  493. self.assertIsNotNone(context.state_group)
  494. @defer.inlineCallbacks
  495. def test_resolve_state_conflict(self):
  496. prev_event_id1 = "event_id1"
  497. prev_event_id2 = "event_id2"
  498. event = create_event(
  499. type="test4",
  500. state_key="",
  501. name="event",
  502. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  503. )
  504. creation = create_event(type=EventTypes.Create, state_key="")
  505. old_state_1 = [
  506. creation,
  507. create_event(type="test1", state_key="1"),
  508. create_event(type="test1", state_key="2"),
  509. create_event(type="test2", state_key=""),
  510. ]
  511. old_state_2 = [
  512. creation,
  513. create_event(type="test1", state_key="1"),
  514. create_event(type="test3", state_key="2"),
  515. create_event(type="test4", state_key=""),
  516. ]
  517. store = _DummyStore()
  518. store.register_events(old_state_1)
  519. store.register_events(old_state_2)
  520. self.dummy_store.get_events = store.get_events
  521. context = yield self._get_context(
  522. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  523. )
  524. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  525. self.assertEqual(len(current_state_ids), 6)
  526. self.assertIsNotNone(context.state_group)
  527. @defer.inlineCallbacks
  528. def test_standard_depth_conflict(self):
  529. prev_event_id1 = "event_id1"
  530. prev_event_id2 = "event_id2"
  531. event = create_event(
  532. type="test4",
  533. name="event",
  534. prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
  535. )
  536. member_event = create_event(
  537. type=EventTypes.Member,
  538. state_key="@user_id:example.com",
  539. content={"membership": Membership.JOIN},
  540. )
  541. power_levels = create_event(
  542. type=EventTypes.PowerLevels,
  543. state_key="",
  544. content={"users": {"@foo:bar": "100", "@user_id:example.com": "100"}},
  545. )
  546. creation = create_event(
  547. type=EventTypes.Create, state_key="", content={"creator": "@foo:bar"}
  548. )
  549. old_state_1 = [
  550. creation,
  551. power_levels,
  552. member_event,
  553. create_event(type="test1", state_key="1", depth=1),
  554. ]
  555. old_state_2 = [
  556. creation,
  557. power_levels,
  558. member_event,
  559. create_event(type="test1", state_key="1", depth=2),
  560. ]
  561. store = _DummyStore()
  562. store.register_events(old_state_1)
  563. store.register_events(old_state_2)
  564. self.dummy_store.get_events = store.get_events
  565. context = yield self._get_context(
  566. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  567. )
  568. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  569. self.assertEqual(old_state_2[3].event_id, current_state_ids[("test1", "1")])
  570. # Reverse the depth to make sure we are actually using the depths
  571. # during state resolution.
  572. old_state_1 = [
  573. creation,
  574. power_levels,
  575. member_event,
  576. create_event(type="test1", state_key="1", depth=2),
  577. ]
  578. old_state_2 = [
  579. creation,
  580. power_levels,
  581. member_event,
  582. create_event(type="test1", state_key="1", depth=1),
  583. ]
  584. store.register_events(old_state_1)
  585. store.register_events(old_state_2)
  586. context = yield self._get_context(
  587. event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
  588. )
  589. current_state_ids = yield defer.ensureDeferred(context.get_current_state_ids())
  590. self.assertEqual(old_state_1[3].event_id, current_state_ids[("test1", "1")])
  591. @defer.inlineCallbacks
  592. def _get_context(
  593. self, event, prev_event_id_1, old_state_1, prev_event_id_2, old_state_2
  594. ):
  595. sg1 = yield defer.ensureDeferred(
  596. self.dummy_store.store_state_group(
  597. prev_event_id_1,
  598. event.room_id,
  599. None,
  600. None,
  601. {(e.type, e.state_key): e.event_id for e in old_state_1},
  602. )
  603. )
  604. self.dummy_store.register_event_id_state_group(prev_event_id_1, sg1)
  605. sg2 = yield defer.ensureDeferred(
  606. self.dummy_store.store_state_group(
  607. prev_event_id_2,
  608. event.room_id,
  609. None,
  610. None,
  611. {(e.type, e.state_key): e.event_id for e in old_state_2},
  612. )
  613. )
  614. self.dummy_store.register_event_id_state_group(prev_event_id_2, sg2)
  615. result = yield defer.ensureDeferred(self.state.compute_event_context(event))
  616. return result