@@ -1539,39 +1539,9 @@ async def _check_event_auth(
15391539 calculated_auth_events = await self ._store .get_events_as_list (
15401540 calculated_auth_event_ids
15411541 )
1542- calculated_auth_event_map = {
1543- (e .type , e .state_key ): e for e in calculated_auth_events
1544- }
15451542
15461543 try :
1547- updated_auth_events = await self ._update_auth_events_for_auth (
1548- event ,
1549- calculated_auth_event_map = calculated_auth_event_map ,
1550- )
1551- except Exception :
1552- # We don't really mind if the above fails, so lets not fail
1553- # processing if it does. However, it really shouldn't fail so
1554- # let's still log as an exception since we'll still want to fix
1555- # any bugs.
1556- logger .exception (
1557- "Failed to double check auth events for %s with remote. "
1558- "Ignoring failure and continuing processing of event." ,
1559- event .event_id ,
1560- )
1561- updated_auth_events = None
1562-
1563- if updated_auth_events :
1564- context = await self ._update_context_for_auth_events (
1565- event , context , updated_auth_events
1566- )
1567- auth_events_for_auth = updated_auth_events
1568- else :
1569- auth_events_for_auth = calculated_auth_event_map
1570-
1571- try :
1572- check_auth_rules_for_event (
1573- room_version_obj , event , auth_events_for_auth .values ()
1574- )
1544+ check_auth_rules_for_event (room_version_obj , event , calculated_auth_events )
15751545 except AuthError as e :
15761546 logger .warning ("Failed auth resolution for %r because %s" , event , e )
15771547 context .rejected = RejectedReason .AUTH_ERROR
@@ -1687,93 +1657,6 @@ async def _check_for_soft_fail(
16871657 soft_failed_event_counter .inc ()
16881658 event .internal_metadata .soft_failed = True
16891659
1690- async def _update_auth_events_for_auth (
1691- self ,
1692- event : EventBase ,
1693- calculated_auth_event_map : StateMap [EventBase ],
1694- ) -> Optional [StateMap [EventBase ]]:
1695- """Helper for _check_event_auth. See there for docs.
1696-
1697- Checks whether a given event has the expected auth events. If it
1698- doesn't then we talk to the remote server to compare state to see if
1699- we can come to a consensus (e.g. if one server missed some valid
1700- state).
1701-
1702- This attempts to resolve any potential divergence of state between
1703- servers, but is not essential and so failures should not block further
1704- processing of the event.
1705-
1706- Args:
1707- event:
1708-
1709- calculated_auth_event_map:
1710- Our calculated auth_events based on the state of the room
1711- at the event's position in the DAG.
1712-
1713- Returns:
1714- updated auth event map, or None if no changes are needed.
1715-
1716- """
1717- assert not event .internal_metadata .outlier
1718-
1719- # check for events which are in the event's claimed auth_events, but not
1720- # in our calculated event map.
1721- event_auth_events = set (event .auth_event_ids ())
1722- different_auth = event_auth_events .difference (
1723- e .event_id for e in calculated_auth_event_map .values ()
1724- )
1725-
1726- if not different_auth :
1727- return None
1728-
1729- logger .info (
1730- "auth_events refers to events which are not in our calculated auth "
1731- "chain: %s" ,
1732- different_auth ,
1733- )
1734-
1735- # XXX: currently this checks for redactions but I'm not convinced that is
1736- # necessary?
1737- different_events = await self ._store .get_events_as_list (different_auth )
1738-
1739- # double-check they're all in the same room - we should already have checked
1740- # this but it doesn't hurt to check again.
1741- for d in different_events :
1742- assert (
1743- d .room_id == event .room_id
1744- ), f"Event { event .event_id } refers to auth_event { d .event_id } which is in a different room"
1745-
1746- # now we state-resolve between our own idea of the auth events, and the remote's
1747- # idea of them.
1748-
1749- local_state = calculated_auth_event_map .values ()
1750- remote_auth_events = dict (calculated_auth_event_map )
1751- remote_auth_events .update ({(d .type , d .state_key ): d for d in different_events })
1752- remote_state = remote_auth_events .values ()
1753-
1754- room_version = await self ._store .get_room_version_id (event .room_id )
1755- new_state = await self ._state_handler .resolve_events (
1756- room_version , (local_state , remote_state ), event
1757- )
1758- different_state = {
1759- (d .type , d .state_key ): d
1760- for d in new_state .values ()
1761- if calculated_auth_event_map .get ((d .type , d .state_key )) != d
1762- }
1763- if not different_state :
1764- logger .info ("State res returned no new state" )
1765- return None
1766-
1767- logger .info (
1768- "After state res: updating auth_events with new state %s" ,
1769- different_state .values (),
1770- )
1771-
1772- # take a copy of calculated_auth_event_map before we modify it.
1773- auth_events = dict (calculated_auth_event_map )
1774- auth_events .update (different_state )
1775- return auth_events
1776-
17771660 async def _load_or_fetch_auth_events_for_event (
17781661 self , destination : str , event : EventBase
17791662 ) -> Collection [EventBase ]:
@@ -1871,61 +1754,6 @@ async def _get_remote_auth_chain_for_event(
18711754
18721755 await self ._auth_and_persist_outliers (room_id , remote_auth_events )
18731756
1874- async def _update_context_for_auth_events (
1875- self , event : EventBase , context : EventContext , auth_events : StateMap [EventBase ]
1876- ) -> EventContext :
1877- """Update the state_ids in an event context after auth event resolution,
1878- storing the changes as a new state group.
1879-
1880- Args:
1881- event: The event we're handling the context for
1882-
1883- context: initial event context
1884-
1885- auth_events: Events to update in the event context.
1886-
1887- Returns:
1888- new event context
1889- """
1890- # exclude the state key of the new event from the current_state in the context.
1891- if event .is_state ():
1892- event_key : Optional [Tuple [str , str ]] = (event .type , event .state_key )
1893- else :
1894- event_key = None
1895- state_updates = {
1896- k : a .event_id for k , a in auth_events .items () if k != event_key
1897- }
1898-
1899- current_state_ids = await context .get_current_state_ids ()
1900- current_state_ids = dict (current_state_ids ) # type: ignore
1901-
1902- current_state_ids .update (state_updates )
1903-
1904- prev_state_ids = await context .get_prev_state_ids ()
1905- prev_state_ids = dict (prev_state_ids )
1906-
1907- prev_state_ids .update ({k : a .event_id for k , a in auth_events .items ()})
1908-
1909- # create a new state group as a delta from the existing one.
1910- prev_group = context .state_group
1911- state_group = await self ._state_storage_controller .store_state_group (
1912- event .event_id ,
1913- event .room_id ,
1914- prev_group = prev_group ,
1915- delta_ids = state_updates ,
1916- current_state_ids = current_state_ids ,
1917- )
1918-
1919- return EventContext .with_state (
1920- storage = self ._storage_controllers ,
1921- state_group = state_group ,
1922- state_group_before_event = context .state_group_before_event ,
1923- state_delta_due_to_event = state_updates ,
1924- prev_group = prev_group ,
1925- delta_ids = state_updates ,
1926- partial_state = context .partial_state ,
1927- )
1928-
19291757 async def _run_push_actions_and_persist_event (
19301758 self , event : EventBase , context : EventContext , backfilled : bool = False
19311759 ) -> None :
0 commit comments