Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 5271c25

Browse files
committed
Factor out a separate EventContext.for_outlier
Constructing an EventContext for an outlier is actually really simple, and there's no sense in going via an `async` method in the `StateHandler`. This also means that we can resolve a bunch of FIXMEs.
1 parent 03db670 commit 5271c25

File tree

5 files changed

+21
-44
lines changed

5 files changed

+21
-44
lines changed

changelog.d/10883.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up some of the federation event authentication code for clarity.

synapse/events/snapshot.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ class EventContext:
8080
8181
(type, state_key) -> event_id
8282
83-
FIXME: what is this for an outlier? it seems ill-defined. It seems like
84-
it could be either {}, or the state we were given by the remote
85-
server, depending on $THINGS
83+
For an outlier, this is {}
8684
8785
Note that this is a private attribute: it should be accessed via
8886
``get_current_state_ids``. _AsyncEventContext impl calculates this
@@ -96,7 +94,7 @@ class EventContext:
9694
9795
(type, state_key) -> event_id
9896
99-
FIXME: again, what is this for an outlier?
97+
For an outlier, this is {}
10098
10199
As with _current_state_ids, this is a private attribute. It should be
102100
accessed via get_prev_state_ids.
@@ -130,6 +128,14 @@ def with_state(
130128
delta_ids=delta_ids,
131129
)
132130

131+
@staticmethod
132+
def for_outlier():
133+
"""Return an EventContext instance suitable for persisting an outlier event"""
134+
return EventContext(
135+
current_state_ids={},
136+
prev_state_ids={},
137+
)
138+
133139
async def serialize(self, event: EventBase, store: "DataStore") -> dict:
134140
"""Converts self to a type that can be serialized as JSON, and then
135141
deserialized by `deserialize`

synapse/handlers/federation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ async def do_knock(
624624
# in the invitee's sync stream. It is stripped out for all other local users.
625625
event.unsigned["knock_room_state"] = stripped_room_state["knock_state_events"]
626626

627-
context = await self.state_handler.compute_event_context(event)
627+
context = EventContext.for_outlier()
628628
stream_id = await self._federation_event_handler.persist_events_and_notify(
629629
event.room_id, [(event, context)]
630630
)
@@ -814,7 +814,7 @@ async def on_invite_request(
814814
)
815815
)
816816

817-
context = await self.state_handler.compute_event_context(event)
817+
context = EventContext.for_outlier()
818818
await self._federation_event_handler.persist_events_and_notify(
819819
event.room_id, [(event, context)]
820820
)
@@ -843,7 +843,7 @@ async def do_remotely_reject_invite(
843843

844844
await self.federation_client.send_leave(host_list, event)
845845

846-
context = await self.state_handler.compute_event_context(event)
846+
context = EventContext.for_outlier()
847847
stream_id = await self._federation_event_handler.persist_events_and_notify(
848848
event.room_id, [(event, context)]
849849
)
@@ -1115,8 +1115,7 @@ async def _persist_auth_tree(
11151115
events_to_context = {}
11161116
for e in itertools.chain(auth_events, state):
11171117
e.internal_metadata.outlier = True
1118-
ctx = await self.state_handler.compute_event_context(e)
1119-
events_to_context[e.event_id] = ctx
1118+
events_to_context[e.event_id] = EventContext.for_outlier()
11201119

11211120
event_map = {
11221121
e.event_id: e for e in itertools.chain(auth_events, state, [event])

synapse/handlers/federation_event.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ async def _auth_and_persist_fetched_events(
12211221
async def prep(ev_info: _NewEventInfo) -> EventContext:
12221222
event = ev_info.event
12231223
with nested_logging_context(suffix=event.event_id):
1224-
res = await self._state_handler.compute_event_context(event)
1224+
res = EventContext.for_outlier()
12251225
res = await self._check_event_auth(
12261226
origin,
12271227
event,
@@ -1540,10 +1540,7 @@ async def _update_auth_events_and_context_for_auth(
15401540
event.event_id,
15411541
auth_event.event_id,
15421542
)
1543-
missing_auth_event_context = (
1544-
await self._state_handler.compute_event_context(auth_event)
1545-
)
1546-
1543+
missing_auth_event_context = EventContext.for_outlier()
15471544
missing_auth_event_context = await self._check_event_auth(
15481545
origin,
15491546
auth_event,

synapse/state/__init__.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ async def get_hosts_in_room_at_events(
263263
async def compute_event_context(
264264
self, event: EventBase, old_state: Optional[Iterable[EventBase]] = None
265265
) -> EventContext:
266-
"""Build an EventContext structure for the event.
266+
"""Build an EventContext structure for a non-outlier event.
267+
268+
(for an outlier, call EventContext.for_outlier directly)
267269
268270
This works out what the current state should be for the event, and
269271
generates a new state group if necessary.
@@ -278,35 +280,7 @@ async def compute_event_context(
278280
The event context.
279281
"""
280282

281-
if event.internal_metadata.is_outlier():
282-
# If this is an outlier, then we know it shouldn't have any current
283-
# state. Certainly store.get_current_state won't return any, and
284-
# persisting the event won't store the state group.
285-
286-
# FIXME: why do we populate current_state_ids? I thought the point was
287-
# that we weren't supposed to have any state for outliers?
288-
if old_state:
289-
prev_state_ids = {(s.type, s.state_key): s.event_id for s in old_state}
290-
if event.is_state():
291-
current_state_ids = dict(prev_state_ids)
292-
key = (event.type, event.state_key)
293-
current_state_ids[key] = event.event_id
294-
else:
295-
current_state_ids = prev_state_ids
296-
else:
297-
current_state_ids = {}
298-
prev_state_ids = {}
299-
300-
# We don't store state for outliers, so we don't generate a state
301-
# group for it.
302-
context = EventContext.with_state(
303-
state_group=None,
304-
state_group_before_event=None,
305-
current_state_ids=current_state_ids,
306-
prev_state_ids=prev_state_ids,
307-
)
308-
309-
return context
283+
assert not event.internal_metadata.is_outlier()
310284

311285
#
312286
# first of all, figure out the state before the event

0 commit comments

Comments
 (0)