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

Rename the EventFormatVersions enum values so that they line up with room version numbers. #13706

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13706.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rename the `EventFormatVersions` enum values so that they line up with room version numbers.
45 changes: 25 additions & 20 deletions synapse/api/room_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@

class EventFormatVersions:
"""This is an internal enum for tracking the version of the event format,
independently from the room version.
independently of the room version.

To reduce confusion, the event format versions are named after the room
versions that they were used or introduced in.
The concept of an 'event format version' is specific to Synapse (the
specification does not mention this term.)
"""

V1 = 1 # $id:server event id format
V2 = 2 # MSC1659-style $hash event id format: introduced for room v3
V3 = 3 # MSC1884-style $hash format: introduced for room v4
ROOM_V1_V2 = 1 # $id:server event id format: used for room v1 and v2
ROOM_V3 = 2 # MSC1659-style $hash event id format: used for room v3
ROOM_V4_PLUS = 3 # MSC1884-style $hash format: introduced for room v4


KNOWN_EVENT_FORMAT_VERSIONS = {
EventFormatVersions.V1,
EventFormatVersions.V2,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V1_V2,
EventFormatVersions.ROOM_V3,
EventFormatVersions.ROOM_V4_PLUS,
}


Expand Down Expand Up @@ -92,7 +97,7 @@ class RoomVersions:
V1 = RoomVersion(
"1",
RoomDisposition.STABLE,
EventFormatVersions.V1,
EventFormatVersions.ROOM_V1_V2,
StateResolutionVersions.V1,
enforce_key_validity=False,
special_case_aliases_auth=True,
Expand All @@ -110,7 +115,7 @@ class RoomVersions:
V2 = RoomVersion(
"2",
RoomDisposition.STABLE,
EventFormatVersions.V1,
EventFormatVersions.ROOM_V1_V2,
StateResolutionVersions.V2,
enforce_key_validity=False,
special_case_aliases_auth=True,
Expand All @@ -128,7 +133,7 @@ class RoomVersions:
V3 = RoomVersion(
"3",
RoomDisposition.STABLE,
EventFormatVersions.V2,
EventFormatVersions.ROOM_V3,
StateResolutionVersions.V2,
enforce_key_validity=False,
special_case_aliases_auth=True,
Expand All @@ -146,7 +151,7 @@ class RoomVersions:
V4 = RoomVersion(
"4",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=False,
special_case_aliases_auth=True,
Expand All @@ -164,7 +169,7 @@ class RoomVersions:
V5 = RoomVersion(
"5",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=True,
Expand All @@ -182,7 +187,7 @@ class RoomVersions:
V6 = RoomVersion(
"6",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -200,7 +205,7 @@ class RoomVersions:
MSC2176 = RoomVersion(
"org.matrix.msc2176",
RoomDisposition.UNSTABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -218,7 +223,7 @@ class RoomVersions:
V7 = RoomVersion(
"7",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -236,7 +241,7 @@ class RoomVersions:
V8 = RoomVersion(
"8",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -254,7 +259,7 @@ class RoomVersions:
V9 = RoomVersion(
"9",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -272,7 +277,7 @@ class RoomVersions:
MSC3787 = RoomVersion(
"org.matrix.msc3787",
RoomDisposition.UNSTABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -290,7 +295,7 @@ class RoomVersions:
V10 = RoomVersion(
"10",
RoomDisposition.STABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand All @@ -308,7 +313,7 @@ class RoomVersions:
MSC2716v4 = RoomVersion(
"org.matrix.msc2716v4",
RoomDisposition.UNSTABLE,
EventFormatVersions.V3,
EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2,
enforce_key_validity=True,
special_case_aliases_auth=False,
Expand Down
4 changes: 2 additions & 2 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def validate_event_for_room_version(event: "EventBase") -> None:
if not is_invite_via_3pid:
raise AuthError(403, "Event not signed by sender's server")

if event.format_version in (EventFormatVersions.V1,):
if event.format_version in (EventFormatVersions.ROOM_V1_V2,):
# Only older room versions have event IDs to check.
event_id_domain = get_domain_from_id(event.event_id)

Expand Down Expand Up @@ -716,7 +716,7 @@ def check_redaction(
if user_level >= redact_level:
return False

if room_version_obj.event_format == EventFormatVersions.V1:
if room_version_obj.event_format == EventFormatVersions.ROOM_V1_V2:
redacter_domain = get_domain_from_id(event.event_id)
if not isinstance(event.redacts, str):
return False
Expand Down
12 changes: 6 additions & 6 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def __repr__(self) -> str:


class FrozenEvent(EventBase):
format_version = EventFormatVersions.V1 # All events of this type are V1
format_version = EventFormatVersions.ROOM_V1_V2 # All events of this type are V1

def __init__(
self,
Expand Down Expand Up @@ -490,7 +490,7 @@ def event_id(self) -> str:


class FrozenEventV2(EventBase):
format_version = EventFormatVersions.V2 # All events of this type are V2
format_version = EventFormatVersions.ROOM_V3 # All events of this type are V2

def __init__(
self,
Expand Down Expand Up @@ -567,7 +567,7 @@ def auth_event_ids(self) -> Sequence[str]:
class FrozenEventV3(FrozenEventV2):
"""FrozenEventV3, which differs from FrozenEventV2 only in the event_id format"""

format_version = EventFormatVersions.V3 # All events of this type are V3
format_version = EventFormatVersions.ROOM_V4_PLUS # All events of this type are V3

@property
def event_id(self) -> str:
Expand Down Expand Up @@ -597,11 +597,11 @@ def _event_type_from_format_version(
`FrozenEvent`
"""

if format_version == EventFormatVersions.V1:
if format_version == EventFormatVersions.ROOM_V1_V2:
return FrozenEvent
elif format_version == EventFormatVersions.V2:
elif format_version == EventFormatVersions.ROOM_V3:
return FrozenEventV2
elif format_version == EventFormatVersions.V3:
elif format_version == EventFormatVersions.ROOM_V4_PLUS:
return FrozenEventV3
else:
raise Exception("No event format %r" % (format_version,))
Expand Down
4 changes: 2 additions & 2 deletions synapse/events/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async def build(
# The types of auth/prev events changes between event versions.
prev_events: Union[List[str], List[Tuple[str, Dict[str, str]]]]
auth_events: Union[List[str], List[Tuple[str, Dict[str, str]]]]
if format_version == EventFormatVersions.V1:
if format_version == EventFormatVersions.ROOM_V1_V2:
auth_events = await self._store.add_event_hashes(auth_event_ids)
prev_events = await self._store.add_event_hashes(prev_event_ids)
else:
Expand Down Expand Up @@ -253,7 +253,7 @@ def create_local_event_from_event_dict(

time_now = int(clock.time_msec())

if format_version == EventFormatVersions.V1:
if format_version == EventFormatVersions.ROOM_V1_V2:
event_dict["event_id"] = _create_event_id(clock, hostname)

event_dict["origin"] = hostname
Expand Down
2 changes: 1 addition & 1 deletion synapse/events/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate_new(self, event: EventBase, config: HomeServerConfig) -> None:
"""
self.validate_builder(event)

if event.format_version == EventFormatVersions.V1:
if event.format_version == EventFormatVersions.ROOM_V1_V2:
EventID.from_string(event.event_id)

required = [
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def _check_sigs_on_pdu(
# event id's domain (normally only the case for joins/leaves), and add additional
# checks. Only do this if the room version has a concept of event ID domain
# (ie, the room version uses old-style non-hash event IDs).
if room_version.event_format == EventFormatVersions.V1:
if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
event_domain = get_domain_from_id(pdu.event_id)
if event_domain != sender_domain:
try:
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ async def _do_send_invite(
# Otherwise, consider it a legitimate error and raise.
err = e.to_synapse_error()
if self._is_unknown_endpoint(e, err):
if room_version.event_format != EventFormatVersions.V1:
if room_version.event_format != EventFormatVersions.ROOM_V1_V2:
raise SynapseError(
400,
"User's homeserver does not support this room version",
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ async def prune_staged_events_in_room(
logger.info("Invalid prev_events for %s", event_id)
continue

if room_version.event_format == EventFormatVersions.V1:
if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
for prev_event_tuple in prev_events:
if (
not isinstance(prev_event_tuple, list)
Expand Down
6 changes: 3 additions & 3 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ async def _fetch_event_ids_and_get_outstanding_redactions(
if format_version is None:
# This means that we stored the event before we had the concept
# of a event format version, so it must be a V1 event.
format_version = EventFormatVersions.V1
format_version = EventFormatVersions.ROOM_V1_V2

room_version_id = row.room_version_id

Expand Down Expand Up @@ -1186,10 +1186,10 @@ async def _fetch_event_ids_and_get_outstanding_redactions(
#
# So, the following approximations should be adequate.

if format_version == EventFormatVersions.V1:
if format_version == EventFormatVersions.ROOM_V1_V2:
# if it's event format v1 then it must be room v1 or v2
room_version = RoomVersions.V1
elif format_version == EventFormatVersions.V2:
elif format_version == EventFormatVersions.ROOM_V3:
# if it's event format v2 then it must be room v3
room_version = RoomVersions.V3
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/databases/main/test_events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _populate_events(self) -> None:
"room_id": self.room_id,
"json": json.dumps(event_json),
"internal_metadata": "{}",
"format_version": EventFormatVersions.V3,
"format_version": EventFormatVersions.ROOM_V4_PLUS,
},
)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def test_prune_inbound_federation_queue(self, room_version: RoomVersion):

def prev_event_format(prev_event_id: str) -> Union[Tuple[str, dict], str]:
"""Account for differences in prev_events format across room versions"""
if room_version.event_format == EventFormatVersions.V1:
if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
return prev_event_id, {}

return prev_event_id
Expand Down
4 changes: 2 additions & 2 deletions tests/test_event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def _alias_event(room_version: RoomVersion, sender: str, **kwargs) -> EventBase:
def _build_auth_dict_for_room_version(
room_version: RoomVersion, auth_events: Iterable[EventBase]
) -> List:
if room_version.event_format == EventFormatVersions.V1:
if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
return [(e.event_id, "not_used") for e in auth_events]
else:
return [e.event_id for e in auth_events]
Expand Down Expand Up @@ -871,7 +871,7 @@ def _join_rules_event(

def _maybe_get_event_id_dict_for_room_version(room_version: RoomVersion) -> dict:
"""If this room version needs it, generate an event id"""
if room_version.event_format != EventFormatVersions.V1:
if room_version.event_format != EventFormatVersions.ROOM_V1_V2:
return {}

global event_count
Expand Down