From c02f1153069d6abae931e46d2351c53ecc8b185a Mon Sep 17 00:00:00 2001
From: Patrick Cloke <patrickc@matrix.org>
Date: Fri, 7 Apr 2023 10:17:21 -0400
Subject: Linearized Matrix events do not have a depth.

---
 synapse/events/__init__.py            |  3 +++
 synapse/events/utils.py               |  3 ++-
 synapse/federation/federation_base.py | 25 +++++++++++++++----------
 synapse/handlers/federation.py        |  4 +++-
 4 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py
index aa48cd2236..d628abc0a6 100644
--- a/synapse/events/__init__.py
+++ b/synapse/events/__init__.py
@@ -605,6 +605,9 @@ class FrozenLinearizedEvent(FrozenEventV3):
 
     format_version = EventFormatVersions.LINEARIZED
 
+    # TODO(LM): Do we re-calculate depth at some point?
+    depth = 0  # type: ignore[assignment]
+
     @property
     def pdu_domain(self) -> str:
         """The domain which added this event to the DAG.
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index 4f6f075290..ff172d66b4 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -103,7 +103,6 @@ def prune_event_dict(room_version: RoomVersion, event_dict: JsonDict) -> JsonDic
         "content",
         "type",
         "state_key",
-        "depth",
         "prev_events",
         "auth_events",
         "origin_server_ts",
@@ -115,6 +114,8 @@ def prune_event_dict(room_version: RoomVersion, event_dict: JsonDict) -> JsonDic
     # The hub server should not be redacted for linear matrix.
     if room_version.linearized_matrix:
         allowed_keys.append("hub_server")
+    else:
+        allowed_keys.append("depth")
 
     # Room versions before MSC3989 kept the origin field.
     if not room_version.msc3989_redaction_rules:
diff --git a/synapse/federation/federation_base.py b/synapse/federation/federation_base.py
index 50213a8237..a0c5048efc 100644
--- a/synapse/federation/federation_base.py
+++ b/synapse/federation/federation_base.py
@@ -295,21 +295,26 @@ def event_from_pdu_json(pdu_json: JsonDict, room_version: RoomVersion) -> EventB
     """
     # we could probably enforce a bunch of other fields here (room_id, sender,
     # origin, etc etc)
-    assert_params_in_dict(pdu_json, ("type", "depth"))
+    if room_version.event_format == EventFormatVersions.LINEARIZED:
+        assert_params_in_dict(pdu_json, ("type",))
+    else:
+        assert_params_in_dict(pdu_json, ("type", "depth"))
+
+        depth = pdu_json["depth"]
+        if type(depth) is not int:
+            raise SynapseError(
+                400, "Depth %r not an integer" % (depth,), Codes.BAD_JSON
+            )
+
+        if depth < 0:
+            raise SynapseError(400, "Depth too small", Codes.BAD_JSON)
+        elif depth > MAX_DEPTH:
+            raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
 
     # Strip any unauthorized values from "unsigned" if they exist
     if "unsigned" in pdu_json:
         _strip_unsigned_values(pdu_json)
 
-    depth = pdu_json["depth"]
-    if type(depth) is not int:
-        raise SynapseError(400, "Depth %r not an intger" % (depth,), Codes.BAD_JSON)
-
-    if depth < 0:
-        raise SynapseError(400, "Depth too small", Codes.BAD_JSON)
-    elif depth > MAX_DEPTH:
-        raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
-
     # Validate that the JSON conforms to the specification.
     if room_version.strict_canonicaljson:
         validate_canonicaljson(pdu_json)
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index cc5ed97730..d90a14788d 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -662,7 +662,9 @@ class FederationHandler:
                 origin = ret.origin
                 state = ret.state
                 auth_chain = ret.auth_chain
-                auth_chain.sort(key=lambda e: e.depth)
+                # TODO(LM) Assume the auth chain is reasonable ordered.
+                if not room_version_obj.linearized_matrix:
+                    auth_chain.sort(key=lambda e: e.depth)
 
                 logger.debug("do_invite_join auth_chain: %s", auth_chain)
                 logger.debug("do_invite_join state: %s", state)
-- 
cgit 1.4.1