diff --git a/pisa/carrier.py b/pisa/carrier.py index ff2d8b9..627b917 100644 --- a/pisa/carrier.py +++ b/pisa/carrier.py @@ -87,21 +87,3 @@ class Carrier: logger.error("JSONRPCException.", method="Carrier.get_transaction", error=e.error) return tx_info - - def check_tx_in_chain(self, txid): - tx_in_chain = False - confirmations = None - - tx_info = self.get_transaction(txid) - - if tx_info is not None: - confirmations = int(tx_info.get("confirmations")) if tx_info.get("confirmations") is not None else None - - if confirmations is not None: - tx_in_chain = True - logger.error("Transaction found in the blockchain", txid=txid) - - else: - logger.error("Transaction found in mempool", txid=txid) - - return tx_in_chain, confirmations diff --git a/pisa/responder.py b/pisa/responder.py index fee87aa..2cc00ff 100644 --- a/pisa/responder.py +++ b/pisa/responder.py @@ -7,7 +7,6 @@ from binascii import unhexlify from pisa.logger import Logger from pisa.cleaner import Cleaner from pisa.carrier import Carrier -from pisa.tools import check_tx_in_chain from pisa.block_processor import BlockProcessor from pisa.utils.zmq_subscriber import ZMQHandler @@ -171,7 +170,7 @@ class Responder: ) # ToDo: #24-properly-handle-reorgs - self.handle_reorgs() + self.handle_reorgs(block_hash) # Register the last processed block for the responder self.db_manager.store_last_block_hash_responder(block_hash) @@ -262,41 +261,43 @@ class Responder: return receipts - # FIXME: Legacy code, must be checked and updated/fixed # NOTCOVERED - def handle_reorgs(self): + def handle_reorgs(self, block_hash): + carrier = Carrier() + for uuid, job in self.jobs.items(): - # First we check if the dispute transaction is still in the blockchain. If not, the justice can not be - # there either, so we'll need to call the reorg manager straight away - dispute_in_chain, _ = check_tx_in_chain(job.dispute_txid, logger=logger, tx_label="Dispute tx") + # First we check if the dispute transaction is known (exists either in mempool or blockchain) + dispute_tx = carrier.get_transaction(job.dispute_txid) - # If the dispute is there, we can check the justice tx - if dispute_in_chain: - justice_in_chain, justice_confirmations = check_tx_in_chain( - job.justice_txid, logger=logger, tx_label="Justice tx" - ) + if dispute_tx is not None: + # If the dispute is there, we check the justice + justice_tx = carrier.get_transaction(job.justice_txid) - # If both transactions are there, we only need to update the justice tx confirmation count - if justice_in_chain: - logger.info( - "Updating confirmation count for transaction.", - justice_txid=job.justice_txid, - prev_count=job.confirmations, - curr_count=justice_confirmations, - ) + if justice_tx is not None: + # If the justice exists we need to check is it's on the blockchain or not so we can update the + # unconfirmed transactions list accordingly. + if justice_tx.get("confirmations") is None: + self.unconfirmed_txs.append(job.justice_txid) - job.confirmations = justice_confirmations + logger.info( + "Justice transaction back in mempool. Updating unconfirmed transactions.", + justice_txid=job.justice_txid, + ) else: - # Otherwise, we will add the job back (implying rebroadcast of the tx) and monitor it again + # If the justice transaction is missing, we need to reset the job. # DISCUSS: Adding job back, should we flag it as retried? # FIXME: Whether we decide to increase the retried counter or not, the current counter should be # maintained. There is no way of doing so with the current approach. Update if required - self.add_response(uuid, job.dispute_txid, job.justice_txid, job.justice_rawtx, job.appointment_end) + self.add_response( + uuid, job.dispute_txid, job.justice_txid, job.justice_rawtx, job.appointment_end, block_hash + ) + + logger.warning("Justice transaction banished. Resetting the job", justice_tx=job.justice_txid) else: # ToDo: #24-properly-handle-reorgs # FIXME: if the dispute is not on chain (either in mempool or not there at all), we need to call the # reorg manager - logger.warning("Dispute and justice transaction missing. Calling the reorg manager") - logger.error("Reorg manager not yet implemented") + logger.warning("Dispute and justice transaction missing. Calling the reorg manager.") + logger.error("Reorg manager not yet implemented.") diff --git a/test/unit/test_carrier.py b/test/unit/test_carrier.py index 00636d0..4e6eddf 100644 --- a/test/unit/test_carrier.py +++ b/test/unit/test_carrier.py @@ -72,24 +72,3 @@ def test_get_non_existing_transaction(): tx_info = Carrier.get_transaction(get_random_value_hex(32)) assert tx_info is None - - -def test_check_tx_in_chain(carrier): - # Let's starts by looking for a random transaction - random_tx = TX.create_dummy_transaction() - random_txid = sha256d(random_tx) - tx_in_chain, confirmations = carrier.check_tx_in_chain(random_txid) - assert tx_in_chain is False and confirmations is None - - # We can now broadcast the transaction and check again - carrier.send_transaction(random_tx, random_txid) - tx_in_chain, confirmations = carrier.check_tx_in_chain(random_txid) - - # The tx should be on mempool now, so same - assert tx_in_chain is False and confirmations is None - - # Finally we can mine a block and check again - generate_block() - tx_in_chain, confirmations = carrier.check_tx_in_chain(random_txid) - - assert tx_in_chain is True and confirmations == 1