diff --git a/pisa/db_manager.py b/pisa/db_manager.py index 339140d..cd6428f 100644 --- a/pisa/db_manager.py +++ b/pisa/db_manager.py @@ -9,6 +9,9 @@ logger = Logger("DBManager") class DBManager: def __init__(self, db_path): + if not isinstance(db_path, str): + raise ValueError("db_path must be a valid path/name") + try: self.db = plyvel.DB(db_path) @@ -22,13 +25,13 @@ class DBManager: for k, v in self.db.iterator(prefix=prefix.encode('utf-8')): # Get uuid and appointment_data from the db - uuid = k[1:].decode('utf-8') + uuid = k[len(prefix):].decode('utf-8') data[uuid] = json.loads(v) return data - def get_last_known_block(self, prefix): - last_block = self.db.get(prefix) + def get_last_known_block(self, key): + last_block = self.db.get(key.encode('utf-8')) if last_block: last_block = last_block.decode('utf-8') @@ -74,8 +77,14 @@ class DBManager: self.delete_entry(uuid, prefix=RESPONDER_PREFIX) logger.info("Deleting appointment from Responder's db", uuid=uuid) - def store_last_block_watcher(self, block_hash): + def load_last_block_hash_watcher(self): + return self.get_last_known_block(WATCHER_LAST_BLOCK_KEY) + + def load_last_block_hash_responder(self): + return self.get_last_known_block(RESPONDER_LAST_BLOCK_KEY) + + def store_last_block_hash_watcher(self, block_hash): self.create_entry(WATCHER_LAST_BLOCK_KEY, block_hash) - def store_last_block_responder(self, block_hash): + def store_last_block_hash_responder(self, block_hash): self.create_entry(RESPONDER_LAST_BLOCK_KEY, block_hash) diff --git a/pisa/responder.py b/pisa/responder.py index 36c1295..e92a4eb 100644 --- a/pisa/responder.py +++ b/pisa/responder.py @@ -165,7 +165,7 @@ class Responder: self.handle_reorgs() # Register the last processed block for the responder - self.db_manager.store_last_block_responder(block_hash) + self.db_manager.store_last_block_hash_responder(block_hash) prev_block_hash = block.get('hash') diff --git a/pisa/watcher.py b/pisa/watcher.py index 944d0c9..2017e58 100644 --- a/pisa/watcher.py +++ b/pisa/watcher.py @@ -141,7 +141,7 @@ class Watcher: self.db_manager.store_watcher_appointment(uuid, appointment.to_json()) # Register the last processed block for the watcher - self.db_manager.store_last_block_watcher(block_hash) + self.db_manager.store_last_block_hash_watcher(block_hash) # Go back to sleep if there are no more appointments self.asleep = True