mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Adds class method to create from dict and on_sync method
This commit is contained in:
@@ -15,14 +15,32 @@ class Appointment:
|
||||
self.cipher = cipher
|
||||
self.hash_function = hash_function
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, appointment_data):
|
||||
locator = appointment_data.get("locator")
|
||||
start_time = appointment_data.get("start_time") # ToDo: #4-standardize-appointment-fields
|
||||
end_time = appointment_data.get("end_time") # ToDo: #4-standardize-appointment-fields
|
||||
dispute_delta = appointment_data.get("dispute_delta")
|
||||
encrypted_blob_data = appointment_data.get("encrypted_blob")
|
||||
cipher = appointment_data.get("cipher")
|
||||
hash_function = appointment_data.get("hash_function")
|
||||
|
||||
if all([locator, start_time, end_time, dispute_delta, encrypted_blob_data, cipher, hash_function]) is not None:
|
||||
appointment = cls(locator, start_time, end_time, dispute_delta, EncryptedBlob(encrypted_blob_data), cipher,
|
||||
hash_function)
|
||||
|
||||
else:
|
||||
raise ValueError("Wrong appointment data, some fields are missing")
|
||||
|
||||
return appointment
|
||||
|
||||
def to_dict(self):
|
||||
# ToDO: #3-improve-appointment-structure
|
||||
appointment = {"locator": self.locator, "start_time": self.start_time, "end_time": self.end_time,
|
||||
"dispute_delta": self.dispute_delta, "encrypted_blob": self.encrypted_blob.data,
|
||||
"cipher": self.cipher, "hash_function": self.hash_function}
|
||||
|
||||
return appointment
|
||||
|
||||
# ToDO: #3-improve-appointment-structure
|
||||
|
||||
def to_json(self):
|
||||
return json.dumps(self.to_dict(), sort_keys=True, separators=(',', ':'))
|
||||
|
||||
@@ -28,6 +28,19 @@ class Job:
|
||||
# can be directly got from DB
|
||||
self.locator = sha256(unhexlify(dispute_txid)).hexdigest()
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, jobs_data):
|
||||
dispute_txid = jobs_data.get("dispute_txid")
|
||||
justice_txid = jobs_data.get("justice_txid")
|
||||
justice_rawtx = jobs_data.get("justice_rawtx")
|
||||
appointment_end = jobs_data.get("appointment_end")
|
||||
|
||||
if all([dispute_txid, justice_txid, justice_rawtx, appointment_end]) is not None:
|
||||
job = cls(dispute_txid, justice_txid, justice_rawtx, appointment_end)
|
||||
|
||||
else:
|
||||
raise ValueError("Wrong job data, some fields are missing")
|
||||
|
||||
def to_dict(self):
|
||||
job = {"locator": self.locator, "justice_rawtx": self.justice_rawtx, "appointment_end": self.appointment_end}
|
||||
|
||||
@@ -48,7 +61,20 @@ class Responder:
|
||||
self.zmq_subscriber = None
|
||||
self.db_manager = db_manager
|
||||
|
||||
def add_response(self, uuid, dispute_txid, justice_txid, justice_rawtx, appointment_end, retry=False):
|
||||
@staticmethod
|
||||
def on_sync(block_hash):
|
||||
block_processor = BlockProcessor()
|
||||
distance_from_tip = block_processor.get_distance_to_tip(block_hash)
|
||||
|
||||
if distance_from_tip is not None and distance_from_tip > 1:
|
||||
synchronized = False
|
||||
|
||||
else:
|
||||
synchronized = True
|
||||
|
||||
return synchronized
|
||||
|
||||
def add_response(self, uuid, dispute_txid, justice_txid, justice_rawtx, appointment_end, block_hash, retry=False):
|
||||
if self.asleep:
|
||||
logger.info("Waking up")
|
||||
|
||||
@@ -63,6 +89,8 @@ class Responder:
|
||||
|
||||
else:
|
||||
# TODO: Add the missing reasons (e.g. RPC_VERIFY_REJECTED)
|
||||
# TODO: Use self.on_sync(block_hash) to check whether or not we failed because we are out of sync
|
||||
logger.warning("Job failed.", uuid=uuid, on_sync=self.on_sync(block_hash))
|
||||
pass
|
||||
|
||||
return receipt
|
||||
|
||||
Reference in New Issue
Block a user