From acd144e93413dd509e2d75bdb381243d5d8187b8 Mon Sep 17 00:00:00 2001
From: Devin Ragotzy <devin.ragotzy@gmail.com>
Date: Sun, 8 Nov 2020 13:54:59 -0500
Subject: [PATCH] Fix get_closest_parent and cleanup federation/send/:txn

---
 Cargo.lock                      | 286 +++++---------------------------
 Cargo.toml                      |  12 +-
 src/client_server/membership.rs |   4 +-
 src/database/rooms.rs           |   5 +-
 src/server_server.rs            |  81 +++------
 src/utils.rs                    |  15 --
 6 files changed, 69 insertions(+), 334 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 3f8703e0..d2eef800 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -221,7 +221,7 @@ dependencies = [
  "reqwest",
  "ring",
  "rocket",
- "ruma 0.0.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma",
  "rust-argon2",
  "serde",
  "serde_json",
@@ -1611,33 +1611,15 @@ source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a
 dependencies = [
  "assign",
  "js_int",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-appservice-api 0.2.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-client-api 0.10.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-federation-api 0.0.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-signatures 0.6.0-dev.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
-]
-
-[[package]]
-name = "ruma"
-version = "0.0.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "assign",
- "js_int",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-appservice-api 0.2.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-client-api 0.10.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-federation-api 0.0.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-signatures 0.6.0-dev.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-api",
+ "ruma-appservice-api",
+ "ruma-client-api",
+ "ruma-common",
+ "ruma-events",
+ "ruma-federation-api",
+ "ruma-identifiers",
+ "ruma-serde",
+ "ruma-signatures",
 ]
 
 [[package]]
@@ -1647,27 +1629,10 @@ source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a
 dependencies = [
  "http",
  "percent-encoding",
- "ruma-api-macros 0.17.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
- "strum",
- "thiserror",
-]
-
-[[package]]
-name = "ruma-api"
-version = "0.17.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "http",
- "percent-encoding",
- "ruma-api-macros 0.17.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-api-macros",
+ "ruma-common",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde",
  "serde_json",
  "strum",
@@ -1685,39 +1650,15 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "ruma-api-macros"
-version = "0.17.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "proc-macro-crate",
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "ruma-appservice-api"
 version = "0.2.0-alpha.1"
 source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889#c15382ca41262058302959eac4029ab4a1ea5889"
 dependencies = [
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
-]
-
-[[package]]
-name = "ruma-appservice-api"
-version = "0.2.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-api",
+ "ruma-common",
+ "ruma-events",
+ "ruma-identifiers",
  "serde",
  "serde_json",
 ]
@@ -1732,31 +1673,11 @@ dependencies = [
  "js_int",
  "maplit",
  "percent-encoding",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
- "strum",
-]
-
-[[package]]
-name = "ruma-client-api"
-version = "0.10.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "assign",
- "http",
- "js_int",
- "maplit",
- "percent-encoding",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-api",
+ "ruma-common",
+ "ruma-events",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde",
  "serde_json",
  "strum",
@@ -1768,23 +1689,9 @@ version = "0.2.0"
 source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889#c15382ca41262058302959eac4029ab4a1ea5889"
 dependencies = [
  "js_int",
- "ruma-common-macros 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
- "strum",
-]
-
-[[package]]
-name = "ruma-common"
-version = "0.2.0"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "js_int",
- "ruma-common-macros 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-common-macros",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde",
  "serde_json",
  "strum",
@@ -1801,42 +1708,16 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "ruma-common-macros"
-version = "0.2.0"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "proc-macro-crate",
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "ruma-events"
 version = "0.22.0-alpha.1"
 source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889#c15382ca41262058302959eac4029ab4a1ea5889"
 dependencies = [
  "js_int",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-events-macros 0.22.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
- "strum",
-]
-
-[[package]]
-name = "ruma-events"
-version = "0.22.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "js_int",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-events-macros 0.22.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-common",
+ "ruma-events-macros",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde",
  "serde_json",
  "strum",
@@ -1853,43 +1734,17 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "ruma-events-macros"
-version = "0.22.0-alpha.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "proc-macro-crate",
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "ruma-federation-api"
 version = "0.0.3"
 source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889#c15382ca41262058302959eac4029ab4a1ea5889"
 dependencies = [
  "js_int",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-common 0.2.0 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "serde_json",
-]
-
-[[package]]
-name = "ruma-federation-api"
-version = "0.0.3"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "js_int",
- "ruma-api 0.17.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-common 0.2.0 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-events 0.22.0-alpha.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-api",
+ "ruma-common",
+ "ruma-events",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde",
  "serde_json",
 ]
