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

Commit 0fb3dd0

Browse files
authored
Refactor the way we set outlier (#11634)
* `_auth_and_persist_outliers`: mark persisted events as outliers Mark any events that get persisted via `_auth_and_persist_outliers` as, well, outliers. Currently this will be a no-op as everything will already be flagged as an outlier, but I'm going to change that. * `process_remote_join`: stop flagging as outlier The events are now flagged as outliers later on, by `_auth_and_persist_outliers`. * `send_join`: remove `outlier=True` The events created here are returned in the result of `send_join` to `FederationHandler.do_invite_join`. From there they are passed into `FederationEventHandler.process_remote_join`, which passes them to `_auth_and_persist_outliers`... which sets the `outlier` flag. * `get_event_auth`: remove `outlier=True` stop flagging the events returned by `get_event_auth` as outliers. This method is only called by `_get_remote_auth_chain_for_event`, which passes the results into `_auth_and_persist_outliers`, which will flag them as outliers. * `_get_remote_auth_chain_for_event`: remove `outlier=True` we pass all the events into `_auth_and_persist_outliers`, which will now flag the events as outliers. * `_check_sigs_and_hash_and_fetch`: remove unused `outlier` parameter This param is now never set to True, so we can remove it. * `_check_sigs_and_hash_and_fetch_one`: remove unused `outlier` param This is no longer set anywhere, so we can remove it. * `get_pdu`: remove unused `outlier` parameter ... and chase it down into `get_pdu_from_destination_raw`. * `event_from_pdu_json`: remove redundant `outlier` param This is never set to `True`, so can be removed. * changelog * update docstring
1 parent eedb452 commit 0fb3dd0

File tree

5 files changed

+16
-47
lines changed

5 files changed

+16
-47
lines changed

changelog.d/11634.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor the way that the `outlier` flag is set on events received over federation.

synapse/federation/federation_base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,12 @@ def _is_invite_via_3pid(event: EventBase) -> bool:
215215
)
216216

217217

