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

Separate creating an event context and persisting the event in the fed handler #9800

Merged
merged 13 commits into from
Apr 14, 2021
63 changes: 49 additions & 14 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ async def _process_received_pdu(
logger.debug("Processing event: %s", event)

try:
await self._handle_new_event(origin, event, state=state)
context = await self._prep_event(origin, event, state=state)
await self._handle_new_event(event, context)
except AuthError as e:
raise FederationError("ERROR", e.code, e.msg, affected=event.event_id)

Expand Down Expand Up @@ -1024,10 +1025,12 @@ async def backfill(
# non-outliers
assert not event.internal_metadata.is_outlier()

context = await self._prep_event(dest, event, backfilled=True)

# We store these one at a time since each event depends on the
# previous to work out the state.
# TODO: We can probably do something more clever here.
await self._handle_new_event(dest, event, backfilled=True)
await self._handle_new_event(event, context, backfilled=True)

return events

Expand Down Expand Up @@ -1667,7 +1670,11 @@ async def on_send_join_request(self, origin: str, pdu: EventBase) -> JsonDict:
# would introduce the danger of backwards-compatibility problems.
event.internal_metadata.send_on_behalf_of = origin

context = await self._handle_new_event(origin, event)
# Calculate the event context and persist the event.
context = await self._prep_event(
origin, event, state=None, auth_events=None, backfilled=False
)
context = await self._handle_new_event(event, context)

logger.debug(
"on_send_join_request: After _handle_new_event: %s, sigs: %s",
Expand Down Expand Up @@ -1879,7 +1886,8 @@ async def on_send_leave_request(self, origin: str, pdu: EventBase) -> None:

event.internal_metadata.outlier = False

await self._handle_new_event(origin, event)
context = await self._prep_event(origin, event)
await self._handle_new_event(event, context)

logger.debug(
"on_send_leave_request: After _handle_new_event: %s, sigs: %s",
Expand Down Expand Up @@ -1992,16 +2000,21 @@ async def get_min_depth_for_context(self, context: str) -> int:

async def _handle_new_event(
self,
origin: str,
event: EventBase,
state: Optional[Iterable[EventBase]] = None,
auth_events: Optional[MutableStateMap[EventBase]] = None,
context: EventContext,
backfilled: bool = False,
) -> EventContext:
context = await self._prep_event(
origin, event, state=state, auth_events=auth_events, backfilled=backfilled
)
"""
Process an event.
richvdh marked this conversation as resolved.
Show resolved Hide resolved

Args:
event: The event itself.
context: The event context.
backfilled: True if the event was backfilled.

Returns:
The event context.
"""
try:
if (
not event.internal_metadata.is_outlier()
Expand Down Expand Up @@ -2182,10 +2195,31 @@ async def _prep_event(
self,
origin: str,
event: EventBase,
state: Optional[Iterable[EventBase]],
auth_events: Optional[MutableStateMap[EventBase]],
backfilled: bool,
state: Optional[Iterable[EventBase]] = None,
auth_events: Optional[MutableStateMap[EventBase]] = None,
backfilled: bool = False,
) -> EventContext:
"""
Prepare an event for sending over federation.
richvdh marked this conversation as resolved.
Show resolved Hide resolved

Args:
origin: The host the event originates from.
event: The event itself.
state: The state events to calculate the event context from.
clokep marked this conversation as resolved.
Show resolved Hide resolved
auth_events:
Map from (event_type, state_key) to event

Normally, our calculated auth_events based on the state of the room
at the event's position in the DAG, though occasionally (eg if the
event is an outlier), may be the auth events claimed by the remote
server.

Also NB that this function adds entries to it.
backfilled: True if the event was backfilled.

Returns:
The event context.
"""
context = await self.state_handler.compute_event_context(event, old_state=state)

if not auth_events:
clokep marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -2471,7 +2505,8 @@ async def _update_auth_events_and_context_for_auth(
logger.debug(
"do_auth %s missing_auth: %s", event.event_id, e.event_id
)
await self._handle_new_event(origin, e, auth_events=auth)
context = await self._prep_event(origin, e, auth_events=auth)
await self._handle_new_event(e, context)

if e.event_id in event_auth_events:
auth_events[(e.type, e.state_key)] = e
Expand Down