@@ -1898,23 +1753,11 @@ dependencies = [
 name = "ruma-identifiers"
 version = "0.17.4"
 source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889#c15382ca41262058302959eac4029ab4a1ea5889"
-dependencies = [
- "ruma-identifiers-macros 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-identifiers-validation 0.1.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde",
- "strum",
-]
-
-[[package]]
-name = "ruma-identifiers"
-version = "0.17.4"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
 dependencies = [
  "rand",
- "ruma-identifiers-macros 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-identifiers-validation 0.1.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-identifiers-macros",
+ "ruma-identifiers-validation",
+ "ruma-serde",
  "serde",
  "strum",
 ]
@@ -1926,18 +1769,7 @@ source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a
 dependencies = [
  "proc-macro2",
  "quote",
- "ruma-identifiers-validation 0.1.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "syn",
-]
-
-[[package]]
-name = "ruma-identifiers-macros"
-version = "0.17.4"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "proc-macro2",
- "quote",
- "ruma-identifiers-validation 0.1.1 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-identifiers-validation",
  "syn",
 ]
 
@@ -1950,15 +1782,6 @@ dependencies = [
  "strum",
 ]
 
-[[package]]
-name = "ruma-identifiers-validation"
-version = "0.1.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "serde",
- "strum",
-]
-
 [[package]]
 name = "ruma-serde"
 version = "0.2.3"
@@ -1971,18 +1794,6 @@ dependencies = [
  "serde_json",
 ]
 
-[[package]]
-name = "ruma-serde"
-version = "0.2.3"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "form_urlencoded",
- "itoa",
- "js_int",
- "serde",
- "serde_json",
-]
-
 [[package]]
 name = "ruma-signatures"
 version = "0.6.0-dev.1"
@@ -1990,21 +1801,8 @@ source = "git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a
 dependencies = [
  "base64",
  "ring",
- "ruma-identifiers 0.17.4 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
- "serde_json",
- "untrusted",
-]
-
-[[package]]
-name = "ruma-signatures"
-version = "0.6.0-dev.1"
-source = "git+https://github.com/DevinR528/ruma?branch=unstable-join#424b138d84ccc47c0b212708a54f66bf88f7d57a"
-dependencies = [
- "base64",
- "ring",
- "ruma-identifiers 0.17.4 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
- "ruma-serde 0.2.3 (git+https://github.com/DevinR528/ruma?branch=unstable-join)",
+ "ruma-identifiers",
+ "ruma-serde",
  "serde_json",
  "untrusted",
 ]
