From 272e61922d821b74f50eadfc811d3dfd45339cc4 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Wed, 22 Jan 2020 16:19:51 +0100 Subject: [PATCH] Move triggered flag clearing to the Watcher when a triggered appointment cannot make it to the mempool When an appointment was triggered a flag was set in the Watcher, and removed later on in the Responder if the transaction ended up being rejected. That's pretty annoying. Since we have information about whether a transaction has made it to the mempool or not via the Carrier's receipt, this can be all done in the Watcher, which makes more sense and reduces the interaction with the db (1 write if succeeds, 0 otherwise instead of 1 write if succeeds, 2 otherwise). --- pisa/responder.py | 4 ---- pisa/watcher.py | 15 +++++++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pisa/responder.py b/pisa/responder.py index cef46c5..5d4ac9d 100644 --- a/pisa/responder.py +++ b/pisa/responder.py @@ -214,10 +214,6 @@ class Responder: "Tracker cannot be created", reason=receipt.reason, uuid=uuid, on_sync=self.on_sync(block_hash) ) - # FIXME: This is only necessary because of the triggered appointment approach. Remove if it changes. - Cleaner.delete_appointment_from_db(uuid, self.db_manager) - Cleaner.update_delete_db_locator_map(uuid, locator, self.db_manager) - return receipt def add_tracker(self, uuid, locator, dispute_txid, penalty_txid, penalty_rawtx, appointment_end, confirmations=0): diff --git a/pisa/watcher.py b/pisa/watcher.py index acc390c..c0e852a 100644 --- a/pisa/watcher.py +++ b/pisa/watcher.py @@ -178,7 +178,7 @@ class Watcher: uuid=uuid, ) - self.responder.handle_breach( + receipt = self.responder.handle_breach( uuid, breach["locator"], breach["dispute_txid"], @@ -188,9 +188,16 @@ class Watcher: block_hash, ) - Cleaner.flag_triggered_appointments( - list(valid_breaches.keys()), self.appointments, self.locator_uuid_map, self.db_manager - ) + Cleaner.delete_appointment_from_memory(uuid, self.appointments, self.locator_uuid_map) + + # Appointments are only flagged as triggered if they are delivered, otherwise they are just deleted. + # FIXME: This is only necessary because of the triggered appointment approach. Fix if it changes. + if receipt.delivered: + self.db_manager.create_triggered_appointment_flag(uuid) + + else: + self.db_manager.delete_watcher_appointment(uuid) + Cleaner.update_delete_db_locator_map(uuid, breach["locator"], self.db_manager) Cleaner.delete_completed_appointments( invalid_breaches, self.appointments, self.locator_uuid_map, self.db_manager