summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2018-11-28 16:20:21 +0000
committerBrendan Abolivier <babolivier@matrix.org>2019-02-13 20:54:34 +0000
commit0d7ec185cbca5d90756a6032631b34d079d1d4fd (patch)
tree2390fae54e9f4f5b4d92f3013f1ebf4228466e96
parentMangle some more PDU fields (diff)
downloadsynapse-0d7ec185cbca5d90756a6032631b34d079d1d4fd.tar.xz
Make event_ids smaller
-rw-r--r--synapse/events/builder.py22
-rw-r--r--synapse/federation/federation_base.py10
-rw-r--r--synapse/federation/units.py3
-rw-r--r--synapse/handlers/federation.py1
4 files changed, 31 insertions, 5 deletions
diff --git a/synapse/events/builder.py b/synapse/events/builder.py
index 03ea2ef14f..481eb2d375 100644
--- a/synapse/events/builder.py
+++ b/synapse/events/builder.py
@@ -14,9 +14,9 @@
 # limitations under the License.
 
 import copy
+import string
 
 from synapse.types import EventID
-from synapse.util.stringutils import random_string
 
 from . import EventBase, FrozenEvent, _event_dict_property
 
@@ -49,10 +49,10 @@ class EventBuilderFactory(object):
         self.event_id_count = 0
 
     def create_event_id(self):
-        i = str(self.event_id_count)
+        i = self.event_id_count
         self.event_id_count += 1
 
-        local_part = random_string(3) + str(i)
+        local_part = _encode_id(i)
 
         e_id = EventID(local_part, self.hostname)
 
@@ -73,3 +73,19 @@ class EventBuilderFactory(object):
         key_values["signatures"] = {}
 
         return EventBuilder(key_values=key_values,)
+
+
+def _numberToBase(n, b):
+    if n == 0:
+        return [0]
+    digits = []
+    while n:
+        digits.append(int(n % b))
+        n //= b
+    return digits[::-1]
+
+
+def _encode_id(i):
+    digits = string.digits + string.ascii_letters
+    val_slice = _numberToBase(i, len(digits))
+    return "".join(digits[x] for x in val_slice)
diff --git a/synapse/federation/federation_base.py b/synapse/federation/federation_base.py
index 758512bd86..6593600247 100644
--- a/synapse/federation/federation_base.py
+++ b/synapse/federation/federation_base.py
@@ -26,7 +26,7 @@ from synapse.crypto.event_signing import check_event_content_hash
 from synapse.events import FrozenEvent
 from synapse.events.utils import prune_event
 from synapse.http.servlet import assert_params_in_dict
-from synapse.types import get_domain_from_id
+from synapse.types import get_domain_from_id, EventID
 from synapse.util import logcontext, unwrapFirstError
 
 logger = logging.getLogger(__name__)
@@ -326,6 +326,14 @@ def event_from_pdu_json(pdu_json, outlier=False):
     elif depth > MAX_DEPTH:
         raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
 
+    event_id = pdu_json["event_id"]
+    if event_id[0] != "$":
+        pdu_json["event_id"] = EventID(
+            event_id,
+            get_domain_from_id(pdu_json["sender"]),
+        ).to_string()
+        event_id = pdu_json["event_id"]
+
     if "auth_events" in pdu_json:
         pdu_json["auth_events"] = [
             (e, {}) if isinstance(e, six.string_types) else e
diff --git a/synapse/federation/units.py b/synapse/federation/units.py
index 308760d9b5..580e8972b5 100644
--- a/synapse/federation/units.py
+++ b/synapse/federation/units.py
@@ -19,6 +19,7 @@ server protocol.
 
 import logging
 
+from synapse.types import get_localpart_from_id
 from synapse.util.jsonobject import JsonEncodedObject
 
 logger = logging.getLogger(__name__)
@@ -125,6 +126,8 @@ def _mangle_pdu(pdu_json):
     pdu_json["auth_events"] = list(_strip_hashes(pdu_json["auth_events"]))
     pdu_json["prev_events"] = list(_strip_hashes(pdu_json["prev_events"]))
 
+    pdu_json["event_id"] = get_localpart_from_id(pdu_json["event_id"])
+
     logger.info("Mangled PDU: %s", pdu_json)
 
     return pdu_json
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index 9848534120..7dbb9d49fb 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -1333,7 +1333,6 @@ class FederationHandler(BaseHandler):
         if not joined:
             logger.error("Giving up on trying to auto-accept invite: too many attempts")
 
-
     @defer.inlineCallbacks
     def do_remotely_reject_invite(self, target_hosts, room_id, user_id):
         origin, event = yield self._make_and_verify_event(