@@ -2263,7 +2061,7 @@ dependencies = [
  "itertools",
  "js_int",
  "maplit",
- "ruma 0.0.1 (git+https://github.com/ruma/ruma?rev=c15382ca41262058302959eac4029ab4a1ea5889)",
+ "ruma",
  "serde",
  "serde_json",
  "thiserror",
diff --git a/Cargo.toml b/Cargo.toml
index b72b92f8..e7b87fa1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,8 +18,8 @@ edition = "2018"
 rocket = { git = "https://github.com/timokoesters/Rocket.git", branch = "empty_parameters", default-features = false, features = ["tls"] }
 
 # Used for matrix spec type definitions and helpers
-#ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "aff914050eb297bd82b8aafb12158c88a9e480e1" }
-ruma = { git = "https://github.com/DevinR528/ruma", features = ["rand", "client-api", "federation-api", "unstable-exhaustive-types", "unstable-pre-spec", "unstable-synapse-quirks"], branch = "unstable-join" }
+ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks", "unstable-exhaustive-types"], rev = "c15382ca41262058302959eac4029ab4a1ea5889" }
+# ruma = { git = "https://github.com/DevinR528/ruma", features = ["rand", "client-api", "federation-api", "unstable-exhaustive-types", "unstable-pre-spec", "unstable-synapse-quirks"], branch = "unstable-join" }
 # ruma = { path = "../ruma/ruma", features = ["unstable-exhaustive-types", "rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"] }
 
 # Used when doing state resolution
@@ -73,11 +73,3 @@ required-features = ["conduit_bin"]
 [lib]
 name = "conduit"
 path = "src/lib.rs"
-
-# [patch."https://github.com/timokoesters/ruma"]
-# ruma = { path = "../ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-exhaustive-types", "unstable-pre-spec", "unstable-synapse-quirks"] }
-# # ruma = { git = "https://github.com/ruma/ruma", rev = "64b9c646d15a359d62ab464a95176ff94adb2554", features = ["rand", "client-api", "federation-api", "unstable-exhaustive-types", "unstable-pre-spec", "unstable-synapse-quirks"]}
-
-# [patch."https://github.com/ruma/state-res"]
-# state-res = { path = "../../state-res", features = ["unstable-pre-spec", "gen-eventid"] }
-# # state-res = { git = "https://github.com/ruma/state-res", branch = "timo-spec-comp" }
diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs
index 5d621fa5..a07fe727 100644
--- a/src/client_server/membership.rs
+++ b/src/client_server/membership.rs
@@ -515,9 +515,7 @@ async fn join_room_by_id_helper(
         let mut canon_json_stub: BTreeMap<_, ruma::signatures::CanonicalJsonValue> =
             serde_json::from_value(join_event_stub_value).expect("json Value is canonical JSON");
 
-        // We don't leave the event id into the pdu because that's only allowed in v1 or v2 rooms
-        // let join_event_stub = join_event_stub_value.as_object_mut().unwrap();
-        // join_event_stub.remove("event_id");
+        // We don't leave the event id in the pdu because that's only allowed in v1 or v2 rooms
         canon_json_stub.remove("event_id");
 
         // In order to create a compatible ref hash (EventID) the `hashes` field needs to be present
diff --git a/src/database/rooms.rs b/src/database/rooms.rs
index 28e1f608..08734b1b 100644
--- a/src/database/rooms.rs
+++ b/src/database/rooms.rs
@@ -405,11 +405,12 @@ impl Rooms {
 
     pub fn get_closest_parent(
         &self,
+        room: &RoomId,
         incoming_prev_ids: &[EventId],
         their_state: &BTreeMap<EventId, Arc<StateEvent>>,
     ) -> Result<Option<ClosestParent>> {
-        match self.pduid_pdu.last()? {
-            Some(val)
+        match self.pduid_pdu.scan_prefix(room.as_bytes()).last() {
+            Some(Ok(val))
                 if incoming_prev_ids.contains(
                     &serde_json::from_slice::<PduEvent>(&val.1)
                         .map_err(|_| {
diff --git a/src/server_server.rs b/src/server_server.rs
index ccdeb749..c865b028 100644
--- a/src/server_server.rs
+++ b/src/server_server.rs
@@ -418,6 +418,7 @@ pub async fn send_transaction_message_route<'a>(
             }
         }
     }
+
     // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we?
     // SPEC:
     // Servers MUST strictly enforce the JSON format specified in the appendices.
@@ -427,42 +428,20 @@ pub async fn send_transaction_message_route<'a>(
     // would return a M_BAD_JSON error.
     let mut resolved_map = BTreeMap::new();
     for pdu in &body.pdus {
-        println!("LOOP");
         let (event_id, value) = process_incoming_pdu(pdu);
         let pdu = serde_json::from_value::<PduEvent>(value.clone())
             .expect("all ruma pdus are conduit pdus");
         let room_id = &pdu.room_id;
 
-        if value.get("state_key").is_none() {
-            if !db.rooms.is_joined(&pdu.sender, &pdu.room_id)? {
-                // TODO: auth rules apply to all events, not only those with a state key
-                log::error!("Unauthorized {}", pdu.kind);
-
-                resolved_map.insert(event_id, Err("User is not in this room".into()));
-                continue;
-            }
-
-            // TODO: We should be doing the same get_closest_parent thing here too?
-            // same as for state events ~100 lines down
-            let count = db.globals.next_count()?;
-            let mut pdu_id = pdu.room_id.as_bytes().to_vec();
-            pdu_id.push(0xff);
-            pdu_id.extend_from_slice(&count.to_be_bytes());
-            db.rooms.append_pdu(
-                &pdu,
-                &value,
-                count,
-                pdu_id.into(),
-                &db.globals,
-                &db.account_data,
-                &db.admin,
-            )?;
-
-            resolved_map.insert(event_id, Ok::<(), String>(()));
+        // If we have no idea about this room
+        // TODO: Does a server only send us events that we should know about or
+        // when everyone on this server leaves a room can we ignore those events?
+        if !db.rooms.exists(&pdu.room_id)? {
+            log::error!("Room does not exist on this server");
+            resolved_map.insert(event_id, Err("Room is unknown to this server".into()));
             continue;
         }
 
-        let now = std::time::Instant::now();
         let get_state_response = match send_request(
             &db.globals,
             body.body.origin.clone(),
@@ -482,11 +461,9 @@ pub async fn send_transaction_message_route<'a>(
             Err(err) => {
                 log::error!("Request failed: {}", err);
                 resolved_map.insert(event_id, Err(err.to_string()));
-                dbg!(now.elapsed());
                 continue;
             }
         };
-        dbg!(now.elapsed());
 
         let their_current_state = get_state_response
             .pdus
@@ -509,40 +486,21 @@ pub async fn send_transaction_message_route<'a>(
 
         if value.get("state_key").is_none() {
             if !db.rooms.is_joined(&pdu.sender, &pdu.room_id)? {
-                // TODO: auth rules apply to all events, not only those with a state key
                 log::error!("Sender is not joined {}", pdu.kind);
-
                 resolved_map.insert(event_id, Err("User is not in this room".into()));
                 continue;
             }
 
-            // // TODO: We should be doing the same get_closest_parent thing here too?
-            // // same as for state events ~100 lines down
-            // let count = db.globals.next_count()?;
-            // let mut pdu_id = pdu.room_id.as_bytes().to_vec();
-            // pdu_id.push(0xff);
-            // pdu_id.extend_from_slice(&count.to_be_bytes());
-            // db.rooms.append_pdu(
-            //     &pdu,
-            //     &value,
-            //     count,
-            //     pdu_id.into(),
-            //     &db.globals,
-            //     &db.account_data,
-            //     &db.sending,
-            // )?;
-
             // If the event is older than the last event in pduid_pdu Tree then find the
             // closest ancestor we know of and insert after the known ancestor by
             // altering the known events pduid to = same roomID + same count bytes + 0x1
             // pushing a single byte every time a simple append cannot be done.
             match db
                 .rooms
-                .get_closest_parent(&pdu.prev_events, &their_current_state)?
+                .get_closest_parent(room_id, &pdu.prev_events, &their_current_state)?
             {
                 Some(ClosestParent::Append) => {
                     let count = db.globals.next_count()?;
-                    dbg!(&count);
                     let mut pdu_id = room_id.as_bytes().to_vec();
                     pdu_id.push(0xff);
                     pdu_id.extend_from_slice(&count.to_be_bytes());
@@ -554,10 +512,12 @@ pub async fn send_transaction_message_route<'a>(
                         pdu_id.into(),
                         &db.globals,
                         &db.account_data,
-                        &db.sending,
+                        &db.admin,
                     )?;
                 }
                 Some(ClosestParent::Insert(old_count)) => {
+                    println!("INSERT PDU FOUND {}", old_count);
+
                     let count = old_count;
                     let mut pdu_id = room_id.as_bytes().to_vec();
                     pdu_id.push(0xff);
@@ -573,7 +533,7 @@ pub async fn send_transaction_message_route<'a>(
                         pdu_id.into(),
                         &db.globals,
                         &db.account_data,
-                        &db.sending,
+                        &db.admin,
                     )?;
                 }
                 _ => panic!("Not a sequential event or no parents found"),
@@ -615,13 +575,13 @@ pub async fn send_transaction_message_route<'a>(
                 // closest ancestor we know of and insert after the known ancestor by
                 // altering the known events pduid to = same roomID + same count bytes + 0x1
                 // pushing a single byte every time a simple append cannot be done.
-                match db
-                    .rooms
-                    .get_closest_parent(&pdu.prev_events, &their_current_state)?
-                {
+                match db.rooms.get_closest_parent(
+                    room_id,
+                    &pdu.prev_events,
+                    &their_current_state,
+                )? {
                     Some(ClosestParent::Append) => {
                         let count = db.globals.next_count()?;
-                        dbg!(&count);
                         let mut pdu_id = room_id.as_bytes().to_vec();
                         pdu_id.push(0xff);
                         pdu_id.extend_from_slice(&count.to_be_bytes());
@@ -633,11 +593,12 @@ pub async fn send_transaction_message_route<'a>(
                             pdu_id.into(),
                             &db.globals,
                             &db.account_data,
-                            &db.sending,
+                            &db.admin,
                         )?;
                     }
                     Some(ClosestParent::Insert(old_count)) => {
-                        println!("INSERT PDU FOUND {}", old_count);
+                        println!("INSERT STATE PDU FOUND {}", old_count);
+
                         let count = old_count;
                         let mut pdu_id = room_id.as_bytes().to_vec();
                         pdu_id.push(0xff);
@@ -653,7 +614,7 @@ pub async fn send_transaction_message_route<'a>(
                             pdu_id.into(),
                             &db.globals,
                             &db.account_data,
-                            &db.sending,
+                            &db.admin,
                         )?;
                     }
                     _ => panic!("Not a sequential event or no parents found"),
diff --git a/src/utils.rs b/src/utils.rs
index e65ec864..edcf48a1 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -94,18 +94,3 @@ pub fn common_elements(
             .all(|b| b)
     }))
 }
-
-#[test]
-fn sled_tests() {
-    let db = sled::Config::new().temporary(true).open().unwrap();
-
-    db.insert(1_u64.to_be_bytes(), vec![10]).unwrap();
-    db.insert(2_u64.to_be_bytes(), vec![20]).unwrap();
-    db.insert(3_u64.to_be_bytes(), vec![30]).unwrap();
-
-    let mut key = 1_u64.to_be_bytes().to_vec();
-    key.push(1);
-    db.insert(key, vec![40]).unwrap();
-
-    println!("{:?}", db.iter().collect::<Result<Vec<_>, _>>().unwrap())
-}