218-
def event_from_pdu_json(
219-
pdu_json: JsonDict, room_version: RoomVersion, outlier: bool = False
220-
) -> EventBase:
218+
def event_from_pdu_json(pdu_json: JsonDict, room_version: RoomVersion) -> EventBase:
221219
"""Construct an EventBase from an event json received over federation
222220
223221
Args:
224222
pdu_json: pdu as received over federation
225223
room_version: The version of the room this event belongs to
226-
outlier: True to mark this event as an outlier
227224
228225
Raises:
229226
SynapseError: if the pdu is missing required fields or is otherwise
@@ -247,6 +244,4 @@ def event_from_pdu_json(
247244
validate_canonicaljson(pdu_json)
248245

249246
event = make_event_from_dict(pdu_json, room_version)
250-
event.internal_metadata.outlier = outlier
251-
252247
return event

synapse/federation/federation_client.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ async def backfill(
265265

266266
room_version = await self.store.get_room_version(room_id)
267267

268-
pdus = [
269-
event_from_pdu_json(p, room_version, outlier=False)
270-
for p in transaction_data_pdus
271-
]
268+
pdus = [event_from_pdu_json(p, room_version) for p in transaction_data_pdus]
272269

273270
# Check signatures and hash of pdus, removing any from the list that fail checks
274271
pdus[:] = await self._check_sigs_and_hash_and_fetch(
@@ -282,7 +279,6 @@ async def get_pdu_from_destination_raw(
282279
destination: str,
283280
event_id: str,
284281
room_version: RoomVersion,
285-
outlier: bool = False,
286282
timeout: Optional[int] = None,
287283
) -> Optional[EventBase]:
288284
"""Requests the PDU with given origin and ID from the remote home
@@ -292,9 +288,6 @@ async def get_pdu_from_destination_raw(
292288
destination: Which homeserver to query
293289
event_id: event to fetch
294290
room_version: version of the room
295-
outlier: Indicates whether the PDU is an `outlier`, i.e. if
296-
it's from an arbitrary point in the context as opposed to part
297-
of the current block of PDUs. Defaults to `False`
298291
timeout: How long to try (in ms) each destination for before
299292
moving to the next destination. None indicates no timeout.
300293
@@ -316,8 +309,7 @@ async def get_pdu_from_destination_raw(
316309
)
317310

318311
pdu_list: List[EventBase] = [
319-
event_from_pdu_json(p, room_version, outlier=outlier)
320-
for p in transaction_data["pdus"]
312+
event_from_pdu_json(p, room_version) for p in transaction_data["pdus"]
321313
]
322314

323315
if pdu_list and pdu_list[0]:
@@ -334,7 +326,6 @@ async def get_pdu(
334326
destinations: Iterable[str],
335327
event_id: str,
336328
room_version: RoomVersion,
337-
outlier: bool = False,
338329
timeout: Optional[int] = None,
339330
) -> Optional[EventBase]:
340331
"""Requests the PDU with given origin and ID from the remote home
@@ -347,9 +338,6 @@ async def get_pdu(
347338
destinations: Which homeservers to query
348339
event_id: event to fetch
349340
room_version: version of the room
350-
outlier: Indicates whether the PDU is an `outlier`, i.e. if
351-
it's from an arbitrary point in the context as opposed to part
352-
of the current block of PDUs. Defaults to `False`
353341
timeout: How long to try (in ms) each destination for before
354342
moving to the next destination. None indicates no timeout.
355343
@@ -377,7 +365,6 @@ async def get_pdu(
377365
destination=destination,
378366
event_id=event_id,
379367
room_version=room_version,
380-
outlier=outlier,
381368
timeout=timeout,
382369
)
383370

@@ -435,7 +422,6 @@ async def _check_sigs_and_hash_and_fetch(
435422
origin: str,
436423
pdus: Collection[EventBase],
437424
room_version: RoomVersion,
438-
outlier: bool = False,
439425
) -> List[EventBase]:
440426
"""Takes a list of PDUs and checks the signatures and hashes of each
441427
one. If a PDU fails its signature check then we check if we have it in
@@ -451,7 +437,6 @@ async def _check_sigs_and_hash_and_fetch(
451437
origin
452438
pdu
453439
room_version
454-
outlier: Whether the events are outliers or not
455440
456441
Returns:
457442
A list of PDUs that have valid signatures and hashes.
@@ -466,7 +451,6 @@ async def _execute(pdu: EventBase) -> None:
466451
valid_pdu = await self._check_sigs_and_hash_and_fetch_one(
467452
pdu=pdu,
468453
origin=origin,
469-
outlier=outlier,
470454
room_version=room_version,
471455
)
472456

@@ -482,7 +466,6 @@ async def _check_sigs_and_hash_and_fetch_one(
482466
pdu: EventBase,
483467
origin: str,
484468
room_version: RoomVersion,
485-
outlier: bool = False,
486469
) -> Optional[EventBase]:
487470
"""Takes a PDU and checks its signatures and hashes. If the PDU fails
488471
its signature check then we check if we have it in the database and if
@@ -494,9 +477,6 @@ async def _check_sigs_and_hash_and_fetch_one(
494477
origin
495478
pdu
496479
room_version
497-
outlier: Whether the events are outliers or not
498-
include_none: Whether to include None in the returned list
499-
for events that have failed their checks
500480
501481
Returns:
502482
The PDU (possibly redacted) if it has valid signatures and hashes.
@@ -521,7 +501,6 @@ async def _check_sigs_and_hash_and_fetch_one(
521501
destinations=[pdu_origin],
522502
event_id=pdu.event_id,
523503
room_version=room_version,
524-
outlier=outlier,
525504
timeout=10000,
526505
)
527506
except SynapseError:
@@ -541,13 +520,10 @@ async def get_event_auth(
541520

542521
room_version = await self.store.get_room_version(room_id)
543522

544-
auth_chain = [
545-
event_from_pdu_json(p, room_version, outlier=True)
546-
for p in res["auth_chain"]
547-
]
523+
auth_chain = [event_from_pdu_json(p, room_version) for p in res["auth_chain"]]
548524

549525
signed_auth = await self._check_sigs_and_hash_and_fetch(
550-
destination, auth_chain, outlier=True, room_version=room_version
526+
destination, auth_chain, room_version=room_version
551527
)
552528

553529
return signed_auth
@@ -816,7 +792,6 @@ async def send_request(destination: str) -> SendJoinResult:
816792
valid_pdu = await self._check_sigs_and_hash_and_fetch_one(
817793
pdu=event,
818794
origin=destination,
819-
outlier=True,
820795
room_version=room_version,
821796
)
822797

@@ -864,7 +839,6 @@ async def _execute(pdu: EventBase) -> None:
864839
valid_pdu = await self._check_sigs_and_hash_and_fetch_one(
865840
pdu=pdu,
866841
origin=destination,
867-
outlier=True,
868842
room_version=room_version,
869843
)
870844

@@ -1235,7 +1209,7 @@ async def get_missing_events(
12351209
]
12361210

12371211
signed_events = await self._check_sigs_and_hash_and_fetch(
1238-
destination, events, outlier=False, room_version=room_version
1212+
destination, events, room_version=room_version
12391213
)
12401214
except HttpResponseException as e:
12411215
if not e.code == 400:

synapse/handlers/federation_event.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ async def process_remote_join(
421421
Raises:
422422
SynapseError if the response is in some way invalid.
423423
"""
424-
for e in itertools.chain(auth_events, state):
425-
e.internal_metadata.outlier = True
426-
427424
event_map = {e.event_id: e for e in itertools.chain(auth_events, state)}
428425

429426
create_event = None
@@ -1194,7 +1191,6 @@ async def get_event(event_id: str) -> None:
11941191
[destination],
11951192
event_id,
11961193
room_version,
1197-
outlier=True,
11981194
)
11991195
if event is None:
12001196
logger.warning(
@@ -1223,9 +1219,10 @@ async def _auth_and_persist_outliers(
12231219
"""Persist a batch of outlier events fetched from remote servers.
12241220
12251221
We first sort the events to make sure that we process each event's auth_events
1226-
before the event itself, and then auth and persist them.
1222+
before the event itself.
12271223
1228-
Notifies about the events where appropriate.
1224+
We then mark the events as outliers, persist them to the database, and, where
1225+
appropriate (eg, an invite), awake the notifier.
12291226
12301227
Params:
12311228
room_id: the room that the events are meant to be in (though this has
@@ -1276,7 +1273,8 @@ async def _auth_and_persist_outliers_inner(
12761273
Persists a batch of events where we have (theoretically) already persisted all
12771274
of their auth events.
12781275
1279-
Notifies about the events where appropriate.
1276+
Marks the events as outliers, auths them, persists them to the database, and,
1277+
where appropriate (eg, an invite), awakes the notifier.
12801278
12811279
Params:
12821280
origin: where the events came from
@@ -1314,6 +1312,9 @@ def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]:
13141312
return None
13151313
auth.append(ae)
13161314

1315+
# we're not bothering about room state, so flag the event as an outlier.
1316+
event.internal_metadata.outlier = True
1317+
13171318
context = EventContext.for_outlier()
13181319
try:
13191320
validate_event_for_room_version(room_version_obj, event)

tests/handlers/test_federation.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,7 @@ async def get_event_auth(
373373
destination: str, room_id: str, event_id: str
374374
) -> List[EventBase]:
375375
return [
376-
event_from_pdu_json(
377-
ae.get_pdu_json(), room_version=room_version, outlier=True
378-
)
376+
event_from_pdu_json(ae.get_pdu_json(), room_version=room_version)
379377
for ae in auth_events
380378
]
381379

0 commit comments

Comments
 (0)