mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-21 03:42:55 +03:00
rename hash to etag, to match MSC
This commit is contained in:
parent
a5a59ab8ac
commit
160f434e13
4 changed files with 40 additions and 33 deletions
|
@ -107,7 +107,7 @@ class E2eRoomKeysHandler(object):
|
||||||
Raises:
|
Raises:
|
||||||
NotFoundError: if the backup version does not exist
|
NotFoundError: if the backup version does not exist
|
||||||
Returns:
|
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
|
# 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)
|
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)
|
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
|
@trace
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
@ -156,7 +161,7 @@ class E2eRoomKeysHandler(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A dict containing the count and hash for the backup version
|
A dict containing the count and etag for the backup version
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
NotFoundError: if there are no versions defined
|
NotFoundError: if there are no versions defined
|
||||||
|
@ -199,7 +204,7 @@ class E2eRoomKeysHandler(object):
|
||||||
user_id, version, room_keys["rooms"]
|
user_id, version, room_keys["rooms"]
|
||||||
)
|
)
|
||||||
to_insert = [] # batch the inserts together
|
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 room_id, room in iteritems(room_keys["rooms"]):
|
||||||
for session_id, room_key in iteritems(room["sessions"]):
|
for session_id, room_key in iteritems(room["sessions"]):
|
||||||
log_kv(
|
log_kv(
|
||||||
|
@ -238,15 +243,15 @@ class E2eRoomKeysHandler(object):
|
||||||
if len(to_insert):
|
if len(to_insert):
|
||||||
yield self.store.add_e2e_room_keys(user_id, version, 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:
|
if changed:
|
||||||
version_hash = version_hash + 1
|
version_etag = version_etag + 1
|
||||||
yield self.store.update_e2e_room_keys_version(
|
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)
|
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
|
@staticmethod
|
||||||
def _should_replace_room_key(current_room_key, room_key):
|
def _should_replace_room_key(current_room_key, room_key):
|
||||||
|
|
|
@ -297,7 +297,7 @@ class EndToEndRoomKeyStore(SQLBaseStore):
|
||||||
version(str)
|
version(str)
|
||||||
algorithm(str)
|
algorithm(str)
|
||||||
auth_data(object): opaque dict supplied by the client
|
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):
|
def _get_e2e_room_keys_version_info_txn(txn):
|
||||||
|
@ -315,12 +315,12 @@ class EndToEndRoomKeyStore(SQLBaseStore):
|
||||||
txn,
|
txn,
|
||||||
table="e2e_room_keys_versions",
|
table="e2e_room_keys_versions",
|
||||||
keyvalues={"user_id": user_id, "version": this_version, "deleted": 0},
|
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["auth_data"] = json.loads(result["auth_data"])
|
||||||
result["version"] = str(result["version"])
|
result["version"] = str(result["version"])
|
||||||
if not result["hash"]:
|
if not result["etag"]:
|
||||||
result["hash"] = 0
|
result["etag"] = 0
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return self.runInteraction(
|
return self.runInteraction(
|
||||||
|
@ -370,7 +370,7 @@ class EndToEndRoomKeyStore(SQLBaseStore):
|
||||||
|
|
||||||
@trace
|
@trace
|
||||||
def update_e2e_room_keys_version(
|
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
|
"""Update a given backup version
|
||||||
|
|
||||||
|
@ -378,14 +378,14 @@ class EndToEndRoomKeyStore(SQLBaseStore):
|
||||||
user_id(str): the user whose backup version we're updating
|
user_id(str): the user whose backup version we're updating
|
||||||
version(str): the version ID of the 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
|
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 = {}
|
updatevalues = {}
|
||||||
|
|
||||||
if info and "auth_data" in info:
|
if info and "auth_data" in info:
|
||||||
updatevalues["auth_data"] = json.dumps(info["auth_data"])
|
updatevalues["auth_data"] = json.dumps(info["auth_data"])
|
||||||
if version_hash:
|
if version_etag:
|
||||||
updatevalues["hash"] = version_hash
|
updatevalues["etag"] = version_etag
|
||||||
|
|
||||||
if updatevalues:
|
if updatevalues:
|
||||||
return self._simple_update(
|
return self._simple_update(
|
||||||
|
|
|
@ -13,5 +13,5 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
-- store the current hash of backup version
|
-- store the current etag of backup version
|
||||||
ALTER TABLE e2e_room_keys_versions ADD COLUMN hash TEXT;
|
ALTER TABLE e2e_room_keys_versions ADD COLUMN etag INTEGER UNSIGNED;
|
|
@ -95,8 +95,8 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# check we can retrieve it as the current version
|
# check we can retrieve it as the current version
|
||||||
res = yield self.handler.get_version_info(self.local_user)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
version_hash = res["hash"]
|
version_etag = res["etag"]
|
||||||
del res["hash"]
|
del res["etag"]
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
res,
|
res,
|
||||||
{
|
{
|
||||||
|
@ -109,8 +109,8 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# check we can retrieve it as a specific version
|
# check we can retrieve it as a specific version
|
||||||
res = yield self.handler.get_version_info(self.local_user, "1")
|
res = yield self.handler.get_version_info(self.local_user, "1")
|
||||||
self.assertEqual(res["hash"], version_hash)
|
self.assertEqual(res["etag"], version_etag)
|
||||||
del res["hash"]
|
del res["etag"]
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
res,
|
res,
|
||||||
{
|
{
|
||||||
|
@ -133,7 +133,7 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# check we can retrieve it as the current version
|
# check we can retrieve it as the current version
|
||||||
res = yield self.handler.get_version_info(self.local_user)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
del res["hash"]
|
del res["etag"]
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
res,
|
res,
|
||||||
{
|
{
|
||||||
|
@ -167,7 +167,7 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# check we can retrieve it as the current version
|
# check we can retrieve it as the current version
|
||||||
res = yield self.handler.get_version_info(self.local_user)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
del res["hash"]
|
del res["etag"]
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
res,
|
res,
|
||||||
{
|
{
|
||||||
|
@ -218,12 +218,14 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# check we can retrieve it as the current version
|
# check we can retrieve it as the current version
|
||||||
res = yield self.handler.get_version_info(self.local_user)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
|
del res["etag"] # etag is opaque, so don't test its contents
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
res,
|
res,
|
||||||
{
|
{
|
||||||
"algorithm": "m.megolm_backup.v1",
|
"algorithm": "m.megolm_backup.v1",
|
||||||
"auth_data": "revised_first_version_auth_data",
|
"auth_data": "revised_first_version_auth_data",
|
||||||
"version": version,
|
"version": version,
|
||||||
|
"count": 0
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -420,9 +422,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
|
|
||||||
yield self.handler.upload_room_keys(self.local_user, version, room_keys)
|
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)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
backup_hash = res["hash"]
|
backup_etag = res["etag"]
|
||||||
self.assertEqual(res["count"], 1)
|
self.assertEqual(res["count"], 1)
|
||||||
|
|
||||||
new_room_keys = copy.deepcopy(room_keys)
|
new_room_keys = copy.deepcopy(room_keys)
|
||||||
|
@ -439,9 +441,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
"SSBBTSBBIEZJU0gK",
|
"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)
|
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
|
# test that marking the session as verified however /does/ replace it
|
||||||
new_room_key["is_verified"] = True
|
new_room_key["is_verified"] = True
|
||||||
|
@ -452,10 +454,10 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
|
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)
|
res = yield self.handler.get_version_info(self.local_user)
|
||||||
self.assertNotEqual(res["hash"], backup_hash)
|
self.assertNotEqual(res["etag"], backup_etag)
|
||||||
backup_hash = res["hash"]
|
backup_etag = res["etag"]
|
||||||
|
|
||||||
# test that a session with a higher forwarded_count doesn't replace one
|
# test that a session with a higher forwarded_count doesn't replace one
|
||||||
# with a lower forwarding count
|
# with a lower forwarding count
|
||||||
|
@ -468,9 +470,9 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
|
||||||
res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
|
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)
|
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
|
# TODO: check edge cases as well as the common variations here
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue