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

This commit is contained in:
reivilibre 2022-09-07 10:08:20 +00:00 committed by GitHub
parent bb5b47b62a
commit c2fe48a6ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 47 additions and 41 deletions

1
changelog.d/13706.misc Normal file
View File

@ -0,0 +1 @@
Rename the `EventFormatVersions` enum values so that they line up with room version numbers.

View File

@ -19,18 +19,23 @@ import attr
class EventFormatVersions: class EventFormatVersions:
"""This is an internal enum for tracking the version of the event format, """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 ROOM_V1_V2 = 1 # $id:server event id format: used for room v1 and v2
V2 = 2 # MSC1659-style $hash event id format: introduced for room v3 ROOM_V3 = 2 # MSC1659-style $hash event id format: used for room v3
V3 = 3 # MSC1884-style $hash format: introduced for room v4 ROOM_V4_PLUS = 3 # MSC1884-style $hash format: introduced for room v4
KNOWN_EVENT_FORMAT_VERSIONS = { KNOWN_EVENT_FORMAT_VERSIONS = {
EventFormatVersions.V1, EventFormatVersions.ROOM_V1_V2,
EventFormatVersions.V2, EventFormatVersions.ROOM_V3,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
} }
@ -92,7 +97,7 @@ class RoomVersions:
V1 = RoomVersion( V1 = RoomVersion(
"1", "1",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V1, EventFormatVersions.ROOM_V1_V2,
StateResolutionVersions.V1, StateResolutionVersions.V1,
enforce_key_validity=False, enforce_key_validity=False,
special_case_aliases_auth=True, special_case_aliases_auth=True,
@ -110,7 +115,7 @@ class RoomVersions:
V2 = RoomVersion( V2 = RoomVersion(
"2", "2",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V1, EventFormatVersions.ROOM_V1_V2,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=False, enforce_key_validity=False,
special_case_aliases_auth=True, special_case_aliases_auth=True,
@ -128,7 +133,7 @@ class RoomVersions:
V3 = RoomVersion( V3 = RoomVersion(
"3", "3",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V2, EventFormatVersions.ROOM_V3,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=False, enforce_key_validity=False,
special_case_aliases_auth=True, special_case_aliases_auth=True,
@ -146,7 +151,7 @@ class RoomVersions:
V4 = RoomVersion( V4 = RoomVersion(
"4", "4",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=False, enforce_key_validity=False,
special_case_aliases_auth=True, special_case_aliases_auth=True,
@ -164,7 +169,7 @@ class RoomVersions:
V5 = RoomVersion( V5 = RoomVersion(
"5", "5",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=True, special_case_aliases_auth=True,
@ -182,7 +187,7 @@ class RoomVersions:
V6 = RoomVersion( V6 = RoomVersion(
"6", "6",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -200,7 +205,7 @@ class RoomVersions:
MSC2176 = RoomVersion( MSC2176 = RoomVersion(
"org.matrix.msc2176", "org.matrix.msc2176",
RoomDisposition.UNSTABLE, RoomDisposition.UNSTABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -218,7 +223,7 @@ class RoomVersions:
V7 = RoomVersion( V7 = RoomVersion(
"7", "7",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -236,7 +241,7 @@ class RoomVersions:
V8 = RoomVersion( V8 = RoomVersion(
"8", "8",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -254,7 +259,7 @@ class RoomVersions:
V9 = RoomVersion( V9 = RoomVersion(
"9", "9",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -272,7 +277,7 @@ class RoomVersions:
MSC3787 = RoomVersion( MSC3787 = RoomVersion(
"org.matrix.msc3787", "org.matrix.msc3787",
RoomDisposition.UNSTABLE, RoomDisposition.UNSTABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -290,7 +295,7 @@ class RoomVersions:
V10 = RoomVersion( V10 = RoomVersion(
"10", "10",
RoomDisposition.STABLE, RoomDisposition.STABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,
@ -308,7 +313,7 @@ class RoomVersions:
MSC2716v4 = RoomVersion( MSC2716v4 = RoomVersion(
"org.matrix.msc2716v4", "org.matrix.msc2716v4",
RoomDisposition.UNSTABLE, RoomDisposition.UNSTABLE,
EventFormatVersions.V3, EventFormatVersions.ROOM_V4_PLUS,
StateResolutionVersions.V2, StateResolutionVersions.V2,
enforce_key_validity=True, enforce_key_validity=True,
special_case_aliases_auth=False, special_case_aliases_auth=False,

View File

@ -109,7 +109,7 @@ def validate_event_for_room_version(event: "EventBase") -> None:
if not is_invite_via_3pid: if not is_invite_via_3pid:
raise AuthError(403, "Event not signed by sender's server") 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. # Only older room versions have event IDs to check.
event_id_domain = get_domain_from_id(event.event_id) event_id_domain = get_domain_from_id(event.event_id)
@ -716,7 +716,7 @@ def check_redaction(
if user_level >= redact_level: if user_level >= redact_level:
return False 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) redacter_domain = get_domain_from_id(event.event_id)
if not isinstance(event.redacts, str): if not isinstance(event.redacts, str):
return False return False

View File

@ -442,7 +442,7 @@ class EventBase(metaclass=abc.ABCMeta):
class FrozenEvent(EventBase): 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__( def __init__(
self, self,
@ -490,7 +490,7 @@ class FrozenEvent(EventBase):
class FrozenEventV2(EventBase): 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__( def __init__(
self, self,
@ -567,7 +567,7 @@ class FrozenEventV2(EventBase):
class FrozenEventV3(FrozenEventV2): class FrozenEventV3(FrozenEventV2):
"""FrozenEventV3, which differs from FrozenEventV2 only in the event_id format""" """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 @property
def event_id(self) -> str: def event_id(self) -> str:
@ -597,11 +597,11 @@ def _event_type_from_format_version(
`FrozenEvent` `FrozenEvent`
""" """
if format_version == EventFormatVersions.V1: if format_version == EventFormatVersions.ROOM_V1_V2:
return FrozenEvent return FrozenEvent
elif format_version == EventFormatVersions.V2: elif format_version == EventFormatVersions.ROOM_V3:
return FrozenEventV2 return FrozenEventV2
elif format_version == EventFormatVersions.V3: elif format_version == EventFormatVersions.ROOM_V4_PLUS:
return FrozenEventV3 return FrozenEventV3
else: else:
raise Exception("No event format %r" % (format_version,)) raise Exception("No event format %r" % (format_version,))

View File

@ -137,7 +137,7 @@ class EventBuilder:
# The types of auth/prev events changes between event versions. # The types of auth/prev events changes between event versions.
prev_events: Union[List[str], List[Tuple[str, Dict[str, str]]]] prev_events: Union[List[str], List[Tuple[str, Dict[str, str]]]]
auth_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) auth_events = await self._store.add_event_hashes(auth_event_ids)
prev_events = await self._store.add_event_hashes(prev_event_ids) prev_events = await self._store.add_event_hashes(prev_event_ids)
else: else:
@ -253,7 +253,7 @@ def create_local_event_from_event_dict(
time_now = int(clock.time_msec()) 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["event_id"] = _create_event_id(clock, hostname)
event_dict["origin"] = hostname event_dict["origin"] = hostname

View File

@ -45,7 +45,7 @@ class EventValidator:
""" """
self.validate_builder(event) self.validate_builder(event)
if event.format_version == EventFormatVersions.V1: if event.format_version == EventFormatVersions.ROOM_V1_V2:
EventID.from_string(event.event_id) EventID.from_string(event.event_id)
required = [ required = [

View File

@ -194,7 +194,7 @@ async def _check_sigs_on_pdu(
# event id's domain (normally only the case for joins/leaves), and add additional # 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 # 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). # (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) event_domain = get_domain_from_id(pdu.event_id)
if event_domain != sender_domain: if event_domain != sender_domain:
try: try:

View File

@ -1190,7 +1190,7 @@ class FederationClient(FederationBase):
# Otherwise, consider it a legitimate error and raise. # Otherwise, consider it a legitimate error and raise.
err = e.to_synapse_error() err = e.to_synapse_error()
if self._is_unknown_endpoint(e, err): 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( raise SynapseError(
400, 400,
"User's homeserver does not support this room version", "User's homeserver does not support this room version",

View File

@ -1606,7 +1606,7 @@ class EventFederationWorkerStore(SignatureWorkerStore, EventsWorkerStore, SQLBas
logger.info("Invalid prev_events for %s", event_id) logger.info("Invalid prev_events for %s", event_id)
continue continue
if room_version.event_format == EventFormatVersions.V1: if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
for prev_event_tuple in prev_events: for prev_event_tuple in prev_events:
if ( if (
not isinstance(prev_event_tuple, list) not isinstance(prev_event_tuple, list)

View File

@ -1156,7 +1156,7 @@ class EventsWorkerStore(SQLBaseStore):
if format_version is None: if format_version is None:
# This means that we stored the event before we had the concept # This means that we stored the event before we had the concept
# of a event format version, so it must be a V1 event. # 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 room_version_id = row.room_version_id
@ -1186,10 +1186,10 @@ class EventsWorkerStore(SQLBaseStore):
# #
# So, the following approximations should be adequate. # 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 # if it's event format v1 then it must be room v1 or v2
room_version = RoomVersions.V1 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 # if it's event format v2 then it must be room v3
room_version = RoomVersions.V3 room_version = RoomVersions.V3
else: else:

View File

@ -254,7 +254,7 @@ class DatabaseOutageTestCase(unittest.HomeserverTestCase):
"room_id": self.room_id, "room_id": self.room_id,
"json": json.dumps(event_json), "json": json.dumps(event_json),
"internal_metadata": "{}", "internal_metadata": "{}",
"format_version": EventFormatVersions.V3, "format_version": EventFormatVersions.ROOM_V4_PLUS,
}, },
) )
) )

View File

@ -513,7 +513,7 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase):
def prev_event_format(prev_event_id: str) -> Union[Tuple[str, dict], str]: def prev_event_format(prev_event_id: str) -> Union[Tuple[str, dict], str]:
"""Account for differences in prev_events format across room versions""" """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, {}
return prev_event_id return prev_event_id

View File

@ -821,7 +821,7 @@ def _alias_event(room_version: RoomVersion, sender: str, **kwargs) -> EventBase:
def _build_auth_dict_for_room_version( def _build_auth_dict_for_room_version(
room_version: RoomVersion, auth_events: Iterable[EventBase] room_version: RoomVersion, auth_events: Iterable[EventBase]
) -> List: ) -> 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] return [(e.event_id, "not_used") for e in auth_events]
else: else:
return [e.event_id for e in auth_events] return [e.event_id for e in auth_events]
@ -871,7 +871,7 @@ event_count = 0
def _maybe_get_event_id_dict_for_room_version(room_version: RoomVersion) -> dict: def _maybe_get_event_id_dict_for_room_version(room_version: RoomVersion) -> dict:
"""If this room version needs it, generate an event id""" """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 {} return {}
global event_count global event_count