From e924b57efcf2b601293ce0af7cde19d215cba91d Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 27 Mar 2020 18:16:25 +0100 Subject: [PATCH] Appointment updates only do not decrease slots if not necessary - For a given appointment, checks if it is an update and computes the difference of sizes if so. - Additional slots are only filled if the new version is bigger. Slots are freed if the update is smaller. - Adds get_appoiment_summary to get information in memory information of an appointment (so the API can check if a request is an update) - The API computes the uuid and requests it to the tower. - Size field has been added to all in memory appointments --- teos/api.py | 32 +++++++++++++++++++++++++++----- teos/watcher.py | 21 ++++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/teos/api.py b/teos/api.py index 5ad1214..6bffe7f 100644 --- a/teos/api.py +++ b/teos/api.py @@ -169,19 +169,41 @@ class API: appointment = self.inspector.inspect(request_data.get("appointment")) user_pk = self.gatekeeper.identify_user(appointment.serialize(), request_data.get("signature")) - # An appointment will fill 1 slot per ENCRYPTED_BLOB_MAX_SIZE_HEX block. - # Temporarily taking out slots to avoid abusing this via race conditions. + # Check if the appointment is an update. Updates will return a summary. + appointment_uuid = hash_160("{}{}".format(appointment.locator, user_pk)) + appointment_summary = self.watcher.get_appointment_summary(appointment_uuid) + + # For updates we only reserve the slot difference provided the new one is bigger. + if appointment_summary: + size_diff = len(appointment.encrypted_blob.data) - appointment_summary.get("size") + slot_diff = ceil(size_diff / ENCRYPTED_BLOB_MAX_SIZE_HEX) + required_slots = slot_diff if slot_diff > 0 else 0 + + # For regular appointments 1 slot is reserved per ENCRYPTED_BLOB_MAX_SIZE_HEX block. + else: + slot_diff = 0 + required_slots = ceil(len(appointment.encrypted_blob.data) / ENCRYPTED_BLOB_MAX_SIZE_HEX) + + # Slots are reserved before adding the appointments to prevent race conditions. # DISCUSS: It may be worth using signals here to avoid race conditions anyway. - required_slots = ceil(len(appointment.encrypted_blob.data) / ENCRYPTED_BLOB_MAX_SIZE_HEX) self.gatekeeper.fill_slots(user_pk, required_slots) + appointment_added, signature = self.watcher.add_appointment(appointment, user_pk) if appointment_added: rcode = HTTP_OK - response = {"locator": appointment.locator, "signature": signature} + response = { + "locator": appointment.locator, + "signature": signature, + "available_slots": self.gatekeeper.registered_users[user_pk], + } + + # If the appointment is added and the update is smaller than the original, the difference is given back. + if slot_diff < 0: + self.gatekeeper.free_slots(slot_diff) else: - # Adding back the slots since they were not used + # If the appointment is not added the reserved slots are given back self.gatekeeper.free_slots(user_pk, required_slots) rcode = HTTP_SERVICE_UNAVAILABLE response = {"error": "appointment rejected"} diff --git a/teos/watcher.py b/teos/watcher.py index feb2f71..757ffc0 100644 --- a/teos/watcher.py +++ b/teos/watcher.py @@ -76,6 +76,21 @@ class Watcher: return watcher_thread + def get_appointment_summary(self, uuid): + """ + Returns the summary of an appointment. The summary consists of the data kept in memory: + locator, end_time, and size. + + Args: + uuid (:obj:`str`): a 16-byte hex string identifying the appointment. + + Returns: + :obj:`dict` or :obj:`None`: a dictionary with the appointment summary, or None if the appointment is not + found. + """ + + return self.appointments.get(uuid) + def add_appointment(self, appointment, user_pk): """ Adds a new appointment to the ``appointments`` dictionary if ``max_appointments`` has not been reached. @@ -112,7 +127,11 @@ class Watcher: # anything about the user from this point on (no need to store user_pk in the database). # If an appointment is requested by the user the uuid can be recomputed and queried straightaway (no maps). uuid = hash_160("{}{}".format(appointment.locator, user_pk)) - self.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time} + self.appointments[uuid] = { + "locator": appointment.locator, + "end_time": appointment.end_time, + "size": len(appointment.encrypted_blob.data), + } if appointment.locator in self.locator_uuid_map: # If the uuid is already in the map it means this is an update.