From 0c4b8eaf4a1419fb6a3688e6313cc231ba075fdb Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 31 Jan 2020 12:48:11 +0100 Subject: [PATCH] Adds a dict of sent receipts to act as a cache and avoid sending the same data more than once The dict is supposed to be reset periodically so it does not grow unbounded --- pisa/carrier.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pisa/carrier.py b/pisa/carrier.py index dec4ba6..160bd34 100644 --- a/pisa/carrier.py +++ b/pisa/carrier.py @@ -38,8 +38,16 @@ class Carrier: """ The :class:`Carrier` is the class in charge of interacting with ``bitcoind`` to send/get transactions. It uses :obj:`Receipt` objects to report about the sending outcome. + + Attributes: + issued_receipts (:obj:`dict`): a dictionary of issued receipts to prevent resending the same transaction over + and over. It should periodically be reset to prevent it from growing unbounded. + """ + def __init__(self): + self.issued_receipts = {} + # NOTCOVERED def send_transaction(self, rawtx, txid): """ @@ -53,6 +61,12 @@ class Carrier: :obj:`Receipt`: A receipt reporting whether the transaction was successfully delivered or not and why. """ + if txid in self.issued_receipts: + logger.info("Transaction already sent", txid=txid) + receipt = self.issued_receipts[txid] + + return receipt + try: logger.info("Pushing transaction to the network", txid=txid, rawtx=rawtx) bitcoin_cli().sendrawtransaction(rawtx) @@ -101,6 +115,8 @@ class Carrier: logger.error("JSONRPCException", method="Carrier.send_transaction", error=e.error) receipt = Receipt(delivered=False, reason=UNKNOWN_JSON_RPC_EXCEPTION) + self.issued_receipts[txid] = receipt + return receipt @staticmethod