Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Split fetching device keys and signatures into two transactions #8233

Merged
merged 3 commits into from
Sep 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions synapse/storage/databases/main/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ async def get_e2e_device_keys_and_signatures(
include_all_devices: bool = False,
include_deleted_devices: bool = False,
) -> Dict[str, Dict[str, Optional[DeviceKeyLookupResult]]]:
"""Fetch a list of device keys, together with their cross-signatures.
"""Fetch a list of device keys

Any cross-signatures made on the keys by the owner of the device are also
included.

Args:
query_list: List of pairs of user_ids and device_ids. Device id can be None
Expand Down Expand Up @@ -178,20 +181,14 @@ async def get_e2e_device_keys_and_signatures(
)

# add each cross-signing signature to the correct device in the result dict.
for row in cross_sigs_result:
signing_user_id = row["user_id"]
signing_key_id = row["key_id"]
target_user_id = row["target_user_id"]
target_device_id = row["target_device_id"]
signature = row["signature"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much easier to read. 👍


target_device_result = result[target_user_id][target_device_id]
for (user_id, key_id, device_id, signature) in cross_sigs_result:
target_device_result = result[user_id][device_id]
target_device_signatures = target_device_result.signatures

signing_user_signatures = target_device_signatures.setdefault(
signing_user_id, {}
user_id, {}
)
signing_user_signatures[signing_key_id] = signature
signing_user_signatures[key_id] = signature

log_kv(result)
return result
Expand Down Expand Up @@ -254,12 +251,13 @@ def _get_e2e_device_keys_txn(

def _get_e2e_cross_signing_signatures_for_devices_txn(
self, txn: Cursor, device_query: Iterable[Tuple[str, str]]
) -> List[Dict]:
) -> List[Tuple[str, str, str, str]]:
"""Get cross-signing signatures for a given list of devices

Returns signatures made by the owner of the devices. Each entry in the result
is a dict containing the fields from the database ('user_id', 'key_id',
'target_user_id', 'target_device_id', 'signature').
Returns signatures made by the owners of the devices.

Returns: a list of results; each entry in the list is a tuple of
(user_id, key_id, target_device_id, signature).
"""
signature_query_clauses = []
signature_query_params = []
Expand All @@ -270,12 +268,15 @@ def _get_e2e_cross_signing_signatures_for_devices_txn(
)
signature_query_params.extend([user_id, device_id, user_id])

signature_sql = "SELECT * FROM e2e_cross_signing_signatures WHERE %s" % (
signature_sql = """
SELECT user_id, key_id, target_device_id, signature
FROM e2e_cross_signing_signatures WHERE %s
""" % (
" OR ".join("(" + q + ")" for q in signature_query_clauses)
)

txn.execute(signature_sql, signature_query_params)
return self.db_pool.cursor_to_dict(txn)
return txn.fetchall()

async def get_e2e_one_time_keys(
self, user_id: str, device_id: str, key_ids: List[str]
Expand Down