mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 14:14:22 +01:00
Adds end time to the user (subscription) information. It will replace the appointment one.
This commit is contained in:
@@ -18,6 +18,7 @@ DEFAULT_CONF = {
|
|||||||
"FEED_PORT": {"value": 28332, "type": int},
|
"FEED_PORT": {"value": 28332, "type": int},
|
||||||
"MAX_APPOINTMENTS": {"value": 1000000, "type": int},
|
"MAX_APPOINTMENTS": {"value": 1000000, "type": int},
|
||||||
"DEFAULT_SLOTS": {"value": 100, "type": int},
|
"DEFAULT_SLOTS": {"value": 100, "type": int},
|
||||||
|
"DEFAULT_SUBSCRIPTION_DURATION": {"value": 4320, "type": int},
|
||||||
"EXPIRY_DELTA": {"value": 6, "type": int},
|
"EXPIRY_DELTA": {"value": 6, "type": int},
|
||||||
"MIN_TO_SELF_DELAY": {"value": 20, "type": int},
|
"MIN_TO_SELF_DELAY": {"value": 20, "type": int},
|
||||||
"LOG_FILE": {"value": "teos.log", "type": str, "path": True},
|
"LOG_FILE": {"value": "teos.log", "type": str, "path": True},
|
||||||
|
|||||||
19
teos/api.py
19
teos/api.py
@@ -130,8 +130,12 @@ class API:
|
|||||||
if client_pk:
|
if client_pk:
|
||||||
try:
|
try:
|
||||||
rcode = HTTP_OK
|
rcode = HTTP_OK
|
||||||
available_slots = self.gatekeeper.add_update_user(client_pk)
|
available_slots, subscription_end_time = self.gatekeeper.add_update_user(client_pk)
|
||||||
response = {"public_key": client_pk, "available_slots": available_slots}
|
response = {
|
||||||
|
"public_key": client_pk,
|
||||||
|
"available_slots": available_slots,
|
||||||
|
"subscription_end_time": subscription_end_time,
|
||||||
|
}
|
||||||
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
rcode = HTTP_BAD_REQUEST
|
rcode = HTTP_BAD_REQUEST
|
||||||
@@ -204,7 +208,9 @@ class API:
|
|||||||
# DISCUSS: It may be worth using signals here to avoid race conditions anyway.
|
# DISCUSS: It may be worth using signals here to avoid race conditions anyway.
|
||||||
self.gatekeeper.fill_slots(user_pk, required_slots)
|
self.gatekeeper.fill_slots(user_pk, required_slots)
|
||||||
|
|
||||||
appointment_added, signature = self.watcher.add_appointment(appointment, user_pk)
|
appointment_added, signature = self.watcher.add_appointment(
|
||||||
|
appointment, user_pk, self.gatekeeper.registered_users[user_pk].subscription_end_time
|
||||||
|
)
|
||||||
|
|
||||||
if appointment_added:
|
if appointment_added:
|
||||||
# If the appointment is added and the update is smaller than the original, the difference is given back.
|
# If the appointment is added and the update is smaller than the original, the difference is given back.
|
||||||
@@ -215,7 +221,8 @@ class API:
|
|||||||
response = {
|
response = {
|
||||||
"locator": appointment.locator,
|
"locator": appointment.locator,
|
||||||
"signature": signature,
|
"signature": signature,
|
||||||
"available_slots": self.gatekeeper.registered_users[user_pk].get("available_slots"),
|
"available_slots": self.gatekeeper.registered_users[user_pk].available_slots,
|
||||||
|
"subscription_end_time": self.gatekeeper.registered_users[user_pk].subscription_end_time,
|
||||||
}
|
}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -288,6 +295,8 @@ class API:
|
|||||||
appointment_data = self.watcher.db_manager.load_responder_tracker(uuid)
|
appointment_data = self.watcher.db_manager.load_responder_tracker(uuid)
|
||||||
if appointment_data:
|
if appointment_data:
|
||||||
rcode = HTTP_OK
|
rcode = HTTP_OK
|
||||||
|
# Remove expiry field from appointment data since it is an internal field
|
||||||
|
appointment_data.pop("expiry")
|
||||||
response = {"locator": locator, "status": "dispute_responded", "appointment": appointment_data}
|
response = {"locator": locator, "status": "dispute_responded", "appointment": appointment_data}
|
||||||
else:
|
else:
|
||||||
rcode = HTTP_NOT_FOUND
|
rcode = HTTP_NOT_FOUND
|
||||||
@@ -298,6 +307,8 @@ class API:
|
|||||||
appointment_data = self.watcher.db_manager.load_watcher_appointment(uuid)
|
appointment_data = self.watcher.db_manager.load_watcher_appointment(uuid)
|
||||||
if appointment_data:
|
if appointment_data:
|
||||||
rcode = HTTP_OK
|
rcode = HTTP_OK
|
||||||
|
# Remove expiry field from appointment data since it is an internal field
|
||||||
|
appointment_data.pop("expiry")
|
||||||
response = {"locator": locator, "status": "being_watched", "appointment": appointment_data}
|
response = {"locator": locator, "status": "being_watched", "appointment": appointment_data}
|
||||||
else:
|
else:
|
||||||
rcode = HTTP_NOT_FOUND
|
rcode = HTTP_NOT_FOUND
|
||||||
|
|||||||
@@ -20,19 +20,48 @@ class IdentificationFailure(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UserInfo:
|
||||||
|
def __init__(self, available_slots, subscription_end_time, appointments=None):
|
||||||
|
self.available_slots = available_slots
|
||||||
|
self.subscription_end_time = subscription_end_time
|
||||||
|
|
||||||
|
if not appointments:
|
||||||
|
self.appointments = {}
|
||||||
|
else:
|
||||||
|
self.appointments = appointments
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_dict(cls, user_data):
|
||||||
|
available_slots = user_data.get("available_slots")
|
||||||
|
appointments = user_data.get("appointments")
|
||||||
|
subscription_end_time = user_data.get("subscription_end_time")
|
||||||
|
|
||||||
|
if any(v is None for v in [available_slots, appointments, subscription_end_time]):
|
||||||
|
raise ValueError("Wrong appointment data, some fields are missing")
|
||||||
|
|
||||||
|
return cls(available_slots, subscription_expiry, appointments)
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return self.__dict__
|
||||||
|
|
||||||
|
|
||||||
class Gatekeeper:
|
class Gatekeeper:
|
||||||
"""
|
"""
|
||||||
The :class:`Gatekeeper` is in charge of managing the access to the tower. Only registered users are allowed to
|
The :class:`Gatekeeper` is in charge of managing the access to the tower. Only registered users are allowed to
|
||||||
perform actions.
|
perform actions.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
registered_users (:obj:`dict`): a map of user_pk:appointment_slots.
|
registered_users (:obj:`dict`): a map of user_pk:UserInfo.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, user_db, default_slots):
|
def __init__(self, user_db, block_processor, default_slots, default_subscription_duration):
|
||||||
self.default_slots = default_slots
|
self.default_slots = default_slots
|
||||||
|
self.block_processor = block_processor
|
||||||
|
self.default_subscription_duration = default_subscription_duration
|
||||||
self.user_db = user_db
|
self.user_db = user_db
|
||||||
self.registered_users = user_db.load_all_users()
|
self.registered_users = {
|
||||||
|
user_id: UserInfo.from_dict(user_data) for user_id, user_data in user_db.load_all_users().items()
|
||||||
|
}
|
||||||
|
|
||||||
def add_update_user(self, user_pk):
|
def add_update_user(self, user_pk):
|
||||||
"""
|
"""
|
||||||
@@ -42,20 +71,27 @@ class Gatekeeper:
|
|||||||
user_pk(:obj:`str`): the public key that identifies the user (33-bytes hex str).
|
user_pk(:obj:`str`): the public key that identifies the user (33-bytes hex str).
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`int`: the number of available slots in the user subscription.
|
:obj:`tuple`: a tuple with the number of available slots in the user subscription and the subscription end
|
||||||
|
time (in absolute block height).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not is_compressed_pk(user_pk):
|
if not is_compressed_pk(user_pk):
|
||||||
raise ValueError("Provided public key does not match expected format (33-byte hex string)")
|
raise ValueError("Provided public key does not match expected format (33-byte hex string)")
|
||||||
|
|
||||||
if user_pk not in self.registered_users:
|
if user_pk not in self.registered_users:
|
||||||
self.registered_users[user_pk] = {"available_slots": self.default_slots}
|
self.registered_users[user_pk] = UserInfo(
|
||||||
|
self.default_slots, self.block_processor.get_block_count() + self.default_subscription_duration
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.registered_users[user_pk]["available_slots"] += self.default_slots
|
# FIXME: For now new calls to register add default_slots to the current count and reset the expiry time
|
||||||
|
self.registered_users[user_pk].available_slots += self.default_slots
|
||||||
|
self.registered_users[user_pk].subscription_expiry = (
|
||||||
|
self.block_processor.get_block_count() + self.default_subscription_duration
|
||||||
|
)
|
||||||
|
|
||||||
self.user_db.store_user(user_pk, self.registered_users[user_pk])
|
self.user_db.store_user(user_pk, self.registered_users[user_pk].to_dict())
|
||||||
|
|
||||||
return self.registered_users[user_pk]["available_slots"]
|
return self.registered_users[user_pk].available_slots, self.registered_users[user_pk].subscription_end_time
|
||||||
|
|
||||||
def identify_user(self, message, signature):
|
def identify_user(self, message, signature):
|
||||||
"""
|
"""
|
||||||
@@ -97,9 +133,9 @@ class Gatekeeper:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# DISCUSS: we may want to return a different exception if the user does not exist
|
# DISCUSS: we may want to return a different exception if the user does not exist
|
||||||
if user_pk in self.registered_users and n <= self.registered_users.get(user_pk).get("available_slots"):
|
if user_pk in self.registered_users and n <= self.registered_users.get(user_pk).available_slots:
|
||||||
self.registered_users[user_pk]["available_slots"] -= n
|
self.registered_users[user_pk].available_slots -= n
|
||||||
self.user_db.store_user(user_pk, self.registered_users[user_pk])
|
self.user_db.store_user(user_pk, self.registered_users[user_pk].to_dict())
|
||||||
else:
|
else:
|
||||||
raise NotEnoughSlots(user_pk, n)
|
raise NotEnoughSlots(user_pk, n)
|
||||||
|
|
||||||
@@ -114,5 +150,5 @@ class Gatekeeper:
|
|||||||
|
|
||||||
# DISCUSS: if the user does not exist we may want to log or return an exception.
|
# DISCUSS: if the user does not exist we may want to log or return an exception.
|
||||||
if user_pk in self.registered_users:
|
if user_pk in self.registered_users:
|
||||||
self.registered_users[user_pk]["available_slots"] += n
|
self.registered_users[user_pk].available_slots += n
|
||||||
self.user_db.store_user(user_pk, self.registered_users[user_pk])
|
self.user_db.store_user(user_pk, self.registered_users[user_pk].to_dict())
|
||||||
|
|||||||
@@ -151,7 +151,12 @@ def main(command_line_conf):
|
|||||||
# Fire the API and the ChainMonitor
|
# Fire the API and the ChainMonitor
|
||||||
# FIXME: 92-block-data-during-bootstrap-db
|
# FIXME: 92-block-data-during-bootstrap-db
|
||||||
chain_monitor.monitor_chain()
|
chain_monitor.monitor_chain()
|
||||||
gatekeeper = Gatekeeper(UsersDBM(config.get("USERS_DB_PATH")), config.get("DEFAULT_SLOTS"))
|
gatekeeper = Gatekeeper(
|
||||||
|
UsersDBM(config.get("USERS_DB_PATH")),
|
||||||
|
block_processor,
|
||||||
|
config.get("DEFAULT_SLOTS"),
|
||||||
|
config.get("DEFAULT_SUBSCRIPTION_DURATION"),
|
||||||
|
)
|
||||||
inspector = Inspector(block_processor, config.get("MIN_TO_SELF_DELAY"))
|
inspector = Inspector(block_processor, config.get("MIN_TO_SELF_DELAY"))
|
||||||
API(config.get("API_BIND"), config.get("API_PORT"), inspector, watcher, gatekeeper).start()
|
API(config.get("API_BIND"), config.get("API_PORT"), inspector, watcher, gatekeeper).start()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class Watcher:
|
|||||||
"""
|
"""
|
||||||
return self.appointments.get(uuid)
|
return self.appointments.get(uuid)
|
||||||
|
|
||||||
def add_appointment(self, appointment, user_pk):
|
def add_appointment(self, appointment, user_pk, end_time):
|
||||||
"""
|
"""
|
||||||
Adds a new appointment to the ``appointments`` dictionary if ``max_appointments`` has not been reached.
|
Adds a new appointment to the ``appointments`` dictionary if ``max_appointments`` has not been reached.
|
||||||
|
|
||||||
@@ -114,6 +114,7 @@ class Watcher:
|
|||||||
appointment (:obj:`Appointment <teos.appointment.Appointment>`): the appointment to be added to the
|
appointment (:obj:`Appointment <teos.appointment.Appointment>`): the appointment to be added to the
|
||||||
:obj:`Watcher`.
|
:obj:`Watcher`.
|
||||||
user_pk(:obj:`str`): the public key that identifies the user who sent the appointment (33-bytes hex str).
|
user_pk(:obj:`str`): the public key that identifies the user who sent the appointment (33-bytes hex str).
|
||||||
|
end_time (:obj:`int`): the block height where the tower will stop watching for breaches.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:obj:`tuple`: A tuple signaling if the appointment has been added or not (based on ``max_appointments``).
|
:obj:`tuple`: A tuple signaling if the appointment has been added or not (based on ``max_appointments``).
|
||||||
@@ -131,7 +132,7 @@ class Watcher:
|
|||||||
uuid = hash_160("{}{}".format(appointment.locator, user_pk))
|
uuid = hash_160("{}{}".format(appointment.locator, user_pk))
|
||||||
self.appointments[uuid] = {
|
self.appointments[uuid] = {
|
||||||
"locator": appointment.locator,
|
"locator": appointment.locator,
|
||||||
"end_time": appointment.end_time,
|
"end_time": end_time,
|
||||||
"size": len(appointment.encrypted_blob),
|
"size": len(appointment.encrypted_blob),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +144,7 @@ class Watcher:
|
|||||||
else:
|
else:
|
||||||
self.locator_uuid_map[appointment.locator] = [uuid]
|
self.locator_uuid_map[appointment.locator] = [uuid]
|
||||||
|
|
||||||
self.db_manager.store_watcher_appointment(uuid, appointment.to_dict())
|
self.db_manager.store_watcher_appointment(uuid, appointment.to_dict(), end_time)
|
||||||
self.db_manager.create_append_locator_map(appointment.locator, uuid)
|
self.db_manager.create_append_locator_map(appointment.locator, uuid)
|
||||||
|
|
||||||
appointment_added = True
|
appointment_added = True
|
||||||
|
|||||||
Reference in New Issue
Block a user