mirror of
https://github.com/aljazceru/python-teos.git
synced 2026-02-01 12:44:25 +01:00
Refactors the codebase to match the new naming from 793d563b8c053136dd5f936b0ef0cd88af215b06 and 3046eae38ee3f34857c96e6b9d43e645f7d2de2d
This commit is contained in:
14
pisa/api.py
14
pisa/api.py
@@ -121,11 +121,11 @@ def get_appointment():
|
||||
appointment_data["status"] = "being_watched"
|
||||
response.append(appointment_data)
|
||||
|
||||
job_data = watcher.db_manager.load_responder_job(uuid)
|
||||
tracker_data = watcher.db_manager.load_responder_tracker(uuid)
|
||||
|
||||
if job_data is not None:
|
||||
job_data["status"] = "dispute_responded"
|
||||
response.append(job_data)
|
||||
if tracker_data is not None:
|
||||
tracker_data["status"] = "dispute_responded"
|
||||
response.append(tracker_data)
|
||||
|
||||
else:
|
||||
response.append({"locator": locator, "status": "not_found"})
|
||||
@@ -144,7 +144,7 @@ def get_all_appointments():
|
||||
|
||||
Returns:
|
||||
``dict``: A json formatted dictionary containing all the appointments hold by the :mod:`Watcher <pisa.watcher>`
|
||||
(``watcher_appointments``) and by the :mod:`Responder <pisa.responder>` (``responder_jobs``).
|
||||
(``watcher_appointments``) and by the :mod:`Responder <pisa.responder>` (``responder_trackers``).
|
||||
|
||||
"""
|
||||
|
||||
@@ -153,9 +153,9 @@ def get_all_appointments():
|
||||
|
||||
if request.remote_addr in request.host or request.remote_addr == "127.0.0.1":
|
||||
watcher_appointments = watcher.db_manager.load_watcher_appointments()
|
||||
responder_jobs = watcher.db_manager.load_responder_jobs()
|
||||
responder_trackers = watcher.db_manager.load_responder_trackers()
|
||||
|
||||
response = jsonify({"watcher_appointments": watcher_appointments, "responder_jobs": responder_jobs})
|
||||
response = jsonify({"watcher_appointments": watcher_appointments, "responder_trackers": responder_trackers})
|
||||
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from queue import Queue
|
||||
|
||||
from pisa.responder import Job
|
||||
from pisa.responder import TransactionTracker
|
||||
from pisa.appointment import Appointment
|
||||
|
||||
|
||||
@@ -44,38 +44,38 @@ class Builder:
|
||||
return appointments, locator_uuid_map
|
||||
|
||||
@staticmethod
|
||||
def build_jobs(jobs_data):
|
||||
def build_trackers(tracker_data):
|
||||
"""
|
||||
Builds a jobs dictionary (``uuid: Jobs``) and a tx_job_map (``penalty_txid: uuid``) given a dictionary of jobs
|
||||
from the database.
|
||||
Builds a tracker dictionary (``uuid: TransactionTracker``) and a tx_tracker_map (``penalty_txid: uuid``) given
|
||||
a dictionary of trackers from the database.
|
||||
|
||||
Args:
|
||||
jobs_data (dict): a dictionary of dictionaries representing all the :mod:`Responder <pisa.responder>` jobs
|
||||
stored in the database. The structure is as follows:
|
||||
tracker_data (dict): a dictionary of dictionaries representing all the :mod:`Responder <pisa.responder>`
|
||||
trackers stored in the database. The structure is as follows:
|
||||
|
||||
``{uuid: {locator: str, dispute_txid: str, ...}, uuid: {locator:...}}``
|
||||
|
||||
Returns:
|
||||
``tuple``: A tuple with two dictionaries. ``jobs`` containing the jobs information in
|
||||
:class:`Job <pisa.responder>` objects and a ``tx_job_map`` containing the map of jobs
|
||||
(``penalty_txid: uuid``).
|
||||
``tuple``: A tuple with two dictionaries. ``trackers`` containing the trackers' information in
|
||||
:class:`TransactionTracker <pisa.responder.TransactionTracker>` objects and a ``tx_tracker_map`` containing
|
||||
the map of trackers (``penalty_txid: uuid``).
|
||||
|
||||
"""
|
||||
|
||||
jobs = {}
|
||||
tx_job_map = {}
|
||||
trackers = {}
|
||||
tx_tracker_map = {}
|
||||
|
||||
for uuid, data in jobs_data.items():
|
||||
job = Job.from_dict(data)
|
||||
jobs[uuid] = job
|
||||
for uuid, data in tracker_data.items():
|
||||
tracker = TransactionTracker.from_dict(data)
|
||||
trackers[uuid] = tracker
|
||||
|
||||
if job.penalty_txid in tx_job_map:
|
||||
tx_job_map[job.penalty_txid].append(uuid)
|
||||
if tracker.penalty_txid in tx_tracker_map:
|
||||
tx_tracker_map[tracker.penalty_txid].append(uuid)
|
||||
|
||||
else:
|
||||
tx_job_map[job.penalty_txid] = [uuid]
|
||||
tx_tracker_map[tracker.penalty_txid] = [uuid]
|
||||
|
||||
return jobs, tx_job_map
|
||||
return trackers, tx_tracker_map
|
||||
|
||||
@staticmethod
|
||||
def build_block_queue(missed_blocks):
|
||||
|
||||
@@ -74,7 +74,7 @@ class Carrier:
|
||||
elif errno == RPC_VERIFY_ALREADY_IN_CHAIN:
|
||||
logger.info("Transaction is already in the blockchain. Getting confirmation count", txid=txid)
|
||||
|
||||
# If the transaction is already in the chain, we get the number of confirmations and watch the job
|
||||
# If the transaction is already in the chain, we get the number of confirmations and watch the tracker
|
||||
# until the end of the appointment
|
||||
tx_info = self.get_transaction(txid)
|
||||
|
||||
@@ -122,7 +122,7 @@ class Carrier:
|
||||
tx_info = None
|
||||
# While it's quite unlikely, the transaction that was already in the blockchain could have been
|
||||
# reorged while we were querying bitcoind to get the confirmation count. In such a case we just
|
||||
# restart the job
|
||||
# restart the tracker
|
||||
if e.error.get("code") == RPC_INVALID_ADDRESS_OR_KEY:
|
||||
logger.info("Transaction not found in mempool nor blockchain", txid=txid)
|
||||
|
||||
|
||||
@@ -67,42 +67,42 @@ class Cleaner:
|
||||
db_manager.store_watcher_appointment(uuid, appointment.to_json(triggered=True))
|
||||
|
||||
@staticmethod
|
||||
def delete_completed_jobs(completed_jobs, height, jobs, tx_job_map, db_manager):
|
||||
def delete_completed_trackers(completed_trackers, height, trackers, tx_tracker_map, db_manager):
|
||||
"""
|
||||
Deletes a completed job both from memory (:mod:`Responder <pisa.responder>`) and disk (from the
|
||||
Deletes a completed tracker both from memory (:mod:`Responder <pisa.responder>`) and disk (from the
|
||||
:mod:`Responder <pisa.responder>` and :mod:`Watcher <pisa.watcher>` databases).
|
||||
|
||||
Args:
|
||||
jobs (dict): a dictionary containing all the :mod:`Responder <pisa.responder>` jobs.
|
||||
tx_job_map (dict): a ``penalty_txid:uuid`` map for the :mod:`Responder <pisa.responder>` jobs.
|
||||
completed_jobs (list): a list of completed jobs to be deleted.
|
||||
height (int): the block height at which the jobs were completed.
|
||||
trackers (dict): a dictionary containing all the :mod:`Responder <pisa.responder>` trackers.
|
||||
tx_tracker_map (dict): a ``penalty_txid:uuid`` map for the :mod:`Responder <pisa.responder>` trackers.
|
||||
completed_trackers (list): a list of completed trackers to be deleted.
|
||||
height (int): the block height at which the trackers were completed.
|
||||
db_manager (DBManager): a :mod:`DBManager <pisa.db_manager>` instance to interact with the database.
|
||||
"""
|
||||
|
||||
for uuid, confirmations in completed_jobs:
|
||||
for uuid, confirmations in completed_trackers:
|
||||
logger.info(
|
||||
"Job completed. Appointment ended after reaching enough confirmations.",
|
||||
"Appointment completed. Appointment ended after reaching enough confirmations.",
|
||||
uuid=uuid,
|
||||
height=height,
|
||||
confirmations=confirmations,
|
||||
)
|
||||
|
||||
penalty_txid = jobs[uuid].penalty_txid
|
||||
locator = jobs[uuid].locator
|
||||
jobs.pop(uuid)
|
||||
penalty_txid = trackers[uuid].penalty_txid
|
||||
locator = trackers[uuid].locator
|
||||
trackers.pop(uuid)
|
||||
|
||||
if len(tx_job_map[penalty_txid]) == 1:
|
||||
tx_job_map.pop(penalty_txid)
|
||||
if len(tx_tracker_map[penalty_txid]) == 1:
|
||||
tx_tracker_map.pop(penalty_txid)
|
||||
|
||||
logger.info("No more jobs for penalty transaction.", penalty_txid=penalty_txid)
|
||||
logger.info("No more trackers for penalty transaction.", penalty_txid=penalty_txid)
|
||||
|
||||
else:
|
||||
tx_job_map[penalty_txid].remove(uuid)
|
||||
tx_tracker_map[penalty_txid].remove(uuid)
|
||||
|
||||
# Delete appointment from the db (both watchers's and responder's)
|
||||
db_manager.delete_watcher_appointment(uuid)
|
||||
db_manager.delete_responder_job(uuid)
|
||||
db_manager.delete_responder_tracker(uuid)
|
||||
|
||||
# Update / delete the locator map
|
||||
locator_map = db_manager.load_locator_map(locator)
|
||||
|
||||
@@ -20,7 +20,7 @@ class DBManager:
|
||||
The database is split in five prefixes:
|
||||
|
||||
- ``WATCHER_PREFIX``, defined as ``b'w``, is used to store :mod:`Watcher <pisa.watcher>` appointments.
|
||||
- ``RESPONDER_PREFIX``, defines as ``b'r``, is used to store :mod:`Responder <pisa.responder>` jobs.
|
||||
- ``RESPONDER_PREFIX``, defines as ``b'r``, is used to store :mod:`Responder <pisa.responder>` trackers.
|
||||
- ``WATCHER_LAST_BLOCK_KEY``, defined as ``b'bw``, is used to store the last block hash known by the :mod:`Watcher <pisa.watcher>`.
|
||||
- ``RESPONDER_LAST_BLOCK_KEY``, defined as ``b'br``, is used to store the last block hash known by the :mod:`Responder <pisa.responder>`.
|
||||
- ``LOCATOR_MAP_PREFIX``, defined as ``b'm``, is used to store the ``locator:uuid`` maps.
|
||||
@@ -51,7 +51,7 @@ class DBManager:
|
||||
prefix (str): the prefix of the data to load.
|
||||
|
||||
Returns:
|
||||
(``dict``): A dictionary containing the requested data (appointments or jobs) indexed by ``uuid``.
|
||||
(``dict``): A dictionary containing the requested data (appointments or trackers) indexed by ``uuid``.
|
||||
|
||||
Returns an empty dictionary if no data is found.
|
||||
"""
|
||||
@@ -108,7 +108,7 @@ class DBManager:
|
||||
key (str): the key that identifies the entry to be loaded.
|
||||
|
||||
Returns:
|
||||
(``dict`` or ``None``): A dictionary containing the requested data (an appointment or a job).
|
||||
(``dict`` or ``None``): A dictionary containing the requested data (an appointment or a tracker).
|
||||
|
||||
Returns ``None`` if the entry is not found.
|
||||
"""
|
||||
@@ -145,12 +145,12 @@ class DBManager:
|
||||
|
||||
return self.load_entry(WATCHER_PREFIX + key)
|
||||
|
||||
def load_responder_job(self, key):
|
||||
def load_responder_tracker(self, key):
|
||||
"""
|
||||
Loads a job from the database using ``RESPONDER_PREFIX`` as a prefix to the given ``key``.
|
||||
Loads a tracker from the database using ``RESPONDER_PREFIX`` as a prefix to the given ``key``.
|
||||
|
||||
Returns:
|
||||
(``dict``): A dictionary containing the job data if they ``key`` is found.
|
||||
(``dict``): A dictionary containing the tracker data if they ``key`` is found.
|
||||
|
||||
Returns ``None`` otherwise.
|
||||
"""
|
||||
@@ -179,12 +179,12 @@ class DBManager:
|
||||
|
||||
return appointments
|
||||
|
||||
def load_responder_jobs(self):
|
||||
def load_responder_trackers(self):
|
||||
"""
|
||||
Loads all the jobs from the database (all entries with the ``RESPONDER_PREFIX`` prefix).
|
||||
Loads all the trackers from the database (all entries with the ``RESPONDER_PREFIX`` prefix).
|
||||
|
||||
Returns:
|
||||
(``dict``): A dictionary with all the jobs stored in the database. An empty dictionary is there are
|
||||
(``dict``): A dictionary with all the trackers stored in the database. An empty dictionary is there are
|
||||
none.
|
||||
"""
|
||||
|
||||
@@ -198,12 +198,12 @@ class DBManager:
|
||||
self.create_entry(uuid, appointment, prefix=WATCHER_PREFIX)
|
||||
logger.info("Adding appointment to Watchers's db", uuid=uuid)
|
||||
|
||||
def store_responder_job(self, uuid, job):
|
||||
def store_responder_tracker(self, uuid, tracker):
|
||||
"""
|
||||
Stores a job in the database using the ``RESPONDER_PREFIX`` prefix.
|
||||
Stores a tracker in the database using the ``RESPONDER_PREFIX`` prefix.
|
||||
"""
|
||||
|
||||
self.create_entry(uuid, job, prefix=RESPONDER_PREFIX)
|
||||
self.create_entry(uuid, tracker, prefix=RESPONDER_PREFIX)
|
||||
logger.info("Adding appointment to Responder's db", uuid=uuid)
|
||||
|
||||
def load_locator_map(self, locator):
|
||||
@@ -280,12 +280,12 @@ class DBManager:
|
||||
self.delete_entry(uuid, prefix=WATCHER_PREFIX)
|
||||
logger.info("Deleting appointment from Watcher's db", uuid=uuid)
|
||||
|
||||
def delete_responder_job(self, uuid):
|
||||
def delete_responder_tracker(self, uuid):
|
||||
"""
|
||||
Deletes a job from the database.
|
||||
Deletes a tracker from the database.
|
||||
|
||||
Args:
|
||||
uuid (str): a 16-byte hex-encoded string identifying the job to be deleted.
|
||||
uuid (str): a 16-byte hex-encoded string identifying the tracker to be deleted.
|
||||
"""
|
||||
|
||||
self.delete_entry(uuid, prefix=RESPONDER_PREFIX)
|
||||
|
||||
@@ -47,11 +47,11 @@ if __name__ == "__main__":
|
||||
db_manager = DBManager(DB_PATH)
|
||||
|
||||
watcher_appointments_data = db_manager.load_watcher_appointments()
|
||||
responder_jobs_data = db_manager.load_responder_jobs()
|
||||
responder_trackers_data = db_manager.load_responder_trackers()
|
||||
|
||||
watcher = Watcher(db_manager)
|
||||
|
||||
if len(watcher_appointments_data) == 0 and len(responder_jobs_data) == 0:
|
||||
if len(watcher_appointments_data) == 0 and len(responder_trackers_data) == 0:
|
||||
logger.info("Fresh bootstrap")
|
||||
|
||||
else:
|
||||
@@ -69,7 +69,7 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
responder = Responder(db_manager)
|
||||
responder.jobs, responder.tx_job_map = Builder.build_jobs(responder_jobs_data)
|
||||
responder.trackers, responder.tx_tracker_map = Builder.build_trackers(responder_trackers_data)
|
||||
responder.block_queue = Builder.build_block_queue(missed_blocks_responder)
|
||||
|
||||
watcher.responder = responder
|
||||
|
||||
@@ -9,7 +9,7 @@ from pisa.logger import Logger
|
||||
from pisa.cleaner import Cleaner
|
||||
from pisa.responder import Responder
|
||||
from pisa.block_processor import BlockProcessor
|
||||
from pisa.utils.zmq_subscriber import ZMQHandler
|
||||
from pisa.utils.zmq_subscriber import ZMQSubscriber
|
||||
from pisa.conf import EXPIRY_DELTA, MAX_APPOINTMENTS, PISA_SECRET_KEY
|
||||
|
||||
logger = Logger("Watcher")
|
||||
@@ -89,7 +89,7 @@ class Watcher:
|
||||
return appointment_added, signature
|
||||
|
||||
def do_subscribe(self):
|
||||
self.zmq_subscriber = ZMQHandler(parent="Watcher")
|
||||
self.zmq_subscriber = ZMQSubscriber(parent="Watcher")
|
||||
self.zmq_subscriber.handle(self.block_queue)
|
||||
|
||||
def do_watch(self):
|
||||
@@ -126,7 +126,7 @@ class Watcher:
|
||||
uuid=uuid,
|
||||
)
|
||||
|
||||
self.responder.add_response(
|
||||
self.responder.handle_breach(
|
||||
uuid,
|
||||
filtered_match["locator"],
|
||||
filtered_match["dispute_txid"],
|
||||
|
||||
Reference in New Issue
Block a user