From 160f434e13c02241907e1fb6de02c019843cd6a6 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 6 Nov 2019 14:45:20 -0500 Subject: [PATCH] rename hash to etag, to match MSC --- synapse/handlers/e2e_room_keys.py | 21 +++++++----- .../storage/data_stores/main/e2e_room_keys.py | 16 +++++----- .../main/schema/delta/56/room_key_etag.sql} | 4 +-- tests/handlers/test_e2e_room_keys.py | 32 ++++++++++--------- 4 files changed, 40 insertions(+), 33 deletions(-) rename synapse/storage/{schema/delta/56/room_key_hash.sql => data_stores/main/schema/delta/56/room_key_etag.sql} (84%) diff --git a/synapse/handlers/e2e_room_keys.py b/synapse/handlers/e2e_room_keys.py index fec88eac26..f1b4424a02 100644 --- a/synapse/handlers/e2e_room_keys.py +++ b/synapse/handlers/e2e_room_keys.py @@ -107,7 +107,7 @@ class E2eRoomKeysHandler(object): Raises: NotFoundError: if the backup version does not exist Returns: - A dict containing the count and hash for the backup version + A dict containing the count and etag for the backup version """ # lock for consistency with uploading @@ -125,8 +125,13 @@ class E2eRoomKeysHandler(object): yield self.store.delete_e2e_room_keys(user_id, version, room_id, session_id) + version_etag = version_info["etag"] + 1 + yield self.store.update_e2e_room_keys_version( + user_id, version, None, version_etag + ) + count = yield self.store.count_e2e_room_keys(user_id, version) - return {"count": count, "hash": version_info["hash"]} + return {"etag": str(version_etag), "count": count} @trace @defer.inlineCallbacks @@ -156,7 +161,7 @@ class E2eRoomKeysHandler(object): } Returns: - A dict containing the count and hash for the backup version + A dict containing the count and etag for the backup version Raises: NotFoundError: if there are no versions defined @@ -199,7 +204,7 @@ class E2eRoomKeysHandler(object): user_id, version, room_keys["rooms"] ) to_insert = [] # batch the inserts together - changed = False # if anything has changed, we need to update the hash + changed = False # if anything has changed, we need to update the etag for room_id, room in iteritems(room_keys["rooms"]): for session_id, room_key in iteritems(room["sessions"]): log_kv( @@ -238,15 +243,15 @@ class E2eRoomKeysHandler(object): if len(to_insert): yield self.store.add_e2e_room_keys(user_id, version, to_insert) - version_hash = int(version_info["hash"]) + version_etag = version_info["etag"] if changed: - version_hash = version_hash + 1 + version_etag = version_etag + 1 yield self.store.update_e2e_room_keys_version( - user_id, version, None, version_hash + user_id, version, None, version_etag ) count = yield self.store.count_e2e_room_keys(user_id, version) - return {"hash": version_hash, "count": count} + return {"etag": str(version_etag), "count": count} @staticmethod def _should_replace_room_key(current_room_key, room_key): diff --git a/synapse/storage/data_stores/main/e2e_room_keys.py b/synapse/storage/data_stores/main/e2e_room_keys.py index 2773616257..2c843c6fbe 100644 --- a/synapse/storage/data_stores/main/e2e_room_keys.py +++ b/synapse/storage/data_stores/main/e2e_room_keys.py @@ -297,7 +297,7 @@ class EndToEndRoomKeyStore(SQLBaseStore): version(str) algorithm(str) auth_data(object): opaque dict supplied by the client - hash(int): tag of the keys in the backup + etag(int): tag of the keys in the backup """ def _get_e2e_room_keys_version_info_txn(txn): @@ -315,12 +315,12 @@ class EndToEndRoomKeyStore(SQLBaseStore): txn, table="e2e_room_keys_versions", keyvalues={"user_id": user_id, "version": this_version, "deleted": 0}, - retcols=("version", "algorithm", "auth_data", "hash"), + retcols=("version", "algorithm", "auth_data", "etag"), ) result["auth_data"] = json.loads(result["auth_data"]) result["version"] = str(result["version"]) - if not result["hash"]: - result["hash"] = 0 + if not result["etag"]: + result["etag"] = 0 return result return self.runInteraction( @@ -370,7 +370,7 @@ class EndToEndRoomKeyStore(SQLBaseStore): @trace def update_e2e_room_keys_version( - self, user_id, version, info=None, version_hash=None + self, user_id, version, info=None, version_etag=None ): """Update a given backup version @@ -378,14 +378,14 @@ class EndToEndRoomKeyStore(SQLBaseStore): user_id(str): the user whose backup version we're updating version(str): the version ID of the backup version we're updating info(dict): the new backup version info to store - version_hash(str): tag of the keys in the backup + version_etag(int): tag of the keys in the backup """ updatevalues = {} if info and "auth_data" in info: updatevalues["auth_data"] = json.dumps(info["auth_data"]) - if version_hash: - updatevalues["hash"] = version_hash + if version_etag: + updatevalues["etag"] = version_etag if updatevalues: return self._simple_update( diff --git a/synapse/storage/schema/delta/56/room_key_hash.sql b/synapse/storage/data_stores/main/schema/delta/56/room_key_etag.sql similarity index 84% rename from synapse/storage/schema/delta/56/room_key_hash.sql rename to synapse/storage/data_stores/main/schema/delta/56/room_key_etag.sql index 74e1b72446..ddb0c8a8d3 100644 --- a/synapse/storage/schema/delta/56/room_key_hash.sql +++ b/synapse/storage/data_stores/main/schema/delta/56/room_key_etag.sql @@ -13,5 +13,5 @@ * limitations under the License. */ --- store the current hash of backup version -ALTER TABLE e2e_room_keys_versions ADD COLUMN hash TEXT; +-- store the current etag of backup version +ALTER TABLE e2e_room_keys_versions ADD COLUMN etag INTEGER UNSIGNED; diff --git a/tests/handlers/test_e2e_room_keys.py b/tests/handlers/test_e2e_room_keys.py index 1d0007dda9..01427da0e5 100644 --- a/tests/handlers/test_e2e_room_keys.py +++ b/tests/handlers/test_e2e_room_keys.py @@ -95,8 +95,8 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): # check we can retrieve it as the current version res = yield self.handler.get_version_info(self.local_user) - version_hash = res["hash"] - del res["hash"] + version_etag = res["etag"] + del res["etag"] self.assertDictEqual( res, { @@ -109,8 +109,8 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): # check we can retrieve it as a specific version res = yield self.handler.get_version_info(self.local_user, "1") - self.assertEqual(res["hash"], version_hash) - del res["hash"] + self.assertEqual(res["etag"], version_etag) + del res["etag"] self.assertDictEqual( res, { @@ -133,7 +133,7 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): # check we can retrieve it as the current version res = yield self.handler.get_version_info(self.local_user) - del res["hash"] + del res["etag"] self.assertDictEqual( res, { @@ -167,7 +167,7 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): # check we can retrieve it as the current version res = yield self.handler.get_version_info(self.local_user) - del res["hash"] + del res["etag"] self.assertDictEqual( res, { @@ -218,12 +218,14 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): # check we can retrieve it as the current version res = yield self.handler.get_version_info(self.local_user) + del res["etag"] # etag is opaque, so don't test its contents self.assertDictEqual( res, { "algorithm": "m.megolm_backup.v1", "auth_data": "revised_first_version_auth_data", "version": version, + "count": 0 }, ) @@ -420,9 +422,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): yield self.handler.upload_room_keys(self.local_user, version, room_keys) - # get the hash to compare to future versions + # get the etag to compare to future versions res = yield self.handler.get_version_info(self.local_user) - backup_hash = res["hash"] + backup_etag = res["etag"] self.assertEqual(res["count"], 1) new_room_keys = copy.deepcopy(room_keys) @@ -439,9 +441,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): "SSBBTSBBIEZJU0gK", ) - # the hash should be the same since the session did not change + # the etag should be the same since the session did not change res = yield self.handler.get_version_info(self.local_user) - self.assertEqual(res["hash"], backup_hash) + self.assertEqual(res["etag"], backup_etag) # test that marking the session as verified however /does/ replace it new_room_key["is_verified"] = True @@ -452,10 +454,10 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new" ) - # the hash should NOT be equal now, since the key changed + # the etag should NOT be equal now, since the key changed res = yield self.handler.get_version_info(self.local_user) - self.assertNotEqual(res["hash"], backup_hash) - backup_hash = res["hash"] + self.assertNotEqual(res["etag"], backup_etag) + backup_etag = res["etag"] # test that a session with a higher forwarded_count doesn't replace one # with a lower forwarding count @@ -468,9 +470,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase): res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new" ) - # the hash should be the same since the session did not change + # the etag should be the same since the session did not change res = yield self.handler.get_version_info(self.local_user) - self.assertEqual(res["hash"], backup_hash) + self.assertEqual(res["etag"], backup_etag) # TODO: check edge cases as well as the common variations here